From 40a4d07067c279ebb8c9e14c7082f7dd396e7aa0 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 10 Dec 2024 12:01:36 +0100 Subject: [PATCH] Fix: The "total" column of the shipping order-lines in the database doesn't include taxes. This is because the shippingTotal is retrieved from the Cart's shippingAddress, and the taxes aren't added to that shippingTotal by the CalculateTax pipeline. Also: The TestTaxDriver doesn't take into account whether prices are stored inclusive of tax or not, which results in it calculating the wrong taxBreakdown if they are. --- packages/core/src/Pipelines/Cart/CalculateTax.php | 15 ++++++++++----- tests/core/Stubs/TestTaxDriver.php | 6 ++++++ tests/core/Unit/Models/CartTest.php | 10 ++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/core/src/Pipelines/Cart/CalculateTax.php b/packages/core/src/Pipelines/Cart/CalculateTax.php index 6927e071d2..ce14d05237 100644 --- a/packages/core/src/Pipelines/Cart/CalculateTax.php +++ b/packages/core/src/Pipelines/Cart/CalculateTax.php @@ -75,11 +75,6 @@ public function handle(Cart $cart, Closure $next) $taxTotal += $shippingTaxTotal?->value; - if ($cart->shippingAddress && ! $cart->shippingOptionOverride) { - $cart->shippingAddress->taxBreakdown = $shippingTax; - $cart->shippingAddress->shippingTaxTotal = $shippingTaxTotal; - } - $taxBreakDownAmounts = $taxBreakDownAmounts->merge( $shippingTax->amounts ); @@ -93,6 +88,16 @@ public function handle(Cart $cart, Closure $next) $cart->currency, 1 ); + + if ($cart->shippingAddress && ! $cart->shippingOptionOverride) { + $cart->shippingAddress->taxBreakdown = $shippingTax; + $cart->shippingAddress->shippingTaxTotal = $shippingTaxTotal; + $cart->shippingAddress->shippingTotal = new Price( + $shippingTotal, + $cart->currency, + 1 + ); + } } $cart->taxTotal = new Price($taxTotal, $cart->currency, 1); diff --git a/tests/core/Stubs/TestTaxDriver.php b/tests/core/Stubs/TestTaxDriver.php index 80a85109ac..807b5599bc 100644 --- a/tests/core/Stubs/TestTaxDriver.php +++ b/tests/core/Stubs/TestTaxDriver.php @@ -108,6 +108,12 @@ public function getBreakdown($subTotal): TaxBreakdown $variant = $this->purchasable ?: ProductVariant::factory()->create(); + if (prices_inc_tax()) { + // Remove tax from price + $totalTaxPercentage = $taxAmounts->sum('percentage') / 100; // E.g. 0.2 for 20% + $subTotal = round($subTotal / (1 + $totalTaxPercentage)); + } + foreach ($taxAmounts as $amount) { $result = round($subTotal * ($amount->percentage / 100)); diff --git a/tests/core/Unit/Models/CartTest.php b/tests/core/Unit/Models/CartTest.php index 8759032a9f..7d4e7bbd9a 100644 --- a/tests/core/Unit/Models/CartTest.php +++ b/tests/core/Unit/Models/CartTest.php @@ -741,12 +741,22 @@ expect($cart->shippingTotal->value)->toEqual(600); expect($cart->total->value)->toEqual(720); + expect($cart->shippingAddress->shippingSubTotal->value)->toEqual(500); + expect($cart->shippingAddress->shippingTaxTotal->value)->toEqual(100); + expect($cart->shippingAddress->shippingTotal->value)->toEqual(600); + Config::set('lunar.pricing.stored_inclusive_of_tax', true); $cart->recalculate(); + expect($cart->subTotal->value)->toEqual(100); + expect($cart->shippingSubTotal->value)->toEqual(500); expect($cart->shippingTotal->value)->toEqual(500); expect($cart->total->value)->toEqual(600); + + expect($cart->shippingAddress->shippingSubTotal->value)->toEqual(500); + expect($cart->shippingAddress->shippingTaxTotal->value)->toEqual(83); + expect($cart->shippingAddress->shippingTotal->value)->toEqual(500); }); test('can create a discount breakdown', function () {