From 174331d100ee3005190096e88db8b32771b90a20 Mon Sep 17 00:00:00 2001 From: SilverFire - Dmytro Naumenko Date: Tue, 29 Oct 2024 16:04:01 +0200 Subject: [PATCH 1/2] Adding Bill::usageInterval --- src/action/UsageInterval.php | 14 ++++++++++++++ src/bill/Bill.php | 14 ++++++++++++++ src/bill/BillInterface.php | 5 +++++ src/charge/Generalizer.php | 14 ++++++++++++-- src/tools/Aggregator.php | 12 +++++++++++- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/action/UsageInterval.php b/src/action/UsageInterval.php index 2daabdaf..a51ae30d 100644 --- a/src/action/UsageInterval.php +++ b/src/action/UsageInterval.php @@ -129,4 +129,18 @@ public function ratioOfMonth(): float return $usageSeconds / $secondsInCurrentMonth; } + + /** + * Extends the usage interval to include both current and other intervals. + * + * @param UsageInterval $other + * @return self + */ + public function extend(self $other): self + { + return new self( + min($this->start, $other->start), + max($this->end, $other->end), + ); + } } diff --git a/src/bill/Bill.php b/src/bill/Bill.php index 53b97117..3fadc274 100755 --- a/src/bill/Bill.php +++ b/src/bill/Bill.php @@ -11,6 +11,7 @@ namespace hiqdev\php\billing\bill; use DateTimeImmutable; +use hiqdev\php\billing\action\UsageInterval; use hiqdev\php\billing\charge\ChargeInterface; use hiqdev\php\billing\customer\CustomerInterface; use hiqdev\php\billing\Exception\CannotReassignException; @@ -63,6 +64,9 @@ class Bill implements BillInterface /** @var string */ protected $comment; + /** @var UsageInterval */ + protected $usageInterval; + public function __construct( $id, TypeInterface $type, @@ -85,6 +89,7 @@ public function __construct( $this->plan = $plan; $this->charges = $charges; $this->state = $state; + $this->setUsageInterval(UsageInterval::wholeMonth($time)); } /** @@ -104,6 +109,10 @@ public function getUniqueString(): string return implode('-', $parts); } + public function getUsageInterval(): UsageInterval + { + } + public function calculatePrice() { $quantity = $this->quantity->getQuantity(); @@ -233,4 +242,9 @@ public function jsonSerialize(): array { return array_filter(get_object_vars($this)); } + + public function setUsageInterval(UsageInterval $usageInterval): void + { + $this->usageInterval = $usageInterval; + } } diff --git a/src/bill/BillInterface.php b/src/bill/BillInterface.php index fe70e5d3..13bb58d5 100644 --- a/src/bill/BillInterface.php +++ b/src/bill/BillInterface.php @@ -11,6 +11,7 @@ namespace hiqdev\php\billing\bill; use DateTimeImmutable; +use hiqdev\php\billing\action\UsageInterval; use hiqdev\php\billing\charge\ChargeInterface; use hiqdev\php\billing\customer\CustomerInterface; use hiqdev\php\billing\EntityInterface; @@ -50,4 +51,8 @@ public function getPlan(): ?PlanInterface; * @return ChargeInterface[] */ public function getCharges(); + + public function getUsageInterval(): UsageInterval; + + public function setUsageInterval(UsageInterval $usageInterval): void; } diff --git a/src/charge/Generalizer.php b/src/charge/Generalizer.php index 0215a297..4fb8409b 100644 --- a/src/charge/Generalizer.php +++ b/src/charge/Generalizer.php @@ -10,6 +10,7 @@ namespace hiqdev\php\billing\charge; +use hiqdev\php\billing\action\UsageInterval; use hiqdev\php\billing\bill\Bill; use hiqdev\php\billing\bill\BillInterface; use hiqdev\php\billing\customer\CustomerInterface; @@ -26,7 +27,7 @@ class Generalizer implements GeneralizerInterface { public function createBill(ChargeInterface $charge): BillInterface { - return new Bill( + $bill = new Bill( null, $this->generalizeType($charge), $this->generalizeTime($charge), @@ -35,8 +36,12 @@ public function createBill(ChargeInterface $charge): BillInterface $this->generalizeCustomer($charge), $this->generalizeTarget($charge), $this->generalizePlan($charge), - [$charge] + [$charge], ); + + $bill->setUsageInterval($this->generalizeUsageInterval($charge)); + + return $bill; } public function generalizeType(ChargeInterface $charge): TypeInterface @@ -83,4 +88,9 @@ public function specializeTarget(TargetInterface $first, TargetInterface $other) { return $first; } + + private function generalizeUsageInterval(ChargeInterface $charge): UsageInterval + { + return $charge->getAction()->getUsageInterval(); + } } diff --git a/src/tools/Aggregator.php b/src/tools/Aggregator.php index 3c277416..befefa29 100644 --- a/src/tools/Aggregator.php +++ b/src/tools/Aggregator.php @@ -10,6 +10,7 @@ namespace hiqdev\php\billing\tools; +use hiqdev\php\billing\action\UsageInterval; use hiqdev\php\billing\bill\Bill; use hiqdev\php\billing\bill\BillInterface; use hiqdev\php\billing\charge\ChargeInterface; @@ -77,7 +78,7 @@ protected function aggregateBills(array $bills, array $others): array protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface { - return new Bill( + $bill = new Bill( $this->aggregateId($first, $other), $first->getType(), $first->getTime(), @@ -88,6 +89,15 @@ protected function aggregateBill(BillInterface $first, BillInterface $other): Bi $first->getPlan(), array_merge($first->getCharges(), $other->getCharges()) ); + + $bill->setUsageInterval($this->aggregateUsageInterval($first, $other)); + + return $bill; + } + + protected function aggregateUsageInterval(BillInterface $first, BillInterface $other): UsageInterval + { + return $first->getUsageInterval()->extend($other->getUsageInterval()); } /** From 6aa91c53a2173f18ee8901ad9b841dc453a7295f Mon Sep 17 00:00:00 2001 From: Drahma Date: Wed, 13 Nov 2024 14:32:11 +0200 Subject: [PATCH 2/2] HP-2166/Fixed getter --- src/action/UsageInterval.php | 10 ++++++++-- src/bill/Bill.php | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/action/UsageInterval.php b/src/action/UsageInterval.php index a51ae30d..dda5f6df 100644 --- a/src/action/UsageInterval.php +++ b/src/action/UsageInterval.php @@ -138,9 +138,15 @@ public function ratioOfMonth(): float */ public function extend(self $other): self { + $newStart = min($this->start, $other->start); + $newEnd = max($this->end, $other->end); + + if ($newStart > $newEnd) { + throw new InvalidArgumentException('Cannot extend intervals: resulting interval would be invalid'); + } return new self( - min($this->start, $other->start), - max($this->end, $other->end), + $newStart, + $newEnd, ); } } diff --git a/src/bill/Bill.php b/src/bill/Bill.php index 3fadc274..6e833f59 100755 --- a/src/bill/Bill.php +++ b/src/bill/Bill.php @@ -111,6 +111,7 @@ public function getUniqueString(): string public function getUsageInterval(): UsageInterval { + return $this->usageInterval; } public function calculatePrice()