diff --git a/classes/order/OrderDetail.php b/classes/order/OrderDetail.php index c3a009baa..96f932f3a 100644 --- a/classes/order/OrderDetail.php +++ b/classes/order/OrderDetail.php @@ -597,6 +597,7 @@ class OrderDetailCore extends ObjectModel $result = $this->saveTaxCalculator($order, true); OrderTaxDetail::rescopeVatAfterReset((int) $this->id); + return $result; } diff --git a/classes/order/OrderTaxDetail.php b/classes/order/OrderTaxDetail.php index c2f2c0302..a878a1c80 100644 --- a/classes/order/OrderTaxDetail.php +++ b/classes/order/OrderTaxDetail.php @@ -346,7 +346,6 @@ class OrderTaxDetailCore extends ObjectModel /** * * @param int $idOrderDetail - * @param int $idCurrency Currency to use when recalculating tourism tax * @return bool */ public static function rescopeVatAfterReset($idOrderDetail) @@ -373,8 +372,6 @@ class OrderTaxDetailCore extends ObjectModel $result = self::recomputeInclusivePrice($idOrderDetail, 0, 0) && $result; } - $result = self::recalculateTourismTax($idOrderDetail) && $result; - return $result; } @@ -886,7 +883,8 @@ class OrderTaxDetailCore extends ObjectModel return false; } - $unitPriceTaxExcl = Tools::ps_round((float) $booking->total_price_tax_excl / $numNights, 6); + // getTaxesAmount() expects the price in the default currency and converts the amount to 'idCurrency' itself + $unitPriceTaxExcl = Tools::ps_round(Tools::convertPrice((float) $booking->total_price_tax_excl / $numNights, (int) $order->id_currency, false), 6); $childrenAges = array(); if ($booking->child_ages) { @@ -970,7 +968,7 @@ class OrderTaxDetailCore extends ObjectModel return array( 'idTaxRulesGroup' => $idTaxRulesGroup, 'address' => $address, - 'unitPriceTaxExcl' => (float) $serviceLine->unit_price_tax_excl, + 'unitPriceTaxExcl' => Tools::convertPrice((float) $serviceLine->unit_price_tax_excl, (int) $order->id_currency, false), 'checkInDate' => $checkInDate, 'numNights' => $numNights, 'numAdults' => $numAdults, @@ -992,7 +990,7 @@ class OrderTaxDetailCore extends ObjectModel * @param array $params * @return void */ - public static function saveTourismTaxFromParams(array $params) + protected static function saveTourismTaxFromParams(array $params) { self::saveTourismTax( $params['idTaxRulesGroup'], @@ -1013,37 +1011,6 @@ class OrderTaxDetailCore extends ObjectModel ); } - /** - * Recalculate the tourism tax of every room booking and service line under an order detail. - * Tourism tax depends on the order currency and the booking/service prices, so call this after they are changed (e.g. order currency change). - * - * @param int $idOrderDetail - * @return void - */ - public static function recalculateTourismTax($idOrderDetail) - { - $idOrderDetail = (int) $idOrderDetail; - $db = Db::getInstance(); - - $idsHtlBooking = $db->executeS( - 'SELECT `id` FROM `' . _DB_PREFIX_ . 'htl_booking_detail` WHERE `id_order_detail` = ' . $idOrderDetail - ); - foreach ((array) $idsHtlBooking as $row) { - if ($params = self::buildRoomTaxParams((int) $row['id'])) { - self::saveTourismTaxFromParams($params); - } - } - - $idsServiceLine = $db->executeS( - 'SELECT `id_service_product_order_detail` FROM `' . _DB_PREFIX_ . 'service_product_order_detail` WHERE `id_order_detail` = ' . $idOrderDetail - ); - foreach ((array) $idsServiceLine as $row) { - if ($params = self::buildServiceLineTaxParams((int) $row['id_service_product_order_detail'])) { - self::saveTourismTaxFromParams($params); - } - } - } - /** * * @param int $idOrder diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index b0d42b9d8..c58d0ab8b 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -2521,6 +2521,42 @@ class AdminOrdersControllerCore extends AdminController $order_detail->updateTaxAmount($order); } + // Tourism tax depends on the order currency, and updateTaxAmount() only rebuilds VAT, so rebuild it from the converted prices + if (Configuration::get('QLO_USE_TOURISM_TAX')) { + $tourismTaxParamsList = array(); + if ($activeRoomBookings = $objHtlBookingDetail->getOrderCurrentDataByOrderId($order->id, array(), false)) { + foreach ($activeRoomBookings as $roomBooking) { + $tourismTaxParamsList[] = OrderTaxDetail::buildRoomTaxParams((int) $roomBooking['id']); + } + } + if ($orderServices = $objRoomTypeServProdOrderDtl->getServiceProductsInOrder($order->id)) { + foreach ($orderServices as $serviceProduct) { + if (!$serviceProduct['is_refunded'] && !$serviceProduct['is_cancelled']) { + $tourismTaxParamsList[] = OrderTaxDetail::buildServiceLineTaxParams((int) $serviceProduct['id_service_product_order_detail']); + } + } + } + foreach (array_filter($tourismTaxParamsList) as $tourismTaxParams) { + OrderTaxDetail::saveTourismTax( + $tourismTaxParams['idTaxRulesGroup'], + $tourismTaxParams['address'], + $tourismTaxParams['unitPriceTaxExcl'], + $tourismTaxParams['checkInDate'], + $tourismTaxParams['numNights'], + $tourismTaxParams['numAdults'], + $tourismTaxParams['childrenAges'], + $tourismTaxParams['idCurrency'], + $tourismTaxParams['collectionType'], + $tourismTaxParams['idLang'], + $tourismTaxParams['quantity'], + $tourismTaxParams['idOrder'], + $tourismTaxParams['idOrderDetail'], + $tourismTaxParams['idHtlBooking'], + $tourismTaxParams['idServiceProductOrderDetail'] + ); + } + } + // update Order refund prices (order_return and order_return_detail) if ($orderReturns = OrderReturn::getOrdersReturn($order->id_customer, $order->id)) { $fields = array(