Skip to content

Commit 0217950

Browse files
Fixed the SimpleTax adjusters' calculation logic when the tax is included in the price
1 parent e036d91 commit 0217950

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Added the polymorphic `source` relation/fields to the `Adjustment` model
3636
- Added the `getSource` and `setSource` methods to the `Adjustment` model
3737
- Changed the `setAdjustable` method to return the itself (was `void` before)
38+
- Changed (fixed, in fact) the `SimpleTax`/`SimpleTaxDeduction` adjusters to properly calculate the tax amount when it is included in the price
3839
- Changed the default-generated video hash to a base 58 ULID instead of nano id
3940
- Changed the minimum Laravel version requirements to v10.48, v11.44 and v12.2 respectively
4041

src/Adjustments/Adjusters/SimpleTax.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function isIncluded(): bool
6161

6262
private function calculateTaxAmount(Adjustable $adjustable): float
6363
{
64-
return $adjustable->preAdjustmentTotal() * $this->rate / 100;
64+
return match ($this->isIncluded) {
65+
true => $adjustable->preAdjustmentTotal() / (100 + $this->rate) * $this->rate,
66+
false => $adjustable->preAdjustmentTotal() * $this->rate / 100,
67+
};
6568
}
6669

6770
private function getModelAttributes(Adjustable $adjustable): array

src/Adjustments/Adjusters/SimpleTaxDeduction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function isIncluded(): bool
6161

6262
private function calculateTaxAmount(Adjustable $adjustable): float
6363
{
64-
return -1 * $adjustable->preAdjustmentTotal() * $this->rate / 100;
64+
return -1 * match ($this->isIncluded) {
65+
true => $adjustable->preAdjustmentTotal() / (100 + $this->rate) * $this->rate,
66+
false => $adjustable->preAdjustmentTotal() * $this->rate / 100,
67+
};
6568
}
6669

6770
private function getModelAttributes(Adjustable $adjustable): array

src/Adjustments/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added the `getSource` and `setSource` methods to the `Adjustment` model
1010
- Changed the `setAdjustable` method to return the itself (was `void` before)
1111
- Changed the minimum Laravel version requirements to v10.48, v11.44 and v12.2 respectively
12+
- Changed (fixed, in fact) the `SimpleTax`/`SimpleTaxDeduction` adjusters to properly calculate the tax amount when it is included in the price
1213

1314
## 5.0.0
1415
##### 2025-09-03
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Feature;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Vanilo\Adjustments\Adjusters\SimpleTax;
9+
use Vanilo\Adjustments\Adjusters\SimpleTaxDeduction;
10+
use Vanilo\Adjustments\Tests\Examples\Order;
11+
use Vanilo\Adjustments\Tests\TestCase;
12+
13+
class SimpleTaxTest extends TestCase
14+
{
15+
#[Test] public function a_simple_non_included_tax_can_be_added_to_an_adjustable_order()
16+
{
17+
$order = Order::create(['items_total' => 100]);
18+
$order->adjustments()->create(new SimpleTax(20, false));
19+
20+
$this->assertCount(1, $order->adjustments());
21+
$this->assertEquals(20, $order->adjustments()->total());
22+
$this->assertEquals(120, $order->total());
23+
}
24+
25+
#[Test] public function an_included_tax_gets_calculated_correctly_based_on_the_rate_and_order_items_total()
26+
{
27+
$order = Order::create(['items_total' => 119]);
28+
$order->adjustments()->create(new SimpleTax(19, true));
29+
30+
$this->assertEquals(119, $order->total());
31+
$this->assertTrue($order->adjustments()->first()->isCharge());
32+
$this->assertCount(1, $order->adjustments());
33+
$this->assertEquals(19, $order->adjustments()->first()->getAmount());
34+
}
35+
36+
#[Test] public function a_simple_non_included_tax_deductaion_can_be_added_to_an_adjustable_order()
37+
{
38+
$order = Order::create(['items_total' => 100]);
39+
$order->adjustments()->create(new SimpleTaxDeduction(20, false));
40+
41+
$this->assertCount(1, $order->adjustments());
42+
$this->assertEquals(-20, $order->adjustments()->total());
43+
$this->assertEquals(80, $order->total());
44+
}
45+
46+
#[Test] public function an_included_tax_deducation_gets_calculated_correctly_based_on_the_rate_and_order_items_total()
47+
{
48+
$order = Order::create(['items_total' => 119]);
49+
$order->adjustments()->create(new SimpleTaxDeduction(19, true));
50+
51+
$this->assertEquals(119, $order->total());
52+
$this->assertTrue($order->adjustments()->first()->isCredit());
53+
$this->assertCount(1, $order->adjustments());
54+
$this->assertEquals(-19, $order->adjustments()->first()->getAmount());
55+
}
56+
}

0 commit comments

Comments
 (0)