Skip to content

Commit 2c6a77c

Browse files
ValeriyShnurovoyDrahma
andauthored
HP-2282 Bill since/till is not updated after sale is closed (#95)
Co-authored-by: Drahma <[email protected]>
1 parent bf48787 commit 2c6a77c

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

src/action/UsageInterval.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use DateInterval;
66
use DateTimeImmutable;
77
use InvalidArgumentException;
8+
use JsonSerializable;
89

910
/** @readonly */
10-
final class UsageInterval
11+
final class UsageInterval implements JsonSerializable
1112
{
1213
/** @readonly */
1314
private DateTimeImmutable $start;
@@ -189,4 +190,9 @@ public function extend(self $other): self
189190
$newEnd,
190191
);
191192
}
193+
194+
public function jsonSerialize(): array
195+
{
196+
return array_filter(get_object_vars($this));
197+
}
192198
}

src/bill/BillCreationDto.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ class BillCreationDto
3636
public $charges;
3737

3838
public $state;
39+
40+
public $usageInterval;
3941
}

src/bill/BillFactory.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
namespace hiqdev\php\billing\bill;
1212

13+
use DateTimeImmutable;
14+
use hiqdev\billing\hiapi\action\UsageIntervalHydrator;
15+
use hiqdev\php\billing\action\UsageInterval;
16+
1317
/**
1418
* Default bill factory.
1519
*
@@ -23,7 +27,7 @@ class BillFactory implements BillFactoryInterface
2327
*/
2428
public function create(BillCreationDto $dto)
2529
{
26-
return new Bill(
30+
$bill = new Bill(
2731
$dto->id,
2832
$dto->type,
2933
$dto->time,
@@ -35,5 +39,21 @@ public function create(BillCreationDto $dto)
3539
$dto->charges ?: [],
3640
$dto->state
3741
);
42+
if (!empty($dto->usageInterval)) {
43+
if ($dto->usageInterval instanceof UsageInterval) {
44+
$interval = $dto->usageInterval;
45+
} else {
46+
$month = $dto->usageInterval['month']['date'];
47+
$start = $dto->usageInterval['start']['date'];
48+
$end = $dto->usageInterval['end']['date'];;
49+
$interval = UsageInterval::withinMonth(
50+
new DateTimeImmutable($month),
51+
new DateTimeImmutable($start),
52+
new DateTimeImmutable($end)
53+
);
54+
}
55+
$bill->setUsageInterval($interval);
56+
}
57+
return $bill;
3858
}
3959
}

src/tools/Merger.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function mergeBill(BillInterface $first, BillInterface $other): BillInter
4646
{
4747
$charges = $this->mergeCharges(array_merge($first->getCharges(), $other->getCharges()));
4848

49-
return new Bill(
49+
$bill = new Bill(
5050
$this->mergeId($first, $other),
5151
$first->getType(),
5252
$first->getTime(),
@@ -57,6 +57,8 @@ public function mergeBill(BillInterface $first, BillInterface $other): BillInter
5757
$first->getPlan(),
5858
$charges
5959
);
60+
$bill->setUsageInterval($first->getUsageInterval());
61+
return $bill;
6062
}
6163

6264
/**

tests/behat/bootstrap/BillingContext.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,36 @@ public function billWithTime($type, $sum, $currency, $quantity, $unit, $target,
300300
}
301301
}
302302

303+
/**
304+
* @Given /bill interval +for (\S+) is +(\S+) (\S+) per (\S+) (\S+) for target (.+?) at (\S+) between (\S+) and (\S+)?$/
305+
*/
306+
public function billInterval($type, $sum, $currency, $quantity, $unit, $target, $time, $since, $till)
307+
{
308+
$this->builder->flushEntitiesCacheByType('bill');
309+
310+
$quantity = $this->prepareQuantity($quantity);
311+
$sum = $this->prepareSum($sum, $quantity);
312+
$time = $this->prepareTime($time);
313+
$bill = $this->findBill([
314+
'type' => $type,
315+
'target' => $target,
316+
'sum' => "$sum $currency",
317+
'quantity' => "$quantity $unit",
318+
'time' => $time,
319+
]);
320+
Assert::assertSame($type, $bill->getType()->getName(), "Bill type mismatch: expected $type, got {$bill->getType()->getName()}");
321+
Assert::assertSame($target, $bill->getTarget()->getFullName(), "Bill target mismatch: expected $target, got {$bill->getTarget()->getFullName()}");
322+
Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount(), "Bill sum mismatch: expected $sum, got {$bill->getSum()->getAmount()}");
323+
Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode(), "Bill currency mismatch: expected $currency, got {$bill->getSum()->getCurrency()->getCode()}");
324+
Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity(), "Bill quantity mismatch: expected $quantity, got {$bill->getQuantity()->getQuantity()}");
325+
Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()), "Bill unit mismatch: expected $unit, got {$bill->getQuantity()->getUnit()->getName()}");
326+
Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime(), "Bill time mismatch: expected $time, got {$bill->getTime()->format(DATE_ATOM)}");
327+
$billStart = $bill->getUsageInterval()->start();
328+
$billEnd = $bill->getUsageInterval()->end();
329+
Assert::assertEquals(new DateTimeImmutable($since), $billStart, "Bill since time mismatch: expected $since, got {$billStart->format(DATE_ATOM)}");
330+
Assert::assertEquals(new DateTimeImmutable($till), $billEnd, "Bill till time mismatch: expected $till, got {$billEnd->format(DATE_ATOM)}");
331+
}
332+
303333
public function findBill(array $params): BillInterface
304334
{
305335
$bills = $this->builder->findBills($params);

0 commit comments

Comments
 (0)