-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HP-1928: array => string type #64
Changes from 7 commits
da86587
dfeb822
2978053
a0b33a7
1d91fdd
02617b7
2f0f4c9
c0c1a82
24ca334
c1cf2f7
bc5b93b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use hiqdev\php\billing\Exception\InvariantException; | ||
use hiqdev\php\billing\plan\PlanInterface; | ||
use hiqdev\php\billing\target\TargetInterface; | ||
use yii\helpers\Json; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary import of This package does not depend on Yii, as noted by SilverFire in a previous comment. Please remove this import to avoid confusion and potential errors.
bladeroot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Sale. | ||
|
@@ -52,22 +53,22 @@ class Sale implements SaleInterface | |
|
||
protected ?DateTimeImmutable $closeTime = null; | ||
|
||
protected ?array $data = null; | ||
protected ?string $data = null; | ||
SilverFire marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you prefer to store data as a JSON string instead of an array? |
||
|
||
public function __construct( | ||
$id, | ||
TargetInterface $target, | ||
CustomerInterface $customer, | ||
?PlanInterface $plan = null, | ||
?DateTimeImmutable $time = null, | ||
?array $data = null, | ||
$data = null, | ||
) { | ||
$this->id = $id; | ||
$this->target = $target; | ||
$this->customer = $customer; | ||
$this->plan = $plan; | ||
$this->time = $time ?? new DateTimeImmutable(); | ||
$this->data = $data; | ||
$this->data = $this->setData($data); | ||
} | ||
|
||
public function getId() | ||
|
@@ -129,9 +130,29 @@ public function setId($id) | |
$this->id = $id; | ||
} | ||
|
||
public function getData() | ||
public function setData($data): self | ||
{ | ||
return $this->data; | ||
if (empty($data)) { | ||
$this->data = null; | ||
return $this; | ||
} | ||
|
||
if (is_string($data) && json_validate($data)) { | ||
$this->data = $data; | ||
return $this; | ||
} | ||
|
||
if (is_array($data)) { | ||
$this->data = Json::encode($data); | ||
return $this; | ||
} | ||
|
||
throw new \Exception("Cannot assign data"); | ||
} | ||
SilverFire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function getData(): ?array | ||
{ | ||
return !empty($this->data) ? Json::decode($this->data, true) : null; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,14 @@ public function setConsumption(string $type, int $amount, string $unit, string $ | |
$this->builder->setConsumption($type, $amount, $unit, $target, $time); | ||
} | ||
|
||
/** | ||
* @Given /recalculate autotariff for target (\S+)( +at (\S+))?$/ | ||
*/ | ||
public function recalculateAutoTariff(string $target, string $time = null): void | ||
{ | ||
$this->builder->clientSetAutoTariff($target, $time); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?? |
||
/** | ||
* @Given /perform billing at (\S+)/ | ||
*/ | ||
|
@@ -231,14 +239,14 @@ public function billWithTime($type, $sum, $currency, $quantity, $unit, $target, | |
'quantity' => "$quantity $unit", | ||
'time' => $time, | ||
]); | ||
Assert::assertSame($type, $bill->getType()->getName()); | ||
Assert::assertSame($target, $bill->getTarget()->getFullName()); | ||
Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount()); | ||
Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode()); | ||
Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity()); | ||
Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName())); | ||
Assert::assertSame($type, $bill->getType()->getName(), "Bill type mismatch: expected $type, got {$bill->getType()->getName()}"); | ||
Assert::assertSame($target, $bill->getTarget()->getFullName(), "Bill target mismatch: expected $target, got {$bill->getTarget()->getFullName()}"); | ||
Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount(), "Bill sum mismatch: expected $sum, got {$bill->getSum()->getAmount()}"); | ||
Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode(), "Bill currency mismatch: expected $currency, got {$bill->getSum()->getCurrency()->getCode()}"); | ||
Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity(), "Bill quantity mismatch: expected $quantity, got {$bill->getQuantity()->getQuantity()}"); | ||
Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()), "Bill unit mismatch: expected $unit, got {$bill->getQuantity()->getUnit()->getName()}"); | ||
if ($time) { | ||
Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime()); | ||
Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime(), "Bill time mismatch: expected $time, got {$bill->getTime()->format(DATE_ATOM)}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems you've mixed different PRs |
||
} | ||
} | ||
|
||
|
@@ -337,6 +345,10 @@ protected function prepareTime(string $time = null) | |
if (strncmp($time, 'pY', 1) === 0) { | ||
return date(substr($time, 1), strtotime('-1 year')); | ||
} | ||
if (str_contains($time, 'pm')) { | ||
$time = str_replace('pm', 'm', $time); | ||
$time = date($time, strtotime('-1 month')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
if (strncmp($time, 'Y', 1) === 0) { | ||
return date($time); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package does not depend on Yii.