From 111361143a5394595404ab261218dcb7e5dce199 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Sat, 18 May 2024 15:51:04 +0530 Subject: [PATCH 01/12] Resolved merge Conflicts --- classes/order/OrderReturn.php | 4 +- controllers/admin/AdminOrdersController.php | 84 ++++++++++------- controllers/front/OrderDetailController.php | 94 +++++++++---------- .../classes/HotelBookingDetail.php | 81 ++++------------ .../AdminOrderRefundRequestsController.php | 15 ++- .../helpers/view/view.tpl | 6 +- .../hotel-reservation-theme/order-detail.tpl | 6 +- 7 files changed, 131 insertions(+), 159 deletions(-) diff --git a/classes/order/OrderReturn.php b/classes/order/OrderReturn.php index aaace5681..b64d9a024 100644 --- a/classes/order/OrderReturn.php +++ b/classes/order/OrderReturn.php @@ -449,8 +449,8 @@ public function changeIdOrderReturnState($newOrderReturnState, $idLang = 0) '{order_reference}' => $objOrder->reference, '{order_date}' => date('Y-m-d h:i:s', strtotime($objOrder->date_add)), '{refunded_amount}' => Tools::displayPrice($this->refunded_amount, new Currency($objOrder->id_currency)), - '{payment_mode}' => $this->payment_mode, - '{id_transaction}' => $this->id_transaction, + '{payment_mode}' => $this->payment_mode ? $this->payment_mode : '--', + '{id_transaction}' => $this->id_transaction ? $this->id_transaction : '--', ); // if mail is true for the customer then send mail to customer with selected template diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 3cfdfdf15..3e09788dc 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -499,11 +499,14 @@ public function initToolbar() ); if (((int) $order->isReturnable()) && !$order->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_OR_REFUND_REQUEST_FLAG)) { + $orderTotalPaid = $order->getTotalPaid(); + $orderDiscounts = $order->getCartRules(); + $hasOrderDiscountOrPayment = ((float)$orderTotalPaid > 0 || $orderDiscounts) ? true : false; $this->toolbar_btn['cancel'] = array( - 'short' => ((float) $order->getTotalPaid()) ? $this->l('Refund') : $this->l('Cancel'), + 'short' => ($hasOrderDiscountOrPayment) ? $this->l('Refund') : $this->l('Cancel'), 'href' => '#refundForm', 'id' => 'desc-order-standard_refund', - 'desc' => ((float) $order->getTotalPaid()) ? $this->l('Initiate refund') : $this->l('Cancel bookings'), + 'desc' => ($hasOrderDiscountOrPayment) ? $this->l('Initiate refund') : $this->l('Cancel bookings'), 'class' => 'icon-exchange', 'target' => true, ); @@ -1518,51 +1521,60 @@ public function postProcess() } if (!count($this->errors)) { - // if amount paid is > 0 then create refund request else cancel the booking directly - if ($order->getTotalPaid() > 0) { - $objOrderReturn = new OrderReturn(); - $objOrderReturn->id_customer = $order->id_customer; - $objOrderReturn->id_order = $order->id; - $objOrderReturn->state = Configuration::get('PS_ORS_PENDING'); - $objOrderReturn->by_admin = 1; - $objOrderReturn->question = $refundReason; - $objOrderReturn->save(); - if ($objOrderReturn->id) { - foreach ($bookings as $idHtlBooking) { - $objHtlBooking = new HotelBookingDetail($idHtlBooking); - $numDays = $objHtlBooking->getNumberOfDays( - $objHtlBooking->date_from, - $objHtlBooking->date_to - ); - $objOrderReturnDetail = new OrderReturnDetail(); - $objOrderReturnDetail->id_order_return = $objOrderReturn->id; - $objOrderReturnDetail->id_order_detail = $objHtlBooking->id_order_detail; - $objOrderReturnDetail->product_quantity = $numDays; - $objOrderReturnDetail->id_htl_booking = $idHtlBooking; - $objOrderReturnDetail->save(); - } + // create refund request + $objOrderReturn = new OrderReturn(); + $objOrderReturn->id_customer = $order->id_customer; + $objOrderReturn->id_order = $order->id; + $objOrderReturn->state = 0; + $objOrderReturn->by_admin = 1; + $objOrderReturn->question = $refundReason; + $objOrderReturn->refunded_amount = 0; + $objOrderReturn->save(); + if ($objOrderReturn->id) { + foreach ($bookings as $idHtlBooking) { + $objHtlBooking = new HotelBookingDetail($idHtlBooking); + $numDays = $objHtlBooking->getNumberOfDays( + $objHtlBooking->date_from, + $objHtlBooking->date_to + ); + $objOrderReturnDetail = new OrderReturnDetail(); + $objOrderReturnDetail->id_order_return = $objOrderReturn->id; + $objOrderReturnDetail->id_order_detail = $objHtlBooking->id_order_detail; + $objOrderReturnDetail->product_quantity = $numDays; + $objOrderReturnDetail->id_htl_booking = $idHtlBooking; + $objOrderReturnDetail->refunded_amount = 0; + $objOrderReturnDetail->save(); } - } else { - // cancel the booking directly + } + + // Change order refund state + // Emails to customer, superadmin and employees on refund request state change + $objOrderReturn->changeIdOrderReturnState(Configuration::get('PS_ORS_PENDING')); + + // process direct cancellations + if (!$order->getCartRules() && $order->getTotalPaid() <= 0) { + // Process refund in booking tables foreach ($bookings as $idHtlBooking) { $objHtlBooking = new HotelBookingDetail($idHtlBooking); if (!$objHtlBooking->processRefundInBookingTables()) { $this->errors[] = Tools::displayError('An error occurred while cancelling the booking.'); - } else { - // set the message for the cancellation - $message = $this->l('Room').': '.$objHtlBooking->room_num.', '.$objHtlBooking->room_type_name.' '.$this->l('has been cancelled by the hotel.'); - $message .= PHP_EOL.$this->l('Reasonn').': '.$refundReason; - $objHtlBooking->setBookingCancellationMessage($message, 1); } } + // complete the booking refund directly in the refund request + $objOrderReturn->changeIdOrderReturnState(Configuration::get('PS_ORS_REFUNDED')); + + // if all bookings are getting cancelled/Refunded then Cancel/Refund the order also + $idOrderState = 0; + if ($order->hasCompletelyRefunded(Order::ORDER_COMPLETE_REFUND_FLAG)) { + $idOrderState = Configuration::get('PS_OS_REFUND'); + } elseif ($order->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)) { + $idOrderState = Configuration::get('PS_OS_CANCELED'); + } - // if all bookings are getting cancelled then cancel the order also - if ($order->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)) { + if ($idOrderState) { $objOrderHistory = new OrderHistory(); $objOrderHistory->id_order = (int)$order->id; - $idOrderState = Configuration::get('PS_OS_CANCELED'); - $useExistingPayment = false; if (!$order->hasInvoice()) { $useExistingPayment = true; diff --git a/controllers/front/OrderDetailController.php b/controllers/front/OrderDetailController.php index afa1ea9e8..5faf12d88 100644 --- a/controllers/front/OrderDetailController.php +++ b/controllers/front/OrderDetailController.php @@ -202,6 +202,8 @@ public function initContent() $cartHotelData[$type_key]['date_diff'][$date_join]['hotel_booking_details'][$data_v['id']]['child_ages'] = $data_v['child_ages']; $cartHotelData[$type_key]['date_diff'][$date_join]['hotel_booking_details'][$data_v['id']]['is_refunded'] = $data_v['is_refunded']; $cartHotelData[$type_key]['date_diff'][$date_join]['hotel_booking_details'][$data_v['id']]['is_cancelled'] = $data_v['is_cancelled']; + $cartHotelData[$type_key]['date_diff'][$date_join]['hotel_booking_details'][$data_v['id']]['check_in'] = $data_v['check_in']; + $cartHotelData[$type_key]['date_diff'][$date_join]['hotel_booking_details'][$data_v['id']]['check_out'] = $data_v['check_out']; $cartHotelData[$type_key]['date_diff'][$date_join]['is_refunded'] = $data_v['is_refunded']; @@ -561,6 +563,12 @@ public function displayAjaxSubmitRefundRequest() break; } + // the room has already been checked in will not be able to be cancelled by the customer + if ($objHotelBookingDetail->check_in != '0000-00-00 00:00:00') { + $this->errors[] = Tools::displayError('Some selected rooms have already been checked in.'); + break; + } + if (OrderReturn::getOrdersReturnDetail($objOrder->id, 0, $idHtlBooking)) { $this->errors[] = Tools::displayError('Some selected rooms have already been requested for cancellation.'); break; @@ -569,66 +577,58 @@ public function displayAjaxSubmitRefundRequest() } if (!count($this->errors)) { - if ((int) $objOrder->total_paid_real == 0) { + // create refund request + $objOrderReturn = new OrderReturn(); + $objOrderReturn->id_customer = $objOrder->id_customer; + $objOrderReturn->id_order = $objOrder->id; + $objOrderReturn->state = 0; + $objOrderReturn->by_admin = 0; + $objOrderReturn->question = $cancellationReason; + $objOrderReturn->refunded_amount = 0; + $objOrderReturn->save(); + if ($objOrderReturn->id) { foreach ($idsHtlBooking as $idHtlBooking) { $objHtlBooking = new HotelBookingDetail($idHtlBooking); - $objHtlBooking->save(); + $numDays = $objHtlBooking->getNumberOfDays( + $objHtlBooking->date_from, + $objHtlBooking->date_to + ); + $objOrderReturnDetail = new OrderReturnDetail(); + $objOrderReturnDetail->id_order_return = $objOrderReturn->id; + $objOrderReturnDetail->id_order_detail = $objHtlBooking->id_order_detail; + $objOrderReturnDetail->product_quantity = $numDays; + $objOrderReturnDetail->id_htl_booking = $idHtlBooking; + $objOrderReturnDetail->refunded_amount = 0; + $objOrderReturnDetail->save(); } } - // if amount paid is > 0 then create refund request else cancel the booking directly - if ($objOrder->getTotalPaid() > 0) { - $objOrderReturn = new OrderReturn(); - $objOrderReturn->id_customer = $objOrder->id_customer; - $objOrderReturn->id_order = $objOrder->id; - $objOrderReturn->state = 0; - $objOrderReturn->by_admin = 0; - $objOrderReturn->question = $cancellationReason; - $objOrderReturn->save(); - if ($objOrderReturn->id) { - foreach ($idsHtlBooking as $idHtlBooking) { - $objHotelBookingDetail = new HotelBookingDetail($idHtlBooking); - $numDays = $objHotelBookingDetail->getNumberOfDays( - $objHotelBookingDetail->date_from, - $objHotelBookingDetail->date_to - ); - - $objOrderReturnDetail = new OrderReturnDetail(); - $objOrderReturnDetail->id_order_return = $objOrderReturn->id; - $objOrderReturnDetail->id_order_detail = $objHotelBookingDetail->id_order_detail; - $objOrderReturnDetail->product_quantity = $numDays; - $objOrderReturnDetail->id_htl_booking = $idHtlBooking; - $objOrderReturnDetail->save(); - } - } + // Emails to customer, superadmin and employees on refund request state change + $objOrderReturn->changeIdOrderReturnState(Configuration::get('PS_ORS_PENDING')); - // Emails to customer, superadmin and employees on refund request state change - $objOrderReturn->changeIdOrderReturnState(Configuration::get('PS_ORS_PENDING')); - } else { - // cancel the booking directly + if ($objOrder->getTotalPaid() <= 0) { + // Process refund in booking tables foreach ($idsHtlBooking as $idHtlBooking) { - $objHotelBookingDetail = new HotelBookingDetail($idHtlBooking); - if (!$objHotelBookingDetail->processRefundInBookingTables()) { - $this->errors[] = Tools::displayError('Something went wrong. Please try later.'); - } else { - // set the message for the cancellation - $message = Tools::displayError(sprintf( - "1 room from %s has been cancelled by hotel.\nReason: %s", - $objHotelBookingDetail->room_type_name, - $cancellationReason - )); - - $objHotelBookingDetail->setBookingCancellationMessage($message); + $objHtlBooking = new HotelBookingDetail($idHtlBooking); + if (!$objHtlBooking->processRefundInBookingTables()) { + $this->errors[] = Tools::displayError('An error occurred while cancelling the booking.'); } } + // complete the booking refund directly in the refund request + $objOrderReturn->changeIdOrderReturnState(Configuration::get('PS_ORS_REFUNDED')); + + // if all bookings are getting cancelled/Refunded then Cancel/Refund the order also + $idOrderState = 0; + if ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_REFUND_FLAG)) { + $idOrderState = Configuration::get('PS_OS_REFUND'); + } elseif ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)) { + $idOrderState = Configuration::get('PS_OS_CANCELED'); + } - // if all bookings are getting cancelled then cancel the order also - if ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)) { + if ($idOrderState) { $objOrderHistory = new OrderHistory(); $objOrderHistory->id_order = (int)$objOrder->id; - $idOrderState = Configuration::get('PS_OS_CANCELED'); - $useExistingPayment = false; if (!$objOrder->hasInvoice()) { $useExistingPayment = true; diff --git a/modules/hotelreservationsystem/classes/HotelBookingDetail.php b/modules/hotelreservationsystem/classes/HotelBookingDetail.php index 047e1de46..e881e0f2f 100644 --- a/modules/hotelreservationsystem/classes/HotelBookingDetail.php +++ b/modules/hotelreservationsystem/classes/HotelBookingDetail.php @@ -2870,9 +2870,12 @@ public function processRefundInBookingTables() ); $objOrder = new Order($this->id_order); $orderTotalPaid = $objOrder->getTotalPaid(); + $orderDiscounts = $objOrder->getCartRules(); + + $hasOrderDiscountOrPayment = ((float)$orderTotalPaid > 0 || $orderDiscounts) ? true : false; // things to do if order is not paid - if ((float) $orderTotalPaid <= 0) { + if (!$hasOrderDiscountOrPayment) { $objHotelBookingDemands = new HotelBookingDemands(); $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); @@ -2945,16 +2948,26 @@ public function processRefundInBookingTables() $objOrderDetail->product_quantity_refunded = $objOrderDetail->product_quantity; } - if ((float) $orderTotalPaid <= 0) { + if (!$hasOrderDiscountOrPayment) { // reduce room amount from order and order detail $objOrderDetail->total_price_tax_incl -= Tools::ps_round((float) $this->total_price_tax_incl, 6); $objOrderDetail->total_price_tax_excl -= Tools::ps_round((float) $this->total_price_tax_excl, 6); if (Validate::isLoadedObject($objOrder = new Order($this->id_order))) { - $objOrder->total_paid -= $reduction_amount['total_price_tax_incl']; + $objOrder->total_paid -= Tools::ps_round($reduction_amount['total_price_tax_incl'], 6); + $objOrder->total_paid = $objOrder->total_paid > 0 ? $objOrder->total_paid : 0; + $objOrder->total_paid_tax_excl -= Tools::ps_round((float) $reduction_amount['total_price_tax_excl'], 6); + $objOrder->total_paid_tax_excl = $objOrder->total_paid_tax_excl > 0 ? $objOrder->total_paid_tax_excl : 0; + $objOrder->total_paid_tax_incl -= Tools::ps_round((float) $reduction_amount['total_price_tax_incl'], 6); + $objOrder->total_paid_tax_incl = $objOrder->total_paid_tax_incl > 0 ? $objOrder->total_paid_tax_incl : 0; + $objOrder->total_products -= Tools::ps_round((float) $reduction_amount['total_products_tax_excl'], 6); + $objOrder->total_products = $objOrder->total_products > 0 ? $objOrder->total_products : 0; + $objOrder->total_products_wt -= Tools::ps_round((float) $reduction_amount['total_products_tax_incl'], 6); + $objOrder->total_products_wt = $objOrder->total_products_wt > 0 ? $objOrder->total_products_wt : 0; + $objOrder->save(); } } @@ -2964,7 +2977,7 @@ public function processRefundInBookingTables() // as refund is completed then set the booking as refunded $this->is_refunded = 1; - if ((float) $orderTotalPaid <= 0) { + if (!$hasOrderDiscountOrPayment) { // Reduce room amount from htl_booking_detail $this->is_cancelled = 1; $this->total_price_tax_excl = 0; @@ -2979,66 +2992,6 @@ public function processRefundInBookingTables() return false; } - public function setBookingCancellationMessage($message, $byAdmin = 0) - { - if (Validate::isLoadedObject($this)) { - // First save the message for the admin for the refund - $objectMessage = new Message(); - $objectMessage->message = $message; - $objectMessage->id_cart = (int)$this->id_cart; - $objectMessage->id_customer = (int)($this->id_customer); - $objectMessage->id_order = (int)$this->id_order; - $objectMessage->private = 1; - - // If cancelled by admin then set the employee id who is doing this cancellation else put 0 - $context = Context::getContext(); - if ($byAdmin) { - $objectMessage->id_employee = (int)$context->employee->id; - } else { - $objectMessage->id_employee = 0; - } - - $objectMessage->add(); - - // save this message for the customer - $objCustomer = new Customer($this->id_customer); - $idCustomerThread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($objCustomer->email, $this->id_order); - if (!$idCustomerThread) { - $objCustomerThread = new CustomerThread(); - $objCustomerThread->id_contact = 0; - $objCustomerThread->id_customer = (int)$this->id_customer; - $objCustomerThread->id_shop = (int)$context->shop->id; - $objCustomerThread->id_order = (int)$this->id_order; - $objCustomerThread->id_lang = (int)$context->language->id; - $objCustomerThread->email = $objCustomer->email; - $objCustomerThread->status = 'open'; - $objCustomerThread->token = Tools::passwdGen(12); - $objCustomerThread->add(); - } else { - $objCustomerThread = new CustomerThread((int)$idCustomerThread); - } - - $objCustomerMessage = new CustomerMessage(); - $objCustomerMessage->id_customer_thread = $objCustomerThread->id; - - // If cancelled by admin then set the employee id who is doing this cancellation else put 0 - if ($byAdmin) { - $objCustomerMessage->id_employee = (int)$context->employee->id; - } else { - $objCustomerMessage->id_employee = 0; - } - - $objCustomerMessage->message = $message; - $objCustomerMessage->private = 0; - - $objCustomerMessage->add(); - - return true; - } - - return false; - } - /** * Get overbooked rooms in the order|hotel * @param [int] $idOrder : id of the order diff --git a/modules/hotelreservationsystem/controllers/admin/AdminOrderRefundRequestsController.php b/modules/hotelreservationsystem/controllers/admin/AdminOrderRefundRequestsController.php index fa74ab46a..9f49d48ac 100644 --- a/modules/hotelreservationsystem/controllers/admin/AdminOrderRefundRequestsController.php +++ b/modules/hotelreservationsystem/controllers/admin/AdminOrderRefundRequestsController.php @@ -237,9 +237,13 @@ public function renderView() } } + $orderTotalPaid = $objOrder->getTotalPaid(); + $orderDiscounts = $objOrder->getCartRules(); + $hasOrderDiscountOrPayment = ((float)$orderTotalPaid > 0 || $orderDiscounts) ? true : false; $this->context->smarty->assign( array ( - 'orderTotalPaid' => $objOrder->getTotalPaid(), + 'hasOrderDiscountOrPayment' => $hasOrderDiscountOrPayment, + 'orderTotalPaid' => $orderTotalPaid, 'customer_name' => $objCustomer->firstname.' '.$objCustomer->lastname, 'customer_email' => $objCustomer->email, 'orderReturnInfo' => (array)$objOrderReturn, @@ -277,11 +281,14 @@ public function postProcess() if (Validate::isLoadedObject($objOrderReturn = new OrderReturn($idOrderReturn))) { $objOrder = new Order($objOrderReturn->id_order); $orderTotalPaid = $objOrder->getTotalPaid(); + $orderDiscounts = $objOrder->getCartRules(); + $hasOrderDiscountOrPayment = ((float)$orderTotalPaid > 0 || $orderDiscounts) ? true : false; + $idRefundState = Tools::getValue('id_refund_state'); if (Validate::isLoadedObject($objRefundState = new OrderReturnState($idRefundState))) { if ($objRefundState->refunded) { $refundedAmounts = Tools::getValue('refund_amounts'); - if ((float) $orderTotalPaid > 0) { + if ($hasOrderDiscountOrPayment) { if ($idsReturnDetail && count($idsReturnDetail)) { if ($refundedAmounts) { foreach ($idsReturnDetail as $idRetDetail) { @@ -392,7 +399,7 @@ public function postProcess() } // if bookings are refunded then set the payment information - if ((float) $orderTotalPaid > 0) { + if ($hasOrderDiscountOrPayment) { if (Tools::isSubmit('refundTransactionAmount')) { $objOrderReturn->payment_mode = $paymentMode; $objOrderReturn->id_transaction = $idTransaction; @@ -415,7 +422,7 @@ public function postProcess() $idOrderState = 0; if ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_REFUND_FLAG)) { $idOrderState = Configuration::get('PS_OS_REFUND'); - } elseif ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_REFUND_FLAG)) { + } elseif ($objOrder->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)) { $idOrderState = Configuration::get('PS_OS_CANCELED'); } diff --git a/modules/hotelreservationsystem/views/templates/admin/order_refund_requests/helpers/view/view.tpl b/modules/hotelreservationsystem/views/templates/admin/order_refund_requests/helpers/view/view.tpl index e65a96df7..767e2801c 100644 --- a/modules/hotelreservationsystem/views/templates/admin/order_refund_requests/helpers/view/view.tpl +++ b/modules/hotelreservationsystem/views/templates/admin/order_refund_requests/helpers/view/view.tpl @@ -131,7 +131,7 @@ {if !$isRefundCompleted} {l s='Rooms cancelation charges' mod='hotelreservationsystem'} {/if} - {if $orderTotalPaid|floatval} + {if $hasOrderDiscountOrPayment} {l s='Refund amount' mod='hotelreservationsystem'} {/if} @@ -184,7 +184,7 @@ {/if} {/if} - {if $orderTotalPaid|floatval} + {if $hasOrderDiscountOrPayment}
{if $isRefundCompleted} @@ -279,7 +279,7 @@
{* Fields to submit refund information *} - {if $orderTotalPaid|floatval} + {if $hasOrderDiscountOrPayment}