Fixed: Refactor group discount calculation to apply after advance price rules

Co-authored-by: Name <sumit201@webkul.in>
This commit is contained in:
amit sharma
2026-07-28 15:38:41 +05:30
parent 5b4d65cbb8
commit c72dbf376e
4 changed files with 89 additions and 32 deletions

View File

@@ -3033,7 +3033,8 @@ class ProductCore extends ObjectModel
* @param null $specific_price_output If a specific price applies regarding the previous parameters,
* this variable is filled with the corresponding SpecificPrice object
* @param bool $with_ecotax Insert ecotax in price output.
* @param bool $use_group_reduction
* @param bool $use_group_reduction @deprecated no longer applied here; call
* Product::applyGroupDiscount() explicitly on your final total.
* @param Context $context
* @param bool $use_customer_price
* @return float Product price
@@ -3202,7 +3203,8 @@ class ProductCore extends ObjectModel
* @param bool $with_ecotax insert ecotax in price output.
* @param null $specific_price If a specific price applies regarding the previous parameters,
* this variable is filled with the corresponding SpecificPrice object
* @param bool $use_group_reduction
* @param bool $use_group_reduction @deprecated no longer applied here; call
* Product::applyGroupDiscount() explicitly on your final total.
* @param int $id_customer
* @param bool $use_customer_price
* @param int $id_cart
@@ -3456,18 +3458,6 @@ class ProductCore extends ObjectModel
$price -= $specific_price_reduction;
}
// Group reduction
if ($use_group_reduction) {
$reduction_from_category = GroupReduction::getValueForProduct($id_product, $id_group);
if ($reduction_from_category !== false) {
$group_reduction = $price * (float)$reduction_from_category;
} else { // apply group reduction if there is no group reduction for this category
$group_reduction = (($reduc = Group::getReductionByIdGroup($id_group)) != 0) ? ($price * $reduc / 100) : 0;
}
$price -= $group_reduction;
}
if ($only_reduc) {
return Tools::ps_round($specific_price_reduction, $decimals);
}
@@ -3482,6 +3472,32 @@ class ProductCore extends ObjectModel
return self::$_prices[$cache_id];
}
/**
* Applies the group discount to an already-computed price.
* @param float $price The price to discount (tax-incl. or tax-excl., either works).
* @param int $id_product
* @param int $id_group
* @param bool $use_group_reduction
* @return float
*/
public static function applyGroupDiscount($price, $id_product, $id_group, $use_group_reduction = true)
{
if (!$use_group_reduction) {
return $price;
}
$reduction_from_category = GroupReduction::getValueForProduct($id_product, $id_group);
if ($reduction_from_category !== false) {
$group_reduction = $price * (float)$reduction_from_category;
} else { // apply group reduction if there is no group reduction for this category
$group_reduction = (($reduc = Group::getReductionByIdGroup($id_group)) != 0) ? ($price * $reduc / 100) : 0;
}
$price -= $group_reduction;
return $price < 0 ? 0 : $price;
}
public static function convertAndFormatPrice($price, $currency = false, ?Context $context = null)
{
if (!$context) {
@@ -6871,6 +6887,15 @@ class ProductCore extends ObjectModel
$price = $price * HotelHelper::getNumberOfDays($dateFrom, $dateTo);
}
return $price * (int)$quantity;
$price = $price * (int)$quantity;
if (!$specificPrice || $specificPrice['price'] < 0) {
if (!$idGroup || !Validate::isLoadedObject(new Group((int)$idGroup))) {
$idGroup = (int)Group::getCurrent()->id;
}
$price = Product::applyGroupDiscount($price, $idProduct, $idGroup);
}
return $price;
}
}

View File

@@ -437,7 +437,7 @@ class AdminOrdersControllerCore extends AdminController
if (!$cart_order_exists) {
$this->context->cart = $cart;
$this->context->currency = new Currency((int)$cart->id_currency);
$this->context->customer = new Customer((int)$cart->id_customer);
// validate cart for removing invalid data from cart before new order creation
// remove not linked services with room types from cart if not allowed to book them
$this->errors = array_merge(
@@ -999,25 +999,45 @@ class AdminOrdersControllerCore extends AdminController
$objProduct = new Product();
$hotelServiceProducts = $objProduct->getServiceProducts(true, Product::SELLING_PREFERENCE_HOTEL_STANDALONE_AND_WITH_ROOM_TYPE);
$roomTypeServiceProducts = $objProduct->getServiceProducts(true, Product::SELLING_PREFERENCE_WITH_ROOM_TYPE);
$serviceProducts = array_merge($roomTypeServiceProducts, $hotelServiceProducts);
} else {
$objRoomTypeServiceProduct = new RoomTypeServiceProduct();
$serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($idProduct, 1, 0, false, 2, null);
}
if ($serviceProducts) {
foreach ($serviceProducts as $key => $servProduct) {
$serviceProducts[$key]['price_tax_exc'] = $servProduct['price'];
if (isset($additionalServices[$productLineData['id']])
&& in_array($servProduct['id_product'], array_column($additionalServices[$productLineData['id']]['additional_services'], 'id_product'))
) {
unset($serviceProducts[$key]);
if ($serviceProducts = array_merge($roomTypeServiceProducts, $hotelServiceProducts)) {
foreach ($serviceProducts as $key => $servProduct) {
if (!empty($additionalServices[$productLineData['id']])
&& in_array($servProduct['id_product'], array_column($additionalServices[$productLineData['id']]['additional_services'], 'id_product'))
) {
unset($serviceProducts[$key]);
} else {
$numDays = 1;
if (Product::PRICE_CALCULATION_METHOD_PER_DAY == $servProduct['price_calculation_method']) {
$numDays = HotelHelper::getNumberOfDays($dateFrom, $dateTo);
}
$serviceProducts[$key]['price_tax_exc'] = Product::getServiceProductPrice(
$servProduct['id_product'],
0,
0,
$idProduct,
false,
1,
$dateFrom,
$dateTo
)/$numDays;
}
}
}
} else {
$objRoomTypeServiceProduct = new RoomTypeServiceProduct();
if ($serviceProducts = $objRoomTypeServiceProduct->getServiceProductsData($idProduct, 1, 0, false, 2, null)) {
foreach ($serviceProducts as $key => $servProduct) {
if (isset($additionalServices[$productLineData['id']])
&& in_array($servProduct['id_product'], array_column($additionalServices[$productLineData['id']]['additional_services'], 'id_product'))
) {
unset($serviceProducts[$key]);
}
}
$smartyVars['serviceProducts'] = $serviceProducts;
}
}
$smartyVars['serviceProducts'] = $serviceProducts;
$objOrderReturn = new OrderReturn();
$refundReqBookings = $objOrderReturn->getOrderRefundRequestedBookings($objOrder->id, 0, 1);
$smartyVars['refundReqBookings'] = $refundReqBookings;

View File

@@ -282,6 +282,7 @@ class HotelRoomTypeFeaturePricing extends ObjectModel
$totalPrice['total_price_tax_excl'] = 0;
$featureImpactPriceTE = 0;
$featureImpactPriceTI = 0;
$isCartPriceOverridden = true;
$productPriceTI = Product::getPriceStatic((int) $id_product, 1, 0, 6, null, 0, $use_reduc, 1, 0, null, null, null, $nothing, 1, 1, null, 1, 0, 0, $id_group);
$productPriceTE = Product::getPriceStatic((int) $id_product, 0, 0, 6, null, 0, $use_reduc, 1, 0, null, null, null, $nothing, 1, 1, null, 1, 0, 0, $id_group);
$id_address = HotelRoomType::getHotelIdAddressByIdProduct($id_product);
@@ -317,6 +318,9 @@ class HotelRoomTypeFeaturePricing extends ObjectModel
$id_guest,
$id_room
))) {
if ((int)$featurePrice['id_cart'] === 0) {
$isCartPriceOverridden = false;
}
if ($featurePrice['impact_type'] == self::IMPACT_TYPE_PERCENTAGE) {
//percentage
$featureImpactPriceTE = $productPriceTE * ($featurePrice['impact_value'] / 100);
@@ -347,6 +351,7 @@ class HotelRoomTypeFeaturePricing extends ObjectModel
$totalPrice['total_price_tax_incl'] += $priceWithFeatureTI;
$totalPrice['total_price_tax_excl'] += $priceWithFeatureTE;
} else {
$isCartPriceOverridden = false;
$totalPrice['total_price_tax_incl'] += $productPriceTI;
$totalPrice['total_price_tax_excl'] += $productPriceTE;
}
@@ -368,6 +373,12 @@ class HotelRoomTypeFeaturePricing extends ObjectModel
'occupancy' => $occupancy
)
);
if (!$isCartPriceOverridden) {
$totalPrice['total_price_tax_incl'] = Product::applyGroupDiscount($totalPrice['total_price_tax_incl'], $id_product, $id_group);
$totalPrice['total_price_tax_excl'] = Product::applyGroupDiscount($totalPrice['total_price_tax_excl'], $id_product, $id_group);
}
if ($with_auto_room_services) {
if ($id_cart && $id_room) {
$objHotelCartBookingData = new HotelCartBookingData();

View File

@@ -144,7 +144,8 @@ class RoomTypeServiceProductPrice extends ObjectModel
$idHotelAddress
);
return $price * (int)$quantity;
$price = $price * (int)$quantity;
return Product::applyGroupDiscount($price, $idProduct, (int)Group::getCurrent()->id);
}
}