diff --git a/classes/Cart.php b/classes/Cart.php index b1e132d31..b5d1eb461 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1816,15 +1816,15 @@ class CartCore extends ObjectModel if ($servicePorducts) { foreach ($servicePorducts as $servicePorduct) { if ($with_taxes) { - $priceAdd = $servicePorduct['unit_price_tax_incl']; + $priceAdd = $servicePorduct['total_price_tax_incl']; } else { - $priceAdd = $servicePorduct['unit_price_tax_excl']; + $priceAdd = $servicePorduct['total_price_tax_excl']; } if ($ps_round_type == Order::ROUND_TOTAL) { - $products_total[$id_tax_rules_group.'_'.$id_address] += Tools::processPriceRounding($priceAdd, (int)$servicePorduct['quantity']); + $products_total[$id_tax_rules_group.'_'.$id_address] += $priceAdd; } else { - $products_total[$id_tax_rules_group] += Tools::processPriceRounding($priceAdd, (int)$servicePorduct['quantity']); + $products_total[$id_tax_rules_group] += $priceAdd; } } } diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index f1eba0008..dde838155 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -1571,8 +1571,12 @@ abstract class PaymentModuleCore extends Module } } } - - $order->updateOrderDetailTax(); + /** + * 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 abcd65007..3268193c2 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -2520,78 +2520,174 @@ class OrderCore extends ObjectModel $order_ecotax_tax = 0; - $tax_rates = array(); - foreach ($order_details as $order_detail) { + $tax_rates = array(); $id_order_detail = $order_detail['id_order_detail']; + + $taxesList = OrderDetail::getTaxListStatic($id_order_detail); + if (!$taxesList) { + continue; + } + $tax_calculator = OrderDetail::getTaxCalculatorStatic($id_order_detail); - - // TODO: probably need to make an ecotax tax breakdown here instead, - // but it seems unlikely there will be different tax rates applied to the - // ecotax in the same order in the real world - $unit_ecotax_tax = $order_detail['ecotax'] * $order_detail['ecotax_tax_rate'] / 100.0; - $order_ecotax_tax += $order_detail['product_quantity'] * $unit_ecotax_tax; - - $discount_ratio = 0; - - if ($this->total_products > 0) { - $discount_ratio = ($order_detail['unit_price_tax_excl'] + $order_detail['ecotax']) / $this->total_products; - } - - // share of global discount - $discounted_price_tax_excl = $order_detail['unit_price_tax_excl'] - $discount_ratio * $order_discount_tax_excl; - // specific discount - if (!empty($product_specific_discounts[$order_detail['product_id']])) { - $discounted_price_tax_excl -= $product_specific_discounts[$order_detail['product_id']]; - } - $quantity = $order_detail['product_quantity']; + $unit_price_tax_excl = $order_detail['total_price_tax_excl'] / $quantity; + + /* + * Discounted taxes are intentionally not calculated here, as we do not want to display them. + * + * TODO: Consider implementing a proper ecotax breakdown if needed in future. + * However, it is unlikely that different tax rates would apply to ecotax within the same order. + * + * + * $unit_ecotax_tax = $order_detail['ecotax'] * $order_detail['ecotax_tax_rate'] / 100.0; + * $order_ecotax_tax += $order_detail['product_quantity'] * $unit_ecotax_tax; + * + * $discount_ratio = 0; + * if ($this->total_products > 0) { + * $discount_ratio = ($order_detail['unit_price_tax_excl'] + $order_detail['ecotax']) / $this->total_products; + * } + * + * // Apply share of global discount + * $discounted_price_tax_excl = $order_detail['unit_price_tax_excl'] - $discount_ratio * $order_discount_tax_excl; + * + * // Apply specific product-level discount + * if (!empty($product_specific_discounts[$order_detail['product_id']])) { + * $discounted_price_tax_excl -= $product_specific_discounts[$order_detail['product_id']]; + * } + */ foreach ($tax_calculator->taxes as $tax) { $tax_rates[$tax->id] = $tax->rate; } - foreach ($tax_calculator->getTaxesAmount($discounted_price_tax_excl) as $id_tax => $unit_amount) { - $total_tax_base = 0; - $total_tax_base = Tools::processPriceRounding($discounted_price_tax_excl, $quantity); - $total_amount = Tools::processPriceRounding($unit_amount, $quantity); - if (!isset($breakdown[$id_tax])) { - $breakdown[$id_tax] = array('tax_base' => 0, 'tax_amount' => 0); + $totalTaxBase = Tools::processPriceRounding($unit_price_tax_excl, $quantity); + if (!$order_detail['is_booking_product']) { + $objServiceProductOrderDetail = new ServiceProductOrderDetail(); + 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['total_price_tax_excl']) ? $item['total_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']; } - - $breakdown[$id_tax]['tax_base'] += $total_tax_base; - $breakdown[$id_tax]['tax_amount'] += $total_amount; - - $order_detail_tax_rows[] = array( - 'id_order_detail' => $id_order_detail, - 'id_tax' => $id_tax, - 'tax_rate' => $tax_rates[$id_tax], - 'unit_tax_base' => $discounted_price_tax_excl, - 'total_tax_base' => $total_tax_base, - 'unit_amount' => $unit_amount, - 'total_amount' => $total_amount - ); - } - } - 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); - $actual_total_base += Tools::ps_round($data['tax_base'], _PS_PRICE_COMPUTE_PRECISION_, $this->round_mode); } - $order_ecotax_tax = Tools::ps_round($order_ecotax_tax, _PS_PRICE_COMPUTE_PRECISION_, $this->round_mode); + $objServiceProductOrderDetail = new ServiceProductOrderDetail(); + $groupedTaxDetails = array(); + $additionalTaxAmounts = array(); - $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'); + if ( + $order_detail['is_booking_product'] && + $autoAddedPriceExcl = $objServiceProductOrderDetail->getRoomTypeServiceProducts( + $order_detail['id_order'], + 0, 0, + $order_detail['product_id'], + 0, 0, 0, + true, false, true, + Product::PRICE_ADDITION_TYPE_WITH_ROOM + ) + ) { + $additionalTaxAmounts = $tax_calculator->getTaxesAmount($autoAddedPriceExcl); + $totalTaxBase += $autoAddedPriceExcl; } - $base_rounding_error = $expected_total_base - $actual_total_base; - if ($base_rounding_error != 0) { - Tools::spreadAmount($base_rounding_error, _PS_PRICE_COMPUTE_PRECISION_, $order_detail_tax_rows, 'total_tax_base'); + $taxBaseShare = $totalTaxBase; + + foreach ($taxesList as $detailTax) { + $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, + 'id_tax' => $taxId, + 'tax_rate' => $tax_rates[$taxId], + 'unit_tax_base' => $unit_price_tax_excl, + 'total_tax_base' => $taxBaseShare, + 'unit_amount' => $unitAmount, + 'total_amount' => $totalAmount, + ); + } else { + $groupedTaxDetails[$taxId]['total_amount'] += $totalAmount; + } } + + if (!empty($groupedTaxDetails)) { + foreach ($groupedTaxDetails as $item) { + $order_detail_tax_rows[] = $item; + } + } + + /* + * 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. + * + * foreach ($tax_calculator->getTaxesAmount($discounted_price_tax_excl) as $id_tax => $unit_amount) { + * $total_tax_base = 0; + * $total_tax_base = Tools::processPriceRounding($discounted_price_tax_excl, $quantity); + * $total_amount = Tools::processPriceRounding($unit_amount, $quantity); + * + * if (!isset($breakdown[$id_tax])) { + * $breakdown[$id_tax] = array('tax_base' => 0, 'tax_amount' => 0); + * } + * + * $breakdown[$id_tax]['tax_base'] += $total_tax_base; + * $breakdown[$id_tax]['tax_amount'] += $total_amount; + * + * $order_detail_tax_rows[] = array( + * 'id_order_detail' => $id_order_detail, + * 'id_tax' => $id_tax, + * 'tax_rate' => $tax_rates[$id_tax], + * 'unit_tax_base' => $discounted_price_tax_excl, + * 'total_tax_base' => $total_tax_base, + * 'unit_amount' => $unit_amount, + * 'total_amount' => $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 + * the discrepancy across order detail tax rows. However, this logic is not required + * due to updated tax calculation and breakdown handling. + */ + + // 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); + // $actual_total_base += Tools::ps_round($data['tax_base'], _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'); + // } + + // $base_rounding_error = $expected_total_base - $actual_total_base; + // if ($base_rounding_error != 0) { + // Tools::spreadAmount($base_rounding_error, _PS_PRICE_COMPUTE_PRECISION_, $order_detail_tax_rows, 'total_tax_base'); + // } + // } return $order_detail_tax_rows; } diff --git a/classes/order/OrderDetail.php b/classes/order/OrderDetail.php index 17b1a420e..c13878059 100644 --- a/classes/order/OrderDetail.php +++ b/classes/order/OrderDetail.php @@ -372,23 +372,6 @@ class OrderDetailCore extends ObjectModel */ public function saveTaxCalculator(Order $order, $replace = false) { - // Nothing to save - if ($this->tax_calculator == null) { - return true; - } - - if (!($this->tax_calculator instanceof TaxCalculator)) { - return false; - } - - if (count($this->tax_calculator->taxes) == 0) { - return true; - } - - if ($order->total_products <= 0) { - return true; - } - $shipping_tax_amount = 0; foreach ($order->getCartRules() as $cart_rule) { @@ -398,27 +381,171 @@ class OrderDetailCore extends ObjectModel } } - $ratio = $this->unit_price_tax_excl / $order->total_products; - $order_reduction_amount = ($order->total_discounts_tax_excl - $shipping_tax_amount) * $ratio; - $discounted_price_tax_excl = $this->unit_price_tax_excl - $order_reduction_amount; - $values = ''; - foreach ($this->tax_calculator->getTaxesAmount($discounted_price_tax_excl) as $id_tax => $amount) { - - $total_amount = Tools::processPriceRounding($amount, $this->product_quantity, $order->round_type, $order->round_mode); - - $values .= '('.(int)$this->id.','.(int)$id_tax.','.(float)$amount.','.(float)$total_amount.'),'; + $idCart = $this->context->cart->id ?? $order->id_cart; + if (empty($this->vat_address)) { + $this->vat_address = new Address((int)$order->id_address_tax); } - if ($replace) { - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'order_detail_tax` WHERE id_order_detail='.(int)$this->id); + /** + * Calculate tax rule groups for the service product based on each associated room type. + */ + if (!$this->is_booking_product && $this->selling_preference_type == Product::SELLING_PREFERENCE_WITH_ROOM_TYPE) { + if ($this->product_auto_add && $this->product_price_addition_type == Product::PRICE_ADDITION_TYPE_WITH_ROOM) { + return true; + } + // Get all associated room type IDs for this service product and cart + $associatedRoomTypes = Db::getInstance()->executeS( + 'SELECT hcbd.`id_product` + FROM `'._DB_PREFIX_.'htl_cart_booking_data` hcbd + INNER JOIN `'._DB_PREFIX_.'service_product_cart_detail` spcd + ON spcd.`htl_cart_booking_id` = hcbd.`id` + WHERE spcd.`id_product` = '.(int)$this->product_id.' + AND spcd.`id_cart` = '.(int)$idCart + ); + + if (!empty($associatedRoomTypes)) { + $associatedRoomTypeIds = array_column($associatedRoomTypes, 'id_product'); + $taxGroupInfoList = array(); + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + foreach ($associatedRoomTypeIds as $idRoomType) { + $key = $this->product_id.'_'.$idRoomType; + if ($serviceProductPriceRoomInfo = $objRoomTypeServiceProductPrice->getProductRoomTypeLinkPriceInfo( + $this->product_id, + $idRoomType, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + )) { + //Special tax rule group for the Service product accroding to Room type + $taxGroupInfoList[$key] = array( + 'id_room_type' => $idRoomType, + 'id_tax_rules_group' => $serviceProductPriceRoomInfo['id_tax_rules_group'] + ); + } else { + // Use default tax rule group for the service product + $taxGroupInfoList[$key] = array( + 'id_room_type' => $idRoomType, + 'id_tax_rules_group' => $this->id_tax_rules_group + ); + } + } + } + } + + /** + * 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 (!$this->is_booking_product && isset($taxGroupInfoList) && $taxGroupInfoList) { + $objServiceProductCartDetail = new ServiceProductCartDetail(); + $objServiceProductOrderDetail = new ServiceProductOrderDetail(); + // Saving tax details according to the service product tax groups for different rooms + foreach ($taxGroupInfoList as $taxGroupInfo) { + $tax_manager = TaxManagerFactory::getManager($this->vat_address, (int)$taxGroupInfo['id_tax_rules_group']); + $this->tax_calculator = $tax_manager->getTaxCalculator(); + + if ($this->tax_calculator == null) { + continue; + } + + if (!($this->tax_calculator instanceof TaxCalculator)) { + continue; + } + + if (count($this->tax_calculator->taxes) == 0) { + continue; + } + + if ($serviceProductData = $objServiceProductOrderDetail->getRoomTypeServiceProducts( + $this->id_order, + $this->product_id, + 0, + $taxGroupInfo['id_room_type'], + )) { + $total_price_tax_excl = array_reduce($serviceProductData, function ($totalPriceTaxExcl, $item) { + return $totalPriceTaxExcl + (isset($item['total_price_tax_excl']) ? $item['total_price_tax_excl'] : 0); + }, 0); + + $serviceProductData = array_shift($serviceProductData); + + $quantity = array_reduce($serviceProductData['additional_services'], function ($totalQty, $item) { + return $totalQty + (isset($item['quantity']) ? $item['quantity'] : 0); + }, 0); + } elseif ($serviceProductData = $objServiceProductCartDetail->getServiceProductsInCart( + $idCart, + array(), + null, + null, + $taxGroupInfo['id_room_type'], + $this->product_id + )) { + $total_price_tax_excl = array_reduce($serviceProductData, function ($totalPriceTaxExcl, $item) { + return $totalPriceTaxExcl + (isset($item['total_price_tax_excl']) ? $item['total_price_tax_excl'] : 0); + }, 0); + + $quantity = array_reduce($serviceProductData, function ($totalQty, $item) { + return $totalQty + (isset($item['quantity']) ? $item['quantity'] : 0); + }, 0); + } + + if (isset($quantity) && isset($total_price_tax_excl)) { + $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, $quantity, $order->round_type, $order->round_mode); + + $values .= '('.(int)$this->id.','.(int)$id_tax.','.(float)$amount.','.(float)$total_amount.'),'; + } + } + } + } else { + if ($this->tax_calculator == null) { + return true; + } + + if (!($this->tax_calculator instanceof TaxCalculator)) { + return false; + } + + if (count($this->tax_calculator->taxes) == 0) { + return true; + } + + if ($order->total_products <= 0) { + return true; + } + + $unit_price_tax_excl = $this->total_price_tax_excl / $this->product_quantity; + /* + * The logic for distributing discount proportionally across products is intentionally skipped, + * as we do not want to save taxes on discounted amounts. + * + * $ratio = $unit_price_tax_excl / $order->total_products; + * $order_reduction_amount = ($order->total_discounts_tax_excl - $shipping_tax_amount) * $ratio; + * $discounted_price_tax_excl = $unit_price_tax_excl - $order_reduction_amount; + */ + + foreach ($this->tax_calculator->getTaxesAmount($unit_price_tax_excl) as $id_tax => $amount) { + + $total_amount = Tools::processPriceRounding($amount, $this->product_quantity, $order->round_type, $order->round_mode); + + $values .= '('.(int)$this->id.','.(int)$id_tax.','.(float)$amount.','.(float)$total_amount.'),'; + } } $values = rtrim($values, ','); - $sql = 'INSERT INTO `'._DB_PREFIX_.'order_detail_tax` (id_order_detail, id_tax, unit_amount, total_amount) - VALUES '.$values; - return Db::getInstance()->execute($sql); + if ($values) { + if ($replace) { + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'order_detail_tax` WHERE id_order_detail='.(int)$this->id); + } + + $sql = 'INSERT INTO `'._DB_PREFIX_.'order_detail_tax` (id_order_detail, id_tax, unit_amount, total_amount) + VALUES '.$values; + + return Db::getInstance()->execute($sql); + } + + return true; } public function updateTaxAmount($order) @@ -550,7 +677,7 @@ class OrderDetailCore extends ObjectModel if ($product !== null) { $this->setContext((int)$product['id_shop']); } - $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct((int)$this->specificPrice['id_product'], $this->context); + $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct((int)$this->product_id, $this->context); $tax_manager = TaxManagerFactory::getManager($this->vat_address, $id_tax_rules); $this->tax_calculator = $tax_manager->getTaxCalculator(); diff --git a/classes/order/OrderInvoice.php b/classes/order/OrderInvoice.php index 6319dc68d..3531e04ac 100644 --- a/classes/order/OrderInvoice.php +++ b/classes/order/OrderInvoice.php @@ -365,14 +365,13 @@ class OrderInvoiceCore extends ObjectModel if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; @@ -430,14 +429,13 @@ class OrderInvoiceCore extends ObjectModel if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; @@ -494,14 +492,13 @@ class OrderInvoiceCore extends ObjectModel if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; @@ -563,14 +560,13 @@ class OrderInvoiceCore extends ObjectModel if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; @@ -632,14 +628,13 @@ class OrderInvoiceCore extends ObjectModel if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; diff --git a/classes/pdf/HTMLTemplateOrderSlip.php b/classes/pdf/HTMLTemplateOrderSlip.php index 9c1dc2579..c520fe521 100644 --- a/classes/pdf/HTMLTemplateOrderSlip.php +++ b/classes/pdf/HTMLTemplateOrderSlip.php @@ -298,14 +298,13 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice if (!isset($grouped_details[$row['id_order_detail']])) { $grouped_details[$row['id_order_detail']] = array( 'tax_rate' => 0, - 'total_tax_base' => 0, + 'total_tax_base' => $row['total_tax_base'], 'total_amount' => 0, 'id_tax' => $row['id_tax'], ); } $grouped_details[$row['id_order_detail']]['tax_rate'] += $row['tax_rate']; - $grouped_details[$row['id_order_detail']]['total_tax_base'] += $row['total_tax_base']; $grouped_details[$row['id_order_detail']]['total_amount'] += $row['total_amount']; } $details = $grouped_details; diff --git a/classes/shop/ShopUrl.php b/classes/shop/ShopUrl.php index 5e43b1aee..94d62f51f 100644 --- a/classes/shop/ShopUrl.php +++ b/classes/shop/ShopUrl.php @@ -49,8 +49,8 @@ class ShopUrlCore extends ObjectModel 'domain' => array('type' => self::TYPE_STRING, 'required' => true, 'size' => 255, 'validate' => 'isCleanHtml'), 'domain_ssl' => array('type' => self::TYPE_STRING, 'size' => 255, 'validate' => 'isCleanHtml'), 'id_shop' => array('type' => self::TYPE_INT, 'required' => true), - 'physical_uri' => array('type' => self::TYPE_STRING, 'size' => 64), - 'virtual_uri' => array('type' => self::TYPE_STRING, 'size' => 64), + 'physical_uri' => array('type' => self::TYPE_STRING, 'size' => 256), + 'virtual_uri' => array('type' => self::TYPE_STRING, 'size' => 256), ), ); diff --git a/classes/tax/TaxRulesGroup.php b/classes/tax/TaxRulesGroup.php index fda98ba26..c37d6367a 100644 --- a/classes/tax/TaxRulesGroup.php +++ b/classes/tax/TaxRulesGroup.php @@ -101,6 +101,10 @@ class TaxRulesGroupCore extends ObjectModel UPDATE '._DB_PREFIX_.'product SET id_tax_rules_group='.(int)$tax_rules_group->id.' WHERE id_tax_rules_group='.(int)$this->id) && + Db::getInstance()->execute(' + UPDATE '._DB_PREFIX_.'htl_room_type_service_product_price + SET id_tax_rules_group='.(int)$tax_rules_group->id.' + WHERE id_tax_rules_group='.(int)$this->id) && Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'product_shop SET id_tax_rules_group='.(int)$tax_rules_group->id.' diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index ad992ba1d..a97598697 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -6139,7 +6139,7 @@ class AdminOrdersControllerCore extends AdminController $product_quantity = $editProductInfo['product_quantity']; if ($updateQty = (int)($product_quantity - $objServiceProductOrderDetail->quantity)) { - $this->checkStockAvailable($objOrderDetail, ($product_quantity - $objOrderDetail->product_quantity)); + $this->checkStockAvailable($objOrderDetail, $updateQty); } $taxRate = $objServiceProductOrderDetail->unit_price_tax_incl / $objServiceProductOrderDetail->unit_price_tax_excl; @@ -6207,7 +6207,7 @@ class AdminOrdersControllerCore extends AdminController // Update product available quantity if ($updateQty) { - $result &= StockAvailable::updateQuantity($objOrderDetail->product_id, $objOrderDetail->product_attribute_id, $updateQty, $objOrder->id_shop); + $result &= StockAvailable::updateQuantity($objOrderDetail->product_id, $objOrderDetail->product_attribute_id, -(int)$updateQty, $objOrder->id_shop); } if (!$result) { diff --git a/controllers/admin/AdminPPreferencesController.php b/controllers/admin/AdminPPreferencesController.php index c5bd1dfbb..4794c1f9a 100644 --- a/controllers/admin/AdminPPreferencesController.php +++ b/controllers/admin/AdminPPreferencesController.php @@ -331,80 +331,80 @@ class AdminPPreferencesControllerCore extends AdminController ), 'submit' => array('title' => $this->l('Save')) ), - 'stock' => array( - 'title' => $this->l('Products stock'), - 'fields' => array( - 'PS_ORDER_OUT_OF_STOCK' => array( - 'title' => $this->l('Allow ordering of out-of-stock products'), - 'hint' => $this->l('By default, the Add to Cart button is hidden when a product is unavailable. You can choose to have it displayed in all cases.'), - 'validation' => 'isBool', - 'cast' => 'intval', - 'required' => false, - 'type' => 'bool' - ), - 'PS_STOCK_MANAGEMENT' => array( - 'title' => $this->l('Enable stock management'), - 'validation' => 'isBool', - 'cast' => 'intval', - 'required' => false, - 'type' => 'bool', - 'js' => array( - 'on' => 'onchange="stockManagementActivationAuthorization()"', - 'off' => 'onchange="stockManagementActivationAuthorization()"' - ) - ), - // 'PS_ADVANCED_STOCK_MANAGEMENT' => array( - // 'title' => $this->l('Enable advanced stock management'), - // 'hint' => $this->l('Allows you to manage physical stock, warehouses and supply orders in a new Stock menu.'), - // 'validation' => 'isBool', - // 'cast' => 'intval', - // 'required' => false, - // 'type' => 'bool', - // 'visibility' => Shop::CONTEXT_ALL, - // 'js' => array( - // 'on' => 'onchange="advancedStockManagementActivationAuthorization()"', - // 'off' => 'onchange="advancedStockManagementActivationAuthorization()"' - // ) - // ), - // 'PS_FORCE_ASM_NEW_PRODUCT' => array( - // 'title' => $this->l('New products use advanced stock management'), - // 'hint' => $this->l('New products will automatically use advanced stock management and depends on stock, but no warehouse will be selected'), - // 'validation' => 'isBool', - // 'cast' => 'intval', - // 'required' => false, - // 'type' => 'bool', - // 'visibility' => Shop::CONTEXT_ALL, - // ), - // 'PS_DEFAULT_WAREHOUSE_NEW_PRODUCT' => array( - // 'title' => $this->l('Default warehouse on new products'), - // 'hint' => $this->l('Automatically set a default warehouse when new product is created'), - // 'type' => 'select', - // 'list' => $warehouse_list, - // 'identifier' => 'id_warehouse' - // ), - // 'PS_PACK_STOCK_TYPE' => array( - // 'title' => $this->l('Default pack stock management'), - // 'type' => 'select', - // 'list' =>array( - // array( - // 'pack_stock' => 0, - // 'name' => $this->l('Decrement pack only.') - // ), - // array( - // 'pack_stock' => 1, - // 'name' => $this->l('Decrement products in pack only.') - // ), - // array( - // 'pack_stock' => 2, - // 'name' => $this->l('Decrement both.') - // ), - // ), - // 'identifier' => 'pack_stock', - // ), - ), - // 'bottom' => '', - 'submit' => array('title' => $this->l('Save')) - ), + // 'stock' => array( + // 'title' => $this->l('Products stock'), + // 'fields' => array( + // 'PS_ORDER_OUT_OF_STOCK' => array( + // 'title' => $this->l('Allow ordering of out-of-stock products'), + // 'hint' => $this->l('By default, the Add to Cart button is hidden when a product is unavailable. You can choose to have it displayed in all cases.'), + // 'validation' => 'isBool', + // 'cast' => 'intval', + // 'required' => false, + // 'type' => 'bool' + // ), + // 'PS_STOCK_MANAGEMENT' => array( + // 'title' => $this->l('Enable stock management'), + // 'validation' => 'isBool', + // 'cast' => 'intval', + // 'required' => false, + // 'type' => 'bool', + // 'js' => array( + // 'on' => 'onchange="stockManagementActivationAuthorization()"', + // 'off' => 'onchange="stockManagementActivationAuthorization()"' + // ) + // ), + // // 'PS_ADVANCED_STOCK_MANAGEMENT' => array( + // // 'title' => $this->l('Enable advanced stock management'), + // // 'hint' => $this->l('Allows you to manage physical stock, warehouses and supply orders in a new Stock menu.'), + // // 'validation' => 'isBool', + // // 'cast' => 'intval', + // // 'required' => false, + // // 'type' => 'bool', + // // 'visibility' => Shop::CONTEXT_ALL, + // // 'js' => array( + // // 'on' => 'onchange="advancedStockManagementActivationAuthorization()"', + // // 'off' => 'onchange="advancedStockManagementActivationAuthorization()"' + // // ) + // // ), + // // 'PS_FORCE_ASM_NEW_PRODUCT' => array( + // // 'title' => $this->l('New products use advanced stock management'), + // // 'hint' => $this->l('New products will automatically use advanced stock management and depends on stock, but no warehouse will be selected'), + // // 'validation' => 'isBool', + // // 'cast' => 'intval', + // // 'required' => false, + // // 'type' => 'bool', + // // 'visibility' => Shop::CONTEXT_ALL, + // // ), + // // 'PS_DEFAULT_WAREHOUSE_NEW_PRODUCT' => array( + // // 'title' => $this->l('Default warehouse on new products'), + // // 'hint' => $this->l('Automatically set a default warehouse when new product is created'), + // // 'type' => 'select', + // // 'list' => $warehouse_list, + // // 'identifier' => 'id_warehouse' + // // ), + // // 'PS_PACK_STOCK_TYPE' => array( + // // 'title' => $this->l('Default pack stock management'), + // // 'type' => 'select', + // // 'list' =>array( + // // array( + // // 'pack_stock' => 0, + // // 'name' => $this->l('Decrement pack only.') + // // ), + // // array( + // // 'pack_stock' => 1, + // // 'name' => $this->l('Decrement products in pack only.') + // // ), + // // array( + // // 'pack_stock' => 2, + // // 'name' => $this->l('Decrement both.') + // // ), + // // ), + // // 'identifier' => 'pack_stock', + // // ), + // ), + // // 'bottom' => '', + // 'submit' => array('title' => $this->l('Save')) + // ), ); } diff --git a/install/data/db_structure.sql b/install/data/db_structure.sql index 0afb0273e..079d3e928 100644 --- a/install/data/db_structure.sql +++ b/install/data/db_structure.sql @@ -2175,8 +2175,8 @@ CREATE TABLE IF NOT EXISTS `PREFIX_shop_url` ( `id_shop` int(11) unsigned NOT NULL, `domain` varchar(150) NOT NULL, `domain_ssl` varchar(150) NOT NULL, - `physical_uri` varchar(64) NOT NULL, - `virtual_uri` varchar(64) NOT NULL, + `physical_uri` varchar(256) NOT NULL, + `virtual_uri` varchar(256) NOT NULL, `main` TINYINT(1) NOT NULL, `active` TINYINT(1) NOT NULL, PRIMARY KEY (`id_shop_url`), diff --git a/modules/blockcart/ajax-cart.js b/modules/blockcart/ajax-cart.js index 0c25d3a45..e3e4ac26f 100644 --- a/modules/blockcart/ajax-cart.js +++ b/modules/blockcart/ajax-cart.js @@ -878,7 +878,7 @@ var ajaxCart = { content += ''; if ((parseInt(product.selling_preference_type) == SELLING_PREFERENCE_HOTEL_STANDALONE || parseInt(product.selling_preference_type) == SELLING_PREFERENCE_HOTEL_STANDALONE_AND_WITH_ROOM_TYPE) - && (typeof(hotel_wise_data) != 'undefined' && hotel_wise_data.length) + && (typeof(hotel_wise_data) != 'undefined' && typeof(hotel_wise_data.hotel_name) != 'undefined') ) { content += '
' + hotel_wise_data.hotel_name + '
'; } diff --git a/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php b/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php index d103e1576..a91163edb 100644 --- a/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php +++ b/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php @@ -430,6 +430,8 @@ class HotelReservationSystemDb `id_hotel` int(11) NOT NULL, `id_htl_booking_detail` int(11) NOT NULL, `id_product_option` int(11) NOT NULL, + `tax_computation_method` tinyint(1) unsigned NOT NULL DEFAULT '0', + `id_tax_rules_group` int(11) unsigned NOT NULL DEFAULT '0', `unit_price_tax_excl` decimal(20,6) NOT NULL, `unit_price_tax_incl` decimal(20,6) NOT NULL, `total_price_tax_excl` decimal(20,6) NOT NULL, diff --git a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php index ca621a1fd..0e668ccbd 100644 --- a/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php +++ b/modules/hotelreservationsystem/classes/HotelRoomTypeFeaturePricing.php @@ -389,9 +389,10 @@ class HotelRoomTypeFeaturePricing extends ObjectModel 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM )) { - $selectedServices = array_shift($roomServicesServices); - $totalPrice['total_price_tax_incl'] += $selectedServices['total_price_tax_incl']; - $totalPrice['total_price_tax_excl'] += $selectedServices['total_price_tax_excl']; + foreach($roomServicesServices as $selectedService) { + $totalPrice['total_price_tax_incl'] += $selectedService['total_price_tax_incl']; + $totalPrice['total_price_tax_excl'] += $selectedService['total_price_tax_excl']; + } } } diff --git a/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php b/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php index 173e2fe3f..33202537b 100644 --- a/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php +++ b/modules/hotelreservationsystem/classes/ServiceProductOrderDetail.php @@ -28,6 +28,8 @@ class ServiceProductOrderDetail extends ObjectModel public $id_hotel; public $id_htl_booking_detail; public $id_product_option; + public $tax_computation_method; + public $id_tax_rules_group; public $unit_price_tax_excl; public $unit_price_tax_incl; public $total_price_tax_excl; @@ -53,6 +55,8 @@ class ServiceProductOrderDetail extends ObjectModel 'id_hotel' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'id_htl_booking_detail' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'id_product_option' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), + 'tax_computation_method' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), + 'id_tax_rules_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'unit_price_tax_excl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), 'unit_price_tax_incl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), 'total_price_tax_excl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true), @@ -69,6 +73,52 @@ class ServiceProductOrderDetail extends ObjectModel ) ); + public function add($autodate = true, $null_values = true) + { + 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)) + ) { + 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 { + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + if ($serviceProductPriceRoomInfo = $objRoomTypeServiceProductPrice->getProductRoomTypeLinkPriceInfo( + $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; + } + } + + $taxCalculator = TaxManagerFactory::getManager($objAddress, $this->id_tax_rules_group)->getTaxCalculator(); + $this->tax_computation_method = (int)$taxCalculator->computation_method; + } + } else { + // 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; + } + + return parent::add($autodate, $null_values); + } + + return false; + } + public function getServiceProductsInOrder( $idOrder, $idOrderDetail = 0, diff --git a/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js b/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js index 0c25d3a45..e3e4ac26f 100644 --- a/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js +++ b/themes/hotel-reservation-theme/js/modules/blockcart/ajax-cart.js @@ -878,7 +878,7 @@ var ajaxCart = { content += ''; if ((parseInt(product.selling_preference_type) == SELLING_PREFERENCE_HOTEL_STANDALONE || parseInt(product.selling_preference_type) == SELLING_PREFERENCE_HOTEL_STANDALONE_AND_WITH_ROOM_TYPE) - && (typeof(hotel_wise_data) != 'undefined' && hotel_wise_data.length) + && (typeof(hotel_wise_data) != 'undefined' && typeof(hotel_wise_data.hotel_name) != 'undefined') ) { content += '
' + hotel_wise_data.hotel_name + '
'; }