Fixed: Invoice data is not correct when multiple invoices created in an order

This commit is contained in:
Sumit Panwar
2024-06-08 16:16:49 +05:30
parent f93d321f6f
commit b5925817de
7 changed files with 125 additions and 49 deletions

View File

@@ -602,13 +602,19 @@ class OrderCore extends ObjectModel
$is_booking = null,
$product_service_type = null,
$product_auto_add = null,
$product_price_addition_type = null
$product_price_addition_type = null,
$ids_order_detail = []
) {
$sql = 'SELECT *
FROM `'._DB_PREFIX_.'order_detail` od
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.id_product = od.product_id)
LEFT JOIN `'._DB_PREFIX_.'product_shop` ps ON (ps.id_product = p.id_product AND ps.id_shop = od.id_shop)
WHERE od.`id_order` = '.(int)$this->id;
if ($ids_order_detail) {
$sql .= ' AND od.`id_order_detail` IN ('.implode(',', $ids_order_detail).')';
}
if ($is_booking !== null) {
$sql .= ' AND od.`is_booking_product` = '. (int)$is_booking;
if (!$is_booking && $product_service_type !== null) {
@@ -1091,11 +1097,12 @@ class OrderCore extends ObjectModel
$bookingProducts = null,
$product_service_type = null,
$product_auto_add = null,
$product_price_addition_type = null
$product_price_addition_type = null,
$ids_order_detail = []
) {
// update
if (!$products) {
$products = $this->getProductsDetail($bookingProducts, $product_service_type, $product_auto_add, $product_price_addition_type);
$products = $this->getProductsDetail($bookingProducts, $product_service_type, $product_auto_add, $product_price_addition_type, $ids_order_detail);
}
$return = 0;
@@ -1116,11 +1123,12 @@ class OrderCore extends ObjectModel
$bookingProducts = null,
$product_service_type = null,
$product_auto_add = null,
$product_price_addition_type = null
$product_price_addition_type = null,
$ids_order_detail = []
) {
/* Retro-compatibility (now set directly on the validateOrder() method) */
if (!$products) {
$products = $this->getProductsDetail($bookingProducts, $product_service_type, $product_auto_add, $product_price_addition_type);
$products = $this->getProductsDetail($bookingProducts, $product_service_type, $product_auto_add, $product_price_addition_type, $ids_order_detail);
}
$return = 0;

View File

@@ -413,7 +413,7 @@ class OrderInvoiceCore extends ObjectModel
// - 'total_price_tax_excl'
// - 'total_amount'
$breakdown = array();
$order_detail = $order->getOrderDetailList();
$order_detail = $this->getProducts();
$order_detail = array_filter($order_detail, function($v) {
return ($v['is_booking_product']
|| ($v['product_auto_add']
@@ -482,7 +482,7 @@ class OrderInvoiceCore extends ObjectModel
// - 'total_price_tax_excl'
// - 'total_amount'
$breakdown = array();
$order_detail = $order->getOrderDetailList();
$order_detail = $this->getProducts();
$order_detail = array_filter($order_detail, function($v) {
return (!$v['is_booking_product'] && !$v['product_auto_add'] && $v['product_service_type'] == Product::SERVICE_PRODUCT_WITH_ROOMTYPE);
});
@@ -546,7 +546,7 @@ class OrderInvoiceCore extends ObjectModel
// - 'total_price_tax_excl'
// - 'total_amount'
$breakdown = array();
$order_detail = $order->getOrderDetailList();
$order_detail = $this->getProducts();
$order_detail = array_filter($order_detail, function($v) {
return (!$v['is_booking_product']
&& $v['product_auto_add']
@@ -616,7 +616,7 @@ class OrderInvoiceCore extends ObjectModel
// - 'total_price_tax_excl'
// - 'total_amount'
$breakdown = array();
$order_detail = $order->getOrderDetailList();
$order_detail = $this->getProducts();
$order_detail = array_filter($order_detail, function($v) {
return (!$v['is_booking_product'] && $v['product_service_type'] == Product::SERVICE_PRODUCT_WITHOUT_ROOMTYPE);
});
@@ -674,8 +674,10 @@ class OrderInvoiceCore extends ObjectModel
$order = $this->getOrder();
}
$breakdown = array();
$order_details = $this->getProducts();
$objBookingDemand = new HotelBookingDemands();
$details = $objBookingDemand->getExtraDemandsTaxesDetails($order->id);
$order_details = array_column($order_details, 'id_order_detail');
$details = $objBookingDemand->getExtraDemandsTaxesDetails($order->id, $order_details);
if (!$this->useOneAfterAnotherTaxComputationMethod()) {
$grouped_details = array();
foreach ($details as $row) {

View File

@@ -349,9 +349,9 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
if ($type_value)
if (isset($customer->id)) {
$cart_obj = new Cart($this->order->id_cart);
$order_bk_data = $obj_htl_bk_dtl->getOnlyOrderBookingData($this->order->id, $cart_obj->id_guest, $type_value['product_id'], $customer->id);
$order_bk_data = $obj_htl_bk_dtl->getOnlyOrderBookingData($this->order->id, $cart_obj->id_guest, $type_value['product_id'], $customer->id, $type_value['id_order_detail']);
} else {
$order_bk_data = $obj_htl_bk_dtl->getOnlyOrderBookingData($this->order->id, $customer->id_guest, $type_value['product_id']);
$order_bk_data = $obj_htl_bk_dtl->getOnlyOrderBookingData($this->order->id, $customer->id_guest, $type_value['product_id'], 0, $type_value['id_order_detail']);
}
$cart_htl_data[$type_key]['id_product'] = $type_value['product_id'];
@@ -387,7 +387,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
0
0,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['extra_demands_price_ti'] = $objBookingDemand->getRoomTypeBookingExtraDemands(
$order_obj->id,
@@ -397,7 +398,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
1
1,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_auto_add_ti'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
$order_obj->id,
@@ -410,7 +412,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
1,
1,
1,
Product::PRICE_ADDITION_TYPE_WITH_ROOM
Product::PRICE_ADDITION_TYPE_WITH_ROOM,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_auto_add_te'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
$order_obj->id,
@@ -423,7 +426,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
1,
0,
1,
Product::PRICE_ADDITION_TYPE_WITH_ROOM
Product::PRICE_ADDITION_TYPE_WITH_ROOM,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['num_rm'] += 1;
@@ -457,7 +461,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
0
0,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['extra_demands_price_ti'] = $objBookingDemand->getRoomTypeBookingExtraDemands(
$order_obj->id,
@@ -467,7 +472,8 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
1
1,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
$order_obj->id,
@@ -476,7 +482,12 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['id_product'],
$data_v['date_from'],
$data_v['date_to'],
0
0,
0,
null,
0,
null,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_ti'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
@@ -488,7 +499,10 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
1
1,
0,
null,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_te'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
$order_obj->id,
@@ -499,7 +513,10 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$data_v['date_to'],
0,
1,
0
0,
0,
null,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_auto_add_ti'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
@@ -513,7 +530,9 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
1,
1,
1,
Product::PRICE_ADDITION_TYPE_WITH_ROOM
Product::PRICE_ADDITION_TYPE_WITH_ROOM,
null,
$type_value['id_order_detail']
);
$cart_htl_data[$type_key]['date_diff'][$date_join]['additional_services_price_auto_add_te'] = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts(
$order_obj->id,
@@ -526,7 +545,9 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
1,
0,
1,
Product::PRICE_ADDITION_TYPE_WITH_ROOM
Product::PRICE_ADDITION_TYPE_WITH_ROOM,
null,
$type_value['id_order_detail']
);
$num_days = $obj_htl_bk_dtl->getNumberOfDays($data_v['date_from'], $data_v['date_to']);
@@ -555,7 +576,11 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$type_value['product_id'],
$data_v['id_room'],
$data_v['date_from'],
$data_v['date_to']
$data_v['date_to'],
1,
0,
1,
$type_value['id_order_detail']
)) {
$extraDemands = array_shift($extraDemands);
$extraDemands['product_id'] = $type_value['product_id'];
@@ -572,7 +597,12 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$type_value['product_id'],
$data_v['date_from'],
$data_v['date_to'],
$data_v['id_room']
$data_v['id_room'],
0,
null,
0,
null,
$type_value['id_order_detail']
)) {
$additionalServices = array_shift($additionalServices);
$additionalServices['product_id'] = $type_value['product_id'];
@@ -610,14 +640,16 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
$footer['total_extra_demands_te'] = $totalDemandsPriceTE;
}
}
$footer['room_price_tax_excl'] = $this->order->getTotalProductsWithoutTaxes(false, true) + $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM);
$footer['room_price_tax_incl'] = $this->order->getTotalProductsWithTaxes(false, true) + $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM);
$idsOrderDetail = array_column($order_details, 'id_order_detail');
$footer['room_price_tax_excl'] = $this->order->getTotalProductsWithoutTaxes(false, true, null, null, null, $idsOrderDetail) + $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM, $idsOrderDetail);
$footer['room_price_tax_incl'] = $this->order->getTotalProductsWithTaxes(false, true, null, null, null, $idsOrderDetail) + $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_WITH_ROOM, $idsOrderDetail);
// $footer['service_products_price_tax_excl'] = $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITHOUT_ROOMTYPE);
// $footer['service_products_price_tax_incl'] = $this->order->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITHOUT_ROOMTYPE);
$footer['additional_service_price_tax_excl'] = $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 0) + $totalDemandsPriceTE;
$footer['additional_service_price_tax_incl'] = $this->order->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 0) + $totalDemandsPriceTI;
$footer['total_convenience_fee_te'] = $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_INDEPENDENT);
$footer['total_convenience_fee_ti'] = $this->order->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_INDEPENDENT);
$footer['additional_service_price_tax_excl'] = $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 0, null, $idsOrderDetail) + $totalDemandsPriceTE;
$footer['additional_service_price_tax_incl'] = $this->order->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 0, null, $idsOrderDetail) + $totalDemandsPriceTI;
$footer['total_convenience_fee_te'] = $this->order->getTotalProductsWithoutTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_INDEPENDENT, $idsOrderDetail);
$footer['total_convenience_fee_ti'] = $this->order->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE, 1, Product::PRICE_ADDITION_TYPE_INDEPENDENT, $idsOrderDetail);
$data = array(
'cart_htl_data' => $cart_htl_data,
'service_product_data' => $service_product_data,
@@ -737,6 +769,7 @@ class HTMLTemplateInvoiceCore extends HTMLTemplate
'ecotax_tax' => $this->order_invoice->getEcoTaxTaxesBreakdown(),
'wrapping_tax' => $this->order_invoice->getWrappingTaxesBreakdown(),
);
foreach ($breakdowns as $type => $bd) {
if (empty($bd)) {
unset($breakdowns[$type]);

View File

@@ -6240,6 +6240,14 @@ class AdminOrdersControllerCore extends AdminController
$res &= $objOrderDetail->update();
}
if ($objOrderDetail->id_order_invoice != 0) {
// values changes as values are calculated
$objOrderInvoice = new OrderInvoice($objOrderDetail->id_order_invoice);
$objOrderInvoice->total_paid_tax_excl -= $priceTaxExcl;
$objOrderInvoice->total_paid_tax_incl -= $priceTaxIncl;
$res &= $objOrderInvoice->update();
}
$order->total_paid_tax_excl -= $priceTaxExcl;
$order->total_paid_tax_incl -= $priceTaxIncl;
$order->total_paid -= $priceTaxIncl;
@@ -6272,6 +6280,9 @@ class AdminOrdersControllerCore extends AdminController
// valiadate services being added
if (Validate::isLoadedObject($objHotelBookingDetail = new HotelBookingDetail($idBookingDetail))) {
$objOrder = new Order($objHotelBookingDetail->id_order);
$objOrderDetail = new OrderDetail($objHotelBookingDetail->id_order_detail);
$objOrderInvoice = new OrderInvoice($objOrderDetail->id_order_invoice);
// set context currency So that we can get prices in the order currency
$this->context->currency = new Currency($objOrder->id_currency);
@@ -6453,7 +6464,7 @@ class AdminOrdersControllerCore extends AdminController
}
$order_detail = new OrderDetail();
$order_detail->createList($order, $cart, $order->getCurrentOrderState(), $productList, (isset($order_invoice) ? $order_invoice->id : 0), true);
$order_detail->createList($order, $cart, $order->getCurrentOrderState(), $productList, (isset($objOrderInvoice) ? $objOrderInvoice->id : 0), true);
$objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail();
$objRoomTypeServiceProductOrderDetail->id_product = $product['id_product'];
@@ -6477,16 +6488,18 @@ class AdminOrdersControllerCore extends AdminController
$order->total_paid_tax_excl += Tools::ps_round((float)($totalPriceChangeTaxExcl), 2);
$order->total_paid_tax_incl += Tools::ps_round((float)($totalPriceChangeTaxIncl), 2);
if (isset($order_invoice) && Validate::isLoadedObject($order_invoice)) {
$order->total_shipping = $order_invoice->total_shipping_tax_incl;
$order->total_shipping_tax_incl = $order_invoice->total_shipping_tax_incl;
$order->total_shipping_tax_excl = $order_invoice->total_shipping_tax_excl;
// update invoice total
if (isset($objOrderInvoice) && Validate::isLoadedObject($objOrderInvoice)) {
$objOrderInvoice->total_paid_tax_excl += Tools::ps_round((float)($totalPriceChangeTaxExcl), 2);
$objOrderInvoice->total_paid_tax_incl += Tools::ps_round((float)($totalPriceChangeTaxIncl), 2);
$objOrderInvoice->save();
}
// discount
$order->total_discounts += (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
$order->total_discounts_tax_excl += (float)abs($cart->getOrderTotal(false, Cart::ONLY_DISCOUNTS));
$order->total_discounts_tax_incl += (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
// Save changes of order
$order->update();
if (Validate::isLoadedObject($specific_price)) {
@@ -6685,8 +6698,9 @@ class AdminOrdersControllerCore extends AdminController
$order_invoice = new OrderInvoice($order_detail->id_order_invoice);
$order_invoice->total_paid_tax_excl += $priceDiffTaxExcl;
$order_invoice->total_paid_tax_incl += $priceDiffTaxIncl;
$res &= $order_invoice->update();
$order_invoice->update();
}
$order = new Order($objBookingDetail->id_order);
$order->total_paid_tax_excl += $priceDiffTaxExcl;
$order->total_paid_tax_incl += $priceDiffTaxIncl;

View File

@@ -74,7 +74,8 @@ class HotelBookingDemands extends ObjectModel
$dateTo = 0,
$groupByRoom = 1,
$getTotalPrice = 0,
$useTax = 1
$useTax = 1,
$idOrderDetail = 0
) {
$moduleObj = Module::getInstanceByName('hotelreservationsystem');
$context = Context::getContext();
@@ -91,6 +92,10 @@ class HotelBookingDemands extends ObjectModel
WHERE hd.`id_htl_booking` IN
(SELECT `id` FROM `'._DB_PREFIX_.'htl_booking_detail`
WHERE `id_order`='.(int) $idOrder;
if ($idOrderDetail) {
$sql .= ' AND `id_order_detail`='.(int)$idOrderDetail;
}
if ($idProduct) {
$sql .= ' AND `id_product`='.(int)$idProduct;
}
@@ -247,16 +252,21 @@ class HotelBookingDemands extends ObjectModel
|| Configuration::get('PS_INVOICE_TAXES_BREAKDOWN');
}
public function getExtraDemandsTaxesDetails($idOrder)
public function getExtraDemandsTaxesDetails($idOrder, $idsOrderDetail = [])
{
$taxDetails = Db::getInstance()->executeS(
'SELECT hb.`id` as id_htl_booking, hbd.`unit_price_tax_excl`, hbd.`total_price_tax_excl`, hb.`id_order_detail`, hdt.*, t.* FROM '._DB_PREFIX_.'orders o '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_detail hb ON hb.id_order = o.id_order '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_demands hbd ON hbd.id_htl_booking = hb.id '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_demands_tax hdt ON hbd.id_booking_demand = hdt.id_booking_demand '.
'INNER JOIN '._DB_PREFIX_.'tax t ON t.id_tax = hdt.id_tax '.
'WHERE o.id_order = '.(int)$idOrder
);
$sql = 'SELECT hb.`id` as id_htl_booking, hbd.`unit_price_tax_excl`, hbd.`total_price_tax_excl`, hb.`id_order_detail`, hdt.*, t.* FROM '._DB_PREFIX_.'orders o '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_detail hb ON hb.id_order = o.id_order '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_demands hbd ON hbd.id_htl_booking = hb.id '.
'INNER JOIN '._DB_PREFIX_.'htl_booking_demands_tax hdt ON hbd.id_booking_demand = hdt.id_booking_demand '.
'INNER JOIN '._DB_PREFIX_.'tax t ON t.id_tax = hdt.id_tax '.
'WHERE o.id_order = '.(int)$idOrder;
if ($idsOrderDetail) {
$sql .= ' AND hb.`id_order_detail` IN ('.implode(',', $idsOrderDetail).')';
}
$taxDetails = Db::getInstance()->executeS($sql);
if ($taxDetails) {
foreach ($taxDetails as &$detail) {
$priceTaxExcl = $detail['unit_price_tax_excl'];

View File

@@ -2242,13 +2242,17 @@ class HotelBookingDetail extends ObjectModel
* @param int $id_customer [description]
* @return [type] [description]
*/
public function getOnlyOrderBookingData($id_order, $id_guest, $id_product, $id_customer = 0)
public function getOnlyOrderBookingData($id_order, $id_guest, $id_product, $id_customer = 0, $id_order_detail = 0)
{
$sql = 'SELECT hbd.*, od.`unit_price_tax_incl`, od.`unit_price_tax_excl`, od.`reduction_amount_tax_excl`,
od.`reduction_amount_tax_incl` FROM `'._DB_PREFIX_.'htl_booking_detail` hbd
INNER JOIN `'._DB_PREFIX_.'order_detail` od ON (od.`id_order_detail` = hbd.`id_order_detail`)
WHERE hbd.`id_order` = '.(int)$id_order.' AND hbd.`id_product` = '.(int)$id_product;
if ($id_order_detail) {
$sql .= ' AND hbd.`id_order_detail` = '.(int)$id_order_detail;
}
if ($id_customer) {
$sql .= ' AND hbd.`id_customer` = '.(int)$id_customer;
}

View File

@@ -68,7 +68,8 @@ class RoomTypeServiceProductOrderDetail extends ObjectModel
$getTotalPrice = 0,
$useTax = null,
$autoAddToCart = 0,
$priceAdditionType = null
$priceAdditionType = null,
$idOrderDetail = 0
) {
if ($useTax === null) {
@@ -85,6 +86,10 @@ class RoomTypeServiceProductOrderDetail extends ObjectModel
$sql .= ' LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON(od.`id_order_detail` = rsod.`id_order_detail`)';
$sql .= ' WHERE rsod.`id_order` = '.(int)$idOrder;
if ($idOrderDetail) {
$sql .= ' AND rsod.`id_order_detail` = '.(int)$idOrderDetail;
}
if (!is_null($autoAddToCart)) {
$sql .= ' AND od.`product_auto_add` = '. (int)$autoAddToCart;
if ($autoAddToCart == 1 && !is_null($priceAdditionType)) {