Merge branch 'develop' into gli-1798

This commit is contained in:
Shreesh Arora
2024-06-21 17:54:12 +05:30
87 changed files with 2793 additions and 1547 deletions

View File

@@ -285,11 +285,6 @@ class AdminOrdersControllerCore extends AdminController
'payment' => array(
'title' => $this->l('Payment')
),
'currency' => array(
'title' => $this->l('Order Currency'),
'hint' => $this->l('This is the currency in which customer created the order.'),
'havingFilter' => true,
),
'order_source' => array(
'title' => $this->l('Order Source'),
'type' => 'select',
@@ -374,36 +369,17 @@ class AdminOrdersControllerCore extends AdminController
$this->errors[] = $this->l('You have to select a shop before creating new orders.');
}
$id_cart = (int)Tools::getValue('id_cart');
$cart = new Cart((int)$id_cart);
if ($id_cart && !Validate::isLoadedObject($cart)) {
$this->errors[] = $this->l('This cart does not exists');
}
if ($id_cart && Validate::isLoadedObject($cart) && !$cart->id_customer) {
$this->errors[] = $this->l('The cart must have a customer');
}
if (count($this->errors) && !Tools::getValue('submitAddorder')) {
return false;
}
// check page is whether ad new order page or order detail page
if ((Tools::isSubmit('addorder') && ($id_cart = Tools::getValue('cart_id')))
|| (Tools::isSubmit('submitAddOrder') && ($id_cart = Tools::getValue('id_cart')))
) {
// check page is new order creation page and cart id is set
if (Tools::isSubmit('addorder') && ($id_cart = Tools::getValue('cart_id'))) {
// set smarty variables if new order creation process has errors
$cart = new Cart($id_cart);
$cart_order_exists = $cart->orderExists();
if (!$cart_order_exists) {
$this->context->cart = $cart;
if ($this->context->employee->isSuperAdmin()) {
$backOrderConfigKey = 'PS_BACKDATE_ORDER_SUPERADMIN';
} else {
$backOrderConfigKey = 'PS_BACKDATE_ORDER_EMPLOYEES';
}
if (!Configuration::get($backOrderConfigKey)) {
$objHotelCartBookingData = new HotelCartBookingData();
$objHotelCartBookingData->removeBackdateRoomsFromCart($this->context->cart->id);
}
// validate cart for removing invalid data from cart before new order creation
$this->errors = array_merge($this->errors, HotelCartBookingData::validateCartBookings());
$this->context->currency = new Currency((int)$cart->id_currency);
$cart_detail_data = array();
$cart_detail_data_obj = new HotelCartBookingData();
@@ -420,61 +396,68 @@ class AdminOrdersControllerCore extends AdminController
$this->context->smarty->assign('cart_normal_data', $normalCartProduct);
}
if (empty($cart_detail_data) && empty($normalCartProduct)) {
// if no rooms added in the cart and user visits add order page then redirect to BOOK NOW page
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHotelRoomsBooking'));
}
$payment_modules = array();
foreach (PaymentModule::getInstalledPaymentModules() as $p_module) {
$payment_modules[] = Module::getInstanceById((int)$p_module['id_module']);
}
$paymentTypes = array();
foreach ($this->getPaymentsTypes() as $paymentType) {
if ($paymentType['value'] != OrderPayment::PAYMENT_TYPE_REMOTE_PAYMENT) {
$paymentTypes[] = $paymentType;
}
}
$occupancyRequiredForBooking = false;
if (Configuration::get('PS_BACKOFFICE_ROOM_BOOKING_TYPE') == HotelBookingDetail::PS_ROOM_UNIT_SELECTION_TYPE_OCCUPANCY) {
$occupancyRequiredForBooking = true;
}
$objHotelAdvancedPayment = new HotelAdvancedPayment();
$this->context->smarty->assign(array(
'order_total' => $cart->getOrderTotal(true),
'is_advance_payment_active' => $objHotelAdvancedPayment->isAdvancePaymentAvailableForCurrentCart(),
'advance_payment_amount_without_tax' => $cart->getOrderTotal(false, Cart::ADVANCE_PAYMENT),
'advance_payment_amount_with_tax' => $cart->getOrderTotal(true, Cart::ADVANCE_PAYMENT),
'cart' => $cart,
'currencies' => Currency::getCurrenciesByIdShop(Context::getContext()->shop->id),
'langs' => Language::getLanguages(true, Context::getContext()->shop->id),
'payment_modules' => $payment_modules,
'payment_types' => $paymentTypes,
'currency' => new Currency((int)$cart->id_currency),
'max_child_in_room' => Configuration::get('WK_GLOBAL_MAX_CHILD_IN_ROOM'),
'max_child_age' => Configuration::get('WK_GLOBAL_CHILD_MAX_AGE'),
'occupancy_required_for_booking' => $occupancyRequiredForBooking,
));
if (empty($cart_detail_data) && empty($normalCartProduct)) {
// if no rooms added in the cart and user visits add order page then redirect to BOOK NOW page
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHotelRoomsBooking'));
}
} else {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHotelRoomsBooking'));
}
$this->context->smarty->assign('is_order_created', $cart_order_exists);
} else {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminHotelRoomsBooking'));
}
//end
parent::renderForm();
unset($this->toolbar_btn['save']);
$this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
$payment_modules = array();
foreach (PaymentModule::getInstalledPaymentModules() as $p_module) {
$payment_modules[] = Module::getInstanceById((int)$p_module['id_module']);
}
$paymentTypes = array();
foreach ($this->getPaymentsTypes() as $paymentType) {
if ($paymentType['value'] != OrderPayment::PAYMENT_TYPE_REMOTE_PAYMENT) {
$paymentTypes[] = $paymentType;
}
}
$occupancyRequiredForBooking = false;
if (Configuration::get('PS_BACKOFFICE_ROOM_BOOKING_TYPE') == HotelBookingDetail::PS_ROOM_UNIT_SELECTION_TYPE_OCCUPANCY) {
$occupancyRequiredForBooking = true;
}
$this->context->smarty->assign(array(
'recyclable_pack' => (int)Configuration::get('PS_RECYCLABLE_PACK'),
'gift_wrapping' => (int)Configuration::get('PS_GIFT_WRAPPING'),
'cart' => $cart,
'currencies' => Currency::getCurrenciesByIdShop(Context::getContext()->shop->id),
'langs' => Language::getLanguages(true, Context::getContext()->shop->id),
'payment_modules' => $payment_modules,
'payment_types' => $paymentTypes,
'show_toolbar' => $this->show_toolbar,
'toolbar_btn' => $this->toolbar_btn,
'toolbar_scroll' => $this->toolbar_scroll,
'PS_CATALOG_MODE' => Configuration::get('PS_CATALOG_MODE'),
'title' => array($this->l('Orders'), $this->l('Create order')),
'currency' => new Currency((int)$cart->id_currency),
'max_child_in_room' => Configuration::get('WK_GLOBAL_MAX_CHILD_IN_ROOM'),
'max_child_age' => Configuration::get('WK_GLOBAL_CHILD_MAX_AGE'),
'occupancy_required_for_booking' => $occupancyRequiredForBooking,
));
$this->content .= $this->createTemplate('form.tpl')->fetch();
}
@@ -927,6 +910,10 @@ class AdminOrdersControllerCore extends AdminController
$smartyVars['roomTypeServiceProducts'] = $roomTypeServiceProducts;
}
$objOrderReturn = new OrderReturn();
$refundReqBookings = $objOrderReturn->getOrderRefundRequestedBookings($objOrder->id, 0, 1);
$smartyVars['refundReqBookings'] = $refundReqBookings;
$this->context->smarty->assign($smartyVars);
$modal = array(
@@ -1057,28 +1044,13 @@ class AdminOrdersControllerCore extends AdminController
if (!Validate::isLoadedObject($order)) {
$this->errors[] = sprintf(Tools::displayError('Order #%d cannot be loaded'), $id_order);
} else {
$current_order_state = $order->getCurrentOrderState();
if ($current_order_state->id == $order_state->id) {
$this->errors[] = $this->displayWarning(sprintf('Order #%d has already been assigned this status.', $id_order));
} else {
$history = new OrderHistory();
$history->id_order = $order->id;
$history->id_employee = (int)$this->context->employee->id;
$use_existings_payment = !$order->hasInvoice();
$history->changeIdOrderState((int)$order_state->id, $order, $use_existings_payment);
$carrier = new Carrier($order->id_carrier, $order->id_lang);
$templateVars = array();
if ($history->addWithemail(true, $templateVars)) {
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
foreach ($order->getProducts() as $product) {
if (StockAvailable::dependsOnStock($product['product_id'])) {
StockAvailable::synchronize($product['product_id'], (int)$product['id_shop']);
}
}
}
$result = $order->changeOrderStatus();
if (!$result['status']) {
if (isset($result['has_mail_error']) && $result['has_mail_error']) {
$this->errors[] = sprintf(
Tools::displayError('Unable to send email to the customer while changing order status for order #%d.'),
$id_order
);
} else {
$this->errors[] = sprintf(Tools::displayError('Cannot change status for order #%d.'), $id_order);
}
@@ -1087,6 +1059,7 @@ class AdminOrdersControllerCore extends AdminController
}
}
}
if (!count($this->errors)) {
Tools::redirectAdmin(self::$currentIndex.'&conf=4&token='.$this->token);
}
@@ -1292,115 +1265,11 @@ class AdminOrdersControllerCore extends AdminController
/* Change order status, add a new entry in order history and send an e-mail to the customer if needed */
elseif (Tools::isSubmit('submitState') && isset($order)) {
if ($this->tabAccess['edit'] === '1') {
$order_state = new OrderState(Tools::getValue('id_order_state'));
if (!Validate::isLoadedObject($order_state)) {
$this->errors[] = Tools::displayError('The new order status is invalid.');
$result = $order->changeOrderStatus();
if ($result['status']) {
Tools::redirectAdmin(self::$currentIndex.'&id_order='.(int)$order->id.'&conf=5&vieworder&token='.$this->token);
} else {
$current_order_state = $order->getCurrentOrderState();
if ($current_order_state->id == Configuration::get('PS_OS_REFUND')) {
$this->errors[] = Tools::displayError('Order status can not be changed once order status is set to Refunded.');
} elseif ($current_order_state->id == Configuration::get('PS_OS_CANCELED')) {
$this->errors[] = Tools::displayError('Order status can not be changed once order status is set to Cancelled.');
} elseif (in_array($order_state->id, array (Configuration::get('PS_OS_OVERBOOKING_PAID'), Configuration::get('PS_OS_OVERBOOKING_UNPAID'), Configuration::get('PS_OS_OVERBOOKING_PARTIAL_PAID')))) {
$objHotelBooking = new HotelBookingDetail();
if (!$objHotelBooking->getOverbookedRooms($order->id)) {
$this->errors[] = Tools::displayError('Order status can not be changed to any overbooking status as there are no overbooked rooms in the order.');
}
} elseif ($order_state->id == Configuration::get('PS_OS_REFUND')
&& !$order->hasCompletelyRefunded(Order::ORDER_COMPLETE_REFUND_FLAG)
) {
$this->errors[] = Tools::displayError('Order status can not be set to Refunded until all bookings in the order are completely refunded.');
} elseif ($order_state->id == Configuration::get('PS_OS_CANCELED')
&& !$order->hasCompletelyRefunded(Order::ORDER_COMPLETE_CANCELLATION_FLAG)
) {
$this->errors[] = Tools::displayError('Order status can not be set to Cancelled until all bookings in the order are cancelled.');
} elseif ($current_order_state->id == Configuration::get('PS_OS_ERROR') && !($order_state->id == Configuration::get('PS_OS_ERROR'))) {
// All rooms must be available before changing status from Payment Error to Other status in which rooms are getting blocked again
$objHotelBooking = new HotelBookingDetail();
if ($orderBookings = $objHotelBooking->getOrderCurrentDataByOrderId($order->id)) {
foreach ($orderBookings as $orderBooking) {
// If booking is refunded then no need to check inventory
if ((OrderReturn::getOrdersReturnDetail($order->id, 0, $orderBooking['id']) && $orderBooking['is_refunded'])
|| ($orderBooking['is_cancelled'] && $orderBooking['is_refunded'])
) {
continue;
} else {
// if inventory is available for that booking
$bookingParams = array(
'date_from' => $orderBooking['date_from'],
'date_to' => $orderBooking['date_to'],
'hotel_id' => $orderBooking['id_hotel'],
'id_room_type' => $orderBooking['id_product'],
'only_search_data' => 1
);
$objHotelBookingDetail = new HotelBookingDetail($orderBooking['id']);
if ($searchRoomsInfo = $objHotelBooking->getBookingData($bookingParams)) {
if (isset($searchRoomsInfo['rm_data'][$orderBooking['id_product']]['data']['available'])
&& $searchRoomsInfo['rm_data'][$orderBooking['id_product']]['data']['available']
) {
$availableRoomsInfo = $searchRoomsInfo['rm_data'][$orderBooking['id_product']]['data']['available'];
if ($roomIdsAvailable = array_column($availableRoomsInfo, 'id_room')) {
// Check If room is still there in the available rooms list
if (!in_array($orderBooking['id_room'], $roomIdsAvailable)) {
$this->errors[] = Tools::displayError('You can not change the order status as some rooms are not available now in this order. You can reallocate/swap rooms with other rooms to make rooms available and then change the order status.');
break;
} else {
$objHotelBookingDetail->is_refunded = 0;
$objHotelBookingDetail->save();
}
} else {
$this->errors[] = Tools::displayError('You can not change the order status as some rooms are not available now in this order. You can reallocate/swap rooms with other rooms to make rooms available and then change the order status.');
break;
}
}
} else {
$this->errors[] = Tools::displayError('You can not change the order status as some rooms are not available now in this order. You can reallocate/swap rooms with other rooms to make rooms available and then change the order status.');
break;
}
}
}
}
} elseif ($current_order_state->id == $order_state->id) {
$this->errors[] = Tools::displayError('The order has already been assigned this status.');
}
// If no errors then we change the order status
if (!count($this->errors)) {
// Create new OrderHistory
$history = new OrderHistory();
$history->id_order = $order->id;
$history->id_employee = (int)$this->context->employee->id;
$use_existings_payment = false;
if (!$order->hasInvoice()) {
$use_existings_payment = true;
}
$history->changeIdOrderState((int)$order_state->id, $order, $use_existings_payment);
$carrier = new Carrier($order->id_carrier, $order->id_lang);
$templateVars = array();
// Save all changes
if ($history->addWithemail(true, $templateVars)) {
// synchronizes quantities if needed..
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
foreach ($order->getProducts() as $product) {
if (StockAvailable::dependsOnStock($product['product_id'])) {
StockAvailable::synchronize($product['product_id'], (int)$product['id_shop']);
}
}
}
Tools::redirectAdmin(self::$currentIndex.'&id_order='.(int)$order->id.'&conf=5&vieworder&token='.$this->token);
}
$this->errors[] = Tools::displayError('An error occurred while changing order status, or we were unable to send an email to the customer.');
}
$this->errors = array_merge($this->errors, $result['errors']);
}
} else {
$this->errors[] = Tools::displayError('You do not have permission to edit this.');
@@ -1624,9 +1493,21 @@ class AdminOrdersControllerCore extends AdminController
} elseif (!Validate::isUnsignedInt($payment_type)) {
$this->errors[] = Tools::displayError('Payment source is invalid');
// Amount cannot be less than paid amount by guest in negative price
} elseif ($amount < 0 && ($order->total_paid_real + $amount) < 0) {
$this->errors[] = sprintf(Tools::displayError('Amount cannot be less than -%s'), Tools::displayPrice($order->total_paid_real, new Currency($order->id_currency)));
} else {
} elseif ($amount < 0) {
if ($currency->id == $order->id_currency) {
if (($order->total_paid_real + $amount) < 0) {
$this->errors[] = sprintf(Tools::displayError('Amount cannot be less than -%s'), Tools::displayPrice($order->total_paid_real, new Currency($order->id_currency)));
}
} else {
$convertedAmount = Tools::ps_round(Tools::convertPriceFull($amount, $currency, new Currency($order->id_currency)), 6);
$convertedPaidAmount = Tools::ps_round(Tools::convertPriceFull($order->total_paid_real, new Currency($order->id_currency), $currency), 6);
if (($order->total_paid_real + $convertedAmount) < 0) {
$this->errors[] = sprintf(Tools::displayError('Amount cannot be less than -%s (-%s)'), Tools::displayPrice($convertedPaidAmount, $currency), Tools::displayPrice($order->total_paid_real, new Currency($order->id_currency)));
}
}
}
if (!count($this->errors)) {
if (!$order->addOrderPayment(
$amount,
Tools::getValue('payment_method'),
@@ -1673,6 +1554,9 @@ class AdminOrdersControllerCore extends AdminController
if ($this->tabAccess['edit'] === '1') {
$objCart = new Cart($id_cart);
if (Validate::isLoadedObject($objCart)) {
$this->context->cart = $objCart;
$this->errors = HotelCartBookingData::validateCartBookings();
$orderTotal = $objCart->getOrderTotal(true, Cart::BOTH);
if ($objCart->is_advance_payment) {
$advancePaymentAmount = $objCart->getOrderTotal(true, Cart::ADVANCE_PAYMENT);
@@ -1690,7 +1574,7 @@ class AdminOrdersControllerCore extends AdminController
if ($paymentAmount == '') {
$this->errors[] = Tools::displayError('Please enter valid Payment amount of the order.');
} elseif ($paymentAmount && !Validate::isPrice($paymentAmount)) {
$this->errors[] = Tools::displayError('Payment amount is invalid. Please enter correct amount.');
$this->errors[] = Tools::displayError('Payment amount is invalid. Please enter valid amount.');
} else {
$paymentAmount = (float) $paymentAmount;
}
@@ -1717,23 +1601,6 @@ class AdminOrdersControllerCore extends AdminController
}
}
// Validate if booking dates are allowed
if ($this->context->employee->isSuperAdmin()) {
$backOrderConfigKey = 'PS_BACKDATE_ORDER_SUPERADMIN';
} else {
$backOrderConfigKey = 'PS_BACKDATE_ORDER_EMPLOYEES';
}
if (!Configuration::get($backOrderConfigKey)) {
$objCartBookingData = new HotelCartBookingData();
if ($cartBookingData = $objCartBookingData->getCartCurrentDataByCartId($cart->id)) {
foreach ($cartBookingData as $cartRoom) {
if (strtotime($cartRoom['date_from']) < strtotime(date('Y-m-d'))) {
$this->errors[] = Tools::displayError('You cannot book rooms before today date.');
}
}
}
}
if (!count($this->errors)) {
$this->context->currency = new Currency((int) $objCart->id_currency);
$this->context->customer = new Customer((int) $objCart->id_customer);
@@ -1811,10 +1678,6 @@ class AdminOrdersControllerCore extends AdminController
if ($objPaymentModule->currentOrder) {
Tools::redirectAdmin(self::$currentIndex.'&id_order='.$objPaymentModule->currentOrder.'&vieworder'.'&token='.$this->token.'&conf=3');
}
} else {
// if errors render add order form
$_GET['addorder'] = '1';
$_GET['cart_id'] = $id_cart;
}
} else {
$this->errors[] = Tools::displayError('Cart can not be loaded.');
@@ -2045,6 +1908,41 @@ class AdminOrdersControllerCore extends AdminController
}
}
// update Order refund prices (order_return and order_return_detail)
if ($orderReturns = OrderReturn::getOrdersReturn($order->id_customer, $order->id)) {
$fields = array(
'refunded_amount',
);
foreach ($orderReturns as $orderReturn) {
$objOrderReturn = new OrderReturn($orderReturn['id_order_return']);
foreach ($fields as $field) {
$objOrderReturn->{$field} = Tools::convertPriceFull(
$objOrderReturn->{$field},
$old_currency,
$currency
);
}
$objOrderReturn->update();
}
}
if ($orderReturnDetails = OrderReturn::getOrdersReturnDetail($order->id)) {
$fields = array(
'refunded_amount',
);
foreach ($orderReturnDetails as $orderReturnDetail) {
$objOrderReturnDetail = new OrderReturnDetail($orderReturnDetail['id_order_return_detail']);
foreach ($fields as $field) {
$objOrderReturnDetail->{$field} = Tools::convertPriceFull(
$objOrderReturnDetail->{$field},
$old_currency,
$currency
);
}
$objOrderReturnDetail->update();
}
}
// If everything is updated, then redirect to view order with success message
Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
} else {
@@ -2125,7 +2023,7 @@ class AdminOrdersControllerCore extends AdminController
switch (Tools::getValue('discount_type')) {
// Percent type
case 1:
if ($discount_value < 100) {
if ($discount_value <= 100) {
if (isset($order_invoice)) {
$cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($order_invoice->total_paid_tax_incl * $discount_value / 100, 2);
$cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * $discount_value / 100, 2);
@@ -2217,60 +2115,62 @@ class AdminOrdersControllerCore extends AdminController
$this->errors[] = Tools::displayError('The discount type is invalid.');
}
$res = true;
foreach ($cart_rules as &$cart_rule) {
$cartRuleObj = new CartRule();
$cartRuleObj->date_from = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($order->date_add)));
$cartRuleObj->date_to = date('Y-m-d H:i:s', strtotime('+1 hour'));
$cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('discount_name');
$cartRuleObj->quantity = 0;
$cartRuleObj->quantity_per_user = 1;
if (Tools::getValue('discount_type') == 1) {
$cartRuleObj->reduction_percent = $discount_value;
} elseif (Tools::getValue('discount_type') == 2) {
$cartRuleObj->reduction_amount = $cart_rule['value_tax_excl'];
} elseif (Tools::getValue('discount_type') == 3) {
$cartRuleObj->free_shipping = 1;
if (!count($this->errors)) {
$res = true;
foreach ($cart_rules as &$cart_rule) {
$cartRuleObj = new CartRule();
$cartRuleObj->date_from = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($order->date_add)));
$cartRuleObj->date_to = date('Y-m-d H:i:s', strtotime('+1 hour'));
$cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('discount_name');
$cartRuleObj->quantity = 0;
$cartRuleObj->quantity_per_user = 1;
if (Tools::getValue('discount_type') == 1) {
$cartRuleObj->reduction_percent = $discount_value;
} elseif (Tools::getValue('discount_type') == 2) {
$cartRuleObj->reduction_amount = $cart_rule['value_tax_excl'];
} elseif (Tools::getValue('discount_type') == 3) {
$cartRuleObj->free_shipping = 1;
}
$cartRuleObj->active = 0;
if ($res = $cartRuleObj->add()) {
$cart_rule['id'] = $cartRuleObj->id;
$cart_rule['free_shipping'] = $cartRuleObj->free_shipping;
} else {
break;
}
}
$cartRuleObj->active = 0;
if ($res = $cartRuleObj->add()) {
$cart_rule['id'] = $cartRuleObj->id;
$cart_rule['free_shipping'] = $cartRuleObj->free_shipping;
if ($res) {
foreach ($cart_rules as $id_order_invoice => $cart_rule) {
// Create OrderCartRule
$order_cart_rule = new OrderCartRule();
$order_cart_rule->id_order = $order->id;
$order_cart_rule->id_cart_rule = $cart_rule['id'];
$order_cart_rule->id_order_invoice = $id_order_invoice;
$order_cart_rule->name = Tools::getValue('discount_name');
$order_cart_rule->value = $cart_rule['value_tax_incl'];
$order_cart_rule->value_tax_excl = $cart_rule['value_tax_excl'];
$order_cart_rule->free_shipping = $cart_rule['free_shipping'];
$res &= $order_cart_rule->add();
$order->total_discounts = Tools::ps_round($order->total_discounts + $order_cart_rule->value, 6);
$order->total_discounts_tax_incl = Tools::ps_round($order->total_discounts_tax_incl + $order_cart_rule->value, 6);
$order->total_discounts_tax_excl = Tools::ps_round($order->total_discounts_tax_excl + $order_cart_rule->value_tax_excl, 6);
$order->total_paid = Tools::ps_round($order->total_paid - $order_cart_rule->value, 6);
$order->total_paid_tax_incl = Tools::ps_round($order->total_paid_tax_incl - $order_cart_rule->value, 6);
$order->total_paid_tax_excl = Tools::ps_round($order->total_paid_tax_excl - $order_cart_rule->value_tax_excl, 6);
}
// Update Order
$res &= $order->update();
}
if ($res) {
Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
} else {
break;
$this->errors[] = Tools::displayError('An error occurred during the OrderCartRule creation');
}
}
if ($res) {
foreach ($cart_rules as $id_order_invoice => $cart_rule) {
// Create OrderCartRule
$order_cart_rule = new OrderCartRule();
$order_cart_rule->id_order = $order->id;
$order_cart_rule->id_cart_rule = $cart_rule['id'];
$order_cart_rule->id_order_invoice = $id_order_invoice;
$order_cart_rule->name = Tools::getValue('discount_name');
$order_cart_rule->value = $cart_rule['value_tax_incl'];
$order_cart_rule->value_tax_excl = $cart_rule['value_tax_excl'];
$order_cart_rule->free_shipping = $cart_rule['free_shipping'];
$res &= $order_cart_rule->add();
$order->total_discounts += $order_cart_rule->value;
$order->total_discounts_tax_incl += $order_cart_rule->value;
$order->total_discounts_tax_excl += $order_cart_rule->value_tax_excl;
$order->total_paid -= $order_cart_rule->value;
$order->total_paid_tax_incl -= $order_cart_rule->value;
$order->total_paid_tax_excl -= $order_cart_rule->value_tax_excl;
}
// Update Order
$res &= $order->update();
}
if ($res) {
Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
} else {
$this->errors[] = Tools::displayError('An error occurred during the OrderCartRule creation');
}
}
} else {
$this->errors[] = Tools::displayError('You do not have permission to edit this.');
@@ -2429,7 +2329,7 @@ class AdminOrdersControllerCore extends AdminController
$dateFrom = date(Context::getContext()->language->date_format_lite, strtotime('-2 day'));
$dateTo = date(Context::getContext()->language->date_format_lite, strtotime('-1 day'));
$helper->subtitle = sprintf($this->l('From %s to %s', null, null, false), $dateFrom, $dateTo);
$helper->href = $this->context->link->getAdminLink('AdminCarts').'&action=filterOnlyAbandonedCarts';
$helper->href = $this->context->link->getAdminLink('AdminCarts').'&action=filterOnlyAbandonedCarts&date_from='.$dateFrom.'&date_to='.$dateTo;
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=abandoned_cart';
$helper->tooltip = $this->l('Total number of abandoned carts in given period of time.', null, null, false);
$kpis[] = $helper;
@@ -4567,103 +4467,6 @@ class AdminOrdersControllerCore extends AdminController
$children = $occupancy['children'];
$child_ages = $occupancy['child_ages'];
/*By webkul to validate fields before deleting the cart and order data form the tables*/
if ($id_hotel == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Hotel Id is mising.'),
)));
} elseif ($id_room == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Room Id is missing.'),
)));
} elseif ($new_date_from == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please Enter Check In Date.'),
)));
} elseif (!Validate::isDateFormat($new_date_from)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please Enter a Valid Check In Date.'),
)));
} elseif ($new_date_to == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please Enter Check Out Date.'),
)));
} elseif (!Validate::isDateFormat($new_date_to)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please Enter a valid Check out Date.'),
)));
} elseif ($new_date_from < $curr_date) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Check In date should not be after current date.'),
)));
} elseif ($new_date_to <= $new_date_from) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Check out Date Should be after Check In date.'),
)));
} elseif ($room_unit_price == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter unit price.'),
)));
} elseif (!Validate::isPrice($room_unit_price)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter a valid unit price.'),
)));
} elseif (!Validate::isUnsignedInt($product_quantity)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid quantity.'),
)));
}
// validations if order is with occupancy
if (!isset($adults) || !$adults || !Validate::isUnsignedInt($adults)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid number of adults.'),
)));
} elseif (!Validate::isUnsignedInt($children)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid number of children.'),
)));
}
if ($children > 0) {
if (!isset($child_ages) || ($children != count($child_ages))) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please provide all children age.'),
)));
} else {
foreach($child_ages as $childAge) {
if (!Validate::isUnsignedInt($childAge)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid children age.'),
)));
}
}
}
}
$rooms_booked = $obj_booking_detail->getRoomBookinInformationForDateRangeByOrder($id_room, $old_date_from, $old_date_to, $new_date_from, $new_date_to);
if ($rooms_booked) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('This Room Unavailable For Selected Duration.'),
)));
}
// By webkul to calculate rates of the product from hotelreservationsystem tables with feature prices....
// add feature price for updated price
@@ -5609,8 +5412,48 @@ class AdminOrdersControllerCore extends AdminController
{
$this->doEditValidation($order_detail, $order, $order_invoice);
$id_hotel_booking = Tools::getValue('id_booking_detail');
$product_price_tax_incl = str_replace(',', '.', Tools::getValue('product_price_tax_incl'));
$product_price_tax_excl = str_replace(',', '.', Tools::getValue('product_price_tax_excl'));
$product_informations = $_POST['edit_product'];
$new_date_from = trim(date('Y-m-d', strtotime($product_informations['date_from'])));
$new_date_to = trim(date('Y-m-d', strtotime($product_informations['date_to'])));
$old_date_from = date('Y-m-d', strtotime(trim(Tools::getValue('date_from'))));
$old_date_to = date('Y-m-d', strtotime(trim(Tools::getValue('date_to'))));
$id_hotel = trim(Tools::getValue('id_hotel'));
$id_room = trim(Tools::getValue('id_room'));
$id_product = trim(Tools::getValue('id_product'));
$room_unit_price = trim(Tools::getValue('room_unit_price'));
$obj_booking_detail = new HotelBookingDetail();
$product_quantity = (int) $obj_booking_detail->getNumberOfDays($new_date_from, $new_date_to);
$old_product_quantity = (int) $obj_booking_detail->getNumberOfDays($old_date_from, $old_date_to);
$qty_diff = $product_quantity - $old_product_quantity;
$occupancy = array_shift(Tools::getValue('occupancy'));
$adults = $occupancy['adults'];
$children = $occupancy['children'];
$child_ages = $occupancy['child_ages'];
// If order is refunded, the validate changes which are not allowed
$objOrderReturn = new OrderReturn();
$refundReqBookings = $objOrderReturn->getOrderRefundRequestedBookings($order->id, 0, 1);
if ($refundReqBookings && (in_array($id_hotel_booking, $refundReqBookings))) {
$objBookingDetail = new HotelBookingDetail($id_hotel_booking);
// If order is cancelled, we can't edit order
if ($objBookingDetail->is_refunded && $objBookingDetail->is_cancelled) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Booking cannot be edited if booking is cancelled.'),
)));
// If order is refunded, we can't edit dates
} elseif ($objBookingDetail->is_refunded
&& (strtotime($old_date_from) != strtotime($new_date_from) || strtotime($old_date_to) != strtotime($new_date_to))
) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Check-In/Check-Out dates cannot be changed if booking is refunded.'),
)));
}
}
if (!Validate::isPrice($product_price_tax_incl) || !Validate::isPrice($product_price_tax_excl)) {
die(json_encode(array(
@@ -5619,46 +5462,89 @@ class AdminOrdersControllerCore extends AdminController
)));
}
$product_informations = Tools::getValue('edit_product');
$old_date_from = date('Y-m-d', strtotime(trim(Tools::getValue('date_from'))));
$old_date_to = date('Y-m-d', strtotime(trim(Tools::getValue('date_to'))));
$new_date_from = trim(date('Y-m-d', strtotime($product_informations['date_from'])));
$new_date_to = trim(date('Y-m-d', strtotime($product_informations['date_to'])));
$obj_booking_detail = new HotelBookingDetail();
$product_quantity = (int) $obj_booking_detail->getNumberOfDays($new_date_from, $new_date_to);
$id_room = Tools::getValue('id_room');
if (trim(Tools::getValue('id_hotel')) == '') {
if ($id_hotel == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Hotel Id is mising.'),
)));
} elseif (trim($id_room) == '') {
} elseif ($id_room == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Room Id is missing.'),
)));
} elseif (trim(date('Y-m-d', strtotime($product_informations['date_from']))) == '') {
} elseif ($new_date_from == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter check-in date.'),
'error' => Tools::displayError('Please enter Check-In date.'),
)));
} elseif (!Validate::isDateFormat($new_date_from)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter a valid check-in date.'),
'error' => Tools::displayError('Please enter a valid Check-In date.'),
)));
} elseif ($new_date_to == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter check-out date.'),
'error' => Tools::displayError('Please enter Check-Out date.'),
)));
} elseif (!Validate::isDateFormat($new_date_to)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter a valid check-out date.'),
'error' => Tools::displayError('Please enter a valid Check-out date.'),
)));
} elseif ($new_date_to <= $new_date_from) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Check-out date should be after Check-In date.'),
)));
} elseif ($room_unit_price == '') {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter unit price.'),
)));
} elseif (!Validate::isPrice($room_unit_price)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please enter a valid unit price.'),
)));
} elseif (!Validate::isUnsignedInt($product_quantity)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid quantity.'),
)));
}
// validations if order is with occupancy
if (!isset($adults) || !$adults || !Validate::isUnsignedInt($adults)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid number of adults.'),
)));
} elseif (!Validate::isUnsignedInt($children)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid number of children.'),
)));
}
if ($children > 0) {
if (!isset($child_ages) || ($children != count($child_ages))) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Please provide all children age.'),
)));
} else {
foreach($child_ages as $childAge) {
if (!Validate::isUnsignedInt($childAge)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid children age.'),
)));
}
}
}
}
if ($this->context->employee->isSuperAdmin()) {
$backOrderConfigKey = 'PS_BACKDATE_ORDER_SUPERADMIN';
} else {
@@ -5669,24 +5555,18 @@ class AdminOrdersControllerCore extends AdminController
if ($new_date_from < $compareDate) {
die(json_encode(array(
'result' => false,
'error' => sprintf(Tools::displayError('Check-in date should not be date before %s.'),$compareDate)
'error' => sprintf(Tools::displayError('Check-In date should not be date before %s.'),$compareDate)
)));
}
}
if ($new_date_to <= $new_date_from) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Check-out date should be after check-in date.'),
)));
} elseif (!Validate::isUnsignedInt($product_quantity)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('Invalid quantity.'),
)));
}
$rooms_booked = $obj_booking_detail->getRoomBookinInformationForDateRangeByOrder($id_room, $old_date_from, $old_date_to, $new_date_from, $new_date_to);
if ($rooms_booked) {
if ($rooms_booked = $obj_booking_detail->getRoomBookinInformationForDateRangeByOrder(
$id_room,
$old_date_from,
$old_date_to,
$new_date_from,
$new_date_to
)) {
die(json_encode(array(
'result' => false,
'error' => Tools::displayError('This room is unavailable for selected duration.'),