Skip to content

Commit d8809f2

Browse files
committed
fix(BO): fix order tax update
1 parent 7922ac1 commit d8809f2

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Adapter/Order/CommandHandler/UpdateProductInOrderHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public function handle(UpdateProductInOrderCommand $command)
126126
// Update invoice, quantity and amounts
127127
$order = $this->orderProductQuantityUpdater->update($order, $orderDetail, $command->getQuantity(), $orderInvoice);
128128

129+
// Update order_detail_tax table without modifying prices
130+
$this->orderDetailUpdater->updateOrderDetailTaxTableOnly($order);
131+
129132
Hook::exec('actionOrderEdited', ['order' => $order]);
130133
} catch (Exception $e) {
131134
throw $e;

src/Adapter/Order/OrderDetailUpdater.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,61 @@ public function updateOrderDetailsTaxes(Order $order): void
219219
}
220220
}
221221

222+
/**
223+
* Updates only the order_detail_tax table without recalculating or modifying order_detail prices
224+
* This is used when prices have been precisely set and we only need to update the tax breakdown
225+
*
226+
* Note: This method does NOT update order_detail table, only order_detail_tax
227+
*/
228+
public function updateOrderDetailTaxTableOnly(Order $order): void
229+
{
230+
list($roundType, $computingPrecision, $taxAddress) = $this->prepareOrderContext($order);
231+
232+
try {
233+
$orderDetailsData = $order->getProducts();
234+
foreach ($orderDetailsData as $orderDetailData) {
235+
$orderDetail = new OrderDetail($orderDetailData['id_order_detail']);
236+
237+
// Clean existing order_detail_tax
238+
Db::getInstance()->delete('order_detail_tax', 'id_order_detail = ' . (int) $orderDetail->id);
239+
240+
$taxCalculator = $this->getTaxCalculatorByAddress($taxAddress, $orderDetail);
241+
$taxesAmount = $taxCalculator->getTaxesAmount($orderDetail->unit_price_tax_excl);
242+
if (!empty($taxesAmount)) {
243+
$orderDetailTaxes = [];
244+
foreach ($taxesAmount as $taxId => $amount) {
245+
$unitAmount = 0;
246+
$totalAmount = 0;
247+
switch ($roundType) {
248+
case Order::ROUND_ITEM:
249+
$unitAmount = (float) Tools::ps_round($amount, $computingPrecision);
250+
$totalAmount = $unitAmount * $orderDetail->product_quantity;
251+
break;
252+
case Order::ROUND_LINE:
253+
$unitAmount = $amount;
254+
$totalAmount = Tools::ps_round($unitAmount * $orderDetail->product_quantity, $computingPrecision);
255+
break;
256+
case Order::ROUND_TOTAL:
257+
$unitAmount = $amount;
258+
$totalAmount = $unitAmount * $orderDetail->product_quantity;
259+
break;
260+
}
261+
$orderDetailTaxes[] = [
262+
'id_order_detail' => $orderDetail->id,
263+
'id_tax' => $taxId,
264+
'unit_amount' => (float) $unitAmount,
265+
'total_amount' => (float) $totalAmount,
266+
];
267+
}
268+
269+
Db::getInstance()->insert('order_detail_tax', $orderDetailTaxes, false);
270+
}
271+
}
272+
} finally {
273+
$this->contextStateManager->restorePreviousContext();
274+
}
275+
}
276+
222277
/**
223278
* @param Order $order
224279
*

0 commit comments

Comments
 (0)