diff --git a/admin/themes/default/template/controllers/normal_products/informations.tpl b/admin/themes/default/template/controllers/normal_products/informations.tpl index 81088a6b0..a9ad62206 100644 --- a/admin/themes/default/template/controllers/normal_products/informations.tpl +++ b/admin/themes/default/template/controllers/normal_products/informations.tpl @@ -412,21 +412,23 @@ -
auto_add_to_cart && $product->isAssociatedToShop()) || !$product->allow_multiple_quantity}style="display:none;"{/if}> - -
- +
auto_add_to_cart && $product->isAssociatedToShop()) || !$product->allow_multiple_quantity}style="display:none;"{/if}> +
+ +
+ +
-
-
-
-
- {l s='By default all products have infinte quantity, Using this setting you can restrict customer to purchase only one product per room.'} +
+
+
+ {l s='By default all products have infinte quantity, Using this setting you can restrict customer to purchase only one product per room.'} +
diff --git a/admin/themes/default/template/helpers/list/list_header.tpl b/admin/themes/default/template/helpers/list/list_header.tpl index b3e7b84ad..7f9266286 100644 --- a/admin/themes/default/template/helpers/list/list_header.tpl +++ b/admin/themes/default/template/helpers/list/list_header.tpl @@ -305,7 +305,7 @@ {elseif $params.type == 'date' || $params.type == 'datetime'}
@@ -387,7 +387,7 @@ {if isset($params.list) && is_array($params.list)} {foreach $params.list AS $option_value => $option_display} - + {/foreach} {/if} diff --git a/classes/Address.php b/classes/Address.php index a9360be5a..b6b69905b 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -539,10 +539,10 @@ class AddressCore extends ObjectModel cl.`name` AS `country_name`, s.`name` AS `state_name` FROM `'._DB_PREFIX_.'address` a LEFT JOIN `'._DB_PREFIX_.'country_lang` cl - ON cl.`id_country` = a.`id_country` + ON cl.`id_country` = a.`id_country` AND cl.`id_lang` ='.(int) $idLang.' LEFT JOIN `'._DB_PREFIX_.'state` s - ON s.`id_country` = cl.`id_country` - WHERE id_customer > 0 AND + ON s.`id_country` = cl.`id_country` AND s.`id_state` = a.`id_state` + WHERE id_customer > 0 AND a.`deleted` = 0 AND (a.`address1` LIKE \'%'.$query.'%\' OR a.`postcode` LIKE \'%'.$query.'%\' OR a.`city` LIKE \'%'.$query.'%\' OR diff --git a/classes/Customer.php b/classes/Customer.php index 662373283..f418f1634 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -141,6 +141,10 @@ class CustomerCore extends ObjectModel const STATUS_DELETED = 2; protected $webserviceParameters = array( + 'objectMethods' => array( + 'add' => 'addWs', + 'update' => 'updateWs' + ), 'fields' => array( 'id_default_group' => array('xlink_resource' => 'groups'), 'id_lang' => array('xlink_resource' => 'languages'), @@ -150,6 +154,7 @@ class CustomerCore extends ObjectModel 'secure_key' => array('setter' => null), 'deleted' => array(), 'passwd' => array('setter' => 'setWsPasswd'), + 'phone' => array() ), 'associations' => array( 'groups' => array('resource' => 'group'), @@ -208,6 +213,32 @@ class CustomerCore extends ObjectModel if ($this->email) { $this->phone = CartCustomerGuestDetail::getCustomerPhone($this->email); } + + if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) { + $this->webserviceParameters['fields']['phone']['required'] = true; + } + } + + public function validateFields($die = true, $error_return = false) + { + if (isset($this->webservice_validation) && $this->webservice_validation) { + if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) { + if (!$this->phone) { + $message = Tools::displayError('Phone is required field.'); + } else if (!Validate::isPhoneNumber(trim($this->phone))) { + $message = Tools::displayError('Invalid phone.'); + } + } + + if (isset($message)) { + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } + + return parent::validateFields($die, $error_return); } public function add($autodate = true, $null_values = true) @@ -262,6 +293,24 @@ class CustomerCore extends ObjectModel return parent::update(true); } + public function addWs($autodate = true, $null_values = false) + { + if ($this->phone) { + CartCustomerGuestDetail::updateCustomerPhoneNumber($this->email, $this->phone); + } + + return $this->add($autodate, $null_values); + } + + public function updateWs($autodate = true, $null_values = false) + { + if ($this->phone) { + CartCustomerGuestDetail::updateCustomerPhoneNumber($this->email, $this->phone); + } + + return $this->update($autodate, $null_values); + } + public function delete() { if (!count(Order::getCustomerOrders((int)$this->id))) { diff --git a/classes/pdf/HTMLTemplateOrderSlip.php b/classes/pdf/HTMLTemplateOrderSlip.php index 479bba46a..97d0900ed 100644 --- a/classes/pdf/HTMLTemplateOrderSlip.php +++ b/classes/pdf/HTMLTemplateOrderSlip.php @@ -154,7 +154,8 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice 'invoice_address' => $formatted_invoice_address, 'addresses' => array('invoice' => $invoice_address, 'delivery' => $delivery_address), 'tax_excluded_display' => $tax_excluded_display, - 'total_cart_rule' => $total_cart_rule + 'total_cart_rule' => $total_cart_rule, + 'customer' => $customer )); $tpls = array( @@ -256,7 +257,7 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice SELECT od.`tax_computation_method` FROM `' . _DB_PREFIX_ . 'order_detail_tax` odt LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` od ON (od.`id_order_detail` = odt.`id_order_detail`) - WHERE od.`id_order` = ' . (int) $this->order->id_order . ' + WHERE od.`id_order` = ' . (int) $this->order->id . ' AND od.`tax_computation_method` = ' . (int) TaxCalculator::ONE_AFTER_ANOTHER_METHOD) || Configuration::get('PS_INVOICE_TAXES_BREAKDOWN'); } @@ -271,8 +272,8 @@ class HTMLTemplateOrderSlipCore extends HTMLTemplateInvoice $breakdown = array(); $order_detail = array_filter($this->order->products, function($v) { - return ($v['is_booking_product'] - || ($v['product_auto_add'] + return ((isset($v['is_booking_product']) && $v['is_booking_product']) + || ((isset($v['product_auto_add']) && $v['product_auto_add']) && $v['product_service_type'] == Product::SERVICE_PRODUCT_WITH_ROOMTYPE && $v['product_price_addition_type'] == ProductCore::PRICE_ADDITION_TYPE_WITH_ROOM) ); diff --git a/classes/webservice/WebserviceOrder.php b/classes/webservice/WebserviceOrder.php index 860fcc0c0..00b0a0b25 100644 --- a/classes/webservice/WebserviceOrder.php +++ b/classes/webservice/WebserviceOrder.php @@ -27,5 +27,6 @@ class WebserviceOrderCore extends PaymentModule { $this->displayName = $this->l('Order from API'); $this->validateOrderAmount = false; + $this->payment_type = OrderPayment::PAYMENT_TYPE_ONLINE; } } diff --git a/classes/webservice/WebserviceOutputBuilder.php b/classes/webservice/WebserviceOutputBuilder.php index cdba833eb..12b29b698 100644 --- a/classes/webservice/WebserviceOutputBuilder.php +++ b/classes/webservice/WebserviceOutputBuilder.php @@ -626,7 +626,21 @@ class WebserviceOutputBuilderCore } $output_details = ''; foreach ($objects_assoc as $object_assoc) { - if ($depth == 0 || $class_name === null) { + if (isset($association['only_leaf_nodes']) && $association['only_leaf_nodes']) { + unset($association['only_leaf_nodes']); + unset($association['getter']); + unset($association['setter']); + foreach ($association as $associationKey => $resource) { + $field = array(); + $field['sqlId'] = $associationKey; + $field['value'] = null; + if (!is_null($this->schemaToDisplay)) { + $field['synopsis_details'] = $this->getSynopsisDetails($resource); + } + + $output_details .= $this->setIndent($depth - 1).$this->objectRender->renderField($field); + } + } elseif ($depth == 0 || $class_name === null) { $value = null; if (!empty($object_assoc)) { $value = $object_assoc; @@ -676,7 +690,30 @@ class WebserviceOutputBuilderCore $output .= $this->setIndent($depth - 1).$this->objectRender->renderNodeHeader($resource_name, array(), $more_attr); foreach ($fields_assoc as $field_name => $field) { - if (!is_array($this->fieldsToDisplay) || in_array($field_name, $this->fieldsToDisplay[$assoc_name])) { + if (isset($field['resource']) && isset($field['fields'])) { + $parent_details = array( + 'object_id' => null, + 'entity_name' => $field['resource'], + 'entities_name' => $field_name, + ); + $output_details = ''; + $output_details = $this->renderFlatAssociation( + $object, + $depth, + $field_name, + $field['resource'], + $field['fields'], + $object_assoc, + $parent_details + ); + if ($output_details != '') { + $output .= $this->setIndent($depth).$this->objectRender->renderNodeHeader($field_name, array()); + $output .= $output_details; + $output .= $this->setIndent($depth).$this->objectRender->renderNodeFooter($field_name, array()); + } else { + $output .= $this->setIndent($depth).$this->objectRender->renderNodeHeader($field_name, array()); + } + } elseif (!is_array($this->fieldsToDisplay) || in_array($field_name, $this->fieldsToDisplay[$assoc_name])) { if (isset($field['id']) && !isset($field['sqlId'])) { $field['sqlId'] = 'id'; $field['value'] = isset($object_assoc['id']) ? $object_assoc['id'] : null; diff --git a/classes/webservice/WebserviceOutputJSON.php b/classes/webservice/WebserviceOutputJSON.php index 25a8082c3..1b78077f6 100644 --- a/classes/webservice/WebserviceOutputJSON.php +++ b/classes/webservice/WebserviceOutputJSON.php @@ -104,7 +104,7 @@ class WebserviceOutputJSON implements WebserviceOutputInterface } // Case 1 : fields of the current entity (not an association) if (!$is_association) { - $this->currentEntity[$field['sqlId']] = $field['value']; + $this->currentEntity[$field['sqlId']] = $field['value']; } else { // Case 2 : fields of an associated entity to the current one $this->currentAssociatedEntity[] = array('name' => $field['entities_name'], 'key' => $field['sqlId'], 'value' => $field['value']); } @@ -146,7 +146,7 @@ class WebserviceOutputJSON implements WebserviceOutputInterface } $this->currentEntity = array(); } - if (count($this->currentAssociatedEntity)) { + if (is_array($this->currentAssociatedEntity) && count($this->currentAssociatedEntity)) { $current = array(); foreach ($this->currentAssociatedEntity as $element) { $current[$element['key']] = $element['value']; @@ -159,9 +159,6 @@ class WebserviceOutputJSON implements WebserviceOutputInterface public function overrideContent($content) { - array_walk($this->content, function (&$item) { - $item = array_filter($item); - }); $content = json_encode($this->content, JSON_UNESCAPED_UNICODE); return (false !== $content) ? $content : ''; @@ -187,20 +184,23 @@ class WebserviceOutputJSON implements WebserviceOutputInterface } public function renderAssociationFooter($obj, $params, $assoc_name) { - return; } + public function renderErrorsHeader() { return ''; } + public function renderErrorsFooter() { return ''; } + public function renderAssociationField($field) { return ''; } + public function renderi18nField($field) { return ''; diff --git a/classes/webservice/WebserviceRequest.php b/classes/webservice/WebserviceRequest.php index dd8383074..c1037bae3 100644 --- a/classes/webservice/WebserviceRequest.php +++ b/classes/webservice/WebserviceRequest.php @@ -255,7 +255,8 @@ class WebserviceRequestCore public static function getResources() { $resources = array( - 'addresses' => array('description' => 'The Customer, Manufacturer and Customer addresses','class' => 'Address'), + 'addresses' => array('description' => 'The Customer address','class' => 'Address'), + 'bookings' => array('description' => 'The Customer bookings', 'specific_management' => 'true','forbidden_method' => array('DELETE')), 'carriers' => array('description' => 'The Carriers','class' => 'Carrier'), 'carts' => array('description' => 'Customer\'s carts', 'class' => 'Cart'), 'cart_rules' => array('description' => 'Cart rules management', 'class' => 'CartRule'), diff --git a/classes/webservice/WebserviceSpecificManagementBookings.php b/classes/webservice/WebserviceSpecificManagementBookings.php new file mode 100644 index 000000000..d83fabe91 --- /dev/null +++ b/classes/webservice/WebserviceSpecificManagementBookings.php @@ -0,0 +1,3616 @@ + +* @copyright since 2010 Webkul IN +* @license https://store.webkul.com/license.html +*/ + +class WebserviceSpecificManagementBookingsCore Extends ObjectModel implements WebserviceSpecificManagementInterface +{ + protected $objOutput; + protected $output; + protected $wsObject; + public $outputType = 'xml'; + + public $wsRequestedRoomTypes = array(); + public $wsRequestedRooms = array(); + public $wsIdServices = false; + public $bookingCustomer = false; + public $wsFeaturePrices = array(); + public $wsCartRules = array(); + public $context; + protected $error_msg = ''; + + public const API_BOOKING_STATUS_NEW = 1; + public const API_BOOKING_STATUS_COMPLETED = 2; + public const API_BOOKING_STATUS_CANCELLED = 3; + public const API_BOOKING_STATUS_REFUNDED = 4; + + public const API_BOOKING_PAYMENT_STATUS_COMPLETED = 1; + public const API_BOOKING_PAYMENT_STATUS_PARTIAL = 2; + public const API_BOOKING_PAYMENT_STATUS_AWATING = 3; + + public const API_CART_RULE_VALUE_TYPE_AMOUNT = 1; + public const API_CART_RULE_VALUE_TYPE_PERCENTAGE = 2; + + public const API_SERVICE_PRICE_MODE_PER_BOOKING = 1; + public const API_SERVICE_PRICE_MODE_PER_DAY = 2; + + public static $definition = array( + 'table' => 'htl_booking_detail', + 'primary' => 'id', + 'fields' => array() + ); + + public $webserviceParameters = array( + 'objectsNodeName' => 'bookings', + 'objectNodeName' => 'booking', + 'fields' => array( + 'id_property' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), + 'currency' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'booking_status' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), + 'payment_status' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), + 'source' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'booking_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'), + 'remark' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'id_language' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + ), + 'associations' => array( + 'customer_detail' => array( + 'only_leaf_nodes' => true, + 'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true), + 'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true), + 'email' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true), + 'phone' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true), + 'address' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'city' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'zip' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'state_code' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'country_code' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + ), + 'price_details' => array( + 'only_leaf_nodes' => true, + 'total_paid' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'total_price_with_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice') + ), + 'payment_detail' => array( + 'only_leaf_nodes' => true, + 'payment_type' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'payment_method' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'transaction_id' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + ), + 'cart_rules' => array( + 'setter' => false, + 'resource' => 'cart_rule', + 'fields' => array( + 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'code' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'value' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedInt'), + 'currency' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + ) + ), + 'room_types' => array( + 'setter' => false, + 'resource' => 'room_type', + 'fields' => array( + 'id_room_type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), + 'checkin_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true), + 'checkout_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true), + 'number_of_rooms' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true), + 'rooms' => array( + 'resource' => 'room', + 'fields' => array( + 'id_room' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'adults' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'child' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'child_ages' => array( + 'child_age' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt') + ), + 'unit_price_without_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'id_tax_rules_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'total_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'services' => array( + 'resource' => 'service', + 'fields' => array( + 'id_service' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'price_mode' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'unit_price_without_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'total_price_without_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'id_tax_rules_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'total_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice') + ) + ), + 'facilities' => array( + 'resource' => 'facility', + 'fields' => array( + 'id_facility' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'id_option' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), + 'unit_price_without_tax' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice'), + 'id_tax_rules_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'), + ) + ) + ) + ) + ), + ) + ) + ); + + /** + * @param WebserviceOutputBuilderCore $obj + * @return WebserviceSpecificManagementInterface + */ + public function setObjectOutput(WebserviceOutputBuilderCore $obj) + { + $this->objOutput = $obj; + return $this; + } + + public function setWsObject(WebserviceRequestCore $obj) + { + $this->wsObject = $obj; + return $this; + } + + public function getWsObject() + { + return $this->wsObject; + } + + public function getObjectOutput() + { + return $this->objOutput; + } + + /** + * Get the JSON response. + */ + public function getResponseJson() + { + if ($this->output && is_array($this->output)) { + $this->output = json_encode($this->output); + } else if (isset($this->wsObject->urlFragments['schema']) && $this->wsObject->method == 'GET') { + $this->output = array(); + $this->output['booking'] = $this->renderJsonFields($this->webserviceParameters['fields']); + $this->output['booking']['associations'] = $this->renderJsonAssoiations($this->webserviceParameters['associations']); + $this->output = json_encode($this->output); + } + } + + public function renderJsonFields($fields) + { + $output = array(); + foreach ($fields as $key => $field) { + if (isset($field['resource']) && isset($field['fields'])) { + $output[$key][] = $this->renderJsonFields($field['fields']); + } else { + $output[$key] = $this->renderJsonField($field); + } + } + + return $output; + } + + public function renderJsonField($field) + { + if (isset($field['type'])) { + $field = ''; + } else { + $field = array(); + } + + return $field; + } + + public function renderJsonAssoiations($fields) + { + $output = array(); + foreach ($fields as $key => $field) { + if (isset($field['only_leaf_nodes'])) { + unset($field['only_leaf_nodes']); + $output[$key] = $this->renderJsonFields($field); + } else if (isset($field['fields'])) { + $output[$key][] = $this->renderJsonFields($field['fields']); + } + } + + return $output; + } + + /** + * Get the XML response. + */ + public function getResponseXml() + { + if (is_array($this->output)) { + $parentKeys = array( + 'room_types' => 'room_type', + 'rooms' => 'room', + 'facilities' => 'facility', + 'services' => 'service', + 'cart_rules' => 'cart_rule', + 'remarks' => 'remark' + ); + $this->output = $this->renderXmlOutputUsingArray($this->output, array(), $parentKeys); + } + + $this->output = $this->objOutput->getObjectRender()->overrideContent($this->output); + } + + public function getContent() + { + return $this->output; + } + + /** + * Always called first, and is used to handle the request. + */ + public function manage() + { + $this->context = Context::getContext(); + if (get_class($this->objOutput->getObjectRender()) == 'WebserviceOutputJSON') { + $this->outputType = 'json'; + } + + switch ($this->wsObject->method) { + case 'GET': + case 'HEAD': + if (isset($this->wsObject->urlSegment[1]) && $this->wsObject->urlSegment[1]) { + $this->getBookingDetails($this->wsObject->urlSegment[1]); + $this->renderResponse(); + } else if (isset($this->wsObject->urlFragments['schema'])) { + if ($this->outputType != 'json') { + $object = new WebserviceSpecificManagementBookings(); + $typeOfView = WebserviceOutputBuilder::VIEW_DETAILS; + $this->wsObject->objects = []; + $this->wsObject->objects[] = $object; + $this->wsObject->objects['empty'] = $object; + $this->wsObject->schemaToDisplay = $this->wsObject->urlFragments['schema']; + + $this->output .= $this->objOutput->getContent( + $this->wsObject->objects, + $this->wsObject->schemaToDisplay, + $this->wsObject->fieldsToDisplay, + $this->wsObject->depth, + $typeOfView, + false + ); + } + + $this->renderResponse(); + } else { + $this->output .= $this->objOutput->getObjectRender()->renderNodeHeader('bookings', array()); + $bookings = Db::getInstance()->executeS('SELECT `id_order` FROM `'._DB_PREFIX_.'orders` WHERE 1'); + foreach ($bookings as $booking) { + $more_attr = array( + 'xlink_resource' => $this->wsObject->wsUrl.$this->wsObject->urlSegment[0].'/'.$booking['id_order'], + 'id' => (int) $booking['id_order'] + ); + $this->output .= $this->objOutput->getObjectRender()->renderNodeHeader('booking', array('objectsNodeName' => 'bookings'), $more_attr, false); + } + $this->output .= $this->objOutput->getObjectRender()->renderNodeFooter('bookings', array()); + $this->output = $this->objOutput->getObjectRender()->overrideContent($this->output); + } + + break; + case 'POST': + $inputData = $this->getRequestParams('booking'); + $this->formatRequestData($inputData); + if ($this->validatePostRequest($inputData)) { + $this->handlePostRequest($inputData); + $this->deleteWsServices(); + $this->deleteWsFeaturePrices(); + $this->deleteWsCartRules(); + $this->renderResponse(); + } else { + throw new WebserviceException( + $this->error_msg, + array(404, 400) + ); + } + break; + case 'PUT': + // handle update here. + $inputData = $this->getRequestParams('booking'); + // since post and put requires different data, validation is also different + $this->formatRequestData($inputData); + if ($this->validatePutRequest($inputData)) { + $this->handlePutRequest($inputData); + $this->deleteWsServices(); + $this->deleteWsFeaturePrices(); + $this->deleteWsCartRules(); + $this->renderResponse(); + } else { + throw new WebserviceException( + $this->error_msg, + array(404, 400) + ); + } + + break; + } + + return $this->output; + } + + /** + * Read the data send with POST and PUT requests. + */ + public function getRequestParams($head = false) + { + $postData = fopen('php://input', 'r'); + $inputXML = ''; + while ($putData = fread($postData, 1024)) { + $inputXML .= $putData; + } + + fclose($postData); + // If xml + if ($array = json_decode($inputXML, true)) { + if (($head ? isset($array[$head]) : true)) { + return ($head ? $array[$head] : $array); + } else { + WebserviceRequest::getInstance()->setError(500, 'Invalid request.', 127); + return; + } + } else if (simplexml_load_string($inputXML)) { + if (isset($inputXML) && strncmp($inputXML, 'xml=', 4) == 0) { + $inputXML = Tools::substr($inputXML, 4); + } + } else { + return false; + } + + try { + $xml = new SimpleXMLElement($inputXML); + } catch (Exception $error) { + WebserviceRequest::getInstance()->setError(500, 'XML error : '.$error->getMessage()."\n".'XML length : '.Tools::strlen($inputXML)."\n".'Original XML : '.$inputXML, 127); + + return; + } + + $xmlEntities = $xml->children(); + // Convert multi-dimention xml into an array + $array = json_decode(json_encode($xmlEntities), true); + + return ($head ? $array[$head] : $array); + } + + /** + * Formatting the request data for easier handeling. + */ + public function formatRequestData(&$data) + { + $associations = array(); + if (isset($data['associations'])) { + $associations = $data['associations']; + unset($data['associations']); + } + + $data = array_merge($data, $associations); + if (isset($data['room_types'])) { + $data['room_types'] = $this->formatRoomTypesInRequestData($data); + } + + if (isset($data['cart_rules'])) { + $data['cart_rules'] = $this->formatCartRulesInRequestData($data); + } + } + + public function formatCartRulesInRequestData($data) + { + $formattedCartRules = array(); + if (isset($data['cart_rules']['cart_rule'][0])) { + $formattedCartRules = $data['cart_rules']['cart_rule']; + } else if (isset($data['cart_rules']) + && !isset($data['cart_rules'][0]) + && isset($data['cart_rules']['cart_rule']) + ) { + $formattedCartRules[] = $data['cart_rules']['cart_rule']; + } else if (isset($data['cart_rules'][0])) { + $formattedCartRules = $data['cart_rules']; + } + + return $formattedCartRules; + } + + public function formatRoomTypesInRequestData($data) + { + $roomTypes = array(); + if (isset($data['room_types']['room_type'][0])) { + $roomTypes = $data['room_types']['room_type']; + } else if (isset($data['room_types']) + && !isset($data['room_types'][0]) + && isset($data['room_types']['room_type']) + ) { + $roomTypes[] = $data['room_types']['room_type']; + } else if (isset($data['room_types']) && isset($data['room_types'][0])) { + $roomTypes = $data['room_types']; + } + + if (count($roomTypes)) { + $formattedRoomTypes = array(); + foreach ($roomTypes as $roomTypeKey => $roomType) { + $dateProductJoinKey = $roomType['id_room_type'].'_'.strtotime($roomType['checkin_date']).strtotime($roomType['checkout_date']); + if (!isset($formattedRoomTypes[$dateProductJoinKey])) { + $formattedRoomTypes[$dateProductJoinKey] = $roomType; + } else { + $formattedRoomTypes[$dateProductJoinKey]['number_of_rooms'] += $roomType['number_of_rooms']; + } + + if (isset($roomType['rooms'])) { + $formattedRoomTypes[$dateProductJoinKey]['rooms'] = $this->formatRoomInRequestData($roomType); + } + } + + $roomTypes = $formattedRoomTypes; + } + + return $roomTypes; + } + + public function formatRoomInRequestData($data) + { + $rooms = array(); + if (isset($data['rooms']['room'][0])) { + $rooms = $data['rooms']['room']; + } else if (isset($data['rooms']) + && !isset($data['rooms'][0]) + && isset($data['rooms']['room']) + ) { + $rooms[] = $data['rooms']['room']; + } else if (isset($data['rooms']) && isset($data['rooms'][0])) { + $rooms = $data['rooms']; + } + + if (count($rooms)) { + $formattedRooms = array(); + foreach ($rooms as $roomKey => $room) { + $selectedDemands = $this->formatDemandsInRequestData($room); + $selectedServices = $this->formatServicesInRequestData($room); + $occupancy = $this->formatOccupancyInRequestData($room, $data['id_room_type']); + $rooms[$roomKey]['facilities'] = $selectedDemands; + $rooms[$roomKey]['services'] = $selectedServices; + $rooms[$roomKey]['occupancy'] = $occupancy; + $key = $roomKey; + if (isset($room['id_room'])) { + $key = 'r_'.$room['id_room']; + } + + $formattedRooms[$key] = $room; + $formattedRooms[$key]['facilities'] = $selectedDemands; + $formattedRooms[$key]['services'] = $selectedServices; + $formattedRooms[$key]['occupancy'] = $occupancy; + if (isset($room['id_tax_rules_group'])) { + $formattedRooms[$key]['id_tax_rules_group'] = $room['id_tax_rules_group']; + } else if ($room['total_tax']) { + $formattedRooms[$key]['total_tax'] = $room['total_tax']; + } + } + + $rooms = $formattedRooms; + } + + return $rooms; + } + + public function formatOccupancyInRequestData($data, $idRoomType) + { + $objRoomType = new HotelRoomType(); + // using to set base occupancy for the room if no occupancy is given, and we are not validating occupancy since Wsorder has booking without occupancy + $roomTypeInfo = $objRoomType->getRoomTypeInfoByIdProduct($idRoomType); + $child_ages = array(); + if (isset($data['child_ages']['child_age'][0]) + && is_array($data['child_ages']['child_age']) + ) { + $child_ages = $data['child_ages']['child_age']; + } else if (isset($data['child_ages']) + && !isset($data['child_ages'][0]) + && isset($data['child_ages']['child_age']) + && $data['child_ages']['child_age'] + ) { + $child_ages = $data['child_ages']['child_age']; + } else if (isset($data['child_ages']) && isset($data['child_ages'][0])) { + $child_ages = $data['child_ages']; + } else { + $data['child'] = 0; + } + + if (isset($data['child']) && !$data['child']) { + $child_ages = array(); + } else if (!is_array($child_ages)) { + $child_ages = array($child_ages); + } + + return array( + array( + 'adults' => isset($data['adult']) && $data['adult'] ? $data['adult'] : $roomTypeInfo['adults'], + 'children' => isset($data['child']) ? $data['child'] : $roomTypeInfo['children'], + 'child_ages' => $child_ages + ) + ); + } + + public function formatServicesInRequestData($data) + { + $selectedServices = array(); + if (isset($data['services']['service'][0])) { + $selectedServices = $data['services']['service']; + } else if (isset($data['services']) + && !isset($data['services'][0]) + && isset($data['services']['service']) + ) { + $selectedServices[] = $data['services']['service']; + } else if (isset($data['services']) && isset($data['services'][0])) { + $selectedServices = $data['services']; + } + + $formattedServices = array(); + foreach ($selectedServices as $service) { + $key = isset($service['id_service']) ? $service['id_service'] : 'new_'.rand(); + if (isset($service['id_service'])) { + $formattedServices[$key]['quantity'] = isset($service['quantity']) ? $service['quantity'] : 1; + $formattedServices[$key]['id_product'] = $service['id_service']; + if (isset($service['unit_price_without_tax'])) { + $formattedServices[$key]['unit_price_without_tax'] = $service['unit_price_without_tax']; + } else if (isset($service['total_price_without_tax'])) { + $formattedServices[$key]['total_price_without_tax'] = $service['total_price_without_tax']; + } + + if (isset($service['id_tax_rules_group'])) { + $formattedServices[$key]['id_tax_rules_group'] = $service['id_tax_rules_group']; + } else if ($service['total_tax']) { + $formattedServices[$key]['total_tax'] = $service['total_tax']; + } + } else { + $service['is_new'] = true; + $formattedServices[$key] = $service; + } + } + + return $formattedServices; + } + + public function formatDemandsInRequestData($data) + { + $selectedDemands = array(); + if (isset($data['facilities']['facility'][0])) { + $selectedDemands = $data['facilities']['facility']; + } else if (isset($data['facilities']) + && !isset($data['facilities'][0]) + && isset($data['facilities']['facility']) + ) { + $selectedDemands[] = $data['facilities']['facility']; + } else if (isset($data['facilities']) && isset($data['facilities'][0])) { + $selectedDemands = $data['facilities']; + } + + $formattedDemands = array(); + foreach ($selectedDemands as $key => $demand) { + $formattedDemands[$key]['id_global_demand'] = isset($demand['id_facility']) ? $demand['id_facility'] : 0; + $formattedDemands[$key]['id_option'] = isset($demand['id_option']) ? $demand['id_option'] : 0; + if (isset($demand['unit_price_without_tax'])) { + $formattedDemands[$key]['unit_price_without_tax'] = $demand['unit_price_without_tax']; + } + + if (isset($demand['id_tax_rules_group'])) { + $formattedDemands[$key]['id_tax_rules_group'] = $demand['id_tax_rules_group']; + } + } + + return $formattedDemands; + } + + /** + * Validating the POST request. + */ + public function validatePostRequest($params) + { + $this->error_msg = ''; + if (isset($params['id'])) { + $this->error_msg = Tools::displayError('id is forbidden when adding a new resource'); + } else if (!isset($params['currency']) + || !$params['currency'] + || !Currency::getIdByIsoCode($params['currency']) + || (!Validate::isLoadedObject((new Currency(Currency::getIdByIsoCode($params['currency']))))) + ) { + $this->error_msg = Tools::displayError('Please provide valid currency.'); + } elseif (!isset($params['customer_detail']) + || !$params['customer_detail'] + ) { + $this->error_msg = Tools::displayError('Customer details not found.'); + } else if (!isset($params['id_property']) + || !Validate::isLoadedObject(new HotelBranchInformation((int) $params['id_property'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid id_property.'); + } else if (isset($params['customer_detail']['id_customer']) + && $params['customer_detail']['id_customer'] + && !Validate::isLoadedObject(new Customer((int) $params['customer_detail']['id_customer'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid id_customer.'); + } else if (!isset($params['customer_detail']['firstname']) + || !$params['customer_detail']['firstname'] + || empty(trim($params['customer_detail']['firstname'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid first name.'); + } else if (!isset($params['customer_detail']['lastname']) + || !$params['customer_detail']['lastname'] + || empty(trim($params['customer_detail']['lastname'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid last name.'); + } else if (!isset($params['customer_detail']['email']) + || !$params['customer_detail']['email'] + || empty(trim($params['customer_detail']['email'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid email.'); + } else if (Configuration::get('PS_ONE_PHONE_AT_LEAST') + && (!isset($params['customer_detail']['phone']) || !$params['customer_detail']['phone'] || empty(trim($params['customer_detail']['phone']))) + ) { + $this->error_msg = Tools::displayError('Please provide a valid phone number.'); + } else if (isset($params['price_details']['total_paid']) + && $params['price_details']['total_paid'] + && (!Validate::isPrice($params['price_details']['total_paid'])) + ) { + $this->error_msg = Tools::displayError('Invalid value for total paid.'); + } else if (isset($params['price_details']['total_price_with_tax']) + && $params['price_details']['total_price_with_tax'] + && (!Validate::isPrice($params['price_details']['total_price_with_tax'])) + ) { + $this->error_msg = Tools::displayError('Invalid value for total amount with tax.'); + } else if (!$this->validateAddressFields($params['customer_detail']) + && $this->error_msg == '' + ) { + $this->error_msg = Tools::displayError('Invalid address provided.'); + } else if (!$this->validateRequestedRoomTypes($params['room_types'], $params['id_property']) + && $this->error_msg == '' + ) { + $this->error_msg = Tools::displayError('Requested room(s) not available.'); + } else if (isset($params['payment_detail']['payment_type']) + && $params['payment_detail']['payment_type'] != 'online' + && $params['payment_detail']['payment_type'] != 'remote' + && $params['payment_detail']['payment_type'] != 'pay at hotel' + ) { + $this->error_msg = Tools::displayError('Invalid payment type.'); + } else if (isset($params['booking_status']) + && ($params['booking_status'] < self::API_BOOKING_STATUS_NEW || $params['booking_status'] > self::API_BOOKING_STATUS_REFUNDED) + ) { + $this->error_msg = Tools::displayError('Invalid booking status.'); + } else if (isset($params['payment_status']) && + ($params['payment_status'] < self::API_BOOKING_PAYMENT_STATUS_COMPLETED || $params['payment_status'] > self::API_BOOKING_PAYMENT_STATUS_AWATING) + ) { + $this->error_msg = Tools::displayError('Invalid payment status.'); + } else if (isset($params['cart_rules']) && $params['cart_rules']) { + if (is_array($params['cart_rules'])) { + $this->validateCartRules($params['cart_rules']); + } else { + $this->error_msg = Tools::displayError('Invalid cart rules.'); + } + } + + if (!$this->error_msg && $this->error_msg == '') { + return true; + } + + return false; + } + + /** + * Validating the address fields in POST and PUT requests. + */ + public function validateAddressFields($params) + { + $status = true; + if (isset($params['address']) + || isset($params['city']) + || isset($params['country_code']) + || isset($params['state_code']) + || isset($params['zip']) + ) { + $status = false; + if (!isset($params['address']) || !$params['address']) { + $this->error_msg = Tools::displayError('Address is required.'); + } elseif (!isset($params['city']) || !$params['city']) { + $this->error_msg = Tools::displayError('City is required.'); + } elseif (!isset($params['country_code']) || !$params['country_code']) { + $this->error_msg = Tools::displayError('Country code is required.'); + } else if (!isset($params['address']) || !$params['address']) { + $this->error_msg = Tools::displayError('Address is required.'); + } else if (!isset($params['country_code']) || !$params['country_code']) { + $this->error_msg = Tools::displayError('Country code is required.'); + } else if (!$idCountry = Country::getByIso($params['country_code'])) { + $this->error_msg = Tools::displayError('Invalid country code.'); + } else if (($objCountry = new Country($idCountry)) && $objCountry->contains_states + && (!isset($params['state_code']) || !$params['state_code']) + ) { + $this->error_msg = Tools::displayError('State code is required for the given country.'); + } elseif ($objCountry->contains_states + && (!$idState = State::getIdByIso($params['state_code'], $objCountry->id)) + ) { + $this->error_msg = Tools::displayError('Invalid state code.'); + } else if ($objCountry->need_zip_code && (!isset($params['zip']) || !$params['zip'])) { + $this->error_msg = Tools::displayError('Zip code is required.'); + } elseif ($objCountry->need_zip_code + && (!Validate::isPostCode($params['zip']) || ($objCountry->zip_code_format && !$objCountry->checkZipCode($params['zip']))) + ) { + $this->error_msg = sprintf(Tools::displayError('The Zip/Postal code you have entered is invalid. It must follow this format: %s'), str_replace('C', $objCountry->iso_code, str_replace('N', '0', str_replace('L', 'A', $objCountry->zip_code_format)))); + } else { + $status = true; + } + } + + return $status; + } + + /** + * Checking room type information validity. + */ + public function validateRequestedRoomTypes($roomTypes = array(), $idHotel) + { + $objBookingDetail = new HotelBookingDetail(); + $objRoomType = new HotelRoomType(); + if (!$roomTypes) { + return false; + } else { + foreach ($roomTypes as $roomType) { + if ($this->validateRoomType($roomType)) { + if (!$idHotel) { + $roomTypeInfo = $objRoomType->getRoomTypeInfoByIdProduct($roomType['id_room_type']); + $idHotel = $roomTypeInfo['id_hotel']; + } + + $dateFrom = date('Y-m-d', strtotime($roomType['checkin_date'])); + $dateTo = date('Y-m-d', strtotime($roomType['checkout_date'])); + $bookingParams = array( + 'date_from' => $dateFrom, + 'date_to' => $dateTo, + 'hotel_id' => $idHotel, + 'id_room_type' => $roomType['id_room_type'], + 'only_search_data' => 1 + ); + if (($hotelRoomData = $objBookingDetail->dataForFrontSearch($bookingParams)) + && isset($hotelRoomData['rm_data'][$roomType['id_room_type']]['data']['available']) + && $hotelRoomData['rm_data'][$roomType['id_room_type']]['data']['available'] + ) { + if ($hotelRoomData['stats']['num_avail'] < $roomType['number_of_rooms']) { + return false; + } elseif (isset($roomType['rooms']) && count($roomType['rooms'])) { + foreach ($roomType['rooms'] as $room) { + if (isset($room['id_room']) && !isset($hotelRoomData['rm_data'][$roomType['id_room_type']]['data']['available'][$room['id_room']])) { + return false; + } + + if (isset($room['services']) && $room['services'] + && !$this->validateRequestedServices($room['services'], $roomType['id_room_type']) + ) { + return false; + } + + if (isset($room['facilities']) && $room['facilities'] + && !$this->validateRequestedDemands($room['facilities'], $roomType['id_room_type']) + ) { + return false; + } + } + } + } else { + return false; + } + } else { + return false; + } + } + } + + return true; + } + + /** + * Operations required for PUT requests. + */ + public function handlePostRequest($params) + { + $this->context->cart = new Cart(); + $this->processGuestDetails($params['customer_detail']); + $this->processLanguage($params); + $this->processCurrency($params); + // Saving the cart after adding the guest, language and the currency in the cart. + $this->context->cart->save(); + $this->addRoomsInCart($params['room_types']); + $this->processCustomer($params['customer_detail']); + // validating Cart rules here since the cart rule checkValidity() only works if there are products in the cart. + if (($error = $this->applyCartRules($params)) && $error != '') { + throw new WebserviceException( + $error, + array(404, 400) + ); + + return false; + } + + $totalAmount = isset($params['price_details']['total_paid']) ? $params['price_details']['total_paid'] : 0; + $objPaymentModule = new WebserviceOrder(); + $bookingStatus = self::API_BOOKING_STATUS_NEW; + if (isset($params['booking_status']) && $params['booking_status']) { + $bookingStatus = $params['booking_status']; + } + + $cartTotal = $this->context->cart->getOrderTotal(true, Cart::BOTH); + switch ($bookingStatus) { + case self::API_BOOKING_STATUS_NEW: + if ($totalAmount > 0 && $totalAmount < $cartTotal) { + $orderStatus = Configuration::get('PS_OS_PARTIAL_PAYMENT_ACCEPTED'); + } else if ($totalAmount >= $cartTotal) { + $orderStatus = $orderStatus = Configuration::get('PS_OS_PAYMENT_ACCEPTED'); + } else { + $paymentStatus = false; + if (isset($params['payment_status'])) { + $paymentStatus = $params['payment_status']; + } + + switch ($paymentStatus) { + case self::API_BOOKING_PAYMENT_STATUS_COMPLETED: + $orderStatus = Configuration::get('PS_OS_PAYMENT_ACCEPTED'); + break; + case self::API_BOOKING_PAYMENT_STATUS_PARTIAL: + $orderStatus = Configuration::get('PS_OS_PARTIAL_PAYMENT_ACCEPTED'); + break; + case self::API_BOOKING_PAYMENT_STATUS_AWATING: + $orderStatus = Configuration::get('PS_OS_AWAITING_PAYMENT'); + break; + default: + $orderStatus = Configuration::get('PS_OS_AWAITING_PAYMENT'); + break; + } + } + break; + case self::API_BOOKING_STATUS_COMPLETED: + $orderStatus = Configuration::get('PS_OS_PAYMENT_ACCEPTED'); + if (!$totalAmount) { + $totalAmount = $cartTotal; + } else if ($totalAmount > 0 && $totalAmount < $cartTotal) { + $orderStatus = Configuration::get('PS_OS_PARTIAL_PAYMENT_ACCEPTED'); + } + + break; + case self::API_BOOKING_STATUS_CANCELLED: + $orderStatus = Configuration::get('PS_OS_CANCELED'); + break; + case self::API_BOOKING_STATUS_REFUNDED: + $orderStatus = Configuration::get('PS_OS_REFUND'); + break; + default: + break; + } + + if (isset($params['source']) && $params['source']) { + $objPaymentModule->orderSource = $params['source']; + } + + $extraVars = array(); + $message = null; + if (isset($params['payment_detail']['transaction_id']) + && $params['payment_detail']['transaction_id'] + ) { + $extraVars['transaction_id'] = $params['payment_detail']['transaction_id']; + } + + if (isset($params['remark'])) { + $message = $params['remark']; + } + + if (isset($params['payment_detail']['payment_method']) + && $params['payment_detail']['payment_method'] + ) { + $objPaymentModule->displayName = $params['payment_detail']['payment_method']; + } + + if (isset($params['payment_detail']['payment_type']) + && $params['payment_detail']['payment_type'] + ) { + if ($params['payment_detail']['payment_type'] == 'remote') { + $objPaymentModule->payment_type = OrderPayment::PAYMENT_TYPE_REMOTE_PAYMENT; + } else if ($params['payment_detail']['payment_type'] == 'pay at hotel') { + $objPaymentModule->payment_type = OrderPayment::PAYMENT_TYPE_PAY_AT_HOTEL; + } + } + + $objOrderState = new OrderState($orderStatus); + if ($objOrderState->paid) { + $orderStatus = Configuration::get('PS_OS_AWAITING_PAYMENT'); + } + + if ($objPaymentModule->validateOrder( + $this->context->cart->id, + $orderStatus, + $totalAmount, + $objPaymentModule->displayName, + $message, + $extraVars, + null, + false, + $this->bookingCustomer->secure_key + )) { + $this->updateServicesAndDemandsInOrder($objPaymentModule->currentOrder); + if (!empty($this->wsRequestedRooms)) { + $this->updateRoomTaxRulesGroupsInOrder($objPaymentModule->currentOrder); + } + + $this->manageOrderPrice($objPaymentModule->currentOrder, $params); + $this->addOrderHistory($objPaymentModule->currentOrder, $objOrderState); + $objOrder = new Order($objPaymentModule->currentOrder); + if (isset($params['booking_date']) + && $params['booking_date'] + && Validate::isDate($params['booking_date']) + ) { + $objOrder->date_add = date('Y-m-d H:i:s', strtotime($params['booking_date'])); + } + + $objOrder->save(); + $this->getBookingDetails($objPaymentModule->currentOrder); + + return true; + } + + $this->wsObject->setError(400, Tools::displayError('Unable to create booking.'), 200); + + return false; + } + + public function manageOrderPrice($idOrder, $params) + { + $objOrder = new Order($idOrder); + if (isset($params['price_details']['total_price_with_tax']) && (int) $params['price_details']['total_price_with_tax']) { + if ($params['price_details']['total_price_with_tax'] < $objOrder->total_paid_tax_incl) { + $cartRule['code'] = Tools::passwdGen(8, 'NO_NUMERIC'); + $cartRule['currency'] = ''; + $cartRule['value'] = $objOrder->total_paid_tax_incl - $params['price_details']['total_price_with_tax']; + if ($cartRule['value']) { + $cartRule['value'] /= count($invoiceCollection = $objOrder->getInvoicesCollection()); + } + + $cartRule['type'] = self::API_CART_RULE_VALUE_TYPE_AMOUNT; + $this->addCartRulesToOrder(array($cartRule), $objOrder->id); + } else if ($params['price_details']['total_price_with_tax'] > $objOrder->total_paid_tax_incl) { + $objHotelBookingDetail = new HotelBookingDetail(); + $objHotelBookingDemands = new HotelBookingDemands(); + if ($roomsInOrder = $objHotelBookingDetail->getOrderCurrentDataByOrderId($idOrder)) { + $requestedPrice = $params['price_details']['total_price_with_tax']; + $roomsToUpdate = array(); + foreach ($roomsInOrder as $orderRoomKey => $orderRoom) { + $dateRoomJoinKey = strtotime($orderRoom['date_from']).''.strtotime($orderRoom['date_to']).$orderRoom['id_room']; + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['room']['unit_price_without_tax'])) { + $objHotelBookingDetail = new HotelBookingDetail((int) $orderRoom['id']); + $requestedPrice -= $objHotelBookingDetail->total_price_tax_incl; + } else { + $roomsToUpdate[] = $orderRoom; + } + } + + $serviceProductPrice = $objOrder->getTotalProductsWithTaxes(false, false, Product::SERVICE_PRODUCT_WITH_ROOMTYPE); + if ($demands = $objHotelBookingDemands->getExtraDemandsTaxesDetails($objOrder->id)) { + $demandsPrice = array_sum(array_column($demands, 'total_price_tax_excl')); + // Adding the tax + $demandsPrice += array_sum(array_column($demands, 'total_amount')); + $serviceProductPrice += $demandsPrice; + } + + $requestedPrice -= $serviceProductPrice; + // This empty means that all the rooms are sent with price in the request, so we will add a service to a room to manage the order total sent in the request. + if ($requestedPrice > 0) { + if (empty($roomsToUpdate)) { + $service = array(); + $service['name'] = 'Created By API'; + $service['total_price_without_tax'] = $requestedPrice; + $service['price_mode'] = self::API_SERVICE_PRICE_MODE_PER_BOOKING; + $service['quantity'] = 1; + $service['is_new'] = 1; + $firstRoomInOrder = array_shift($roomsInOrder); + $this->addServicesInOrderedRoom(array($service), $firstRoomInOrder['id']); + } else { + $roomsTotal = array_sum(array_column($roomsToUpdate, 'total_price_tax_incl')); + $objOrder = new Order($idOrder); + foreach ($roomsToUpdate as $roomInfo) { + $objHotelBookingDetail = new HotelBookingDetail((int) $roomInfo['id']); + $taxMultiplier = $objHotelBookingDetail->total_price_tax_incl / $objHotelBookingDetail->total_price_tax_excl; + $roomNewPrice = ($objHotelBookingDetail->total_price_tax_incl / $roomsTotal) * $requestedPrice; + + $oldPriceTaxIncl = $objHotelBookingDetail->total_price_tax_incl; + $oldPriceTaxExcl = $objHotelBookingDetail->total_price_tax_excl; + + $objHotelBookingDetail->total_price_tax_incl = Tools::ps_round($roomNewPrice, _PS_PRICE_COMPUTE_PRECISION_); + $objHotelBookingDetail->total_price_tax_excl = Tools::ps_round(($roomNewPrice / $taxMultiplier), _PS_PRICE_COMPUTE_PRECISION_); + $objHotelBookingDetail->save(); + // Updating the price + $priceDiffTaxIncl = $objHotelBookingDetail->total_price_tax_incl - $oldPriceTaxIncl; + $priceDiffTaxExcl = $objHotelBookingDetail->total_price_tax_excl - $oldPriceTaxExcl; + + $objOrderDetail = new OrderDetail((int) $objHotelBookingDetail->id_order_detail); + $objOrderDetail->total_price_tax_incl += $priceDiffTaxIncl; + $objOrderDetail->total_price_tax_excl += $priceDiffTaxExcl; + $objOrderDetail->save(); + $objOrder->total_paid_tax_incl += $priceDiffTaxIncl; + $objOrder->total_products_wt += $priceDiffTaxIncl; + + $objOrder->total_products += $priceDiffTaxExcl; + $objOrder->total_paid_tax_excl += $priceDiffTaxExcl; + + $objOrder->total_paid += $priceDiffTaxIncl; + } + + $objOrder->save(); + } + } + } + } + } + } + + public function updateRoomTaxRulesGroupsInOrder($idOrder) + { + $objOrder = new Order($idOrder); + $objHotelBookingDetail = new HotelBookingDetail(); + if ($roomsInOrder = $objHotelBookingDetail->getOrderCurrentDataByOrderId($idOrder)) { + foreach ($roomsInOrder as $orderRoomKey => $orderRoom) { + $dateRoomJoinKey = strtotime($orderRoom['date_from']).''.strtotime($orderRoom['date_to']).$orderRoom['id_room']; + if (isset($this->wsRequestedRooms[$dateRoomJoinKey])) { + $objHotelBookingDetail = new HotelBookingDetail((int) $orderRoom['id']); + $objOrderDetail = new OrderDetail((int) $objHotelBookingDetail->id_order_detail); + + $priceWithTax = $objHotelBookingDetail->total_price_tax_incl; + if (isset($this->wsRequestedRooms[$dateRoomJoinKey]['id_tax_rules_group'])) { + // Getting new Tax + $objAddress = new Address((int) $objOrder->id_address_tax); + $objTaxManager = TaxManagerFactory::getManager($objAddress, $this->wsRequestedRooms[$dateRoomJoinKey]['id_tax_rules_group']); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + $priceWithTax = $objTaxCalculator->addTaxes($objHotelBookingDetail->total_price_tax_excl); + } else if (isset($this->wsRequestedRooms[$dateRoomJoinKey]['total_tax'])) { + $priceWithTax = $objHotelBookingDetail->total_price_tax_excl + $this->wsRequestedRooms[$dateRoomJoinKey]['total_tax']; + } + + $taxDiff = $objHotelBookingDetail->total_price_tax_incl - $priceWithTax; + + // Updating the price + $objHotelBookingDetail->total_price_tax_incl += $taxDiff; + $objHotelBookingDetail->save(); + + $objOrderDetail->total_price_tax_incl += $taxDiff; + $objOrderDetail->save(); + + $objOrder->total_paid += $taxDiff; + $objOrder->total_paid_tax_incl += $taxDiff; + $objOrder->total_products_wt += $taxDiff; + } + } + + $objOrder->update(); + } + } + + /** + * Updating the services and their prices. + */ + public function updateServicesAndDemandsInOrder($idOrder) + { + $objOrder = new Order($idOrder); + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + if (isset($this->wsRequestedRoomTypes) && $this->wsRequestedRoomTypes) { + if ($orderedServices = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts($objOrder->id, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + null, + null + )) { + foreach ($orderedServices as $orderedService) { + $objHotelBookingDetail = new HotelBookingDetail($orderedService['id_htl_booking_detail']); + $dateRoomJoinKey = strtotime($objHotelBookingDetail->date_from).''.strtotime($objHotelBookingDetail->date_to).$orderedService['id_room']; + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services']) + && $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'] + && isset($orderedService['additional_services']) + && $orderedService['additional_services'] + ) { + foreach ($orderedService['additional_services'] as $service) { + $objOrderDetail = new OrderDetail($service['id_order_detail']); + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail($service['id_room_type_service_product_order_detail']); + $quantity = $objRoomTypeServiceProductOrderDetail->quantity; + if ($objOrderDetail->product_price_calculation_method == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $quantity = $quantity * HotelHelper::getNumberOfDays( + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to + ); + } + + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']])) { + $oldPriceTaxExcl = $objRoomTypeServiceProductOrderDetail->total_price_tax_excl; + $oldPriceTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + if ($oldPriceTaxExcl > 0) { + $oldTaxMultiplier = $oldPriceTaxIncl / $oldPriceTaxExcl; + } else { + $oldTaxMultiplier = 1; + } + + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax'])) { + $unitPriceTaxExcl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax']; + $unitPriceTaxIncl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax'] * $oldTaxMultiplier; + $totalPriceTaxExcl = $unitPriceTaxExcl * $quantity; + $totalPriceTaxIncl = $unitPriceTaxIncl * $quantity; + $objRoomTypeServiceProductOrderDetail->unit_price_tax_excl = Tools::ps_round($unitPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_excl = Tools::ps_round($totalPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + + $priceDiffTaxExcl = $objRoomTypeServiceProductOrderDetail->total_price_tax_excl - $oldPriceTaxExcl; + $priceDiffTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl - $oldPriceTaxIncl; + + $objOrderDetail->total_price_tax_excl += $priceDiffTaxExcl; + $objOrderDetail->total_price_tax_incl += $priceDiffTaxIncl; + + $objOrderDetail->unit_price_tax_excl = Tools::ps_round(($objOrderDetail->total_price_tax_excl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderDetail->unit_price_tax_incl = Tools::ps_round(($objOrderDetail->total_price_tax_incl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + + $objOrder->total_paid_tax_excl += $priceDiffTaxExcl; + $objOrder->total_paid_tax_incl += $priceDiffTaxIncl; + $objOrder->total_paid += $priceDiffTaxIncl; + } else if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_price_without_tax'])) { + $totalPriceTaxExcl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_price_without_tax']; + $totalPriceTaxIncl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_price_without_tax'] * $oldTaxMultiplier; + $unitPriceTaxExcl = $totalPriceTaxExcl / $quantity; + $unitPriceTaxIncl = $totalPriceTaxIncl / $quantity; + + $objRoomTypeServiceProductOrderDetail->unit_price_tax_excl = Tools::ps_round($unitPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_excl = Tools::ps_round($totalPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + + $priceDiffTaxExcl = $objRoomTypeServiceProductOrderDetail->total_price_tax_excl - $oldPriceTaxExcl; + $priceDiffTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl - $oldPriceTaxIncl; + + $objOrderDetail->total_price_tax_excl += $priceDiffTaxExcl; + $objOrderDetail->total_price_tax_incl += $priceDiffTaxIncl; + + $objOrderDetail->unit_price_tax_excl = Tools::ps_round(($objOrderDetail->total_price_tax_excl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderDetail->unit_price_tax_incl = Tools::ps_round(($objOrderDetail->total_price_tax_incl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + + $objOrder->total_paid_tax_excl += $priceDiffTaxExcl; + $objOrder->total_paid_tax_incl += $priceDiffTaxIncl; + $objOrder->total_paid += $priceDiffTaxIncl; + } + } + + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['id_tax_rules_group'])) { + $objAddress = new Address((int) $objOrder->id_address_tax); + $objTaxManager = TaxManagerFactory::getManager($objAddress, $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['id_tax_rules_group']); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + $unitPriceTaxIncl = $objTaxCalculator->addTaxes($objRoomTypeServiceProductOrderDetail->unit_price_tax_excl); + $oldPriceTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round(($unitPriceTaxIncl * $quantity), _PS_PRICE_COMPUTE_PRECISION_); + + $priceDiffTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl - $oldPriceTaxIncl; + + $objOrderDetail->total_price_tax_incl += $priceDiffTaxIncl; + $objOrderDetail->unit_price_tax_incl = Tools::ps_round(($objOrderDetail->total_price_tax_incl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + + $objOrder->total_paid_tax_incl += $priceDiffTaxIncl; + $objOrder->total_paid += $priceDiffTaxIncl; + } else if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_tax']) + && $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_tax'] + ) { + $oldPriceTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = $objRoomTypeServiceProductOrderDetail->total_price_tax_excl + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_tax']; + + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($objRoomTypeServiceProductOrderDetail->total_price_tax_incl / $quantity, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round(($objRoomTypeServiceProductOrderDetail->total_price_tax_incl), _PS_PRICE_COMPUTE_PRECISION_); + + $priceDiffTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl - $oldPriceTaxIncl; + $objOrderDetail->total_price_tax_incl += $priceDiffTaxIncl; + $objOrderDetail->unit_price_tax_incl = Tools::ps_round(($objOrderDetail->total_price_tax_incl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + + $objOrder->total_paid_tax_incl += $priceDiffTaxIncl; + $objOrder->total_paid += $priceDiffTaxIncl; + } + + $objRoomTypeServiceProductOrderDetail->save(); + $objOrderDetail->save(); + } + } + } + + // To save the changes made till now, since we are again loading this order while adding demands if any. + $objOrder->save(); + } + + + $objHotelBookingDetail = new HotelBookingDetail(); + if ($orderedRooms = $objHotelBookingDetail->getOrderCurrentDataByOrderId($objOrder->id)) { + foreach ($orderedRooms as $orderedRoom) { + $dateRoomJoinKey = strtotime($orderedRoom['date_from']).''.strtotime($orderedRoom['date_to']).$orderedRoom['id_room']; + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands']) && $this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands']) { + if ($demands = json_decode($this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands'], true)) { + $this->addDemandsInOrderedRoom($demands, $orderedRoom['id']); + } + } + } + } + } + } + + /** + * Adding the required guest informations into the cart. + */ + public function processGuestDetails($guestDetails) + { + if (isset($guestDetails['id_customer']) + && Validate::isLoadedObject($objCustomer = new Customer((int) $guestDetails['id_customer'])) + ) { + $this->bookingCustomer = $objCustomer; + } else { + $objCustomer = new Customer(); + $this->bookingCustomer = $objCustomer->getByEmail($guestDetails['email']); + } + + if (isset($this->bookingCustomer->id) + && $this->bookingCustomer->id + ) { + $idGuest = Guest::getFromCustomer($this->bookingCustomer->id); + } else { + $idGuest = $this->createGuest(); + } + + $this->context->cart->id_guest = $idGuest; + } + + /** + * Adding the cart language. + */ + public function processLanguage($params) + { + $idLang = Configuration::get('PS_LANG_DEFAULT'); + if (isset($params['id_language']) + && Validate::isLoadedObject($objLanguage = new Language((int) $params['id_language'])) + && $objLanguage->active + ) { + $idLang = $objLanguage->id; + } + + $this->context->language = new Language((int) $idLang); + $this->context->cart->id_lang = $idLang; + } + + /** + * Adding the cart currency. + */ + public function processCurrency($params) + { + $idCurrency = Configuration::get('PS_CURRENCY_DEFAULT'); + if (isset($params['currency']) + && ($selectedCurrency = Currency::getIdByIsoCode($params['currency'])) + && Validate::isLoadedObject($objCurrency = new Currency($selectedCurrency)) + && $objCurrency->active + ) { + $idCurrency = $selectedCurrency; + } + + $this->context->currency = new Currency((int) $idCurrency); + $this->context->cart->id_currency = $idCurrency; + } + + + /** + * Adding the cart customer. + */ + public function processCustomer($customerDetails) + { + $this->context->cookie->id_guest = $this->context->cart->id_guest; + if (!isset($this->bookingCustomer->id)) { + $objCustomer = new Customer(); + $objCustomer->firstname = $customerDetails['firstname']; + $objCustomer->lastname = $customerDetails['lastname']; + $objCustomer->email = $customerDetails['email']; + $objCustomer->passwd = md5(time()._COOKIE_KEY_); + $objCustomer->cleanGroups(); + $objCustomer->add(); + $this->bookingCustomer = $objCustomer; + } else { + if (isset($customerDetails['firstname']) && Validate::isName($customerDetails['firstname'])) { + $this->bookingCustomer->firstname = $customerDetails['firstname']; + } + + if (isset($customerDetails['lastname']) && Validate::isName($customerDetails['lastname'])) { + $this->bookingCustomer->lastname = $customerDetails['lastname']; + } + + if (isset($customerDetails['email']) && Validate::isEmail($customerDetails['email'])) { + $this->bookingCustomer->email = $customerDetails['email']; + } else { + $customerDetails['email'] = $this->bookingCustomer->email; + } + + $this->bookingCustomer->save(); + } + + // Since the address is validated if even a single address field is present in the request. + if (isset($customerDetails['country_code']) + && $customerDetails['country_code'] + ) { + $customerDetails['id_country'] = Country::getByIso($customerDetails['country_code']); + $objCountry = new Country($customerDetails['id_country']); + if ($objCountry->contains_states) { + $customerDetails['id_state'] = State::getIdByIso($customerDetails['state_code']); + } + + $active = true; + $cache_id = 'Address::getFirstCustomerAddressId_'.(int) $this->bookingCustomer->id.'-'.(bool)$active; + Cache::clean($cache_id); + if ($idAddress = Address::getFirstCustomerAddressId($this->bookingCustomer->id)) { + $objAddress = new Address((int) $idAddress); + } else { + $objAddress = new Address(); + $objAddress->alias = 'Generated by bookings API'; + $objAddress->id_customer = $this->bookingCustomer->id; + $objAddress->auto_generated = true; + } + + $objAddress->firstname = $customerDetails['firstname']; + $objAddress->lastname = $customerDetails['lastname']; + if (isset($customerDetails['phone'])) { + $objAddress->phone = $customerDetails['phone']; + } + + $objAddress->address1 = $customerDetails['address']; + $objAddress->city = $customerDetails['city']; + $objAddress->postcode = isset($customerDetails['zip']) ? $customerDetails['zip'] : ''; + $objAddress->id_country = $customerDetails['id_country']; + $objAddress->id_state = isset($customerDetails['id_state']) ? $customerDetails['id_state'] : 0; + + $objAddress->save(); + } + + if (isset($customerDetails['phone']) && Validate::isPhoneNumber($customerDetails['phone'])) { + CartCustomerGuestDetail::updateCustomerPhoneNumber($customerDetails['email'], $customerDetails['phone']); + } + + // to remove the older non ordered cart for this customer. + $this->context->cookie->id_cart = $this->context->cart->id; + $this->context->updateCustomer($this->bookingCustomer, 1); + } + + /** + * Adding the cart rules, after creating them. + */ + public function applyCartRules($params) + { + $error = ''; + if ($cartRules = $this->formatCartRulesInRequestData($params)) { + if ($cartRules = $this->createCartRules($cartRules)) { + foreach ($cartRules as $cartRule) { + $objCartRule = new CartRule((int) $cartRule); + if (($error = $objCartRule->checkValidity($this->context)) + && $error != '' + ) { + break; + } else { + $this->context->cart->addCartRule($objCartRule->id); + } + } + } + } + + return $error; + } + + /** + * Getting cart rules and creating new ones if they do not exists. + */ + public function createCartRules($requestedCartRules) + { + $cartRules = array(); + if ($requestedCartRules) { + $languags = Language::getIDs(true); + foreach ($requestedCartRules as $cartRule) { + if (Validate::isLoadedObject($objCartRule = new CartRule(CartRule::getIdByCode($cartRule['code'])))) { + $cartRules[] = $objCartRule->id; + } else { + $idCurrency = $this->context->currency->id; + if (isset($cartRule['currency']) + && ($selectedCurrency = Currency::getIdByIsoCode($cartRule['currency'])) + && Validate::isLoadedObject($objCurrency = new Currency($selectedCurrency)) + && $objCurrency->active + ) { + $idCurrency = $objCurrency->id; + } + + $objCartRule = new CartRule(); + foreach ($languags as $idLang) { + $objCartRule->name[$idLang] = $cartRule['code']; + } + + $objCartRule->quantity = 1; + $objCartRule->quantity_per_user = 1; + $objCartRule->id_customer = $this->bookingCustomer->id; + $objCartRule->highlight = 1; + $objCartRule->date_from = date('Y-m-d H:i:s'); + $objCartRule->date_to = date('Y-m-d H:i:s', strtotime($objCartRule->date_from) + (3600 * 24 * 365.25));; + $objCartRule->active = 1; + $objCartRule->reduction_tax = false; + $objCartRule->minimum_amount_currency = $idCurrency; + $objCartRule->reduction_currency = $idCurrency; + $objCartRule->code = $cartRule['code']; + if ($cartRule['type'] == self::API_CART_RULE_VALUE_TYPE_PERCENTAGE) { + $objCartRule->reduction_percent = $cartRule['value']; + } else if ($cartRule['type'] == self::API_CART_RULE_VALUE_TYPE_AMOUNT) { + $objCartRule->reduction_amount = $cartRule['value']; + } + + if ($objCartRule->add()) { + $cartRules[] = $objCartRule->id; + $this->wsCartRules[] = $objCartRule->id; + } + } + } + } + + return $cartRules; + } + + /** + * Validating the cart rules. + */ + public function validateCartRules($cartRules, $inOrder = false) + { + foreach ($cartRules as $cartRule) { + if (!isset($cartRule['code']) || !$cartRule['code']) { + $this->error_msg = Tools::displayError('Invalid code for cart rule.'); + break; + } else if (!($code = trim($cartRule['code'])) + || !Validate::isCleanHtml($code) + ) { + $this->error_msg = Tools::displayError('Invalid cart rule.'); + break; + } else if ($inOrder && !Validate::isLoadedObject($objCartRule = new CartRule(CartRule::getIdByCode($code))) + && (!isset($cartRule['value']) || !$cartRule['value'] || !isset($cartRule['type']) || !$cartRule['type']) + ) { + $this->error_msg = Tools::displayError('Invalid cart rule parameters.'); + break; + } else if (!$inOrder && !Validate::isLoadedObject($objCartRule = new CartRule(CartRule::getIdByCode($code))) + && (!isset($cartRule['type']) || $cartRule['type'] != self::API_CART_RULE_VALUE_TYPE_PERCENTAGE || $cartRule['type'] != self::API_CART_RULE_VALUE_TYPE_AMOUNT) + && (!isset($cartRule['value']) || !$cartRule['value'] || ($cartRule['type'] == self::API_CART_RULE_VALUE_TYPE_PERCENTAGE && $cartRule['value'] > 100)) + ) { + $this->error_msg = Tools::displayError('Invalid cart rule parameters.'); + break; + } else if (isset($cartRule['currency']) + && !Validate::isLoadedObject($objCurrency = new Currency(Currency::getIdByIsoCode($cartRule['currency']))) + ) { + $this->error_msg = Tools::displayError('Invalid currency for cart rules.'); + break; + } + } + } + + /** + * Validating the services related information. + */ + public function validateRequestedServices($services, $idRoomType) + { + if ($services) { + $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); + foreach ($services as $key => $service) { + if (isset($service['id_tax_rules_group']) && !Validate::isLoadedObject(new TaxRulesGroup((int) $service['id_tax_rules_group']))) { + $this->error_msg = Tools::displayError('Invalid id_tax_rules_group for services.'); + return false; + } else if (isset($service['total_tax']) && !Validate::isPrice($service['total_tax'])) { + $this->error_msg = Tools::displayError('Invalid tax for services.'); + return false; + } else if (isset($service['id_product'])) { + if (!Validate::isLoadedObject(new Product((int) $service['id_product']))) { + $this->error_msg = Tools::displayError('Invalid request for service.'); + return false; + } else if (!$objRoomTypeServiceProduct->isRoomTypeLinkedWithProduct($idRoomType, $service['id_product'])) { + $this->error_msg = Tools::displayError('Service is not linked with the requested room type.'); + return false; + } + } else { + if (!isset($service['name'])) { + $this->error_msg = Tools::displayError('Service name is required for non existing services.'); + return false; + } else if (!Validate::isCatalogName($service['name'])) { + $this->error_msg = sprintf(Tools::displayError('Invalid service name %s.'), $service['name']); + return false; + } else if (!isset($service['total_price_without_tax']) && !isset($service['unit_price_without_tax'])) { + $this->error_msg = sprintf(Tools::displayError('Price required for service %s.'), $service['name']); + return false; + } else if ((isset($service['total_price_without_tax']) && !Validate::isPrice($service['total_price_without_tax'])) + || (isset($service['unit_price_without_tax']) && !Validate::isPrice($service['unit_price_without_tax'])) + ) { + $this->error_msg = sprintf(Tools::displayError('Invalid price for service %s.'), $service['name']); + return false; + } else if (isset($service['quantity']) && $service['quantity'] < 1) { + $this->error_msg = sprintf(Tools::displayError('Invalid quantity for service %s.'), $service['name']); + return false; + } else if (isset($service['price_mode']) + && $service['price_mode'] != self::API_SERVICE_PRICE_MODE_PER_BOOKING + && $service['price_mode'] != self::API_SERVICE_PRICE_MODE_PER_DAY + ) { + $this->error_msg = sprintf(Tools::displayError('Invalid price mode for service %s.'), $service['name']); + return false; + } + } + } + } + + return true; + } + + /** + * Validating the demands related information. + */ + public function validateRequestedDemands($requestedDemands, $idRoomType) + { + $objHotelRoomTypeDemand = new HotelRoomTypeDemand(); + $objHotelDemandOptions = new HotelRoomTypeGlobalDemandAdvanceOption(); + // Incase the there is no demand for this room but demands are still sent in the request for this room type. + if ($roomTypeDemands = $objHotelRoomTypeDemand->getRoomTypeDemands($idRoomType)) { + foreach ($requestedDemands as $demandKey => $requestedDemand) { + if (!isset($requestedDemand['id_global_demand']) + || !isset($roomTypeDemands[$requestedDemand['id_global_demand']]) + ) { + $this->error_msg = Tools::displayError('Invalid request for facilities.'); + return false; + } else if (isset($roomTypeDemands[$requestedDemand['id_global_demand']]['adv_option']) + && (!isset($requestedDemand['id_option'])) + ) { + $this->error_msg = sprintf(Tools::displayError('Id option is required for the facility with ID %s.'), $requestedDemand['id_global_demand']); + return false; + } else if (isset($roomTypeDemands[$requestedDemand['id_global_demand']]['adv_option']) + && !isset($roomTypeDemands[$requestedDemand['id_global_demand']]['adv_option'][$requestedDemand['id_option']]) + ) { + $this->error_msg = sprintf(Tools::displayError('Invalid id option for the facility with ID %s.'), $requestedDemand['id_global_demand']); + return false; + } else if (isset($requestedDemand['id_tax_rules_group']) && !Validate::isLoadedObject(new TaxRulesGroup((int) $requestedDemand['id_tax_rules_group']))) { + $this->error_msg = Tools::displayError('Invalid id_tax_rules_group for facilities.'); + return false; + } else if (isset($requestedDemand['total_tax']) && !Validate::isPrice($requestedDemand['total_tax'])) { + $this->error_msg = Tools::displayError('Invalid tax for facilities.'); + return false; + } + } + } else { + $this->error_msg = Tools::displayError('Invalid request for facilities.'); + return false; + } + + return true; + } + + /** + * Adding rooms in the cart. + */ + public function addRoomsInCart($roomTypes) + { + $objRoomType = new HotelRoomType(); + $objHotelCartBookingData = new HotelCartBookingData(); + $roomUnitSelectionType = Configuration::get('PS_FRONT_ROOM_UNIT_SELECTION_TYPE'); + + $quantityWise = false; + if ($roomUnitSelectionType != HotelBookingDetail::PS_ROOM_UNIT_SELECTION_TYPE_OCCUPANCY) { + $quantityWise = true; + } + + foreach ($roomTypes as $roomType) { + $dateFrom = date('Y-m-d', strtotime($roomType['checkin_date'])); + $dateTo = date('Y-m-d', strtotime($roomType['checkout_date'])); + $roomTypeInfo = $objRoomType->getRoomTypeInfoByIdProduct($roomType['id_room_type']); + $idHotel = $roomTypeInfo['id_hotel']; + $occupancy = array( + array( + 'adults' => $roomTypeInfo['adults'], + 'children' => $roomTypeInfo['children'], + 'child_ages' => array() + ) + ); + + if (isset($roomType['rooms']) && count($roomType['rooms'])) { + foreach ($roomType['rooms'] as $room) { + $roomServices = array(); + if (isset($room['services']) + && $room['services'] + ) { + foreach ($room['services'] as $serviceKey => $service) { + if (isset($service['is_new'])) { + if (!isset($service['price_mode'])) { + $service['price_mode'] = self::API_SERVICE_PRICE_MODE_PER_BOOKING; + } + + $service['id_product'] = $this->createWsService($service, $roomType['id_room_type']); + $serviceKey = $service['id_product']; + if (!isset($service['quantity'])) { + $service['quantity'] = 1; + } + } + + $roomServices[$serviceKey] = $service; + } + } + + // since we cannot update them after ordering them and will have to replace them if orderd here. So, we will not add them for now. + $roomDemands = json_encode(array()); + + if (isset($room['occupancy']) && count($room['occupancy'])) { + $occupancy = $room['occupancy']; + } + + $idRoom = 0; + if (isset($room['id_room'])) { + $idRoom = $room['id_room']; + } + + if ($quantityWise) { + $occupancy = 1; // since we are adding rooms one by one into the cart. + } + + if ($idHtlCartBookingData = $objHotelCartBookingData->updateCartBooking( + $roomType['id_room_type'], + $occupancy, + 'up', + $idHotel, + $idRoom, + $dateFrom, + $dateTo, + $roomDemands, + $roomServices, + $this->context->cart->id, + $this->context->cart->id_guest + )) { + $objCartBookingData = new HotelCartBookingData((int) $idHtlCartBookingData); + $dateRoomJoinKey = strtotime($dateFrom).strtotime($dateTo).$objCartBookingData->id_room; + // To update the price after valiate order is called. + if (isset($room['facilities']) + && $room['facilities'] + ) { + $roomDemands = json_encode($room['facilities']); + } + + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'] = $roomServices; + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands'] = $roomDemands; + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['room'] = $room; + if (isset($room['id_tax_rules_group']) && Validate::isLoadedObject(new TaxRulesGroup((int) $room['id_tax_rules_group']))) { + $this->wsRequestedRooms[$dateRoomJoinKey]['id_tax_rules_group'] = $room['id_tax_rules_group']; + } else if (isset($room['total_tax'])) { + $this->wsRequestedRooms[$dateRoomJoinKey]['total_tax'] = $room['total_tax']; + } + + if (isset($room['unit_price_without_tax'])) { + $room['unit_price_without_tax'] = Tools::ps_round($room['unit_price_without_tax'], _PS_PRICE_COMPUTE_PRECISION_); + // need the id Room of the latest added room type + $this->wsFeaturePrices[] = $this->createFeaturePrice( + array( + 'id_product' => (int) $roomType['id_room_type'], + 'id_cart' => (int) $this->context->cart->id, + 'id_guest' => (int) $this->context->cart->id_guest, + 'date_from' => date('Y-m-d', strtotime($dateFrom)), + 'date_to' => date('Y-m-d', strtotime($dateTo)), + 'id_room' => $objCartBookingData->id_room, + 'price' => $room['unit_price_without_tax'] + ) + ); + } + } + } + } else { + $roomWiseOccupancy = $occupancy; + if (isset($roomType['number_of_rooms'])) { + while ($roomType['number_of_rooms'] > 1) { + $roomWiseOccupancy[] = reset($occupancy); + $roomType['number_of_rooms']--; + } + } + + if ($quantityWise) { + $roomWiseOccupancy = count($roomWiseOccupancy); + } + + $roomDemands = json_encode(array()); + $roomServices = array(); + $objHotelCartBookingData->updateCartBooking( + $roomType['id_room_type'], + $roomWiseOccupancy, + 'up', + $idHotel, + 0, + $dateFrom, + $dateTo, + $roomDemands, + $roomServices, + $this->context->cart->id, + $this->context->cart->id_guest + ); + } + } + + $this->removeAutoAddedServicesFromCart(); + } + + /** + * Creating new service product for the request. + */ + public function createWsService($service, $idRoomType) + { + // A single service will be created and will be deleted after the booking is completed. + $objProduct = new Product(); + foreach (Language::getLanguages(false) as $language) { + $objProduct->name[$language['id_lang']] = $service['name']; + $linkRewrite = Tools::link_rewrite($service['name']); + $objProduct->link_rewrite[$language['id_lang']] = $linkRewrite; + } + + $price = 0; + $quantity = 1; + if (isset($service['quantity'])) { + $quantity = $service['quantity']; + } + + if (isset($service['unit_price_without_tax'])) { + $price = $service['unit_price_without_tax']; + } else if (isset($service['total_price_without_tax'])) { + $price = $service['total_price_without_tax'] / $quantity; + } + + $objProduct->booking_product = false; + $objProduct->available_for_order = true; + $objProduct->id_category_default = Configuration::get('PS_SERVICE_CATEGORY'); + $objProduct->active = 1; + $objProduct->id_shop_default = Configuration::get('PS_SHOP_DEFAULT'); + $objProduct->indexed = false; + $objProduct->condition = 'new'; + $objProduct->price = $price; + $objProduct->out_of_stock = false; + $objProduct->id_tax_rules_group = 0; + $objProduct->is_virtual = 1; + $objProduct->show_price = true; + $objProduct->auto_add_to_cart = false; + $objProduct->show_at_front = false; + $objProduct->price_calculation_method = $service['price_mode']; + $objProduct->redirect_type = '404'; + $objProduct->visibility = 'none'; + $objProduct->minimal_quantity = 1; + $objProduct->service_product_type = Product::SERVICE_PRODUCT_WITH_ROOMTYPE; + $objProduct->allow_multiple_quantity = true; + $objProduct->max_quantity = $quantity; + if ($objProduct->save()) { + $objProduct->updateCategories(array( + Configuration::get('PS_SERVICE_CATEGORY') + )); + $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); + $objRoomTypeServiceProduct->addRoomProductLink( + $objProduct->id, + array($idRoomType), + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + ); + + StockAvailable::setQuantity($objProduct->id, 0, 99999999); + $this->wsIdServices[] = $objProduct->id; + } + + return $objProduct->id; + } + + /** + * Removing the non requested auto added services. + */ + public function removeAutoAddedServicesFromCart() + { + if (Validate::isLoadedobject($this->context->cart)) { + $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail(); + if ($serviceProducts = $objRoomTypeServiceProductCartDetail->getServiceProductsInCart( + $this->context->cart->id, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + null, + 1 // for auto added products + )) { + foreach ($serviceProducts as $serviceProduct) { + $dateRoomJoinKey = strtotime($serviceProduct['date_from']).strtotime($serviceProduct['date_to']).$serviceProduct['id_room']; + if (isset($serviceProduct['selected_products_info']) && $serviceProduct['selected_products_info']) { + foreach ($serviceProduct['selected_products_info'] as $service) { + // Checking if the auto add service was sent in the request + if (!isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]) + && ($idRoomTypeServiceProductCartDetail = $objRoomTypeServiceProductCartDetail->alreadyExists( + $service['id_product'], + $this->context->cart->id, + $serviceProduct['htl_cart_booking_id']) + )) { + $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail((int) $idRoomTypeServiceProductCartDetail); + $this->context->cart->updateQty($objRoomTypeServiceProductCartDetail->quantity, $objRoomTypeServiceProductCartDetail->id_product, null, false, 'down'); + $objRoomTypeServiceProductCartDetail->delete(); + } + } + } + } + } + } + } + + /** + * Processing the PUT request. + */ + public function handlePutRequest($params) + { + $this->processCurrency($params); + $this->processGuestDetails($params['customer_detail']); + $this->processCustomer($params['customer_detail']); + $objOrder = new Order((int) $params['id']); + $objHotelBookingDetail = new HotelBookingDetail(); + $objRoomType = new HotelRoomType(); + $roomsToRemove = array(); + $roomsToAdd = array(); + $roomsToUpdate = array(); + $roomTypes = $params['room_types']; + if ($roomsInOrder = $objHotelBookingDetail->getOrderCurrentDataByOrderId($objOrder->id)) { + foreach ($roomsInOrder as $orderRoomKey => $orderRoom) { + $dateProductJoinKey = $orderRoom['id_product'].'_'.strtotime($orderRoom['date_from']).strtotime($orderRoom['date_to']); + if (isset($roomTypes[$dateProductJoinKey])) { + if (isset($roomTypes[$dateProductJoinKey]['number_of_rooms'])) { + $room = array(); + // if there are multiple rooms then there can be a room at index 1 so we are adding a prifix for the below condition. + $room_key = 'r_'.$orderRoom['id_room']; + if (isset($roomTypes[$dateProductJoinKey]['rooms'][$room_key])) { + $room = $roomTypes[$dateProductJoinKey]['rooms'][$room_key]; + unset($roomTypes[$dateProductJoinKey]['rooms'][$room_key]); + } else if (isset($roomTypes[$dateProductJoinKey]['rooms'][0])) { + $room = array_shift($roomTypes[$dateProductJoinKey]['rooms']); + } + + if ($room) { + // update the room only if there is room wise breakdown. + $roomsToUpdate[$dateProductJoinKey]['requested'][$orderRoom['id_room']] = $room; + $roomsToUpdate[$dateProductJoinKey]['order'][$orderRoom['id_room']] = $roomsInOrder[$orderRoomKey]; + } else { + $roomsToUpdate[$dateProductJoinKey]['order'][$orderRoom['id_room']] = $roomsInOrder[$orderRoomKey]; + } + + if ($roomTypes[$dateProductJoinKey]['number_of_rooms'] > 1) { + // Since we are traversing the rooms from order one by one. + $roomTypes[$dateProductJoinKey]['number_of_rooms']--; + } else { + unset($roomTypes[$dateProductJoinKey]); + } + } + + unset($roomsInOrder[$orderRoomKey]); + } + } + + $roomsToRemove = $roomsInOrder; + } + + $roomsToAdd = $roomTypes; + if ($roomsToAdd && !$this->validateRequestedRoomTypes($roomsToAdd, $params['id_property'])) { + if ($this->error_msg == '') { + throw new WebserviceException( + Tools::displayError('Requested room(s) not available'), + array(404, 400) + ); + } + + return false; + } + + // Adding new rooms in the booking + if (count($roomsToAdd)) { + $this->addRoomsInOrder($objOrder->id, $roomsToAdd); + } + + // only perform any update if request is valid. + // Update the information for the services that were updated in the existing rooms + if (count($roomsToUpdate)) { + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + $objBookingDemand = new HotelBookingDemands(); + foreach ($roomsToUpdate as $roomsByDate) { + if (isset($roomsByDate['requested']) + && $roomsByDate['requested'] + ) { + foreach ($roomsByDate['requested'] as $roomsKey => $room) { + $idOrder = $roomsByDate['order'][$roomsKey]['id_order']; + $idProduct = $roomsByDate['order'][$roomsKey]['id_product']; + $idRoom = $roomsByDate['order'][$roomsKey]['id_room']; + $dateFrom = $roomsByDate['order'][$roomsKey]['date_from']; + $dateTo = $roomsByDate['order'][$roomsKey]['date_to']; + $dateRoomJoinKey = strtotime($dateFrom).strtotime($dateTo).$idRoom; + + if (isset($room['id_tax_rules_group']) && Validate::isLoadedObject(new TaxRulesGroup((int) $room['id_tax_rules_group']))) { + $this->wsRequestedRooms[$dateRoomJoinKey]['id_tax_rules_group'] = $room['id_tax_rules_group']; + } else if (isset($room['total_tax'])) { + $this->wsRequestedRooms[$dateRoomJoinKey]['total_tax'] = $room['total_tax']; + } + + $numDays = HotelHelper::getNumberOfDays($dateFrom, $dateTo); + if (isset($room['unit_price_without_tax']) + && ((float) $room['unit_price_without_tax']) != ((float) $roomsByDate['order'][$roomsKey]['total_price_tax_incl']/$numDays) + ) { + $this->updateRoomPriceInOrder($room, $roomsByDate['order'][$roomsKey]); + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['room'] = $room; + } + + $idHotelBookingDetail = $roomsByDate['order'][$roomsKey]['id']; + if ($existingServices = $objRoomTypeServiceProductOrderDetail->getSelectedServicesForRoom($idHotelBookingDetail)) { + if (!empty($existingServices['additional_services'])) { + $this->removeServicesFromOrderedRoom($existingServices['additional_services']); + } + } + + $this->addServicesInOrderedRoom($room['services'], $idHotelBookingDetail); + // Since we don't store the id_global_demand in the order, we will remove the previous ones and add the new ones. + $requestedDemands = $room['facilities']; + $roomExtraDemand = $objBookingDemand->getRoomTypeBookingExtraDemands( + $idOrder, + $idProduct, + $idRoom, + $dateFrom, + $dateTo, + 0 + ); + $this->removeDemandsInOrderedRoom($roomExtraDemand); + $this->addDemandsInOrderedRoom($requestedDemands, $idHotelBookingDetail); + } + } else if (isset($roomsByDate['order']) + && $roomsByDate['order'] + ) { + $hotelBookingDetail = array_shift($roomsByDate['order']); + if ($existingServices = $objRoomTypeServiceProductOrderDetail->getSelectedServicesForRoom($hotelBookingDetail['id'])) { + if (!empty($existingServices['additional_services'])) { + $this->removeServicesFromOrderedRoom($existingServices['additional_services']); + } + } + + } + } + } + + // removing in the last. + if (count($roomsToRemove)) { + $this->removeRoomLineFromOrder($params, $roomsToRemove); + } + + $this->updateRoomTaxRulesGroupsInOrder($objOrder->id); + if (isset($params['remark']) && $params['remark'] && !empty(trim($params['remark']))) { + $this->addCustomerMessage($params['id'], $params['remark']); + } + + $cartRules = $objOrder->getCartRules(); + //Removing the stored cached object + $this->removeOrderCartRules($objOrder->id, $cartRules); + if (isset($params['cart_rules']) && $params['cart_rules']) { + $this->addCartRulesToOrder($params['cart_rules'], $objOrder->id); + } + + // Calling this after the older cart rules are removed from the order. + $this->manageOrderPrice($objOrder->id, $params); + if (isset($params['booking_status'])) { + $objOrderState = false; + if ($params['booking_status'] == self::API_BOOKING_STATUS_CANCELLED) { + $objOrderState = new OrderState(Configuration::get('PS_OS_CANCELED')); + } else if ($params['booking_status'] == self::API_BOOKING_STATUS_REFUNDED) { + $objOrderState = new OrderState(Configuration::get('PS_OS_REFUND')); + } else if ($params['booking_status'] == self::API_BOOKING_STATUS_COMPLETED) { + $objOrderState = new OrderState(Configuration::get('PS_OS_PAYMENT_ACCEPTED')); + } else if ($params['booking_status'] == self::API_BOOKING_STATUS_NEW) { + $objOrderState = new OrderState(Configuration::get('PS_OS_AWAITING_PAYMENT')); + $paymentStatus = false; + if (isset($params['payment_status'])) { + $paymentStatus = $params['payment_status']; + } + + switch ($paymentStatus) { + case self::API_BOOKING_PAYMENT_STATUS_COMPLETED: + $objOrderState = new OrderState(Configuration::get('PS_OS_PAYMENT_ACCEPTED')); + break; + case self::API_BOOKING_PAYMENT_STATUS_PARTIAL: + $objOrderState = new OrderState(Configuration::get('PS_OS_PARTIAL_PAYMENT_ACCEPTED')); + break; + case self::API_BOOKING_PAYMENT_STATUS_AWATING: + $objOrderState = new OrderState(Configuration::get('PS_OS_AWAITING_PAYMENT')); + break; + default: + $objOrderState = new OrderState(Configuration::get('PS_OS_AWAITING_PAYMENT')); + break; + } + } + + if ($objOrderState) { + $this->addOrderHistory($params['id'], $objOrderState); + } + } + + if (isset($params['price_details']['total_paid']) && $objOrder->total_paid_real != $params['price_details']['total_paid']) { + $this->addOrderPayment($params); + } + + $this->getBookingDetails($objOrder->id); + } + + public function addRoomsInOrder($idOrder, $roomTypes) + { + $objOrder = new Order($idOrder); + $objHotelBookingDetail = new HotelBookingDetail(); + $objRoomType = new HotelRoomType(); + $this->createCartForOrder($objOrder->id); + $this->addRoomsInCart($roomTypes); + if ($objOrder->hasInvoice()) { + $objOrderInvoice = new OrderInvoice(); + // Create new invoice + // If we create a new invoice, we calculate shipping cost + $total_method = Cart::BOTH; + // Create Cart rule in order to make free shipping + + $objOrderInvoice->id_order = $objOrder->id; + if ($objOrderInvoice->number) { + Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $objOrder->id_shop); + } else { + $objOrderInvoice->number = Order::getLastInvoiceNumber() + 1; + } + + $invoice_address = new Address((int) $objOrder->{Configuration::get('PS_TAX_ADDRESS_TYPE', null, null, $objOrder->id_shop)}); + $carrier = new Carrier((int) $objOrder->id_carrier); + $tax_calculator = $carrier->getTaxCalculator($invoice_address); + + $objOrderInvoice->total_paid_tax_excl = Tools::ps_round($this->context->cart->getOrderTotal(false, $total_method), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_paid_tax_incl = Tools::ps_round($this->context->cart->getOrderTotal(true, $total_method), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_products = Tools::ps_round($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_products_wt = Tools::ps_round($this->context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_shipping_tax_excl = Tools::ps_round($this->context->cart->getTotalShippingCost(null, false), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_shipping_tax_incl = Tools::ps_round($this->context->cart->getTotalShippingCost(), _PS_PRICE_COMPUTE_PRECISION_); + + $objOrderInvoice->total_wrapping_tax_excl = Tools::ps_round($this->context->cart->getOrderTotal(false, Cart::ONLY_WRAPPING), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->total_wrapping_tax_incl = Tools::ps_round($this->context->cart->getOrderTotal(true, Cart::ONLY_WRAPPING), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderInvoice->shipping_tax_computation_method = (int)$tax_calculator->computation_method; + + // Update current order field, only shipping because other field is updated later + $objOrder->total_shipping += $objOrderInvoice->total_shipping_tax_incl; + $objOrder->total_shipping_tax_excl += $objOrderInvoice->total_shipping_tax_excl; + $objOrder->total_shipping_tax_incl += $objOrderInvoice->total_shipping_tax_incl; + + $objOrder->total_wrapping += Tools::ps_round($this->context->cart->getOrderTotal(true, Cart::ONLY_WRAPPING)); + $objOrder->total_wrapping_tax_excl += Tools::ps_round($this->context->cart->getOrderTotal(false, Cart::ONLY_WRAPPING)); + $objOrder->total_wrapping_tax_incl += Tools::ps_round($this->context->cart->getOrderTotal(true, Cart::ONLY_WRAPPING)); + $objOrderInvoice->add(); + + $objOrderInvoice->saveCarrierTaxCalculator($tax_calculator->getTaxesAmount($objOrderInvoice->total_shipping_tax_excl)); + + $order_carrier = new OrderCarrier(); + $order_carrier->id_order = (int)$objOrder->id; + $order_carrier->id_carrier = (int)$objOrder->id_carrier; + $order_carrier->id_order_invoice = Tools::ps_round($objOrderInvoice->id, _PS_PRICE_COMPUTE_PRECISION_); + $order_carrier->weight = Tools::ps_round($this->context->cart->getTotalWeight(), _PS_PRICE_COMPUTE_PRECISION_); + $order_carrier->shipping_cost_tax_excl = Tools::ps_round($objOrderInvoice->total_shipping_tax_excl, _PS_PRICE_COMPUTE_PRECISION_); + $order_carrier->shipping_cost_tax_incl = Tools::ps_round($objOrderInvoice->total_shipping_tax_incl, _PS_PRICE_COMPUTE_PRECISION_); + $order_carrier->add(); + } + + $objCart = $this->context->cart; + $objOrderDetail = new OrderDetail(); + $objOrderDetail->createList($objOrder, $objCart, $objOrder->getCurrentOrderState(), $objCart->getProducts(true), 0, true, 0); + + // update totals amount of order + // creating the new object to reload the data changes made while removing the rooms. + $objOrder = new Order($idOrder); + $objOrder->total_products += (float)$objCart->getOrderTotal(false, Cart::ONLY_ROOMS); + $objOrder->total_products_wt += (float)$objCart->getOrderTotal(true, Cart::ONLY_ROOMS); + $objOrder->total_paid += Tools::ps_round((float)($objCart->getOrderTotal(true, Cart::ONLY_ROOMS)), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_excl += Tools::ps_round((float)($objCart->getOrderTotal(false, Cart::ONLY_ROOMS)), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_incl += Tools::ps_round((float)($objCart->getOrderTotal(true, Cart::ONLY_ROOMS)), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_discounts += (float)abs($objCart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)); + $objOrder->total_discounts_tax_excl += (float)abs($objCart->getOrderTotal(false, Cart::ONLY_DISCOUNTS)); + $objOrder->total_discounts_tax_incl += (float)abs($objCart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)); + + // Save changes of order + $res = $objOrder->update(); + $objAddress = new Address((int) $objOrder->id_address_tax); + $idLang = (int) $this->context->cart->id_lang; + foreach ($roomTypes as $roomType) { + $orderDetails = $objHotelBookingDetail->getPsOrderDetailsByProduct($roomType['id_room_type'], $objOrder->id); + $IdOrderDetail = end($orderDetails)['id_order_detail']; // to get the max id_order_detail + $objCartBookingData = new HotelCartBookingData(); + if ($cartBookingData = $objCartBookingData->getOnlyCartBookingData( + $this->context->cart->id, + $this->context->cart->id_guest, + $roomType['id_room_type'] + )) { + foreach ($cartBookingData as $cb_k => $cb_v) { + $objCartBookingData = new HotelCartBookingData($cb_v['id']); + $objCartBookingData->id_order = $objOrder->id; + $objCartBookingData->save(); + $objBookingDetail = new HotelBookingDetail(); + $objBookingDetail->id_product = $roomType['id_room_type']; + $objBookingDetail->id_order = $objOrder->id; + $objBookingDetail->id_order_detail = $IdOrderDetail; + $objBookingDetail->id_cart = $this->context->cart->id; + $objBookingDetail->id_room = $objCartBookingData->id_room; + $objBookingDetail->id_hotel = $objCartBookingData->id_hotel; + $objBookingDetail->id_customer = $objOrder->id_customer; + $objBookingDetail->booking_type = $objCartBookingData->booking_type; + $objBookingDetail->id_status = 1; + $objBookingDetail->comment = $objCartBookingData->comment; + $objBookingDetail->room_type_name = Product::getProductName($roomType['id_room_type'], null, $objOrder->id_lang); + + $objBookingDetail->date_from = $objCartBookingData->date_from; + $objBookingDetail->date_to = $objCartBookingData->date_to; + $objBookingDetail->adults = $objCartBookingData->adults; + $objBookingDetail->children = $objCartBookingData->children; + $objBookingDetail->child_ages = $objCartBookingData->child_ages; + + $total_price = HotelRoomTypeFeaturePricing::getRoomTypeTotalPrice( + $roomType['id_room_type'], + $objCartBookingData->date_from, + $objCartBookingData->date_to, + 0, + Group::getCurrent()->id, + $this->context->cart->id, + $this->context->cart->id_guest, + $objCartBookingData->id_room, + 0 + ); + $objBookingDetail->total_price_tax_excl = $total_price['total_price_tax_excl']; + $objBookingDetail->total_price_tax_incl = $total_price['total_price_tax_incl']; + $objBookingDetail->total_paid_amount = Tools::ps_round($total_price['total_price_tax_incl'], _PS_PRICE_COMPUTE_PRECISION_); + + // Save hotel information/location/contact + if (Validate::isLoadedObject($objRoom = new HotelRoomInformation($objCartBookingData->id_room))) { + $objBookingDetail->room_num = $objRoom->room_num; + } + + if (Validate::isLoadedObject($objHotelBranch = new HotelBranchInformation( + $objCartBookingData->id_hotel, + $idLang + ))) { + $addressInfo = $objHotelBranch->getAddress($objCartBookingData->id_hotel); + $objBookingDetail->hotel_name = $objHotelBranch->hotel_name; + $objBookingDetail->city = $addressInfo['city']; + $objBookingDetail->state = State::getNameById($addressInfo['id_state']); + $objBookingDetail->country = Country::getNameById($idLang, $addressInfo['id_country']); + $objBookingDetail->zipcode = $addressInfo['postcode'];; + $objBookingDetail->phone = $addressInfo['phone']; + $objBookingDetail->email = $objHotelBranch->email; + $objBookingDetail->check_in_time = $objHotelBranch->check_in; + $objBookingDetail->check_out_time = $objHotelBranch->check_out; + } + + if ($roomTypeInfo = $objRoomType->getRoomTypeInfoByIdProduct($roomType['id_room_type'])) { + $objBookingDetail->adults = $objCartBookingData->adults; + $objBookingDetail->children = $objCartBookingData->children; + $objBookingDetail->child_ages = $objCartBookingData->child_ages; + } + + if ($objBookingDetail->save()) { + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail(); + $dateRoomJoinKey = strtotime($objCartBookingData->date_from).strtotime($objCartBookingData->date_to).$objCartBookingData->id_room; + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services']) + && ($services = $objRoomTypeServiceProductCartDetail->getRoomServiceProducts($objCartBookingData->id)) + ) { + foreach ($services as $service) { + $insertedServiceProductIdOrderDetail = $objBookingDetail->getLastInsertedServiceIdOrderDetail($objOrder->id, $service['id_product']); + $numDays = 1; + if (Product::getProductPriceCalculation($service['id_product']) == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $numDays = HotelHelper::getNumberOfDays($objBookingDetail->date_from, $objBookingDetail->date_to); + } + + $totalPriceTaxExcl = $objRoomTypeServiceProductPrice->getServicePrice( + $service['id_product'], + $roomTypeInfo['id'], + $service['quantity'], + $objBookingDetail->date_from, + $objBookingDetail->date_to, + false + ); + $totalPriceTaxIncl = $objRoomTypeServiceProductPrice->getServicePrice( + $service['id_product'], + $roomTypeInfo['id'], + $service['quantity'], + $objBookingDetail->date_from, + $objBookingDetail->date_to, + true + ); + $unitPriceTaxExcl = $totalPriceTaxExcl / ($numDays * $service['quantity']); + $unitPriceTaxIncl = $totalPriceTaxIncl / ($numDays * $service['quantity']); + if ($unitPriceTaxExcl > 0) { + $taxMultiplier = $unitPriceTaxIncl / $unitPriceTaxExcl; + } else { + $taxMultiplier = 1; + } + + $quantity = $service['quantity'] * $numDays; + $objOrderDetail = new OrderDetail($insertedServiceProductIdOrderDetail); + $totalPriceTaxExclOld = $totalPriceTaxExcl; + $totalPriceTaxInclOld = $totalPriceTaxIncl; + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]) + && isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax']) + ) { + $totalPriceTaxExcl = 0; + $totalPriceTaxIncl = 0; + if ((int) $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax']) { + $totalPriceTaxExcl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax']; + $totalPriceTaxIncl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['unit_price_without_tax'] * $taxMultiplier; + } + + if ($totalPriceTaxExcl > 0) { + $unitPriceTaxExcl = $totalPriceTaxExcl / $quantity; + $unitPriceTaxIncl = $totalPriceTaxIncl / $quantity; + } + } else if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_price_without_tax'])) { + $totalPriceTaxExcl = $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_price_without_tax']; + $totalPriceTaxIncl = $totalPriceTaxExcl * $taxMultiplier; + + $unitPriceTaxExcl = $totalPriceTaxExcl / $quantity; + $unitPriceTaxIncl = $totalPriceTaxIncl / $quantity; + } + + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['id_tax_rules_group'])) { + $objAddress = new Address((int) $objOrder->id_address_tax); + $objTaxManager = TaxManagerFactory::getManager($objAddress, $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['id_tax_rules_group']); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + $unitPriceTaxIncl = $objTaxCalculator->addTaxes($unitPriceTaxExcl); + + $unitPriceTaxIncl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $totalPriceTaxIncl = Tools::ps_round(($unitPriceTaxIncl * $quantity), _PS_PRICE_COMPUTE_PRECISION_); + } else if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_tax'])) { + $totalPriceTaxIncl = $totalPriceTaxExcl + $this->wsRequestedRoomTypes[$dateRoomJoinKey]['services'][$service['id_product']]['total_tax']; + $totalPriceTaxIncl = Tools::ps_round(($totalPriceTaxIncl), _PS_PRICE_COMPUTE_PRECISION_); + + $unitPriceTaxIncl = Tools::ps_round($totalPriceTaxIncl / $quantity, _PS_PRICE_COMPUTE_PRECISION_); + } + + $priceDiffTaxExcl = $totalPriceTaxExcl - $totalPriceTaxExclOld; + $priceDiffTaxIncl = $totalPriceTaxIncl - $totalPriceTaxInclOld; + $objOrderDetail->total_price_tax_excl += Tools::ps_round($priceDiffTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrderDetail->total_price_tax_incl += Tools::ps_round($priceDiffTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + + $objOrderDetail->unit_price_tax_excl = Tools::ps_round(($objOrderDetail->total_price_tax_excl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderDetail->unit_price_tax_incl = Tools::ps_round(($objOrderDetail->total_price_tax_incl / $objOrderDetail->product_quantity), _PS_PRICE_COMPUTE_PRECISION_); + $objOrderDetail->save(); + + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + $objRoomTypeServiceProductOrderDetail->id_product = $service['id_product']; + $objRoomTypeServiceProductOrderDetail->id_order = $objBookingDetail->id_order; + $objRoomTypeServiceProductOrderDetail->id_order_detail = $insertedServiceProductIdOrderDetail; + $objRoomTypeServiceProductOrderDetail->id_cart = $this->context->cart->id; + $objRoomTypeServiceProductOrderDetail->id_htl_booking_detail = $objBookingDetail->id; + $objRoomTypeServiceProductOrderDetail->unit_price_tax_excl = Tools::ps_round($unitPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_excl = Tools::ps_round($totalPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->name = $service['name']; + $objRoomTypeServiceProductOrderDetail->quantity = $service['quantity']; + $objRoomTypeServiceProductOrderDetail->save(); + + $objOrder->total_products += $objRoomTypeServiceProductOrderDetail->total_price_tax_excl; + $objOrder->total_products_wt += $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + + $objOrder->total_paid += Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_excl += Tools::ps_round($totalPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_incl += Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + } + + $objOrder->save(); + } + + if (isset($this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands']) && $this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands']) { + if ($demands = json_decode($this->wsRequestedRoomTypes[$dateRoomJoinKey]['demands'], true)) { + $this->addDemandsInOrderedRoom($demands, $objBookingDetail->id); + } + } + } + } + } + } + + $this->deleteWsFeaturePrices(); + HotelRoomTypeFeaturePricing::deleteByIdCart($this->context->cart->id); + } + + /** + * Removing the cart rules from the order. + */ + public function removeOrderCartRules($idOrder, $cartRules = array()) + { + if ($cartRules) { + $objOrder = new Order($idOrder); + foreach ($cartRules as $rule) { + $objOrderCartRule = new OrderCartRule($rule['id_order_cart_rule']); + if ($objOrderCartRule->id_order_invoice) { + $objOrderInvoice = new OrderInvoice($objOrderCartRule->id_order_invoice); + $objOrderInvoice->total_discount_tax_excl = ($objOrderInvoice->total_discount_tax_excl - $objOrderCartRule->value_tax_excl) > 0 ? ($objOrderInvoice->total_discount_tax_excl - $objOrderCartRule->value_tax_excl) : 0; + $objOrderInvoice->total_discount_tax_incl = ($objOrderInvoice->total_discount_tax_incl - $objOrderCartRule->value) > 0 ? ($objOrderInvoice->total_discount_tax_incl - $objOrderCartRule->value) : 0; + + $objOrderInvoice->total_paid_tax_excl += $objOrderCartRule->value_tax_excl; + $objOrderInvoice->total_paid_tax_incl += $objOrderCartRule->value; + + // Update Order Invoice + $objOrderInvoice->update(); + } + + $objOrder->total_discounts = ($objOrder->total_discounts - $objOrderCartRule->value) > 0 ? ($objOrder->total_discounts - $objOrderCartRule->value) : 0; + $objOrder->total_discounts_tax_incl = ($objOrder->total_discounts_tax_incl - $objOrderCartRule->value) > 0 ? ($objOrder->total_discounts_tax_incl - $objOrderCartRule->value) : 0; + $objOrder->total_discounts_tax_excl = ($objOrder->total_discounts_tax_excl - $objOrderCartRule->value_tax_excl) > 0 ? ($objOrder->total_discounts_tax_excl - $objOrderCartRule->value_tax_excl) : 0; + + $objOrder->total_paid += $objOrderCartRule->value; + $objOrder->total_paid_tax_incl += $objOrderCartRule->value; + $objOrder->total_paid_tax_excl += $objOrderCartRule->value_tax_excl; + + // Delete Order Cart Rule and update Order + $objOrderCartRule->delete(); + } + + $objOrder->update(); + } + } + + /** + * Adding the cart rules in to the order. + */ + public function addCartRulesToOrder($cartRules, $idOrder) + { + $objOrder = new Order((int) $idOrder); + $cartRulesFormatted = array(); + foreach ($cartRules as $key => $cartRule) { + $cartRulesFormatted[$key]['code'] = $cartRule['code']; + $cartRulesFormatted[$key]['value'] = $cartRule['value']; + $cartRulesFormatted[$key]['type'] = $cartRule['type']; + $cartRulesFormatted[$key]['currency'] = isset($cartRule['currency']) ? $cartRule['currency'] : ''; + } + + if ($idCartRules = $this->createCartRules($cartRulesFormatted)) { + foreach ($idCartRules as $idCartRule) { + $objCartRule = new CartRule($idCartRule); + $invoiceCartRules = array(); + if ($invoiceCollection = $objOrder->getInvoicesCollection()) { + foreach ($invoiceCollection as $orderInvoice) { + if ((int) $objCartRule->reduction_amount) { + if (!($objCartRule->reduction_amount > $orderInvoice->total_paid_tax_incl)) { + $this->error_msg = Tools::displayError('The discount value is greater than the order invoice total.').$orderInvoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$objOrder->id_shop).')'; + $invoiceCartRules[$orderInvoice->id]['value_tax_incl'] = Tools::ps_round($objCartRule->reduction_amount, _PS_PRICE_COMPUTE_PRECISION_); + $invoiceCartRules[$orderInvoice->id]['value_tax_excl'] = Tools::ps_round($objCartRule->reduction_amount / (1 + ($objOrder->getTaxesAverageUsed() / 100)), _PS_PRICE_COMPUTE_PRECISION_); + + // Update OrderInvoice + $this->applyDiscountOnInvoice($orderInvoice, $invoiceCartRules[$orderInvoice->id]['value_tax_incl'], $invoiceCartRules[$orderInvoice->id]['value_tax_excl']); + } + } else if ($objCartRule->reduction_percent) { + $invoiceCartRules[$orderInvoice->id]['value_tax_incl'] = Tools::ps_round($objOrder->total_paid_tax_incl * $objCartRule->reduction_percent / 100, _PS_PRICE_COMPUTE_PRECISION_); + $invoiceCartRules[$orderInvoice->id]['value_tax_excl'] = Tools::ps_round($objOrder->total_paid_tax_excl * $objCartRule->reduction_percent / 100, _PS_PRICE_COMPUTE_PRECISION_); + + $this->applyDiscountOnInvoice($orderInvoice, $invoiceCartRules[$orderInvoice->id]['value_tax_incl'], $invoiceCartRules[$orderInvoice->id]['value_tax_excl']); + } + } + } else { + if ($objCartRule->reduction_percent) { + $invoiceCartRules[0]['value_tax_incl'] = Tools::ps_round($objOrder->total_paid_tax_incl * $objCartRule->reduction_percent / 100, _PS_PRICE_COMPUTE_PRECISION_); + $invoiceCartRules[0]['value_tax_excl'] = Tools::ps_round($objOrder->total_paid_tax_excl * $objCartRule->reduction_percent / 100, _PS_PRICE_COMPUTE_PRECISION_); + } else if ($objCartRule->reduction_amount) { + $invoiceCartRules[0]['value_tax_incl'] = Tools::ps_round($objCartRule->reduction_amount, _PS_PRICE_COMPUTE_PRECISION_); + $invoiceCartRules[0]['value_tax_excl'] = Tools::ps_round($objCartRule->reduction_amount / (1 + ($objOrder->getTaxesAverageUsed() / 100)), _PS_PRICE_COMPUTE_PRECISION_); + } + } + + // Create OrderCartRule + foreach ($invoiceCartRules as $idInvoice => $rule) { + $ObjOrderCartRule = new OrderCartRule(); + $ObjOrderCartRule->id_order = $objOrder->id; + $ObjOrderCartRule->id_cart_rule = $objCartRule->id; + $ObjOrderCartRule->id_order_invoice = $idInvoice; + $ObjOrderCartRule->name = $objCartRule->code; + $ObjOrderCartRule->value = $rule['value_tax_incl']; + $ObjOrderCartRule->value_tax_excl = $rule['value_tax_excl']; + $ObjOrderCartRule->free_shipping = 0; + $ObjOrderCartRule->add(); + + $objOrder->total_discounts = Tools::ps_round($objOrder->total_discounts + $ObjOrderCartRule->value, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_discounts_tax_incl = Tools::ps_round($objOrder->total_discounts_tax_incl + $ObjOrderCartRule->value, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_discounts_tax_excl = Tools::ps_round($objOrder->total_discounts_tax_excl + $ObjOrderCartRule->value_tax_excl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid = Tools::ps_round($objOrder->total_paid - $ObjOrderCartRule->value, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_incl = Tools::ps_round($objOrder->total_paid_tax_incl - $ObjOrderCartRule->value, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_excl = Tools::ps_round($objOrder->total_paid_tax_excl - $ObjOrderCartRule->value_tax_excl, _PS_PRICE_COMPUTE_PRECISION_); + } + } + } + + $objOrder->update(); + } + + protected function applyDiscountOnInvoice($objOrderInvoice, $valueTaxIncl, $valueTaxExcl) + { + // Update OrderInvoice + $objOrderInvoice->total_discount_tax_incl += $valueTaxIncl; + $objOrderInvoice->total_discount_tax_excl += $valueTaxExcl; + $objOrderInvoice->total_paid_tax_incl -= $valueTaxIncl; + $objOrderInvoice->total_paid_tax_excl -= $valueTaxExcl; + $objOrderInvoice->update(); + } + + /** + * Removing room from the order. + */ + public function removeRoomLineFromOrder($params, $roomsToRemove) + { + $objOrder = new Order((int) $params['id']); + $objBookingDemand = new HotelBookingDemands(); + $objHotelBookingDetail = new HotelBookingDetail(); + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + foreach ($roomsToRemove as $roomType) { + $dateFrom = $roomType['date_from']; + $dateTo = $roomType['date_to']; + $quantity = (int) HotelHelper::getNumberOfDays($dateFrom, $dateTo); + $objOrderDetail = new OrderDetail((int) $roomType['id_order_detail']); + $idHotelBooking = $roomType['id']; + $idHotel = $roomType['id_hotel']; + $bookingPriceTaxIncl = $roomType['total_price_tax_incl']; + $bookingPriceTaxExcl = $roomType['total_price_tax_excl']; + $roomExtraDemandTI = $objBookingDemand->getRoomTypeBookingExtraDemands( + $objOrder->id, + $roomType['id_product'], + $roomType['id_room'], + $dateFrom, + $dateTo, + 0, + 1, + 1 + ); + $roomExtraDemandTE = $objBookingDemand->getRoomTypeBookingExtraDemands( + $objOrder->id, + $roomType['id_product'], + $roomType['id_room'], + $dateFrom, + $dateTo, + 0, + 1, + 0 + ); + $additionlServicesTI = $objRoomTypeServiceProductOrderDetail->getSelectedServicesForRoom( + $idHotelBooking, + 1, + 1 + ); + $additionlServicesTE = $objRoomTypeServiceProductOrderDetail->getSelectedServicesForRoom( + $idHotelBooking, + 1, + 0 + ); + $selectedAdditonalServices = $objRoomTypeServiceProductOrderDetail->getSelectedServicesForRoom( + $idHotelBooking + ); + $diffProductsTaxIncl = $bookingPriceTaxIncl; + $diffProductsTaxExcl = $bookingPriceTaxExcl; + $objHotelBookingDetail = new HotelBookingDetail((int) $idHotelBooking); + $roomQuantity = (int) HotelHelper::getNumberOfDays($dateFrom, $dateTo); + if (isset($selectedAdditonalServices['additional_services']) + && count($selectedAdditonalServices['additional_services']) + ) { + foreach ($selectedAdditonalServices['additional_services'] as $service) { + $serviceOrderDetail = new OrderDetail($service['id_order_detail']); + $cart_quantity = $service['quantity']; + if ($service['product_price_calculation_method'] == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $cart_quantity = $cart_quantity * $quantity; + } + + if ($cart_quantity >= $serviceOrderDetail->product_quantity) { + $serviceOrderDetail->delete(); + } else { + $serviceOrderDetail->total_price_tax_incl -= Tools::ps_round($service['total_price_tax_incl'], _PS_PRICE_COMPUTE_PRECISION_); + $serviceOrderDetail->total_price_tax_excl -= Tools::ps_round($service['total_price_tax_excl'], _PS_PRICE_COMPUTE_PRECISION_); + $serviceOrderDetail->product_quantity -= $cart_quantity; + + // update taxes + $serviceOrderDetail->updateTaxAmount($objOrder); + + // Save order detail + $serviceOrderDetail->update(); + } + } + } + + // Update Order + // values changes as values are calculated accoding to the quantity of the product by webkul + $objOrder->total_paid = Tools::ps_round($objOrder->total_paid - ($diffProductsTaxIncl + $roomExtraDemandTI + $additionlServicesTI), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_incl = Tools::ps_round($objOrder->total_paid_tax_incl - ($diffProductsTaxIncl + $roomExtraDemandTI + $additionlServicesTI), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_excl = Tools::ps_round($objOrder->total_paid_tax_excl - ($diffProductsTaxExcl + $roomExtraDemandTE + $additionlServicesTE), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_products = Tools::ps_round($objOrder->total_products - ($diffProductsTaxExcl + $additionlServicesTE), _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_products_wt = Tools::ps_round($objOrder->total_products_wt - ($diffProductsTaxIncl + $additionlServicesTI), _PS_PRICE_COMPUTE_PRECISION_); + + if ($roomQuantity >= $objOrderDetail->product_quantity) { + $objOrderDetail->delete(); + } else { + $objOrderDetail->total_price_tax_incl -= $diffProductsTaxIncl; + $objOrderDetail->total_price_tax_excl -= $bookingPriceTaxExcl; + $oldRoomQuantity = $objOrderDetail->product_quantity; + $objOrderDetail->product_quantity = $oldRoomQuantity - $roomQuantity; + $objOrderDetail->reduction_percent = 0; + // update taxes + $objOrderDetail->updateTaxAmount($objOrder); + // Save order detail + $objOrderDetail->update(); + } + + $objOrder->update(); + // delete the demands of this booking + $objBookingDemand->deleteBookingDemands($idHotelBooking); + $objRoomTypeServiceProductOrderDetail->deleteRoomSevices($idHotelBooking); + $objHotelCartBookingData = new HotelCartBookingData(); + $objHotelCartBookingData->deleteOrderedRoomFromCart( + $objOrder->id, + $idHotel, + $roomType['id_room'], + $dateFrom, + $dateTo + ); + $objHotelBookingDetail = new HotelBookingDetail(); + $objHotelBookingDetail->deleteOrderedRoomFromOrder( + $objOrder->id, + $idHotel, + $roomType['id_room'], + $dateFrom, + $dateTo + ); + $objCart = new Cart($roomType['id_cart']); + $objCart->updateQty($quantity, $roomType['id_product'], null, false, 'down', 0, null, true); + } + } + + /** + * Adding history to the order. + */ + public function addOrderHistory($idOrder, $orderStatus) + { + $objOrder = new Order((int) $idOrder); + $currentOrderStatus = $objOrder->getCurrentOrderState(); + if ($currentOrderStatus->id != $orderStatus->id) { + $objOrderHistory = new OrderHistory(); + $objOrderHistory->id_order = $objOrder->id; + $useExistingsPayment = false; + if (!$objOrder->hasInvoice()) { + $useExistingsPayment = true; + } + + $objOrderHistory->changeIdOrderState((int)$orderStatus->id, $objOrder, $useExistingsPayment); + $objOrderHistory->add(true, array()); + } + } + + /** + * Adding new custmer messages. + */ + public function addCustomerMessage($idOrder, $remark) + { + $objOrder = new Order((int) $idOrder); + $objMessage = new Message(); + $message = strip_tags($remark, '
'); + $saveMessage = true; + $idCart = Cart::getCartIdByOrderId($objOrder->id); + if ($customerMessages = Message::getMessagesByOrderId($objOrder->id, true)) { + foreach ($customerMessages as $customerMessage) { + if ($customerMessage['message'] == $message) { + $saveMessage = false; + } + } + } + + if (Validate::isCleanHtml($message) && $saveMessage) { + $objMessage->message = $message; + $objMessage->id_cart = (int) $idCart; + $objMessage->id_customer = (int) ($objOrder->id_customer); + $objMessage->id_order = (int) $objOrder->id; + $objMessage->private = 1; + $objMessage->add(); + } + } + + /** + * Adding order payment from PUT requests. + */ + public function addOrderPayment($params) + { + if (isset($params['payment_detail']) && $params['payment_detail'] + && isset($params['price_details']['total_paid']) && $params['price_details']['total_paid'] + ) { + $objOrder = new Order($params['id']); + $amount = $params['price_details']['total_paid'] - $objOrder->total_paid_real; + $paymentMethod = null; + if (isset($params['payment_details']['payment_method']) && $params['payment_details']['payment_method']) { + $paymentMethod = $params['payment_details']['payment_method']; + } + + $transactionId = null; + if (isset($params['payment_details']['transaction_id']) && $params['payment_details']['transaction_id']) { + $transactionId = $params['payment_details']['transaction_id']; + } + + $paymentType = OrderPayment::PAYMENT_TYPE_ONLINE; + if (isset($params['payment_details']['payment_type']) && $params['payment_details']['payment_type']) { + $paymentType = $params['payment_details']['payment_type']; + } + + $idCurrency = $objOrder->id_currency; + $paymentCurrency = null; + if (isset($params['currency']) + && ($selectedCurrency = Currency::getIdByIsoCode($params['currency'])) + ) { + $objCurrency = new Currency($selectedCurrency); + if ($objCurrency->active) { + $idCurrency = $selectedCurrency; + } + } + + $newCurrency = null; + if ($idCurrency != $objOrder->id_currency) { + $newCurrency = new Currency($idCurrency); + } + + $orderInvoice = null; + if ($invoice = $objOrder->hasInvoice()) { + $orderInvoice = new OrderInvoice((int) $invoice); + } + + if ($amount) { + $objOrder->addOrderPayment( + $amount, + $paymentMethod, + $transactionId, + $newCurrency, + null, + $orderInvoice, + $paymentType + ); + } + + } + } + + /** + * Since we have to create new cart to add rooms and services in the order. + * So, we are creating new cart. + */ + public function createCartForOrder($idOrder) + { + $objOrder = new Order((int) $idOrder); + $objCart = new Cart(); + $objCart->id_shop_group = $objOrder->id_shop_group; + $objCart->id_shop = $objOrder->id_shop; + $objCart->id_customer = $objOrder->id_customer; + $objCart->id_carrier = $objOrder->id_carrier; + $objCart->id_address_delivery = $objOrder->id_address_delivery; + $objCart->id_address_invoice = $objOrder->id_address_invoice; + $objCart->id_currency = $objOrder->id_currency; + $objCart->id_lang = $objOrder->id_lang; + $objCart->secure_key = $objOrder->secure_key; + $objCart->id_guest = Guest::getFromCustomer((int) $objOrder->id_customer); + // Save new cart + $objCart->add(); + + // Save context (in order to apply cart rule) + $this->context->cart = $objCart; + $this->context->customer = new Customer((int) $objOrder->id_customer); + } + + /** + * Validating the PUT request. + */ + public function validatePutRequest($params) + { + $objCustomer = new Customer(); + $this->error_msg = ''; + if (!isset($params['id']) || !$params['id']) { + $this->error_msg = Tools::displayError('id is required with PUT requests.'); + } else if (!Validate::isLoadedObject(new Order($params['id']))) { + $this->error_msg = Tools::displayError('Booking not found.'); + } else if (isset($params['booking_status']) && ($params['booking_status'] < 0 || $params['booking_status'] > 4)) { + $this->error_msg = Tools::displayError('Invalid booking status.'); + } else if (!isset($params['room_types']) + || !count($params['room_types']) + ) { + $this->error_msg = Tools::displayError('Rooms not found in the request.'); + } else if (isset($params['customer_detail']['id_customer']) + && !Validate::isLoadedObject(new Customer($params['customer_detail']['id_customer'])) + ) { + $this->error_msg = Tools::displayError('Invalid ID customer.'); + } else if (!isset($params['id_property']) + || !Validate::isLoadedObject(new HotelBranchInformation((int) $params['id_property'])) + ) { + $this->error_msg = Tools::displayError('Please provide a valid id_property.'); + } else if (!isset($params['customer_detail']['email']) + || !Validate::isEmail($params['customer_detail']['email']) + || !$objCustomer->getByEmail($params['customer_detail']['email']) + ) { + $this->error_msg = Tools::displayError('Customer not found.'); + } else if (isset($params['booking_status']) + && ($params['booking_status'] < self::API_BOOKING_STATUS_NEW || $params['booking_status'] > self::API_BOOKING_STATUS_REFUNDED) + ) { + $this->error_msg = Tools::displayError('Invalid booking status.'); + } else if (!$this->validatePutRequestRoomTypes($params['room_types']) + && $this->error_msg == '' + ) { + $this->error_msg = Tools::displayError('Requested room(s) not available'); + } else if (isset($params['cart_rules']) && $params['cart_rules']) { + if (is_array($params['cart_rules'])) { + $this->validateCartRules($params['cart_rules']); + } else { + $this->error_msg = Tools::displayError('Invalid cart rules.'); + } + } + + if (!$this->error_msg && $this->error_msg == '') { + return true; + } + + return false; + } + + /** + * Validating the PUT request Room types. + */ + public function validatePutRequestRoomTypes($roomTypes) + { + foreach ($roomTypes as $roomType) { + if ($this->validateRoomType($roomType)) { + if (isset($roomType['rooms']) && count($roomType['rooms'])) { + foreach ($roomType['rooms'] as $room) { + if (!isset($room['id_room'])) { + if ($roomType['number_of_rooms']) { + $roomType['number_of_rooms']--; + } else { + return false; + } + } else if (isset($room['id_room']) + && (!Validate::isLoadedObject($objHotelRoomInfomation = new HotelRoomInformation($room['id_room'])) + || $objHotelRoomInfomation->id_product != $roomType['id_room_type'] + ) + ) { + $this->error_msg = Tools::displayError('Invalid Id room.'); + return false; + } + + if (isset($room['services']) && $room['services'] + && !$this->validateRequestedServices($room['services'], $roomType['id_room_type']) + ) { + return false; + } + + if (isset($room['facilities']) && $room['facilities'] + && !$this->validateRequestedDemands($room['facilities'], $roomType['id_room_type']) + ) { + return false; + } + + if (isset($room['id_tax_rules_group']) && !Validate::isLoadedObject(new TaxRulesGroup((int) $room['id_tax_rules_group']))) { + $this->error_msg = Tools::displayError('Invalid id_tax_rules_group.'); + return false; + } else if (isset($room['total_tax']) && !Validate::isPrice($room['total_tax'])) { + $this->error_msg = Tools::displayError('Invalid total tax.'); + return false; + } + } + } + } else { + return false; + } + } + + return true; + } + + /** + * Validating the Room types fields. + */ + public function validateRoomType($roomType) + { + $status = true; + if (!isset($roomType['id_room_type'])){ + $this->error_msg = Tools::displayError('Id room type is missing'); + $status = false; + } else if (!isset($roomType['checkin_date']) || !Validate::isDate($roomType['checkin_date'])) { + $this->error_msg = Tools::displayError('Invalid check in date in the request'); + $status = false; + } else if (!isset($roomType['checkout_date']) || !Validate::isDate($roomType['checkout_date'])) { + $this->error_msg = Tools::displayError('Invalid check out date in the request'); + $status = false; + } else if (!isset($roomType['number_of_rooms'])) { + $this->error_msg = Tools::displayError('number of rooms is missing'); + $status = false; + } else if (!Validate::isLoadedObject(new Product((int) $roomType['id_room_type'])) + || !Product::isBookingProduct((int) $roomType['id_room_type']) + ) { + $this->error_msg = Tools::displayError('Invalid room type in the request'); + $status = false; + } else if (isset($roomType['rooms']) && count($roomType['rooms']) && count($roomType['rooms']) != $roomType['number_of_rooms']) { + $this->error_msg = Tools::displayError('Room count does not matches with the number of rooms'); + $status = false; + } else if (strtotime($roomType['checkin_date']) > strtotime($roomType['checkout_date'])) { + $this->error_msg = Tools::displayError('Invalid check in and check out dates in the request'); + $status = false; + } + + return $status; + } + + /** + * Creating feature price for the room. + * Also they will be deleted after the booking has been created or updated. + */ + public function createFeaturePrice($params) + { + $feature_price_name = array(); + foreach (Language::getIDs(true) as $idLang) { + $feature_price_name[$idLang] = 'Api-Booking-Price'; + } + + $numDays = HotelHelper::getNumberOfDays($params['date_from'], $params['date_to']); + if (!$numDays) { + $numDays = 1; + } + + $objRoomTypeFeaturePricing = new HotelRoomTypeFeaturePricing(); + $objRoomTypeFeaturePricing->id_product = (int) $params['id_product']; + $objRoomTypeFeaturePricing->id_cart = (int) $params['id_cart']; + $objRoomTypeFeaturePricing->id_guest = (int) $params['id_guest']; + $objRoomTypeFeaturePricing->id_room = (int) $params['id_room']; + $objRoomTypeFeaturePricing->feature_price_name = $feature_price_name; + $objRoomTypeFeaturePricing->date_selection_type = HotelRoomTypeFeaturePricing::DATE_SELECTION_TYPE_RANGE; + $objRoomTypeFeaturePricing->date_from = date('Y-m-d', strtotime($params['date_from'])); + $objRoomTypeFeaturePricing->date_to = date('Y-m-d', strtotime($params['date_to'])); + $objRoomTypeFeaturePricing->is_special_days_exists = 0; + $objRoomTypeFeaturePricing->special_days = json_encode(false); + $objRoomTypeFeaturePricing->impact_way = HotelRoomTypeFeaturePricing::IMPACT_WAY_FIX_PRICE; + $objRoomTypeFeaturePricing->impact_type = HotelRoomTypeFeaturePricing::IMPACT_TYPE_FIXED_PRICE; + $objRoomTypeFeaturePricing->impact_value = $params['price']/$numDays; + $objRoomTypeFeaturePricing->active = 1; + $objRoomTypeFeaturePricing->groupBox = array_column(Group::getGroups(Configuration::get('PS_LANG_DEFAULT')), 'id_group'); + + if ($objRoomTypeFeaturePricing->add()) { + return $objRoomTypeFeaturePricing->id; + } + + return false; + } + + /** + * Deleting the newly created services. + */ + public function deleteWsServices() + { + if (isset($this->wsIdServices) && $this->wsIdServices) { + foreach ($this->wsIdServices as $wsIdService) { + // To filter false ids + if ((int) $wsIdService) { + $objProduct = new Product($wsIdService); + $objProduct->delete(); + } + } + } + } + + /** + * Deleting the newly created feature price. + */ + public function deleteWsFeaturePrices() + { + if (isset($this->wsFeaturePrices) && $this->wsFeaturePrices) { + foreach ($this->wsFeaturePrices as $idFeaturePrice) { + // To filter false ids + if ((int) $idFeaturePrice) { + $objFeaturePrice = new HotelRoomTypeFeaturePricing((int) $idFeaturePrice); + $objFeaturePrice->delete(); + } + } + } + } + + /** + * Deleting the newly created cart Rules. + */ + public function deleteWsCartRules() + { + if (isset($this->wsCartRules) && $this->wsCartRules) { + foreach ($this->wsCartRules as $idCartRule) { + $objCartRule = new CartRule((int) $idCartRule); + if (Validate::isLoadedobject($objCartRule)) { + $objCartRule->delete(); + } + } + } + } + + /** + * Updating the room price. + */ + public function updateRoomPriceInOrder($room, $bookingData) + { + $idHotelBooking = $bookingData['id']; + if (Validate::isLoadedObject($objHotelBookingDetail = new HotelBookingDetail((int) $idHotelBooking))) { + $objOrder = new Order((int) $objHotelBookingDetail->id_order); + $objCart = new Cart($objOrder->id_cart); + $objOrderDetail = new OrderDetail((int) $objHotelBookingDetail->id_order_detail); + //removing the old price + $objOrder->total_paid -= $objHotelBookingDetail->total_price_tax_incl; + $objOrder->total_paid_tax_incl -= $objHotelBookingDetail->total_price_tax_incl; + $objOrder->total_paid_tax_excl -= $objHotelBookingDetail->total_price_tax_excl; + $objOrder->total_products -= $objHotelBookingDetail->total_price_tax_excl; + $objOrder->total_products_wt -= $objHotelBookingDetail->total_price_tax_incl; + + $objOrderDetail->total_price_tax_incl -= $objHotelBookingDetail->total_price_tax_incl; + $objOrderDetail->total_price_tax_excl -= $objHotelBookingDetail->total_price_tax_excl; + + if (isset($room['unit_price_without_tax'])) { + $room['unit_price_without_tax'] = Tools::ps_round($room['unit_price_without_tax'], _PS_PRICE_COMPUTE_PRECISION_); + $this->wsFeaturePrices[] = $this->createFeaturePrice( + array( + 'id_product' => (int) $objHotelBookingDetail->id_product, + 'id_cart' => (int) $objCart->id, + 'id_guest' => (int) $objCart->id_guest, + 'date_from' => date('Y-m-d', strtotime($objHotelBookingDetail->date_from)), + 'date_to' => date('Y-m-d', strtotime($objHotelBookingDetail->date_to)), + 'id_room' => $objHotelBookingDetail->id_room, + 'price' => $room['unit_price_without_tax'] + ) + ); + } + + $roomTotalPrice = HotelRoomTypeFeaturePricing::getRoomTypeTotalPrice( + $objHotelBookingDetail->id_product, + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to, + 0, + Group::getCurrent()->id, + $objCart->id, + $objCart->id_guest, + $objHotelBookingDetail->id_room, + 0 + ); + + $objHotelBookingDetail->total_price_tax_incl = $roomTotalPrice['total_price_tax_incl']; + $objHotelBookingDetail->total_price_tax_excl = $roomTotalPrice['total_price_tax_excl']; + $objHotelBookingDetail->total_paid_amount = $roomTotalPrice['total_price_tax_excl']; + $objHotelBookingDetail->save(); + + // Updating the price + $objOrderDetail->total_price_tax_incl += $objHotelBookingDetail->total_price_tax_incl; + $objOrderDetail->total_price_tax_excl += $objHotelBookingDetail->total_price_tax_excl; + $objOrderDetail->save(); + + $objOrder->total_paid += $objHotelBookingDetail->total_price_tax_incl; + $objOrder->total_paid_tax_incl += $objHotelBookingDetail->total_price_tax_incl; + $objOrder->total_paid_tax_excl += $objHotelBookingDetail->total_price_tax_excl; + $objOrder->total_products += $objHotelBookingDetail->total_price_tax_excl; + $objOrder->total_products_wt += $objHotelBookingDetail->total_price_tax_incl; + $objOrder->update(); + + HotelRoomTypeFeaturePricing::deleteByIdCart($objCart->id); + $this->deleteWsFeaturePrices(); + } + } + + /** + * Deleting the service from the order. + */ + public function removeServicesFromOrderedRoom($services) + { + if (count($services)) { + foreach ($services as $service) { + $idRoomTypeServiceProductOrderDetail = $service['id_room_type_service_product_order_detail']; + if (Validate::isLoadedObject($objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail((int) $idRoomTypeServiceProductOrderDetail))) { + $objOrderDetail = new OrderDetail((int) $objRoomTypeServiceProductOrderDetail->id_order_detail); + $priceTaxExcl = $objRoomTypeServiceProductOrderDetail->total_price_tax_excl; + $priceTaxIncl = $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + $objHotelBookingDetail = new HotelBookingDetail($objRoomTypeServiceProductOrderDetail->id_htl_booking_detail); + $numDays = 1; + if (Product::getProductPriceCalculation($service['id_product']) == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $numDays = HotelHelper::getNumberOfDays($objHotelBookingDetail->date_from, $objHotelBookingDetail->date_to); + } + + $quantity = $objRoomTypeServiceProductOrderDetail->quantity * $numDays; + if ($objRoomTypeServiceProductOrderDetail->delete()) { + $objOrder = new Order($objRoomTypeServiceProductOrderDetail->id_order); + if ($quantity >= $objOrderDetail->product_quantity) { + $objOrderDetail->delete(); + } else { + $objOrderDetail->product_quantity -= $quantity; + + $objOrderDetail->total_price_tax_excl -= $priceTaxExcl; + $objOrderDetail->total_price_tax_incl -= $priceTaxIncl; + + $objOrderDetail->updateTaxAmount($objOrder); + + $objOrderDetail->update(); + } + + $objOrder->total_paid_tax_excl -= $priceTaxExcl; + $objOrder->total_paid_tax_incl -= $priceTaxIncl; + $objOrder->total_paid -= $priceTaxIncl; + + $objOrder->update(); + } + } + } + } + } + + /** + * Adding the service into the order. + */ + public function addServicesInOrderedRoom($services, $idHotelBookingDetail) + { + if ($services) { + $objHotelBookingDetail = new HotelBookingDetail((int) $idHotelBookingDetail); + $objOrder = new Order($objHotelBookingDetail->id_order); + // set context currency So that we can get prices in the order currency + $this->context->currency = new Currency($objOrder->id_currency); + $objHotelCartBookingData = new HotelCartBookingData(); + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail(); + $roomHtlCartInfo = $objHotelCartBookingData->getRoomRowByIdProductIdRoomInDateRange( + $objHotelBookingDetail->id_cart, + $objHotelBookingDetail->id_product, + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to, + $objHotelBookingDetail->id_room + ); + + $this->createCartForOrder($objOrder->id); + $objCart = $this->context->cart; + $formattedServices = array(); + foreach ($services as $serviceKey => $service) { + if (!isset($service['id_product']) && isset($service['is_new'])) { + $serviceKey = $this->createWsService($service, $objHotelBookingDetail->id_product); + $service['id_product'] = $serviceKey; + } + + $formattedServices[$serviceKey] = $service; + } + + $services = $formattedServices; + foreach ($services as $service) { + $objRoomTypeServiceProductCartDetail->addServiceProductInCart( + $service['id_product'], + $service['quantity'], + $objCart->id, + $roomHtlCartInfo['id'] + ); + } + $unitPriceTaxIncl = 0; + $unitPriceTaxExcl = 0; + $productList = $objCart->getProducts(true); + $objOrderDetail = new OrderDetail(); + $objOrderDetail->createList($objOrder, $objCart, $objOrder->getCurrentOrderState(), $productList, 0, true); + foreach ($productList as &$product) { + if ($idRoomTypeServiceProductCartDetail = $objRoomTypeServiceProductCartDetail->alreadyExists( + $product['id_product'], + $objCart->id, + $roomHtlCartInfo['id']) + ) { + $objRoomTypeServiceProductCartDetail = new RoomTypeServiceProductCartDetail((int) $idRoomTypeServiceProductCartDetail); + $numDays = 1; + if (Product::getProductPriceCalculation($product['id_product']) == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $numDays = HotelHelper::getNumberOfDays($objHotelBookingDetail->date_from, $objHotelBookingDetail->date_to); + } + + $quantity = $objRoomTypeServiceProductCartDetail->quantity * $numDays; + $unitPriceTaxExcl = $objRoomTypeServiceProductPrice->getServicePrice( + (int) $product['id_product'], + 0, + 1, + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to, + false, + $objCart->id + )/ $numDays; + $unitPriceTaxIncl = $objRoomTypeServiceProductPrice->getServicePrice( + (int) $product['id_product'], + 0, + 1, + $objHotelBookingDetail->date_from, + $objHotelBookingDetail->date_to, + true, + $objCart->id + )/ $numDays; + + if ($unitPriceTaxIncl > 0) { + $oldTaxMultiplier = $unitPriceTaxExcl / $unitPriceTaxIncl; + } else { + $oldTaxMultiplier = 1; + } + + $objOrderDetail->unit_price_tax_incl -= $unitPriceTaxIncl; + $objOrderDetail->unit_price_tax_excl -= $unitPriceTaxExcl; + + $totalPriceTaxExcl = $unitPriceTaxExcl * $quantity; + $totalPriceTaxIncl = $unitPriceTaxIncl * $quantity; + if (isset($service['unit_price_without_tax'])) { + $unitPriceTaxExcl = $service['unit_price_without_tax']; + $unitPriceTaxIncl = $unitPriceTaxExcl * $oldTaxMultiplier; + + $totalPriceTaxExcl = $unitPriceTaxExcl * $quantity; + $totalPriceTaxIncl = $unitPriceTaxIncl * $quantity; + } else if (isset($service['total_price_without_tax'])) { + $totalPriceTaxExcl = $service['total_price_without_tax']; + $totalPriceTaxIncl = $totalPriceTaxExcl * $oldTaxMultiplier; + + $unitPriceTaxExcl = $totalPriceTaxExcl / $quantity; + $unitPriceTaxIncl = $totalPriceTaxIncl / $quantity; + } + + if (isset($services[$product['id_product']]['id_tax_rules_group'])) { + $objAddress = new Address((int) $objOrder->id_address_tax); + $objTaxManager = TaxManagerFactory::getManager($objAddress, $services[$product['id_product']]['id_tax_rules_group']); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + $unitPriceTaxIncl = $objTaxCalculator->addTaxes($unitPriceTaxExcl); + + $unitPriceTaxIncl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $totalPriceTaxIncl = Tools::ps_round(($unitPriceTaxIncl * $quantity), _PS_PRICE_COMPUTE_PRECISION_); + } else if (isset($services[$product['id_product']]['total_tax'])) { + $totalPriceTaxIncl = $totalPriceTaxExcl + $services[$product['id_product']]['total_tax']; + $totalPriceTaxIncl = Tools::ps_round(($totalPriceTaxIncl), _PS_PRICE_COMPUTE_PRECISION_); + + $unitPriceTaxIncl = Tools::ps_round($totalPriceTaxIncl / $quantity, _PS_PRICE_COMPUTE_PRECISION_); + } + + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + $objRoomTypeServiceProductOrderDetail->id_product = $product['id_product']; + $objRoomTypeServiceProductOrderDetail->id_order = $objHotelBookingDetail->id_order; + $objRoomTypeServiceProductOrderDetail->id_order_detail = $objOrderDetail->id; + $objRoomTypeServiceProductOrderDetail->id_cart = $objCart->id; + $objRoomTypeServiceProductOrderDetail->id_htl_booking_detail = $objHotelBookingDetail->id; + $objRoomTypeServiceProductOrderDetail->unit_price_tax_excl = Tools::ps_round($unitPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->unit_price_tax_incl = Tools::ps_round($unitPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_excl = Tools::ps_round($totalPriceTaxExcl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->total_price_tax_incl = Tools::ps_round($totalPriceTaxIncl, _PS_PRICE_COMPUTE_PRECISION_); + $objRoomTypeServiceProductOrderDetail->name = $product['name']; + $objRoomTypeServiceProductOrderDetail->quantity = $objRoomTypeServiceProductCartDetail->quantity; + $objRoomTypeServiceProductOrderDetail->save(); + + // update totals amount of order + $objOrder->total_products += $objRoomTypeServiceProductOrderDetail->total_price_tax_excl; + $objOrder->total_products_wt += $objRoomTypeServiceProductOrderDetail->total_price_tax_incl; + + $objOrder->total_paid += Tools::ps_round($objRoomTypeServiceProductOrderDetail->total_price_tax_incl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_excl += Tools::ps_round($objRoomTypeServiceProductOrderDetail->total_price_tax_excl, _PS_PRICE_COMPUTE_PRECISION_); + $objOrder->total_paid_tax_incl += Tools::ps_round($objRoomTypeServiceProductOrderDetail->total_price_tax_incl, _PS_PRICE_COMPUTE_PRECISION_); + + $objOrderDetail->unit_price_tax_incl += $unitPriceTaxIncl; + $objOrderDetail->total_price_tax_incl += $unitPriceTaxIncl * $objRoomTypeServiceProductOrderDetail->quantity; + $objOrderDetail->unit_price_tax_excl += $unitPriceTaxExcl; + $objOrderDetail->total_price_tax_excl += $unitPriceTaxExcl * $objRoomTypeServiceProductOrderDetail->quantity; + } + } + + $objOrderDetail->update(); + + $objOrder->total_discounts += (float)abs($objCart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)); + $objOrder->total_discounts_tax_excl += (float)abs($objCart->getOrderTotal(false, Cart::ONLY_DISCOUNTS)); + $objOrder->total_discounts_tax_incl += (float)abs($objCart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)); + $objOrder->update(); + } + } + + /** + * Adding the demands into the order. + */ + public function addDemandsInOrderedRoom($demands, $idHotelBooking) + { + if (Validate::isLoadedObject($objBookingDetail = new HotelBookingDetail((int) $idHotelBooking))) { + if ($demands) { + $objOrder = new Order($objBookingDetail->id_order); + // set context currency So that we can get prices in the order currency + $this->context->currency = new Currency($objOrder->id_currency); + + $objAddress = new Address((int) $objOrder->id_address_tax); + $idLang = (int) $objOrder->id_lang; + $idProduct = $objBookingDetail->id_product; + $objHtlBkDtl = new HotelBookingDetail(); + $objRoomDemandPrice = new HotelRoomTypeDemandPrice(); + foreach ($demands as $demand) { + $idGlobalDemand = $demand['id_global_demand']; + $idOption = $demand['id_option']; + $objBookingDemand = new HotelBookingDemands(); + $objBookingDemand->id_htl_booking = $idHotelBooking; + $objGlobalDemand = new HotelRoomTypeGlobalDemand($idGlobalDemand, $idLang); + if ($idOption) { + $objOption = new HotelRoomTypeGlobalDemandAdvanceOption($idOption, $idLang); + $objBookingDemand->name = $objOption->name; + } else { + $idOption = 0; + $objBookingDemand->name = $objGlobalDemand->name; + } + + $unitPriceTaxExcl = HotelRoomTypeDemand::getPriceStatic($idProduct, $idGlobalDemand, $idOption, 0); + $unitPriceTaxIncl = HotelRoomTypeDemand::getPriceStatic($idProduct, $idGlobalDemand, $idOption, 1); + $taxMultiplier = 1; + if ($unitPriceTaxExcl > 0) { + $taxMultiplier = $unitPriceTaxIncl / $unitPriceTaxExcl; + } + + $qty = 1; + if ($objGlobalDemand->price_calc_method == HotelRoomTypeGlobalDemand::WK_PRICE_CALC_METHOD_EACH_DAY) { + $numDays = $objHtlBkDtl->getNumberOfDays( + $objBookingDetail->date_from, + $objBookingDetail->date_to + ); + if ($numDays > 1) { + $qty *= $numDays; + } + } + + $totalPriceTaxExcl = $unitPriceTaxExcl * $qty; + $totalPriceTaxIncl = $unitPriceTaxIncl * $qty; + if (isset($demand['unit_price_without_tax'])) { + $unitPriceTaxExcl = $demand['unit_price_without_tax']; + $unitPriceTaxIncl = $demand['unit_price_without_tax'] * $taxMultiplier; + $totalPriceTaxExcl = $unitPriceTaxExcl * $qty; + $totalPriceTaxIncl = $unitPriceTaxIncl * $qty; + } + + if (isset($demand['id_tax_rules_group'])) { + $objTaxManager = TaxManagerFactory::getManager( + $objAddress, + $demand['id_tax_rules_group'] + ); + $objTaxManager = TaxManagerFactory::getManager($objAddress, $demand['id_tax_rules_group']); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + + $unitPriceTaxIncl = $objTaxCalculator->addTaxes($unitPriceTaxExcl); + $totalPriceTaxIncl = $unitPriceTaxIncl * $qty; + } else { + $objTaxManager = TaxManagerFactory::getManager( + $objAddress, + $objGlobalDemand->id_tax_rules_group + ); + $objTaxCalculator = $objTaxManager->getTaxCalculator(); + } + + $objBookingDemand->unit_price_tax_excl = $unitPriceTaxExcl; + $objBookingDemand->unit_price_tax_incl = $unitPriceTaxIncl; + $objBookingDemand->total_price_tax_excl = $totalPriceTaxExcl; + $objBookingDemand->total_price_tax_incl = $totalPriceTaxIncl; + $objOrderDetail = new OrderDetail($objBookingDetail->id_order_detail); + + // Update OrderInvoice of this OrderDetail + if ($objOrderDetail->id_order_invoice != 0) { + // values changes as values are calculated accoding to the quantity of the product by webkul + $objOrderInvoice = new OrderInvoice($objOrderDetail->id_order_invoice); + $objOrderInvoice->total_paid_tax_excl += $objBookingDemand->total_price_tax_excl; + $objOrderInvoice->total_paid_tax_incl += $objBookingDemand->total_price_tax_incl; + $objOrderInvoice->update(); + } + + // change order total + $objOrder->total_paid_tax_excl += $objBookingDemand->total_price_tax_excl; + $objOrder->total_paid_tax_incl += $objBookingDemand->total_price_tax_incl; + $objOrder->total_paid += $objBookingDemand->total_price_tax_incl; + $objBookingDemand->price_calc_method = $objGlobalDemand->price_calc_method; + $objBookingDemand->id_tax_rules_group = $objGlobalDemand->id_tax_rules_group; + if ($objBookingDemand->save() + && Validate::isLoadedObject($objAddress) + ) { + $objBookingDemand->tax_computation_method = (int)$objTaxCalculator->computation_method; + $objBookingDemand->tax_calculator = $objTaxCalculator; + // Now save tax details of the extra demand + $objBookingDemand->setBookingDemandTaxDetails(); + } + } + + $objOrder->save(); + } + } + } + + /** + * Deleting the demands into the order. + */ + public function removeDemandsInOrderedRoom($demands) + { + if (count($demands)) { + foreach ($demands as $demand) { + $idBookingDemand = $demand['id_booking_demand']; + if (Validate::isLoadedObject($objBookingDemand = new HotelBookingDemands($idBookingDemand))) { + if ($objBookingDemand->deleteBookingDemandTaxDetails($idBookingDemand)) { + if ($objBookingDemand->delete()) { + if (Validate::isLoadedObject($objBookingDetail = new HotelBookingDetail($objBookingDemand->id_htl_booking))) { + // change order total + $objOrder = new Order($objBookingDetail->id_order); + $objOrder->total_paid_tax_excl -= $objBookingDemand->total_price_tax_excl; + $objOrder->total_paid_tax_incl -= $objBookingDemand->total_price_tax_incl; + $objOrder->total_paid -= $objBookingDemand->total_price_tax_incl; + $objOrder->save(); + + $objOrderDetail = new OrderDetail($objBookingDetail->id_order_detail); + // Update OrderInvoice of this OrderDetail + if ($objOrderDetail->id_order_invoice != 0) { + // values changes as values are calculated accoding to the quantity of the product by webkul + $objOrderInvoice = new OrderInvoice($objOrderDetail->id_order_invoice); + $objOrderInvoice->total_paid_tax_excl -= $objBookingDemand->total_price_tax_excl; + $objOrderInvoice->total_paid_tax_incl -= $objBookingDemand->total_price_tax_incl; + $objOrderInvoice->update(); + } + } + } + } + } + } + } + + } + + /** + * Used to get the booking details for the GET, POST and PUT request. + */ + public function getBookingDetails($idBooking) + { + $objOrder = new Order($idBooking); + if (!Validate::isLoadedObject($objOrder)) { + $this->objOutput->setStatus(404); + $this->getWsObject()->setOutputEnabled(false); + return false; + } else { + $objCurrency = new Currency($objOrder->id_currency); + $objBookingDetail = new HotelBookingDetail(); + $objBookingDemand = new HotelBookingDemands(); + $objRoomTypeServiceProductOrderDetail = new RoomTypeServiceProductOrderDetail(); + $objOrderReturn = new OrderReturn(); + $idHotel = HotelBookingDetail::getIdHotelByIdOrder($objOrder->id); + $objHotelBranchInformation = new HotelBranchInformation($idHotel, Configuration::get('PS_LANG_DEFAULT')); + $objOrderState = new OrderState($objOrder->current_state, Configuration::get('PS_LANG_DEFAULT')); + $objCustomer = new Customer($objOrder->id_customer); + $params['id'] = (int) $objOrder->id; + $params['id_property'] = (int) $idHotel; + $params['currency'] = strtoupper($objCurrency->iso_code); + // $params['current_status'] = $objOrderState->name; + $params['source'] = $objOrder->source; + $params['booking_date'] = $objOrder->date_add; + $params['id_language'] = (int) $objOrder->id_lang; + $assoc = array(); + $customerDetails = array( + 'id_customer' => (int) $objCustomer->id, + 'firstname' => $objCustomer->firstname, + 'lastname' => $objCustomer->lastname, + 'email' => $objCustomer->email, + 'phone' => isset($objCustomer->phone) ? $objCustomer->phone : '' + ); + + $priceDetails = array( + 'total_paid' => Tools::ps_round($objOrder->total_paid_real, _PS_PRICE_COMPUTE_PRECISION_), + 'total_price_without_tax' => Tools::ps_round($objOrder->total_paid_tax_excl, _PS_PRICE_COMPUTE_PRECISION_), + 'total_tax' => Tools::ps_round(($objOrder->total_paid_tax_incl - $objOrder->total_paid_tax_excl), _PS_PRICE_COMPUTE_PRECISION_) + ); + + $orderCartRules = array(); + if ($cartRules = $objOrder->getCartRules()) { + foreach ($cartRules as $cartRule) { + $rule = array(); + $rule['code'] = $cartRule['name']; + $rule['value'] = Tools::ps_round($cartRule['value'], _PS_PRICE_COMPUTE_PRECISION_); + $rule['currency'] = strtoupper($objCurrency->iso_code); + $rule['id_order_invoice'] = (int) $cartRule['id_order_invoice']; + $orderCartRules[] = $rule; + } + } + + $params['associations']['customer_detail'] = $customerDetails; + $params['associations']['price_details'] = $priceDetails; + $params['associations']['cart_rules'] = $orderCartRules; + $roomTypeInfo = array(); + if (Group::getPriceDisplayMethod($objCustomer->id_default_group) == PS_TAX_INC) { + $useTax = 1; + } + + $params['associations']['remarks'] = array(); + if ($customerMessages = Message::getMessagesByOrderId($objOrder->id, true)) { + foreach ($customerMessages as $customerMessage) { + $message = $customerMessage['message']; + $params['associations']['remarks'][] = $message; + } + } + + + if ($orderDetailData = $objBookingDetail->getOrderFormatedBookinInfoByIdOrder($objOrder->id)) { + foreach ($orderDetailData as $orderDetailKey => $orderData) { + $dateJoin = $orderData['id_product'].'_'.strtotime($orderData['date_from']).strtotime($orderData['date_to']); + if (!isset($roomTypeInfo[$dateJoin])) { + $roomTypeInfo[$dateJoin]['id_room_type'] = (int) $orderData['id_product']; + $roomTypeInfo[$dateJoin]['checkin_date'] = $orderData['date_from']; + $roomTypeInfo[$dateJoin]['checkout_date'] = $orderData['date_to']; + $roomTypeInfo[$dateJoin]['total_tax'] = ($orderData['total_price_tax_incl'] - $orderData['total_price_tax_excl']); + $roomTypeInfo[$dateJoin]['number_of_rooms'] = 1; + $roomTypeInfo[$dateJoin]['name'] = $orderData['room_type_name']; + } else { + $roomTypeInfo[$dateJoin]['total_tax'] += ($orderData['total_price_tax_incl'] - $orderData['total_price_tax_excl']); + $roomTypeInfo[$dateJoin]['number_of_rooms'] += 1; + } + + $roomTypeInfo[$dateJoin]['total_tax'] = Tools::ps_round($roomTypeInfo[$dateJoin]['total_tax'], _PS_PRICE_COMPUTE_PRECISION_); + + $roomInfo = array(); + $roomInfo['id_room'] = (int) $orderData['id_room']; + $roomInfo['id_hotel_booking'] = (int) $orderData['id']; + $roomInfo['adults'] = (int) $orderData['adults']; + $roomInfo['child'] = (int) $orderData['children']; + $roomInfo['unit_price_without_tax'] = Tools::ps_round($orderData['total_price_tax_excl'], _PS_PRICE_COMPUTE_PRECISION_); + $roomInfo['total_tax'] = Tools::ps_round(($orderData['total_price_tax_incl'] - $orderData['total_price_tax_excl']), _PS_PRICE_COMPUTE_PRECISION_); + if(isset($roomInfo['facilities'])) { + unset($roomInfo['facilities']); + } + if ($extraDemands = $objBookingDemand->getRoomTypeBookingExtraDemands( + $orderData['id_order'], + $orderData['id_product'], + $orderData['id_room'], + $orderData['date_from'], + $orderData['date_to'], + 0, + 0, + $useTax + )) { + $roomInfo['facilities'] = array(); + foreach ($extraDemands as $extraDemand) { + $demand = array(); + $demand['name'] = $extraDemand['name']; + $demand['quantity'] = 1; + $demand['unit_price_without_tax'] = Tools::ps_round($extraDemand['unit_price_tax_excl'], _PS_PRICE_COMPUTE_PRECISION_); + $demand['total_tax'] = Tools::ps_round(($extraDemand['unit_price_tax_incl']- $extraDemand['unit_price_tax_excl']), _PS_PRICE_COMPUTE_PRECISION_); + + $demand['per_night'] = 0; + if ($extraDemand['price_calc_method'] == HotelRoomTypeGlobalDemand::WK_PRICE_CALC_METHOD_EACH_DAY) { + $demand['per_night'] = 1; + } + + $roomInfo['facilities'][] = $demand; + } + } + + if(isset($roomInfo['services'])) { + unset($roomInfo['services']); + } + + if ($additionalServices = $objRoomTypeServiceProductOrderDetail->getroomTypeServiceProducts( + $orderData['id_order'], + 0, + 0, + $orderData['id_product'], + $orderData['date_from'], + $orderData['date_to'], + $orderData['id_room'], + 0, + $useTax + )) { + $roomInfo['services'] = array(); + foreach ($additionalServices as $additionalService) { + foreach ($additionalService['additional_services'] as $service) { + $services = array(); + $services['id_service'] = (int) $service['id_product']; + $services['name'] = $service['name']; + $services['quantity'] = (int) $service['quantity']; + $services['unit_price_without_tax'] = Tools::ps_round(($service['total_price_tax_excl'] / $services['quantity']), _PS_PRICE_COMPUTE_PRECISION_); + $services['total_price_without_tax'] = Tools::ps_round(($service['total_price_tax_excl']), _PS_PRICE_COMPUTE_PRECISION_); + $services['total_tax'] = Tools::ps_round(($service['total_price_tax_incl'] - $service['total_price_tax_excl']), _PS_PRICE_COMPUTE_PRECISION_); + + $objProduct = new Product($service['id_product']); + $services['per_night'] = 0; + if ($objProduct->price_calculation_method == Product::PRICE_CALCULATION_METHOD_PER_DAY) { + $services['per_night'] = 1; + } + + $services['price_mode'] = (int) $objProduct->price_calculation_method; + $roomInfo['services'][] = $services; + } + } + } + + $roomTypeInfo[$dateJoin]['rooms'][] = $roomInfo; + } + } + + $params['associations']['room_types'] = array_values($roomTypeInfo); + $this->output['booking'] = $params; + } + } + + public function createGuest() + { + $guest = new Guest(); + $guest->id_operating_system = 7; // For Android Device + $guest->id_web_browser = 1; // For Other(Opera) + $guest->mobile_theme = 1; // For Mobile device + $guest->save(); + if ($guest->id) { + return $guest->id; + } + + return 0; + } + + public function renderResponse() + { + if ($this->outputType == 'json') { + $this->getResponseJson(); + } else { + $this->getResponseXml(); + } + } + + public function renderHeader($header) + { + return '<'.$header.'>'; + } + + public function renderFooter($footer) + { + return ''; + } + + /** + * Used to render output in XML using array. + */ + public function renderXmlOutputUsingArray($response, $keyToIgnore = array(), $parentKeys = array(), $parentKey = '', $useEmpty = false) + { + $output = ''; + foreach ($response as $key => $res) { + if (in_array($key, $keyToIgnore) && $key) { + continue; + } + + if (gettype($key) == 'integer' && isset($parentKeys[$parentKey])) { + $key = $parentKeys[$parentKey]; + } + + if (is_array($res) && count($res)) { + $output .= $this->renderHeader($key); + $output .= $this->renderXmlOutputUsingArray($res, $keyToIgnore, $parentKeys, $key, $useEmpty); + $output .= $this->renderFooter($key); + } else { + if (empty($res) && !$useEmpty) { + $res = 0; + } + + if (isset($this->wsObject->urlFragments['schema']) && $this->wsObject->method == 'GET') { + if ($this->wsObject->urlFragments['schema'] == 'blank' || $this->wsObject->urlFragments['schema'] == 'synopsis') { + $res = null; + } else { + throw new WebserviceException( + 'Please select a schema of type \'synopsis\' to get the whole schema informations (which fields are required, which kind of content...) or \'blank\' to get an empty schema to fill before using POST request.', + array(100, 400) + ); + } + } + + $output .= $this->objOutput->objectRender->renderField( + array( + 'sqlId' => $key, + 'value' => $res + ) + ); + } + } + + return $output; + } +} diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index 2f579a095..afc8f587b 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -140,6 +140,9 @@ class AdminAddressesControllerCore extends AdminController 'name' => 'email' ), ); + } else if (isset($this->object->id) && $this->object->id_hotel) { + $this->errors[] = Tools::displayError('The object cannot be loaded (or found)'); + return false; } $this->fields_form = array( diff --git a/controllers/admin/AdminCartRulesController.php b/controllers/admin/AdminCartRulesController.php index 0e21ec140..a09ffc12b 100644 --- a/controllers/admin/AdminCartRulesController.php +++ b/controllers/admin/AdminCartRulesController.php @@ -752,10 +752,12 @@ class AdminCartRulesControllerCore extends AdminController public function renderForm() { $limit = 40; - $this->toolbar_btn['save-and-stay'] = array( - 'href' => '#', - 'desc' => $this->l('Save and Stay') - ); + if (!Tools::getValue('liteDisplaying')) { + $this->toolbar_btn['save-and-stay'] = array( + 'href' => '#', + 'desc' => $this->l('Save and Stay') + ); + } /** @var CartRule $current_object */ $current_object = $this->loadObject(true); diff --git a/controllers/admin/AdminCartsController.php b/controllers/admin/AdminCartsController.php index 9cf872c27..2ce84593f 100644 --- a/controllers/admin/AdminCartsController.php +++ b/controllers/admin/AdminCartsController.php @@ -207,7 +207,7 @@ class AdminCartsControllerCore extends AdminController $date_from = date(Context::getContext()->language->date_format_lite, strtotime('-2 day')); $date_to = date(Context::getContext()->language->date_format_lite, strtotime('-1 day')); $helper->subtitle = sprintf($this->l('From %s to %s', null, null, false), $date_from, $date_to); - $helper->href = $this->context->link->getAdminLink('AdminCarts').'&action=filterOnlyAbandonedCarts&date_from='.$date_from.'&date_to='.$date_to; + $helper->href = $this->context->link->getAdminLink('AdminCarts').'&action=filterOnlyAbandonedCarts&date_from='.date('Y-m-d', strtotime('-2 day')).'&date_to='.date('Y-m-d', strtotime('-1 day')); $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=abandoned_cart'; $this->kpis[] = $helper; diff --git a/controllers/admin/AdminCustomerThreadsController.php b/controllers/admin/AdminCustomerThreadsController.php index 26bb3eebe..2d7b52891 100644 --- a/controllers/admin/AdminCustomerThreadsController.php +++ b/controllers/admin/AdminCustomerThreadsController.php @@ -66,9 +66,8 @@ class AdminCustomerThreadsControllerCore extends AdminController $this->access_select = ' SELECT a.`id_customer_thread` FROM '._DB_PREFIX_.'customer_thread a'; $this->access_join = ' LEFT JOIN '._DB_PREFIX_.'orders ord ON (a.id_order = ord.id_order)'; $this->access_join .= ' LEFT JOIN '._DB_PREFIX_.'htl_booking_detail hbd ON (hbd.id_order = ord.id_order)'; - $this->access_where = ' WHERE 1 '; if ($acsHtls = HotelBranchInformation::getProfileAccessedHotels($this->context->employee->id_profile, 1, 1)) { - $this->access_where .= ' AND IF(a.`id_order`, hbd.`id_hotel` IN ('.implode(',', $acsHtls).'), 1)'; + $this->access_where = ' AND IF(a.`id_order`, hbd.`id_hotel` IN ('.implode(',', $acsHtls).'), 1)'; } $this->fields_list = array( diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index c523ffd3c..c31aad99f 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -375,6 +375,10 @@ class AdminCustomersControllerCore extends AdminController /** @var Customer $obj */ if (!($obj = $this->loadObject(true))) { return; + } else if ($this->object->deleted == Customer::STATUS_DELETED) { + $this->warnings[] = $this->l('This is a deleted customer and you cannot update the information of a deleted customer.'); + } else if ($this->object->deleted == Customer::STATUS_BANNED) { + $this->warnings[] = $this->l('This is a banned customer and you cannot update the information of a banned customer.'); } $genders = Gender::getGenders(); @@ -761,7 +765,7 @@ class AdminCustomersControllerCore extends AdminController $nbDaysNewCustomers = Validate::isUnsignedInt(Configuration::get('PS_KPI_NEW_CUSTOMERS_NB_DAYS')) ? Configuration::get('PS_KPI_NEW_CUSTOMERS_NB_DAYS') : 30; $date_from = date('Y-m-d', strtotime('-'.$nbDaysNewCustomers.' day')); $date_to = date('Y-m-d'); - $helper->href = $this->context->link->getAdminLink('AdminCustomers').'&customerFilter_date_add[]='.$date_from.'&customerFilter_date_add[]='.$date_to; + $helper->href = $this->context->link->getAdminLink('AdminCustomers').'&customerFilter_a!date_add[]='.$date_from.'&customerFilter_a!date_add[]='.$date_to; $helper->subtitle = sprintf($this->l('%d Days', null, null, false), (int) $nbDaysNewCustomers); $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=total_new_customers'; $helper->tooltip = $this->l('The total number of new customers who registered in given period of time.', null, null, false); @@ -1114,6 +1118,14 @@ class AdminCustomersControllerCore extends AdminController public function processUpdate() { if (Validate::isLoadedObject($this->object)) { + if ($this->object->deleted == Customer::STATUS_DELETED) { + $this->errors[] = $this->l('You can not update a deleted customer.'); + return false; + } else if ($this->object->deleted == Customer::STATUS_BANNED) { + $this->errors[] = $this->l('You can not update a banned customer.'); + return false; + } + $customer_email = trim(strval(Tools::getValue('email'))); // check if e-mail already used diff --git a/controllers/admin/AdminImportController.php b/controllers/admin/AdminImportController.php index b66f7a772..f7b347fc7 100644 --- a/controllers/admin/AdminImportController.php +++ b/controllers/admin/AdminImportController.php @@ -318,8 +318,10 @@ class AdminImportControllerCore extends AdminController ); break; case $this->entities[$this->l('Customers')]: - //Overwrite required_fields AS only email is required whereas other entities $this->required_fields = array('email', 'passwd', 'lastname', 'firstname'); + if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) { + $this->required_fields[] = 'phone'; + } $this->available_fields = array( 'no' => array('label' => $this->l('Ignore this column')), @@ -331,6 +333,7 @@ class AdminImportControllerCore extends AdminController 'birthday' => array('label' => $this->l('Birthday (yyyy-mm-dd)')), 'lastname' => array('label' => $this->l('Last Name *')), 'firstname' => array('label' => $this->l('First Name *')), + 'phone' => array('label' => $this->l('Phone'). (Configuration::get('PS_ONE_PHONE_AT_LEAST') ? ' *' : '' )), 'newsletter' => array('label' => $this->l('Newsletter (0/1)')), 'optin' => array('label' => $this->l('Opt-in (0/1)')), 'group' => array('label' => $this->l('Groups (x,y,z...)')), @@ -2699,6 +2702,10 @@ class AdminImportControllerCore extends AdminController $convert = Tools::getValue('convert'); $force_ids = Tools::getValue('forceIDs'); + $phoneRequired = false; + if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) { + $phoneRequired = true; + } for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++) { if ($convert) { @@ -2707,7 +2714,13 @@ class AdminImportControllerCore extends AdminController $info = AdminImportController::getMaskedRow($line); $has_required_fields = $this->checkRequiredFields($info, 'firstname'); - if ($has_required_fields) { + if ($phoneRequired && !Validate::isPhoneNumber(trim($info['phone']))) { + $this->errors[] = sprintf( + Tools::displayError('Invalid phone for %1$s (ID: %2$s).'), + $info['email'], + (isset($info['id']) && !empty($info['id']))? $info['id'] : 'null' + ); + } else if ($has_required_fields) { AdminImportController::setDefaultValues($info); $customerExists = false; if (isset($info['id']) @@ -2723,7 +2736,7 @@ class AdminImportControllerCore extends AdminController $customer->force_id = $info['id']; } } else { - $customer = new Customer(); + $customer = new Customer(); } $customer_exist = false; @@ -2833,6 +2846,9 @@ class AdminImportControllerCore extends AdminController if ($res && isset($customer_groups)) { $customer->updateGroup($customer_groups); + if ($phoneRequired) { + CartCustomerGuestDetail::updateCustomerPhoneNumber($customer->email, $customer->phone); + } } } } diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 9a4a3da7d..0027eb0bb 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -2546,7 +2546,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&date_from='.$dateFrom.'&date_to='.$dateTo; + $helper->href = $this->context->link->getAdminLink('AdminCarts').'&action=filterOnlyAbandonedCarts&date_from='.date('Y-m-d', strtotime('-2 day')).'&date_to='.date('Y-m-d', strtotime('-1 day')); $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); $this->kpis[] = $helper; diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index f96439145..cf55c7d7f 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -3072,25 +3072,22 @@ class AdminProductsControllerCore extends AdminController if ($serviceProductPriceInfo) { $associationInfo['custom_price'] = $serviceProductPriceInfo['price']; $associationInfo['id_tax_rules_group'] = $serviceProductPriceInfo['id_tax_rules_group']; - if ($serviceProductPriceInfo['id_tax_rules_group'] == 0) { - $associationInfo['tax_rules_group_name'] = $this->l('No tax'); - } else { - $objTaxRuleGroup = new TaxRulesGroup( - $serviceProductPriceInfo['id_tax_rules_group'], - $this->context->language->id - ); + $associationInfo['tax_rules_group_name'] = $this->l('No tax'); + + if (Validate::isLoadedObject($objTaxRuleGroup = new TaxRulesGroup( + $serviceProductPriceInfo['id_tax_rules_group'], + $this->context->language->id + ))) { $associationInfo['tax_rules_group_name'] = $objTaxRuleGroup->name; } $associationInfo['id_room_type_service_product_price'] = $serviceProductPriceInfo['id_room_type_service_product_price']; } - if ($objProduct->id_tax_rules_group == 0) { - $associationInfo['default_tax_rules_group_name'] = $this->l('No tax'); - } else { - $objTaxRuleGroup = new TaxRulesGroup( - $objProduct->id_tax_rules_group, - $this->context->language->id - ); + $associationInfo['default_tax_rules_group_name'] = $this->l('No tax'); + if (Validate::isLoadedObject($objTaxRuleGroup = new TaxRulesGroup( + $objProduct->id_tax_rules_group, + $this->context->language->id + ))) { $associationInfo['default_tax_rules_group_name'] = $objTaxRuleGroup->name; } @@ -3100,9 +3097,13 @@ class AdminProductsControllerCore extends AdminController $associatedServiceProducts[$associationInfo['position']] = $serviceProduct; } else { $serviceProduct['is_associated'] = false; - - $objTaxRulesGroup = new TaxRulesGroup($serviceProduct['id_tax_rules_group'], $this->context->language->id); - $serviceProduct['tax_rules_group_name'] = $objTaxRulesGroup->name; + $serviceProduct['tax_rules_group_name'] = $this->l('No tax'); + if (Validate::isLoadedObject($objTaxRulesGroup = new TaxRulesGroup( + $serviceProduct['id_tax_rules_group'], + $this->context->language->id + ))) { + $serviceProduct['tax_rules_group_name'] = $objTaxRulesGroup->name; + } $unassociatedServiceProducts[] = $serviceProduct; } @@ -3135,35 +3136,9 @@ class AdminProductsControllerCore extends AdminController } if (Validate::isLoadedObject($objProduct = new Product($idProduct))) { - // validate submitted details - $availableServiceProducts = Tools::getValue('available_service_products'); - $objServiceProducts = array(); - foreach ($availableServiceProducts as $idServiceProduct) { - $prefix = 'service_product_'.$idServiceProduct.'_'; - - $isAssociated = in_array(Tools::getValue($prefix.'associated'), array('on', 'true', '1')); - $price = Tools::getValue($prefix.'price'); - $idTaxRulesGroup = Tools::getValue($prefix.'id_tax_rules_group'); - - if ($isAssociated) { - $objServiceProduct = new Product($idServiceProduct, false, $this->context->language->id); - if (Validate::isLoadedObject($objServiceProduct)) { - // cache for faster access in next foreach loop - $objServiceProducts[$idServiceProduct] = $objServiceProduct; - - if (!$price) { - $this->errors[] = sprintf($this->l('Price for service product \'%s\' is empty.'), $objServiceProduct->name); - } elseif (!Validate::isPrice($price)) { - $this->errors[] = sprintf($this->l('Price for service product \'%s\' is invalid.'), $objServiceProduct->name); - } - } else{ - $this->errors[] = sprintf($this->l('Service product #%s is not available.'), $idServiceProduct); - } - } - } - - if (!count($this->errors)) { - // save submitted details + if ($availableServiceProducts = Tools::getValue('available_service_products')) { + // validate submitted details + $objServiceProducts = array(); foreach ($availableServiceProducts as $idServiceProduct) { $prefix = 'service_product_'.$idServiceProduct.'_'; @@ -3172,59 +3147,86 @@ class AdminProductsControllerCore extends AdminController $idTaxRulesGroup = Tools::getValue($prefix.'id_tax_rules_group'); if ($isAssociated) { - $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); + $objServiceProduct = new Product($idServiceProduct, false, $this->context->language->id); + if (Validate::isLoadedObject($objServiceProduct)) { + // cache for faster access in next foreach loop + $objServiceProducts[$idServiceProduct] = $objServiceProduct; - // if already associated - if ($objRoomTypeServiceProduct->isRoomTypeLinkedWithProduct($idProduct, $idServiceProduct)) { - $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); - $priceInfo = $objRoomTypeServiceProductPrice->getProductRoomTypeLinkPriceInfo( - $idServiceProduct, - $idProduct, - RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE - ); + if (!$price) { + $this->errors[] = sprintf($this->l('Price for service product \'%s\' is empty.'), $objServiceProduct->name); + } elseif (!Validate::isPrice($price)) { + $this->errors[] = sprintf($this->l('Price for service product \'%s\' is invalid.'), $objServiceProduct->name); + } + } else{ + $this->errors[] = sprintf($this->l('Service product #%s is not available.'), $idServiceProduct); + } + } + } - $objRoomTypeServiceProductPrice = null; - if ($priceInfo) { - $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice($priceInfo['id_room_type_service_product_price']); + if (!count($this->errors)) { + // save submitted details + foreach ($availableServiceProducts as $idServiceProduct) { + $prefix = 'service_product_'.$idServiceProduct.'_'; + + $isAssociated = in_array(Tools::getValue($prefix.'associated'), array('on', 'true', '1')); + $price = Tools::getValue($prefix.'price'); + $idTaxRulesGroup = Tools::getValue($prefix.'id_tax_rules_group'); + + if ($isAssociated) { + $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); + + // if already associated + if ($objRoomTypeServiceProduct->isRoomTypeLinkedWithProduct($idProduct, $idServiceProduct)) { + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + $priceInfo = $objRoomTypeServiceProductPrice->getProductRoomTypeLinkPriceInfo( + $idServiceProduct, + $idProduct, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + ); + + $objRoomTypeServiceProductPrice = null; + if ($priceInfo) { + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice($priceInfo['id_room_type_service_product_price']); + } else { + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); + $objRoomTypeServiceProductPrice->id_product = $idServiceProduct; + $objRoomTypeServiceProductPrice->id_element = $idProduct; + $objRoomTypeServiceProductPrice->element_type = RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE; + } + $objRoomTypeServiceProductPrice->price = $price; + $objRoomTypeServiceProductPrice->id_tax_rules_group = $idTaxRulesGroup; + $objRoomTypeServiceProductPrice->save(); } else { + // create new association + $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); + $objRoomTypeServiceProduct->addRoomProductLink( + $idServiceProduct, + $idProduct, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + ); + $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); $objRoomTypeServiceProductPrice->id_product = $idServiceProduct; $objRoomTypeServiceProductPrice->id_element = $idProduct; $objRoomTypeServiceProductPrice->element_type = RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE; + $objRoomTypeServiceProductPrice->price = $price; + $objRoomTypeServiceProductPrice->id_tax_rules_group = $idTaxRulesGroup; + $objRoomTypeServiceProductPrice->save(); } - $objRoomTypeServiceProductPrice->price = $price; - $objRoomTypeServiceProductPrice->id_tax_rules_group = $idTaxRulesGroup; - $objRoomTypeServiceProductPrice->save(); } else { - // create new association - $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); - $objRoomTypeServiceProduct->addRoomProductLink( + // remove association + RoomTypeServiceProduct::deleteRoomProductLink( $idServiceProduct, - $idProduct, - RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE, + $idProduct ); - $objRoomTypeServiceProductPrice = new RoomTypeServiceProductPrice(); - $objRoomTypeServiceProductPrice->id_product = $idServiceProduct; - $objRoomTypeServiceProductPrice->id_element = $idProduct; - $objRoomTypeServiceProductPrice->element_type = RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE; - $objRoomTypeServiceProductPrice->price = $price; - $objRoomTypeServiceProductPrice->id_tax_rules_group = $idTaxRulesGroup; - $objRoomTypeServiceProductPrice->save(); + RoomTypeServiceProductPrice::deleteRoomProductPrices( + $idServiceProduct, + RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE, + $idProduct + ); } - } else { - // remove association - RoomTypeServiceProduct::deleteRoomProductLink( - $idServiceProduct, - RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE, - $idProduct - ); - - RoomTypeServiceProductPrice::deleteRoomProductPrices( - $idServiceProduct, - RoomTypeServiceProduct::WK_ELEMENT_TYPE_ROOM_TYPE, - $idProduct - ); } } } diff --git a/controllers/admin/AdminSearchController.php b/controllers/admin/AdminSearchController.php index 1b4812a05..fd856dbfe 100644 --- a/controllers/admin/AdminSearchController.php +++ b/controllers/admin/AdminSearchController.php @@ -238,24 +238,13 @@ class AdminSearchControllerCore extends AdminController ) { $objRoomType = new HotelRoomType(); $accessibleHotels = HotelBranchInformation::getProfileAccessedHotels($this->context->employee->id_profile, 1, 1); - $objRoomTypeServiceProduct = new RoomTypeServiceProduct(); foreach ($this->_list['products'] as $key => $product) { if ($roomInfo = $objRoomType->getRoomTypeInfoByIdProduct($product['id_product'])) { if (!in_array($roomInfo['id_hotel'], $accessibleHotels)) { unset($this->_list['products'][$key]); } - } else if ($associatedData = $objRoomTypeServiceProduct->getAssociatedHotelsAndRoomType($product['id_product'])) { - $count = 0; - foreach ($associatedData['room_type'] as $id_room_type) { - $objRoomType = new HotelRoomType($id_room_type); - if (in_array($objRoomType->id_hotel, $accessibleHotels)) { - $count += 1; - } - } - - if ($count) { - $this->_list['service_products'][] = $this->_list['products'][$key]; - } + } else if (!Product::isBookingProduct($product['id_product'])) { + $this->_list['service_products'][] = $this->_list['products'][$key]; unset($this->_list['products'][$key]); } diff --git a/controllers/admin/AdminTabsController.php b/controllers/admin/AdminTabsController.php index d2449776f..509a55057 100644 --- a/controllers/admin/AdminTabsController.php +++ b/controllers/admin/AdminTabsController.php @@ -324,7 +324,7 @@ class AdminTabsControllerCore extends AdminController break; } } - } else { + } else if ($this->display == 'add') { // Temporary add the position depend of the selection of the parent category if (!Tools::isSubmit('id_tab')) { // @todo Review $_POST['position'] = Tab::getNbTabs(Tools::getValue('id_parent')); diff --git a/docs/csv_import/customers_import.csv b/docs/csv_import/customers_import.csv index 9f801fc00..a44de3d3d 100644 --- a/docs/csv_import/customers_import.csv +++ b/docs/csv_import/customers_import.csv @@ -1,10 +1,10 @@ -Customer ID;Active (0/1);Titles ID (Mr = 1, Mrs = 2, else 0);Email *;Password *;Birthday (yyyy-mm-dd);Last Name *;First Name *;Newsletter (0/1);Opt-in (0/1);Groups (x,y,z...);Default Group ID -1;1;1;peterpan@qloapps.com;123456789;1987-04-02;Pan;Peter;1;0;Customer;3 -2;1;2;monkeydluffy@qloapps.com;123456789;1989-11-12;Luffy;Monkey D;1;0;Customer;3 -3;1;2;tigerlily@qloapps.com;123456789;1982-02-28;Lily;Tiger;1;0;Customer;3 -4;1;1;jameshook@qloapps.com;123456789;1952-12-04;Hook;James;0;1;Guest;2 -5;1;2;tinkerbell@qloapps.com;123456789;1900-01-01;Bell;Tinker;1;1;Customer;3 -6;1;1;bobsmee@qloapps.com;123456789;1971-10-21;Smee;Bob;1;1;Customer;3 -7;1;1;tictaccrocodile@qloapps.com;123456789;1991-09-27;Crocodile;Tictac;0;0;Guest;2 -8;1;1;jackbanning@qloapps.com;123456789;2003-07-05;Banning;Jack;0;0;Visitor;1 -9;0;2;maggiedoe@qloapps.com;123456789;2006-02-27;Doe;Maggie;0;0;Visitor;1 +Customer ID;Active (0/1);Titles ID (Mr = 1, Mrs = 2, else 0);Email *;Password *;Birthday (yyyy-mm-dd);Last Name *;First Name *;Phone;Newsletter (0/1);Opt-in (0/1);Groups (x,y,z...);Default Group ID +1;1;1;peterpan@qloapps.com;123456789;1987-04-02;Pan;Peter;1234567890;1;0;Customer;3 +2;1;2;monkeydluffy@qloapps.com;123456789;1989-11-12;Luffy;Monkey D;1234567890;1;0;Customer;3 +3;1;2;tigerlily@qloapps.com;123456789;1982-02-28;Lily;Tiger;1234567890;1;0;Customer;3 +4;1;1;jameshook@qloapps.com;123456789;1952-12-04;Hook;James;1234567890;0;1;Guest;2 +5;1;2;tinkerbell@qloapps.com;123456789;1900-01-01;Bell;Tinker;1234567890;1;1;Customer;3 +6;1;1;bobsmee@qloapps.com;123456789;1971-10-21;Smee;Bob;1234567890;1;1;Customer;3 +7;1;1;tictaccrocodile@qloapps.com;123456789;1991-09-27;Crocodile;Tictac;1234567890;0;0;Guest;2 +8;1;1;jackbanning@qloapps.com;123456789;2003-07-05;Banning;Jack;1234567890;0;0;Visitor;1 +9;0;2;maggiedoe@qloapps.com;123456789;2006-02-27;Doe;Maggie;1234567890;0;0;Visitor;1 diff --git a/modules/blocknavigationmenu/controllers/admin/AdminCustomNavigationLinkSettingController.php b/modules/blocknavigationmenu/controllers/admin/AdminCustomNavigationLinkSettingController.php index dd1b59fb4..61be47752 100644 --- a/modules/blocknavigationmenu/controllers/admin/AdminCustomNavigationLinkSettingController.php +++ b/modules/blocknavigationmenu/controllers/admin/AdminCustomNavigationLinkSettingController.php @@ -217,15 +217,15 @@ class AdminCustomNavigationLinkSettingController extends ModuleAdminController } if ($cmsCategories = CMSCategory::getCategories($this->context->language->id, false, false)) { - if (!isset($smartyVars['id_cms_category'])) { - $smartyVars['id_cms_category'] = $cmsCategories[0]['id_cms_category']; - } - foreach ($cmsCategories as $cmsCatKey => $cmsCategory) { if (!isset($categoryWiseCmsPages[$cmsCategory['id_cms_category']])) { unset($cmsCategories[$cmsCatKey]); } } + + if (!isset($smartyVars['id_cms_category']) && $cmsCategories) { + $smartyVars['id_cms_category'] = reset($cmsCategories)['id_cms_category']; + } } $smartyVars['categoryWiseCmsPages'] = $categoryWiseCmsPages; diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index 3a1609409..761ab8be4 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -156,7 +156,7 @@ class Blocknewsletter extends Module public function hookActionValidateCartRule($params) { $customer = new Customer($params['context']->cart->id_customer); - if ($params['cart_rule']->code == Configuration::get('NW_VOUCHER_CODE')) { + if ($params['cart_rule']->code && $params['cart_rule']->code == Configuration::get('NW_VOUCHER_CODE')) { if ($customer->id && !$customer->is_guest) { if (count(Order::getCustomerOrders($customer->id))) { $params['isValidatedByModules'] = false; diff --git a/modules/dashactivity/dashactivity.php b/modules/dashactivity/dashactivity.php index 4f30c3beb..7bb1e1203 100644 --- a/modules/dashactivity/dashactivity.php +++ b/modules/dashactivity/dashactivity.php @@ -93,7 +93,7 @@ class Dashactivity extends Module array( 'dashactivity_config_form' => $this->renderConfigForm(), 'link' => $this->context->link, - 'new_customer_filter_link' => $this->context->link->getAdminLink('AdminCustomers').'&customerFilter_date_add[]='.$date_from.'&customerFilter_date_add[]='.$date_to + 'new_customer_filter_link' => $this->context->link->getAdminLink('AdminCustomers').'&customerFilter_a!date_add[]='.$date_from.'&customerFilter_a!date_add[]='.$date_to ) ); diff --git a/modules/dashproducts/dashproducts.php b/modules/dashproducts/dashproducts.php index c51bf7301..d71187b31 100644 --- a/modules/dashproducts/dashproducts.php +++ b/modules/dashproducts/dashproducts.php @@ -262,7 +262,7 @@ class DashProducts extends Module (!is_null($id_hotel) ? HotelBranchInformation::addHotelRestriction($id_hotel, 'hbd') : '').' GROUP BY hbd.`id_product` ORDER BY `sales` DESC - LIMIT '.(int) Configuration::get('DASHPRODUCT_NBR_SHOW_BEST_SELLER', 10) + LIMIT '.(int) Configuration::get('DASHPRODUCT_NBR_SHOW_BEST_SELLER') ); $body = array(); @@ -366,6 +366,7 @@ class DashProducts extends Module $products = $this->getTotalViewed($date_from, $date_to, (int)Configuration::get('DASHPRODUCT_NBR_SHOW_MOST_VIEWED'), $id_hotel); $body = array(); if (is_array($products) && count($products)) { + $objHotelRoomType = new HotelRoomType(); foreach ($products as $product) { $product_obj = new Product((int)$product['id_object'], true, $this->context->language->id); if (!Validate::isLoadedObject($product_obj)) { @@ -379,8 +380,8 @@ class DashProducts extends Module $img = ImageManager::thumbnail($path_to_image, 'product_mini_'.$product_obj->id.'.'.$this->context->controller->imageType, 45, $this->context->controller->imageType); } - $objHotelRoomType = new HotelRoomType($product_obj->id); - $objHotelBranch = new HotelBranchInformation($objHotelRoomType->id_hotel, $this->context->language->id); + $roomTypeInfo = $objHotelRoomType->getRoomTypeInfoByIdProduct($product_obj->id); + $objHotelBranch = new HotelBranchInformation($roomTypeInfo['id_hotel'], $this->context->language->id); $tr = array(); $tr[] = array( diff --git a/modules/hotelreservationsystem/classes/HotelCartBookingData.php b/modules/hotelreservationsystem/classes/HotelCartBookingData.php index 2a3bb3e5b..0cd9c678d 100644 --- a/modules/hotelreservationsystem/classes/HotelCartBookingData.php +++ b/modules/hotelreservationsystem/classes/HotelCartBookingData.php @@ -527,7 +527,7 @@ class HotelCartBookingData extends ObjectModel } } if ($res && $objCart->updateQty((int)($roomsRequired * $num_days), $id_product)) { - if ($id_room) { + if ($roomsRequired == 1) { return $obj_htl_cart_booking_data->id; } else { return true; @@ -1020,7 +1020,7 @@ class HotelCartBookingData extends ObjectModel $context->language->id ); $errors[] = sprintf( - 'You can not book rooms for hotel \'%s\' after date %s. Please remove rooms from %s - %s to proceed.', + $objModule->l('You can not book rooms for hotel "%s" after date %s. Please remove rooms from %s - %s to proceed.', 'HotelOrderRestrictDate'), $objHotelBranchInformation->hotel_name, Tools::displayDate($maxOrderDate), Tools::displayDate($roomData['date_from']), @@ -1040,7 +1040,7 @@ class HotelCartBookingData extends ObjectModel $context->language->id ); $errors[] = sprintf( - 'You can not book rooms for hotel \'%s\' before date %s. Please remove rooms from %s - %s to proceed.', + $objModule->l('You can not book rooms for hotel "%s" before date %s. Please remove rooms from %s - %s to proceed.', 'HotelOrderRestrictDate'), $objHotelBranchInformation->hotel_name, Tools::displayDate($minOrderDate), Tools::displayDate($roomData['date_from']), @@ -1066,9 +1066,9 @@ class HotelCartBookingData extends ObjectModel } } if ($isRoomBooked) { - $errors[] = sprintf($objModule->l('The Room \'%s\' has been booked by another customer from \'%s\' to \'%s\' Please remove rooms from cart to proceed', 'HotelOrderRestrictDate'), $product['name'], date('d-m-Y', strtotime($roomData['date_from'])), date('d-m-Y', strtotime($roomData['date_to']))); + $errors[] = sprintf($objModule->l('The Room "%s" has been booked by another customer from "%s" to "%s" Please remove rooms from cart to proceed', 'HotelOrderRestrictDate'), $product['name'], date('d-m-Y', strtotime($roomData['date_from'])), date('d-m-Y', strtotime($roomData['date_to']))); } else { - $errors[] = sprintf($objModule->l('The Room \'%s\' is no longer avalable from \'%s\' to \'%s\' Please remove rooms from cart to proceed', 'HotelOrderRestrictDate'), $product['name'], date('d-m-Y', strtotime($roomData['date_from'])), date('d-m-Y', strtotime($roomData['date_to']))); + $errors[] = sprintf($objModule->l('The Room "%s" is no longer available from "%s" to "%s" Please remove rooms from cart to proceed', 'HotelOrderRestrictDate'), $product['name'], date('d-m-Y', strtotime($roomData['date_from'])), date('d-m-Y', strtotime($roomData['date_to']))); } } } diff --git a/modules/hotelreservationsystem/classes/HotelHelper.php b/modules/hotelreservationsystem/classes/HotelHelper.php index dc442e32d..373da959c 100644 --- a/modules/hotelreservationsystem/classes/HotelHelper.php +++ b/modules/hotelreservationsystem/classes/HotelHelper.php @@ -2229,15 +2229,7 @@ class HotelHelper $idProduct = $objProduct->id; // add to applicable categories - $objCategory = new Category($serviceProduct['id_category_default']); - if ($categories = $objCategory->getParentsCategories()) { - $idsCategory = array(); - foreach ($categories as $category) { - $idsCategory[] = $category['id_category']; - } - $objProduct->addToCategories($idsCategory); - } - + $objProduct->addToCategories($serviceProduct['id_category_default']); Search::indexation(Tools::link_rewrite($serviceProdDataDefult['name']), $idProduct); StockAvailable::updateQuantity($idProduct, null, 999999999); diff --git a/modules/hotelreservationsystem/hotelreservationsystem.php b/modules/hotelreservationsystem/hotelreservationsystem.php index 9ecea89e2..959653aa2 100644 --- a/modules/hotelreservationsystem/hotelreservationsystem.php +++ b/modules/hotelreservationsystem/hotelreservationsystem.php @@ -51,7 +51,7 @@ class HotelReservationSystem extends Module 'feature_prices' => array('description' => 'Feature prices', 'class' => 'HotelRoomTypeFeaturePricing'), 'advance_payments' => array('description' => 'Room type advance payment', 'class' => 'HotelAdvancedPayment'), 'cart_bookings' => array('description' => 'Cart bookings', 'class' => 'HotelCartBookingData'), - 'bookings' => array('description' => 'Order bookings', 'class' => 'HotelBookingDetail'), + 'room_bookings' => array('description' => 'Room bookings', 'class' => 'HotelBookingDetail'), 'booking_extra_demands' => array('description' => 'Booking extra demands', 'class' => 'HotelBookingDemands'), 'extra_demands' => array('description' => 'Extra demands', 'class' => 'HotelRoomTypeGlobalDemand'), 'extra_services' => array('description' => 'Extra services', 'class' => 'Product', 'parameters_attribute' => 'webserviceRoomTypeServicesParameters'), diff --git a/modules/hotelreservationsystem/views/js/admin/hotel_rooms_booking.js b/modules/hotelreservationsystem/views/js/admin/hotel_rooms_booking.js index 5ea3c2f5e..86850226e 100644 --- a/modules/hotelreservationsystem/views/js/admin/hotel_rooms_booking.js +++ b/modules/hotelreservationsystem/views/js/admin/hotel_rooms_booking.js @@ -593,6 +593,7 @@ $(document).ready(function() { $(document).on('click', '.booking_occupancy_wrapper .remove-room-link', function(e) { e.preventDefault(); + e.stopPropagation(); booking_occupancy_inner = $(this).closest('.booking_occupancy_inner'); $(this).closest('.occupancy_info_block').remove(); @@ -780,8 +781,8 @@ $(document).ready(function() { occupancy_block += '
'; occupancy_block += '
'; occupancy_block += '
'; + occupancy_block += '
'; occupancy_block += '
'; - occupancy_block += '
'; $(booking_occupancy_wrapper).find('.booking_occupancy_inner').append(occupancy_block); diff --git a/modules/qlohotelreview/classes/QhrHotelReviewHelper.php b/modules/qlohotelreview/classes/QhrHotelReviewHelper.php index 6d5b711d7..25cff78b8 100644 --- a/modules/qlohotelreview/classes/QhrHotelReviewHelper.php +++ b/modules/qlohotelreview/classes/QhrHotelReviewHelper.php @@ -29,6 +29,8 @@ class QhrHotelReviewHelper return (bool) !Db::getInstance()->getValue( 'SELECT COUNT(*) FROM `'._DB_PREFIX_.'htl_booking_detail` hbd WHERE hbd.`id_status` != '.(int) HotelBookingDetail::STATUS_CHECKED_OUT.' + AND hbd.`is_refunded` != 1 + AND hbd.`is_cancelled` != 1 AND hbd.`id_order` = '.(int) $idOrder ); } @@ -57,6 +59,11 @@ class QhrHotelReviewHelper return false; } + $objOrder = new Order($idOrder); + if ($objOrder->hasCompletelyRefunded()) { + return false; + } + if (self::getIsOrderCheckedOut($idOrder)) { return true; } @@ -120,12 +127,6 @@ class QhrHotelReviewHelper ); } - public static function getPathForCreation($idHotelReview) - { - $folders = str_split((string)$id_image); - return implode('/', $folders).'/'; - } - public static function createDirectory($dir) { if (!file_exists($dir)) {