Skip to content

Commit

Permalink
Changed the total calculation of AdjustmentCollection so that include…
Browse files Browse the repository at this point in the history
…d ones aren't in the sum by default

- Removed the `Adjustment::isNeutral()` method
- Added the `Adjustment::isNotIncluded()` method
  • Loading branch information
fulopattila122 committed Mar 4, 2024
1 parent 6385b81 commit 1c1c00a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 22 deletions.
7 changes: 5 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 4.x Series

## Unreleased
##### 2023-XX-YY
##### 2024-XX-YY

- Dropped PHP 8.0 & PHP 8.1 Support
- Dropped Laravel 9 Support
Expand Down Expand Up @@ -62,7 +62,10 @@
- BC: The `invalidateAdjustments()` method has been added to the `Adjustable` interface
- BC: The `exceptTypes()` method has been added to the `AdjustmentCollection` interface
- BC: The `AdjustmentType` interface extends the `EnumInterface`
BC: Added the `isNeutral()` method to the `Adjustment` interface (true if the adjustment amount is 0)
- BC: Added the `isNotIncluded()` method to the `Adjustment` interface
- BC: Changed the behavior of `AdjustmentCollection::total()`:
1. it ignores "included" adjustments by default,
2. to incorporate the "included" adjustments pass true to the method: `$adjustments->total(withIncluded: true)`
- BC: Changed the `TaxRate` interface so that it extends the `Configurable` interface
- BC: Added the `itemsTotal()` method to the `CheckoutSubject` interface
- BC: Added argument and return types to all `Cart` and `CartManager` interface methods
Expand Down
5 changes: 4 additions & 1 deletion src/Adjustments/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
- Changed minimal Enum requirement to v4.2
- Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection
- BC: Added the `deleteByType()` method to the `AdjustmentCollection` interface + both implementation
- BC: Changed the behavior of `AdjustmentCollection::total()`:
1. it ignores "included" adjustments by default,
2. to incorporate the "included" adjustments pass true to the method: `$adjustments->total(withIncluded: true)`
- BC: The `Adjustable::itemsTotal()` has been renamed to `preAdjustmentTotal()`
- BC: The `invalidateAdjustments()` method has been added to the `Adjustable` interface
- BC: The `exceptTypes()` method has been added to the `AdjustmentCollection` interface
- BC: The `AdjustmentType` interface extends the `EnumInterface`
- BC: Added the `isNeutral()` method to the `Adjustment` interface (true if the adjustment amount is 0)
- BC: Added the `isNotIncluded()` method to the `Adjustment` interface

## 3.x Series

Expand Down
7 changes: 2 additions & 5 deletions src/Adjustments/Contracts/Adjustment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ public function isCharge(): bool;
*/
public function isCredit(): bool;

/**
* Adjustments with value 0 are considered neutral
*/
public function isNeutral(): bool;

public function isIncluded(): bool;

public function isNotIncluded(): bool;

public function isLocked(): bool;

public function lock(): void;
Expand Down
2 changes: 1 addition & 1 deletion src/Adjustments/Contracts/AdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface AdjustmentCollection extends IteratorAggregate, ArrayAccess, Countable
{
public function adjustable(): Adjustable;

public function total(): float;
public function total(bool $withIncluded = false): float;

public function isEmpty(): bool;

Expand Down
9 changes: 4 additions & 5 deletions src/Adjustments/Models/Adjustment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Konekt\Enum\Eloquent\CastsEnums;
use Nette\Utils\Floats;
use Vanilo\Adjustments\Contracts\Adjustable;
use Vanilo\Adjustments\Contracts\Adjuster;
use Vanilo\Adjustments\Contracts\Adjustment as AdjustmentContract;
Expand Down Expand Up @@ -146,14 +145,14 @@ public function isCredit(): bool
return $this->amount < 0;
}

public function isNeutral(): bool
public function isIncluded(): bool
{
return Floats::isZero($this->amount);
return (bool) $this->is_included;
}

public function isIncluded(): bool
public function isNotIncluded() : bool
{
return (bool) $this->is_included;
return !$this->isIncluded();
}

public function isLocked(): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Adjustments/Support/ArrayAdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function adjustable(): Adjustable
return $this->adjustable;
}

public function total(): float
public function total(bool $withIncluded = false): float
{
$result = 0;
foreach ($this->items as $adjustment) {
$result += $adjustment->getAmount();
$result += ($adjustment->isNotIncluded() || $withIncluded) ? $adjustment->getAmount() : 0;
}

return $result;
Expand Down
7 changes: 5 additions & 2 deletions src/Adjustments/Support/RelationAdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ public function create(Adjuster $adjuster): Adjustment
return $adjustment;
}

public function total(): float
public function total(bool $withIncluded = false): float
{
return floatval($this->eloquentCollection()->sum('amount'));
return $withIncluded ?
floatval($this->eloquentCollection()->sum('amount'))
:
floatval($this->eloquentCollection()->filter->isNotIncluded()->sum('amount'));
}

public function isEmpty(): bool
Expand Down
22 changes: 22 additions & 0 deletions src/Adjustments/Tests/Unit/ArrayAdjustmentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public function the_total_can_be_retrieved()
$this->assertEquals(53.53, $collection->total());
}

/** @test */
public function the_total_excludes_non_included_elements_by_default()
{
$collection = new ArrayAdjustmentCollection(new Order());

$collection->add(new Adjustment(['amount' => 40, 'is_included' => true]));
$collection->add(new Adjustment(['amount' => 20, 'is_included' => false]));

$this->assertEquals(20, $collection->total());
}

/** @test */
public function the_total_included_adjustments_can_be_incorporated_in_the_total()
{
$collection = new ArrayAdjustmentCollection(new Order());

$collection->add(new Adjustment(['amount' => 13, 'is_included' => true]));
$collection->add(new Adjustment(['amount' => 21, 'is_included' => false]));

$this->assertEquals(34, $collection->total(true));
}

/** @test */
public function items_can_be_accessed_as_array_members()
{
Expand Down
30 changes: 28 additions & 2 deletions src/Adjustments/Tests/Unit/RelationAdjustmentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ public function the_total_can_be_retrieved()
$this->assertEquals(53.53, $collection->total());
}

/** @test */
public function the_total_excludes_non_included_elements_by_default()
{
$collection = new RelationAdjustmentCollection(
Order::create(['items_total' => 224])
);

$collection->add($this->makeAnAdjustment(40, included: true));
$collection->add($this->makeAnAdjustment(20, included: false));

$this->assertEquals(20, $collection->total());
}

/** @test */
public function the_total_included_adjustments_can_be_incorporated_in_the_total()
{
$collection = new RelationAdjustmentCollection(
Order::create(['items_total' => 224])
);

$collection->add($this->makeAnAdjustment(13, included: true));
$collection->add($this->makeAnAdjustment(21, included: false));

$this->assertEquals(34, $collection->total(true));
}

/** @test */
public function items_can_be_accessed_as_array_members()
{
Expand Down Expand Up @@ -217,7 +243,7 @@ public function items_can_be_filtered_by_type()
$this->assertCount(1, $shippingFees);
}

private function makeAnAdjustment(float $amount = 4.99, AdjustmentType $type = null): Adjustment
private function makeAnAdjustment(float $amount = 4.99, AdjustmentType $type = null, bool $included = false): Adjustment
{
return new Adjustment([
'type' => $type ?? AdjustmentType::SHIPPING(),
Expand All @@ -228,7 +254,7 @@ private function makeAnAdjustment(float $amount = 4.99, AdjustmentType $type = n
'data' => [],
'amount' => $amount,
'is_locked' => false,
'is_included' => false,
'is_included' => $included,
]);
}
}
3 changes: 1 addition & 2 deletions src/Adjustments/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"konekt/concord": "^1.13",
"konekt/enum": "^4.2",
"konekt/enum-eloquent": "^1.9",
"laravel/framework": "^10.38.2",
"nette/utils": "^4.0"
"laravel/framework": "^10.38.2"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down

0 comments on commit 1c1c00a

Please sign in to comment.