From 2900872f322e3437167fa8fd6fcd53ac0f45fbe4 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 21 Jun 2023 19:35:50 +0100 Subject: [PATCH] Rollback tax calculation fix from the other day --- src/Tax/Standard/TaxEngine.php | 8 ++++---- tests/Tax/StandardTaxEngineTest.php | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Tax/Standard/TaxEngine.php b/src/Tax/Standard/TaxEngine.php index e487ce26d..2d8f2759c 100644 --- a/src/Tax/Standard/TaxEngine.php +++ b/src/Tax/Standard/TaxEngine.php @@ -38,8 +38,8 @@ public function calculateForLineItem(Order $order, LineItem $lineItem): TaxCalcu } } - $taxAmount = $lineItem->total() * ($taxRate->rate() / 100); - $itemTax = (int) round($taxAmount); + $taxAmount = ($lineItem->total() / 100) * ($taxRate->rate() / (100 + $taxRate->rate())); + $itemTax = (int) round($taxAmount * 100); return new TaxCalculation($itemTax, $taxRate->rate(), $taxRate->includeInPrice()); } @@ -111,8 +111,8 @@ public function calculateForShipping(Order $order, ShippingMethod $shippingMetho } } - $taxAmount = $order->shippingTotal() * ($taxRate->rate() / 100); - $itemTax = (int) round($taxAmount); + $taxAmount = ($order->shippingTotal() / 100) * ($taxRate->rate() / (100 + $taxRate->rate())); + $itemTax = (int) round($taxAmount * 100); return new TaxCalculation($itemTax, $taxRate->rate(), $taxRate->includeInPrice()); } diff --git a/tests/Tax/StandardTaxEngineTest.php b/tests/Tax/StandardTaxEngineTest.php index d5f37c6f8..c7bd008dc 100644 --- a/tests/Tax/StandardTaxEngineTest.php +++ b/tests/Tax/StandardTaxEngineTest.php @@ -93,13 +93,13 @@ // Ensure tax on line items are right $this->assertSame($recalculate->lineItems()->first()->tax(), [ - 'amount' => 200, + 'amount' => 167, 'rate' => 20, 'price_includes_tax' => false, ]); // Ensure global order tax is right - expect(200)->toBe($recalculate->taxTotal()); + expect(167)->toBe($recalculate->taxTotal()); }); test('can correctly calculate line item tax rate based on region', function () { @@ -162,13 +162,13 @@ // Ensure tax on line items are right $this->assertSame($recalculate->lineItems()->first()->tax(), [ - 'amount' => 150, + 'amount' => 130, 'rate' => 15, 'price_includes_tax' => false, ]); // Ensure global order tax is right - expect(150)->toBe($recalculate->taxTotal()); + expect(130)->toBe($recalculate->taxTotal()); }); test('can calculate line item tax rate when included in price', function () { @@ -229,13 +229,13 @@ // Ensure tax on line items are right $this->assertSame($recalculate->lineItems()->first()->tax(), [ - 'amount' => 200, + 'amount' => 167, 'rate' => 20, 'price_includes_tax' => true, ]); // Ensure global order tax is right - expect(200)->toBe($recalculate->taxTotal()); + expect(167)->toBe($recalculate->taxTotal()); }); test('can use default line item tax rate if no rate available', function () { @@ -289,7 +289,7 @@ expect(12)->toBe($recalculate->lineItems()->first()->tax()['rate']); // Ensure global order tax is right - expect(120)->toBe($recalculate->taxTotal()); + expect(107)->toBe($recalculate->taxTotal()); }); test('throws prevent checkout exception if no rate available', function () { @@ -400,7 +400,7 @@ expect(99)->toBe($recalculate->lineItems()->first()->tax()['rate']); // Ensure global order tax is right - expect(990)->toBe($recalculate->taxTotal()); + expect(497)->toBe($recalculate->taxTotal()); }); test('throws prevent checkout exception if no address provided', function () { @@ -525,8 +525,8 @@ $recalculate = $order->recalculate(); // Ensure 10% is deducted from shipping_total (500 -> 450) - expect($recalculate->shippingTotal())->toBe(450); + expect($recalculate->shippingTotal())->toBe(455); // Ensure tax total is 50 - expect($recalculate->taxTotal())->toBe(50); + expect($recalculate->taxTotal())->toBe(45); });