Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/formula-and-modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Spreads a charge over a fixed term as monthly payments.
installment.since('08.2018').lasts('3 months').reason('TEST')
```

Returns a single charge with type `leasing` (pre-2024) or `installment` (2024+). The `till()` method is forbidden — use `lasts()` instead. Records domain events `InstallmentWasStarted` / `InstallmentWasFinished`.
Returns a single charge with type `leasing` (pre-2024) or `installment` (2024+). The `till()` method is forbidden — use `lasts()` instead. Records domain events `InstallmentWasCharged` / `InstallmentWasFinished`.

### Cap / MonthlyCap

Expand Down
2 changes: 1 addition & 1 deletion docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ src/
├── bill/ Bill entity — aggregated invoice line item
├── charge/ Charge entity, ChargeModifier interface, modifier classes and addons
├── customer/ Customer entity — billable party with seller hierarchy
├── event/ Domain events (e.g., InstallmentWasStarted)
├── event/ Domain events (e.g., InstallmentWasCharged)
├── Exception/ Shared exceptions (CannotReassignException, etc.)
├── formula/ FormulaEngine — parses DSL strings into ChargeModifier objects
├── helpers/ Utility classes
Expand Down
19 changes: 3 additions & 16 deletions src/charge/modifiers/Installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use hiqdev\php\billing\charge\modifiers\addons\Period;
use hiqdev\php\billing\charge\modifiers\addons\Reason;
use hiqdev\php\billing\charge\modifiers\addons\Since;
use hiqdev\php\billing\charge\modifiers\event\InstallmentWasCharged;
use hiqdev\php\billing\charge\modifiers\event\InstallmentWasFinished;
use hiqdev\php\billing\charge\modifiers\event\InstallmentWasStarted;
use hiqdev\php\billing\formula\FormulaSemanticsError;
use hiqdev\php\billing\price\SinglePrice;
use hiqdev\php\billing\target\Target;
Expand Down Expand Up @@ -102,15 +102,6 @@ protected function ensureIsValid(): void
}
}

private function isFirstMonthInInstallmentPassed(DateTimeImmutable $time): bool
{
$since = $this->getSince();
if ($since && $since->getValue() > $time) {
return false;
}
return $since->getValue()->diff($time)->format('%a') === '0';
}

private function isFirstMonthAfterInstallmentPassed(DateTimeImmutable $time): bool
{
$since = $this->getSince();
Expand Down Expand Up @@ -148,7 +139,7 @@ private function createInstallmentFinishingCharge(ChargeInterface $charge, DateT

private function createInstallmentStartingCharge(Charge $charge, DateTimeImmutable $month): ChargeInterface
{
$charge->recordThat(InstallmentWasStarted::onCharge($charge, $month));
$charge->recordThat(InstallmentWasCharged::onCharge($charge, $month));

return $charge;
}
Expand All @@ -169,11 +160,7 @@ private function createInstallmentCharge(ChargeInterface $charge, DateTimeImmuta
$result->setComment($charge->getComment());
}

if ($this->isFirstMonthInInstallmentPassed($month)) {
return $this->createInstallmentStartingCharge($result, $month);
}

return $result;
return $this->createInstallmentStartingCharge($result, $month);
}

public function getRemainingPeriods(DateTimeImmutable $currentDate): ?Period
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use League\Event\AbstractEvent;
use DateTimeImmutable;

class InstallmentWasStarted extends AbstractEvent implements \JsonSerializable
class InstallmentWasCharged extends AbstractEvent implements \JsonSerializable
{
private function __construct(
private readonly ChargeInterface $charge,
Expand All @@ -38,14 +38,6 @@ public function getTime(): \DateTimeImmutable
return $this->time;
}

/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource
* @since 5.4.0
*/
public function jsonSerialize(): array
{
return [
Expand Down
9 changes: 4 additions & 5 deletions tests/behat/Combination.feature
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Feature: Combination
| 2018-08-01 | monthly 100 USD | | discount -10 USD reason TWO |
| 2018-09-01 | monthly 100 USD | | discount -20 USD reason TWO |
| 2018-10-01 | monthly 100 USD | | discount -30 USD reason TWO |
| 2018-11-01 | leasing 100 USD reason ONE | InstallmentWasStarted | |
| 2018-12-01 | leasing 100 USD reason ONE | | |
| 2018-11-01 | leasing 100 USD reason ONE | InstallmentWasCharged | |
| 2018-12-01 | leasing 100 USD reason ONE | InstallmentWasCharged | |
| 2019-01-11 | leasing 0 USD reason ONE | InstallmentWasFinished | |
| 2028-11-01 | | | |

Expand All @@ -52,8 +52,8 @@ Feature: Combination
| 2018-08-01 | monthly 100 USD | | discount -10 USD reason ONE |
| 2018-09-01 | monthly 100 USD | | discount -20 USD reason ONE |
| 2018-10-01 | monthly 100 USD | | discount -30 USD reason ONE |
| 2018-11-01 | leasing 60 USD reason TWO | InstallmentWasStarted | |
| 2018-12-01 | leasing 50 USD reason TWO | | |
| 2018-11-01 | leasing 60 USD reason TWO | InstallmentWasCharged | |
| 2018-12-01 | leasing 50 USD reason TWO | InstallmentWasCharged | |
| 2019-01-11 | leasing 0 USD reason TWO | InstallmentWasFinished | |
| 2028-11-01 | | | |

Expand Down Expand Up @@ -99,4 +99,3 @@ Feature: Combination
| 2022-01-01 | 1 | monthly 100 USD for 672 hour | monthly 0 USD for 72 hour | |
| 2022-04-01 | 1 | monthly 90 USD for 672 hour | monthly 0 USD for 48 hour | |
| 2022-05-15 | 0.5 | monthly 49.82 USD for 408 hour | | |

18 changes: 9 additions & 9 deletions tests/behat/Installment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Feature: installment
Examples:
| date | first | events |
| 2018-07-01 | | |
| 2018-08-01 | leasing 100 USD reason TEST | InstallmentWasStarted |
| 2018-09-01 | leasing 100 USD reason TEST | |
| 2018-10-01 | leasing 100 USD reason TEST | |
| 2018-08-01 | leasing 100 USD reason TEST | InstallmentWasCharged |
| 2018-09-01 | leasing 100 USD reason TEST | InstallmentWasCharged |
| 2018-10-01 | leasing 100 USD reason TEST | InstallmentWasCharged |
| 2018-11-01 | leasing 0 USD reason TEST | InstallmentWasFinished |
| 2028-01-01 | | |

Expand All @@ -28,9 +28,9 @@ Feature: installment
Examples:
| date | first | events |
| 2023-12-01 | | |
| 2024-01-01 | installment 100 USD reason TEST | InstallmentWasStarted |
| 2024-02-01 | installment 100 USD reason TEST | |
| 2024-03-01 | installment 100 USD reason TEST | |
| 2024-01-01 | installment 100 USD reason TEST | InstallmentWasCharged |
| 2024-02-01 | installment 100 USD reason TEST | InstallmentWasCharged |
| 2024-03-01 | installment 100 USD reason TEST | InstallmentWasCharged |
| 2024-04-01 | installment 0 USD reason TEST | InstallmentWasFinished |
| 2028-01-01 | | |

Expand All @@ -42,8 +42,8 @@ Feature: installment
Examples:
| date | first | events |
| 2018-07-01 | | |
| 2018-08-01 | leasing 0 USD reason TEST | InstallmentWasStarted |
| 2018-09-01 | leasing 0 USD reason TEST | |
| 2018-10-01 | leasing 0 USD reason TEST | |
| 2018-08-01 | leasing 0 USD reason TEST | InstallmentWasCharged |
| 2018-09-01 | leasing 0 USD reason TEST | InstallmentWasCharged |
| 2018-10-01 | leasing 0 USD reason TEST | InstallmentWasCharged |
| 2018-11-01 | leasing 0 USD reason TEST | InstallmentWasFinished |
| 2028-01-01 | | |
4 changes: 2 additions & 2 deletions tests/unit/charge/modifiers/InstallmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use DateTimeImmutable;
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod;
use hiqdev\php\billing\charge\modifiers\addons\YearPeriod;
use hiqdev\php\billing\charge\modifiers\event\InstallmentWasStarted;
use hiqdev\php\billing\charge\modifiers\event\InstallmentWasCharged;
use hiqdev\php\billing\charge\modifiers\Installment;
use hiqdev\php\billing\price\SinglePrice;
use hiqdev\php\billing\tests\unit\action\ActionTest;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function testModifyCharge()
$charge = $this->calculator->calculateCharge($this->price, $action);
$charges = $installment->modifyCharge($charge, $action);
$event = $charges[0]->releaseEvents()[0];
$this->assertInstanceOf(InstallmentWasStarted::class, $event);
$this->assertInstanceOf(InstallmentWasCharged::class, $event);
$this->assertIsArray($charges);
$this->assertSame(1, count($charges));
$this->assertEquals($charge, $charges[0]);
Expand Down
Loading