@@ -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