From f0eac28b42a492450e8ddeac7c9a6e1e9d126424 Mon Sep 17 00:00:00 2001 From: Aman deep sharma Date: Fri, 20 Jun 2025 15:39:19 +0530 Subject: [PATCH] Remove isBookingProduct usage and optimize related logic --- classes/PaymentModule.php | 6 +++ classes/order/Order.php | 34 ++++++++---- classes/order/OrderDetail.php | 48 +++++++++-------- .../classes/ServiceProductOrderDetail.php | 54 ++++++++++--------- 4 files changed, 87 insertions(+), 55 deletions(-) diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 9cca21cb6..dde838155 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -1571,6 +1571,12 @@ abstract class PaymentModuleCore extends Module } } } + /** + * This function is not being called because taxes are already saved correctly. + * The issue lies only in displaying the tax breakdown. + * + * $order->updateOrderDetailTax(); + */ } else { $error = Tools::displayError('Order creation failed'); PrestaShopLogger::addLog($error, 4, '0000002', 'Cart', intval($order->id_cart)); diff --git a/classes/order/Order.php b/classes/order/Order.php index 99814f532..39fe39691 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -2561,22 +2561,38 @@ class OrderCore extends ObjectModel $tax_rates[$tax->id] = $tax->rate; } - if (!Product::isBookingProduct($order_detail['product_id'])) { - $quantity = Db::getInstance()->getValue( - 'SELECT SUM(`quantity`) - FROM `'._DB_PREFIX_.'service_product_order_detail` - WHERE `id_order_detail` = '.(int)$id_order_detail.' - AND `id_tax_rules_group` > 0 ' - ); - } $totalTaxBase = Tools::processPriceRounding($unit_price_tax_excl, $quantity); + if (!$order_detail['is_booking_product']) { + $objServiceProductOrderDetail = new ServiceProductOrderDetail(); + $serviceProductDetail = $objServiceProductOrderDetail->getServiceProductsInOrder( + $order_detail['id_order'], + $id_order_detail + ); + if ($serviceProductDetail = $objServiceProductOrderDetail->getServiceProductsInOrder( + $order_detail['id_order'], + $id_order_detail + )) { + $totals = array_reduce($serviceProductDetail, function ($carry, $item) { + if (!empty($item['id_tax_rules_group'])) { + $qty = isset($item['quantity']) ? $item['quantity'] : 0; + $price = isset($item['unit_price_tax_excl']) ? $item['unit_price_tax_excl'] : 0; + + $carry['quantity'] += $qty; + $carry['total_price_tax_excl'] += $price; + } + return $carry; + }, ['quantity' => 0, 'total_price_tax_excl' => 0]); + $quantity = $totals['quantity']; + $totalTaxBase = $totals['total_price_tax_excl']; + } + } $objServiceProductOrderDetail = new ServiceProductOrderDetail(); $groupedTaxDetails = array(); $additionalTaxAmounts = array(); if ( - Product::isBookingProduct($order_detail['product_id']) && + $order_detail['is_booking_product'] && $autoAddedPriceExcl = $objServiceProductOrderDetail->getRoomTypeServiceProducts( $order_detail['id_order'], 0, 0, diff --git a/classes/order/OrderDetail.php b/classes/order/OrderDetail.php index b0e165b1d..75d254e40 100644 --- a/classes/order/OrderDetail.php +++ b/classes/order/OrderDetail.php @@ -404,7 +404,7 @@ class OrderDetailCore extends ObjectModel /** * Calculate tax rule groups for the service product based on each associated room type. */ - if (!Product::isBookingProduct($this->product_id)) { + if (!$this->is_booking_product && $this->selling_preference_type == Product::SELLING_PREFERENCE_WITH_ROOM_TYPE) { // Get all associated room type IDs for this service product and cart $associatedRoomTypes = Db::getInstance()->executeS( 'SELECT hcbd.`id_product` @@ -420,22 +420,21 @@ class OrderDetailCore extends ObjectModel $taxGroupInfoList = array(); foreach ($associatedRoomTypeIds as $idRoomType) { - $taxGroupInfo = Db::getInstance()->getRow( - 'SELECT `id_element`, `id_tax_rules_group` - FROM `'._DB_PREFIX_.'htl_room_type_service_product_price` - WHERE `id_product` = '.(int)$this->product_id.' - AND `id_element` = '.(int)$idRoomType - ); - - if ($taxGroupInfo) { - // Specific tax rules found - $taxGroupInfoList[] = $taxGroupInfo; - } else { - // Use default tax rule for the service product - $defaultTaxGroupId = (int)Product::getIdTaxRulesGroupByIdProduct($this->product_id, $this->context); + if ($serviceProductPriceRoomInfo = RoomTypeServiceProductPrice::getProductRoomTypePriceAndTax( + $this->product_id, + $idRoomType, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + )) { + //Special tax rule group for the Service product accroding to Room type $taxGroupInfoList[] = array( - 'id_element' => $idRoomType, - 'id_tax_rules_group' => $defaultTaxGroupId + 'id_room_type' => $idRoomType, + 'id_tax_rules_group' => $serviceProductPriceRoomInfo['id_tax_rules_group'] + ); + } else { + // Use default tax rule group for the service product + $taxGroupInfoList[] = array( + 'id_room_type' => $idRoomType, + 'id_tax_rules_group' => $objOrderDetail->id_tax_rules_group ); } } @@ -446,7 +445,7 @@ class OrderDetailCore extends ObjectModel * Calculate service product tax separately for each room type because * a service product can be attached to multiple room types with different tax rules. */ - if (!Product::isBookingProduct($this->product_id) && isset($taxGroupInfoList) && $taxGroupInfoList) { + if (!$this->is_booking_product && isset($taxGroupInfoList) && $taxGroupInfoList) { $objServiceProductCartDetail = new ServiceProductCartDetail(); // Saving tax details according to the service product tax groups for different rooms foreach ($taxGroupInfoList as $taxGroupInfo) { @@ -470,15 +469,22 @@ class OrderDetailCore extends ObjectModel array(), null, null, - $taxGroupInfo['id_element'], + $taxGroupInfo['id_room_type'], $this->product_id ); - $serviceProduct = array_shift($serviceProductCartData); - $unit_price_tax_excl = $serviceProduct['unit_price_tax_excl']; + $total_price_tax_excl = array_reduce($serviceProductCartData, function ($totalPriceTaxExcl, $item) { + return $totalPriceTaxExcl + (isset($item['total_price_tax_excl']) ? $item['total_price_tax_excl'] : 0); + }, 0); + + $quantity = array_reduce($serviceProductCartData, function ($totalQty, $item) { + return $totalQty + (isset($item['quantity']) ? $item['quantity'] : 0); + }, 0); + + $unit_price_tax_excl = $total_price_tax_excl / $quantity; foreach ($this->tax_calculator->getTaxesAmount($unit_price_tax_excl) as $id_tax => $amount) { - $total_amount = Tools::processPriceRounding($amount, $serviceProduct['quantity'], $order->round_type, $order->round_mode); + $total_amount = Tools::processPriceRounding($amount, $quantity, $order->round_type, $order->round_mode); $values .= '('.(int)$this->id.','.(int)$id_tax.','.(float)$amount.','.(float)$total_amount.'),'; } diff --git a/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php b/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php index 76151f738..6802a6700 100644 --- a/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php +++ b/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php @@ -77,37 +77,41 @@ class ServiceProductOrderDetail extends ObjectModel { if (Validate::isLoadedObject($objOrder = new Order((int)$this->id_order)) && Validate::isLoadedObject($objServiceProduct = new Product((int)$this->id_product)) + && Validate::isLoadedObject($objOrderDetail = new OrderDetail((int)$this->id_order_detail)) ) { - $objAddress = new Address((int)$objOrder->id_address_tax); - $idRoomType = Db::getInstance()->getValue( - 'SELECT `id_product` - FROM `'._DB_PREFIX_.'htl_booking_detail` - WHERE `id` = '.(int)$this->id_htl_booking_detail - ); + if ($objOrderDetail->selling_preference_type == Product::SELLING_PREFERENCE_WITH_ROOM_TYPE) { + if ($this->id_htl_booking_detail + && Validate::isLoadedObject($objHotelBookingDetail = new HotelBookingDetail((int)$this->id_htl_booking_detail)) + ) { + $idRoomType = $objHotelBookingDetail->id_product; + $objAddress = new Address((int)$objOrder->id_address_tax); + if ($objServiceProduct->auto_add_to_cart && $objServiceProduct->price_addition_type == Product::PRICE_ADDITION_TYPE_WITH_ROOM) { + if (Validate::isLoadedObject($objRoomTypeProduct = new Product((int)$idRoomType))) { + $this->id_tax_rules_group = $objRoomTypeProduct->id_tax_rules_group; + } + } else { + if ($serviceProductPriceRoomInfo = RoomTypeServiceProductPrice::getProductRoomTypePriceAndTax( + $this->id_product, + $idRoomType, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + )) { + //Special tax rule group for the Service product accroding to Room type + $this->id_tax_rules_group = $serviceProductPriceRoomInfo['id_tax_rules_group']; + } else { + // Use default tax rule group for the service product + $this->id_tax_rules_group = $objOrderDetail->id_tax_rules_group; + } + } - if ($objServiceProduct->price_addition_type == Product::PRICE_ADDITION_TYPE_WITH_ROOM && $objServiceProduct->auto_add_to_cart) { - if (Validate::isLoadedObject($objRoomTypeProduct = new Product((int)$idRoomType))) { - $this->id_tax_rules_group = $objRoomTypeProduct->id_tax_rules_group; + $taxCalculator = TaxManagerFactory::getManager($objAddress, $this->id_tax_rules_group)->getTaxCalculator(); + $this->tax_computation_method = (int)$taxCalculator->computation_method; } } else { - $idTaxRulesGroup = Db::getInstance()->getValue( - 'SELECT `id_tax_rules_group` - FROM `'._DB_PREFIX_.'htl_room_type_service_product_price` - WHERE `id_product` = '.(int)$this->id_product.' - AND `id_element` = '.(int)$idRoomType - ); - - if ($idTaxRulesGroup === false || $idTaxRulesGroup === null) { - // Use default tax rule for the service product - $idTaxRulesGroup = (int)Product::getIdTaxRulesGroupByIdProduct($this->id_product, Context::getContext()); - } - - $this->id_tax_rules_group = $idTaxRulesGroup; + // Use default tax rule group for the service product + $this->id_tax_rules_group = $objOrderDetail->id_tax_rules_group; + $this->tax_computation_method = $objOrderDetail->tax_computation_method; } - $taxCalculator = TaxManagerFactory::getManager($objAddress, $this->id_tax_rules_group)->getTaxCalculator(); - $this->tax_computation_method = (int)$taxCalculator->computation_method; - return parent::add($autodate, $null_values); }