From c15a2fb166bed0829207e721432b1f017fd594fe Mon Sep 17 00:00:00 2001 From: aradhana kumari Date: Thu, 5 Feb 2026 12:45:49 +0530 Subject: [PATCH] Added : Add new credit slip option --- classes/order/OrderSlip.php | 23 +-- classes/pdf/HTMLTemplateOrderSlip.php | 13 +- controllers/admin/AdminSlipController.php | 185 +++++++++++++++++++--- js/admin/slips.js | 45 ++++++ 4 files changed, 217 insertions(+), 49 deletions(-) diff --git a/classes/order/OrderSlip.php b/classes/order/OrderSlip.php index 3f1b2694c..758b6773a 100644 --- a/classes/order/OrderSlip.php +++ b/classes/order/OrderSlip.php @@ -79,6 +79,8 @@ class OrderSlipCore extends ObjectModel const REDEEM_STATUS_ACTIVE = 1; const REDEEM_STATUS_REDEEMED = 2; + const REFUND_ORDER_SLIP_TYPE = 1; + const MANUAL_CREDIT_SLIP_TYPE = 2; /** * @see ObjectModel::$definition @@ -276,6 +278,7 @@ class OrderSlipCore extends ObjectModel SELECT `id_order_slip` FROM `'._DB_PREFIX_.'order_slip` os LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_order` = os.`id_order`) + INNER JOIN `'._DB_PREFIX_.'htl_booking_detail` hbd ON (o.id_order = hbd.id_order) WHERE os.`date_add` BETWEEN \''.pSQL($dateFrom).' 00:00:00\' AND \''.pSQL($dateTo).' 23:59:59\' '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o'). ' ' .HotelBranchInformation::addHotelRestriction(false).' ORDER BY os.`date_add` ASC'); @@ -310,7 +313,7 @@ class OrderSlipCore extends ObjectModel return OrderSlip::create($order, $product_list, $shipping); } - public static function create(Order $order, $product_list, $shipping_cost = false, $amount = 0, $amount_choosen = false, $add_tax = true) + public static function create(Order $order, $product_list, $shipping_cost = false, $amount = 0, $amount_choosen = false, $add_tax = true , $orderSlipType = false) { $currency = new Currency((int)$order->id_currency); $order_slip = new OrderSlip(); @@ -365,17 +368,6 @@ class OrderSlipCore extends ObjectModel } $price = (float)$product_row['unit_price']; - - $order_slip_resume = OrderSlip::getProductSlipResume((int)$order_detail->id); - - if ($quantity + $order_slip_resume['product_quantity'] > $order_detail->product_quantity) { - $quantity = $order_detail->product_quantity - $order_slip_resume['product_quantity']; - } - - if ($quantity == 0) { - continue; - } - if (!Tools::isSubmit('cancelProduct') && $order->hasBeenPaid()) { $order_detail->product_quantity_refunded += $quantity; } @@ -442,11 +434,8 @@ class OrderSlipCore extends ObjectModel $order_slip->{'total_products_tax_'.$inc_or_ex_2} -= (float)$amount && !$amount_choosen ? (float)$amount : 0; $order_slip->amount = $amount_choosen ? (float)$amount : $order_slip->{'total_products_tax_'.$inc_or_ex_1}; - if ((float)$amount && !$amount_choosen) { - $order_slip->order_slip_type = 1; - } - if (((float)$amount && $amount_choosen)) { - $order_slip->order_slip_type = 2; + if (isset($orderSlipType) && $orderSlipType) { + $order_slip->order_slip_type = $orderSlipType; } if (!$order_slip->add()) { diff --git a/classes/pdf/HTMLTemplateOrderSlip.php b/classes/pdf/HTMLTemplateOrderSlip.php index 16396243f..01e3f555c 100644 --- a/classes/pdf/HTMLTemplateOrderSlip.php +++ b/classes/pdf/HTMLTemplateOrderSlip.php @@ -143,15 +143,6 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice $this->order->total_paid_tax_excl += $this->order->total_shipping_tax_excl; $total_cart_rule = 0; - if ($this->order_slip->order_slip_type == 1 && is_array($cart_rules = $this->order->getCartRules($this->order_invoice->id))) { - foreach ($cart_rules as $cart_rule) { - if ($tax_excluded_display) { - $total_cart_rule += $cart_rule['value_tax_excl']; - } else { - $total_cart_rule += $cart_rule['value']; - } - } - } $this->smarty->assign(array( 'order' => $this->order, @@ -159,8 +150,8 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice 'order_details' => $this->order->products, 'roomsDetails' => $roomsDetails, 'productsDetails' => $productsDetails, - 'cart_rules' => $this->order_slip->order_slip_type == 1 ? $this->order->getCartRules($this->order_invoice->id) : false, - 'amount_choosen' => $this->order_slip->order_slip_type == 2 ? true : false, + 'cart_rules' => false, + 'amount_choosen' => false, 'delivery_address' => $formatted_delivery_address, 'invoice_address' => $formatted_invoice_address, 'addresses' => array('invoice' => $invoice_address, 'delivery' => $delivery_address), diff --git a/controllers/admin/AdminSlipController.php b/controllers/admin/AdminSlipController.php index 6f104daee..1b767a2f5 100644 --- a/controllers/admin/AdminSlipController.php +++ b/controllers/admin/AdminSlipController.php @@ -115,33 +115,96 @@ class AdminSlipControllerCore extends AdminController public function initPageHeaderToolbar() { + if (empty($this->display)) { + $this->page_header_toolbar_btn['new_credit_slip'] = array( + 'href' => self::$currentIndex . '&add' . $this->table . '&token=' . $this->token, + 'desc' => $this->l('Add new credit slip', null, null, false), + 'icon' => 'process-icon-new' + ); + } + parent::initPageHeaderToolbar(); } public function renderForm() { - $this->fields_form = array( - 'legend' => array( - 'title' => $this->l('Print a PDF'), - 'icon' => 'icon-print' - ), - 'input' => array( - array( - 'type' => 'date', - 'label' => $this->l('From'), - 'name' => 'date_from', - 'maxlength' => 10, - 'required' => true, - 'hint' => $this->l('Format: 2011-12-31 (inclusive).') + if ($this->display == 'add') { + $order = new Order(); + $orderList = $order->getOrdersWithInformations(); + foreach ($orderList as &$order) { + $order['order_label'] = $order['reference'] . ' #' . (int) $order['id_order']; + } + unset($order); + + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Credit slip'), + 'icon' => 'icon-print' ), - array( - 'type' => 'date', - 'label' => $this->l('To'), - 'name' => 'date_to', - 'maxlength' => 10, - 'required' => true, - 'hint' => $this->l('Format: 2012-12-31 (inclusive).') + 'description' => $this->l('Kindly check the credit slip again before saving it, as it cannot be changed once created.'), + 'input' => array( + array( + 'type' => 'select', + 'label' => $this->l('Order id'), + 'name' => 'id_order', + 'required' => true, + 'hint' => $this->l('Select id order'), + 'class' => 'chosen', + 'options' => array( + 'query' => $orderList, + 'id' => 'id_order', + 'name' => 'order_label' + ) + ), + array( + 'type' => 'select', + 'label' => $this->l('Booking Detail'), + 'name' => 'id_booking_detail', + 'required' => true, + 'options' => array( + 'query' => array(), + 'id' => 'id', + 'name' => 'booking_label' + ) + ), + array( + 'type' => 'text', + 'label' => $this->l('Amount of credit slip'), + 'hint' => $this->l('Enter the amount of the credit slip for the customer'), + 'name' => 'credit_slip_amount', + 'required' => true, + 'col' => 2, + 'suffix' => $this->context->currency->sign, + ), + ), + 'submit' => array( + 'title' => $this->l('Save'), + 'name' => 'submitCreditSlip', ) + ); + } else { + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Print a PDF'), + 'icon' => 'icon-print' + ), + 'input' => array( + array( + 'type' => 'date', + 'label' => $this->l('From'), + 'name' => 'date_from', + 'maxlength' => 10, + 'required' => true, + 'hint' => $this->l('Format: 2011-12-31 (inclusive).') + ), + array( + 'type' => 'date', + 'label' => $this->l('To'), + 'name' => 'date_to', + 'maxlength' => 10, + 'required' => true, + 'hint' => $this->l('Format: 2012-12-31 (inclusive).') + ) ), 'submit' => array( 'title' => $this->l('Generate PDF file'), @@ -149,6 +212,7 @@ class AdminSlipControllerCore extends AdminController 'icon' => 'process-icon-download-alt' ) ); + } $this->fields_value = array( 'date_from' => date('Y-m-d'), @@ -161,7 +225,67 @@ class AdminSlipControllerCore extends AdminController public function postProcess() { - if (Tools::getValue('submitAddorder_slip')) { + if (Tools::getValue('submitCreditSlip')) { + if (empty(trim(Tools::getValue('credit_slip_amount')))) { + $this->errors[] = $this->l('Amount is required for the generate credit slip'); + } + if (!count($this->errors)) { + $objOrder = new Order(Tools::getValue('id_order')); + $creditAmount = Tools::getValue('credit_slip_amount'); + $idBookingDetail = Tools::getValue('id_booking_detail'); + $customer = new Customer($objOrder->id_customer); + $bookingList = array(); + $objHotelBookingDetail = new HotelBookingDetail($idBookingDetail); + + if ($idHtlBooking = $objHotelBookingDetail->id) { + $numDays = HotelHelper::getNumberOfDays( + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to + ); + + $idOrderDetail = $objHotelBookingDetail->id_order_detail; + + $bookingList = array( + array( + 'id_htl_booking' => $idHtlBooking, + 'id_order_detail' => $idOrderDetail, + 'quantity' => $numDays, + 'num_days' => $numDays, + 'unit_price' => (float) $creditAmount / $numDays, + 'amount' => (float) $creditAmount, + ) + ); + } + + if (!$idCreditSlip = OrderSlip::create($objOrder, $bookingList, 0, $creditAmount, $creditAmount, 0 , OrderSlip::MANUAL_CREDIT_SLIP_TYPE)) { + $this->errors[] = $this->l('A credit slip cannot be generated. '); + } else { + + Hook::exec('actionOrderSlipAdd', array('order' => $objOrder, 'bookingList' => $bookingList)); + + $params['{credit_slip_url}'] = $this->context->link->getPageLink('order-slip', true); + + @Mail::Send( + (int)$objOrder->id_lang, + 'credit_slip', + Mail::l('New credit slip generated for you', (int)$objOrder->id_lang), + $params, + $customer->email, + $customer->firstname.' '.$customer->lastname, + null, + null, + null, + null, + _PS_MAIL_DIR_, + true, + (int)$objOrder->id_shop + ); + } + Tools::redirectAdmin(self::$currentIndex . '&token=' . $this->token . '&conf=3'); + } else { + return parent::postProcess(); + } + } else if (Tools::getValue('submitAddorder_slip')) { if (!Validate::isDate(Tools::getValue('date_from'))) { $this->errors[] = $this->l('Invalid "From" date'); } @@ -236,9 +360,13 @@ class AdminSlipControllerCore extends AdminController $this->initTabModuleList(); $this->initToolbar(); $this->initPageHeaderToolbar(); + if ($this->display != "add") { $this->content .= $this->renderList(); + } $this->content .= $this->renderForm(); + if ($this->display != "add") { $this->content .= $this->renderOptions(); + } $this->context->smarty->assign(array( 'content' => $this->content, @@ -342,12 +470,27 @@ class AdminSlipControllerCore extends AdminController $this->ajaxDie(json_encode($response)); } + public function ajaxProcessGetBookingDetails() + { + $idOrder = (int) Tools::getValue('id_order'); + $objOrder = new Order($idOrder); + $currency = Currency::getCurrency((int)$objOrder->id_currency); + $objHotelBookingDetail = new HotelBookingDetail(); + $bookingDetails = $objHotelBookingDetail->getBookingDataByOrderId($idOrder); + + die(Tools::jsonEncode(array( + 'bookings' => $bookingDetails, + 'currency' => $currency + ))); + } + public function setMedia() { parent::setMedia(); Media::addJsDef( array( 'admin_order_slip_tab_link' => $this->context->link->getAdminLink('AdminSlip'), + 'ajax_booking_url' => $this->context->link->getAdminLink('AdminSlip', true), ) ); $this->addJS(_PS_JS_DIR_.'admin/slips.js'); diff --git a/js/admin/slips.js b/js/admin/slips.js index c133888e7..7d5c10e79 100644 --- a/js/admin/slips.js +++ b/js/admin/slips.js @@ -103,4 +103,49 @@ $(document).ready(function() { }); }); } + + $('select[name="id_order"]').on('change', function () { + + var idOrder = $(this).val(); + + if (!idOrder) return; + + $.ajax({ + url: ajax_booking_url, + type: 'POST', + dataType: 'json', + data: { + ajax: 1, + action: 'GetBookingDetails', + id_order: idOrder + }, + success: function (response) { + + var $bookingSelect = $('select[name="id_booking_detail"]'); + $bookingSelect.empty(); + + if (response.bookings.length > 0) { + $.each(response.bookings, function (index, booking) { + $bookingSelect.append( + $('