From a90d91cb2004a309ba02274d460062ea92aecdcd Mon Sep 17 00:00:00 2001 From: Aman deep sharma Date: Fri, 4 Jul 2025 11:58:22 +0530 Subject: [PATCH] Fixed: Incorrect data in order slip due to spread tax rounding difference --- classes/order/Order.php | 48 +++++++++-------------------------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/classes/order/Order.php b/classes/order/Order.php index 84af723d7..76661e343 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -2579,7 +2579,7 @@ class OrderCore extends ObjectModel if (!empty($item['id_tax_rules_group'])) { $qty = isset($item['quantity']) ? $item['quantity'] : 0; $price = isset($item['total_price_tax_excl']) ? $item['total_price_tax_excl'] : 0; - + $carry['quantity'] += $qty; $carry['total_price_tax_excl'] += $price; } @@ -2589,7 +2589,7 @@ class OrderCore extends ObjectModel $totalTaxBase = $totals['total_price_tax_excl']; } } - + $objServiceProductOrderDetail = new ServiceProductOrderDetail(); $additionalTaxAmounts = array(); @@ -2621,21 +2621,21 @@ class OrderCore extends ObjectModel 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM ); - + // We are getting auto added service for specific room.There will be only on htl_booking_detail but can have multiple auto added service with room. // Note: All the auto added service with room have same id_tax_rule group $autoAddedServiceData = array_shift($autoAddedServiceData); $autoAddedServices = $autoAddedServiceData['additional_services']; - + // Calculate total quantity of auto-added services $totalAutoAddedQty = array_reduce($autoAddedServices, function ($quantity, $service) { $qty = isset($service['quantity']) ? $service['quantity'] : 0; return $quantity + $qty; }, 0); - + $firstAutoAddedService = array_shift($autoAddedServices); $idTaxRuleGroup = $firstAutoAddedService['id_tax_rules_group']; - + // If tax group is different from order detail, add tax info separately if ($order_detail['id_tax_rules_group'] != $idTaxRuleGroup) { $autoAddedServiceTaxManager = TaxManagerFactory::getManager($objAddress, (int)$idTaxRuleGroup); @@ -2653,11 +2653,6 @@ class OrderCore extends ObjectModel 'unit_amount' => $amount, 'total_amount' => Tools::processPriceRounding($amount, $totalAutoAddedQty), ); - - if (!isset($breakdown[$taxId])) { - $breakdown[$taxId] = array('tax_amount' => 0); - } - $breakdown[$taxId]['tax_amount'] += Tools::processPriceRounding($amount, $totalAutoAddedQty); } } else { // Calculate tax for the total price @@ -2673,10 +2668,10 @@ class OrderCore extends ObjectModel foreach ($taxesList as $detailTax) { if ($detailTax['total_amount'] > 0) { $taxId = $detailTax['id_tax']; - + $unitAmount = $detailTax['unit_amount'] + ($additionalTaxAmounts[$taxId] ?? 0); $totalAmount = $detailTax['total_amount'] + ($additionalTaxAmounts[$taxId] ?? 0); - + if (!isset($groupedTaxDetails[$taxId])) { $groupedTaxDetails[$taxId] = array( 'id_order_detail' => $id_order_detail, @@ -2694,11 +2689,6 @@ class OrderCore extends ObjectModel $groupedTaxDetails[$taxId]['total_amount'] += $totalAmount; } } - - if (!isset($breakdown[$taxId])) { - $breakdown[$taxId] = array('tax_amount' => 0); - } - $breakdown[$taxId]['tax_amount'] += $taxBaseShare > 0 ? $totalAmount : 0; } } } else { @@ -2709,7 +2699,7 @@ class OrderCore extends ObjectModel foreach ($tax_calculator->getTaxesAmount($unit_price_tax_excl) as $id_tax => $unit_amount) { $total_tax_base = 0; $total_amount = Tools::processPriceRounding($unit_amount, $quantity); - + if (!isset($groupedTaxDetails[$id_tax])) { $groupedTaxDetails[$id_tax] = array( 'id_order_detail' => $id_order_detail, @@ -2726,11 +2716,6 @@ class OrderCore extends ObjectModel $groupedTaxDetails[$id_tax]['total_amount'] += $total_amount; } } - - if (!isset($breakdown[$id_tax])) { - $breakdown[$id_tax] = array('tax_amount' => 0); - } - $breakdown[$id_tax]['tax_amount'] += $taxBaseShare > 0 ? $totalAmount : 0; } } @@ -2742,7 +2727,7 @@ class OrderCore extends ObjectModel /* * This tax recalculation logic is intentionally disabled. - * + * * Taxes have already been calculated and stored in the `order_detail_tax` table, * so there is no need to recompute them here. * @@ -2772,19 +2757,6 @@ class OrderCore extends ObjectModel } - if (!empty($order_detail_tax_rows)) { - foreach ($breakdown as $data) { - $actual_total_tax += Tools::ps_round($data['tax_amount'], _PS_PRICE_COMPUTE_PRECISION_, $this->round_mode); - } - - $order_ecotax_tax = Tools::ps_round($order_ecotax_tax, _PS_PRICE_COMPUTE_PRECISION_, $this->round_mode); - - $tax_rounding_error = $expected_total_tax - $actual_total_tax - $order_ecotax_tax; - if ($tax_rounding_error != 0) { - Tools::spreadAmount($tax_rounding_error, _PS_PRICE_COMPUTE_PRECISION_, $order_detail_tax_rows, 'total_amount'); - } - } - /* * The following tax adjustment logic was disabled as it is not currently in use. * It was intended to handle rounding errors in tax and base amounts by spreading