<?php
class ControllerApiOrder extends Controller {
	public function add() {
		$this->load->language('api/order');

		$json = array();

		if (!isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			// Customer
			if (!isset($this->session->data['customer'])) {
				$json['error'] = $this->language->get('error_customer');
			}

			// Payment Address
			/*if (!isset($this->session->data['payment_address'])) {
				$json['error'] = $this->language->get('error_payment_address');
			}*/

			// Payment Method
			/*if (!$json && !empty($this->request->post['payment_method'])) {
				if (empty($this->session->data['payment_methods'])) {
					$json['error'] = $this->language->get('error_no_payment');
				} elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) {
					$json['error'] = $this->language->get('error_payment_method');
				}

				if (!$json) {
					$this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
				}
			}

			if (!isset($this->session->data['payment_method'])) {
				$json['error'] = $this->language->get('error_payment_method');
			}*/

			// Shipping
			if ($this->cart->hasShipping()) {
				// Shipping Address
				if (!isset($this->session->data['shipping_address'])) {
					$json['error'] = $this->language->get('error_shipping_address');
				}

				// Shipping Method
				if (!$json && !empty($this->request->post['shipping_method'])) {
					if (empty($this->session->data['shipping_methods'])) {
						$json['error'] = $this->language->get('error_no_shipping');
					} else {
						$shipping = explode('.', $this->request->post['shipping_method']);

						if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
							$json['error'] = $this->language->get('error_shipping_method');
						}
					}

					if (!$json) {
						$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
					}
				}

				// Shipping Method
				if (!isset($this->session->data['shipping_method'])) {
					$json['error'] = $this->language->get('error_shipping_method');
				}
			} else {
				unset($this->session->data['shipping_address']);
				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
			}

			// Cart
			if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
				$json['error'] = $this->language->get('error_stock');
			}

			// Validate minimum quantity requirements.
			$products = $this->cart->getProducts();

			foreach ($products as $product) {
				$product_total = 0;

				foreach ($products as $product_2) {
					if ($product_2['product_id'] == $product['product_id']) {
						$product_total += $product_2['quantity'];
					}
				}

				if ($product['minimum'] > $product_total) {
					$json['error'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);

					break;
				}
			}

			if (!$json) {
				$json['success'] = $this->language->get('text_success');

				$order_data = array();

				// Store Details
				$order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
				$order_data['store_id'] = $this->config->get('config_store_id');
				$order_data['store_name'] = $this->config->get('config_name');
				$order_data['store_url'] = $this->config->get('config_url');

				// Customer Details
				$order_data['customer_id'] = $this->session->data['customer']['customer_id'];
				$order_data['customer_group_id'] = $this->session->data['customer']['customer_group_id'];
				$order_data['firstname'] = $this->session->data['customer']['firstname'];
				$order_data['lastname'] = $this->session->data['customer']['lastname'];
				$order_data['email'] = $this->session->data['customer']['email'] ? $this->session->data['customer']['email'] : 'there-is@no.email';
				$order_data['telephone'] = $this->session->data['customer']['telephone'];
				$order_data['fax'] = $this->session->data['customer']['fax'];
				$order_data['custom_field'] = $this->session->data['customer']['custom_field'];

				// Payment Details
				$order_data['payment_firstname'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['firstname'] : '';
				$order_data['payment_lastname'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['lastname'] : '';
				$order_data['payment_company'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['company'] : '';
				$order_data['payment_address_1'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['address_1'] : '';
				$order_data['payment_address_2'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['address_2'] : '';
				$order_data['payment_city'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['city'] : '';
				$order_data['payment_postcode'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['postcode'] : '';
				$order_data['payment_zone'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['zone'] : '';
				$order_data['payment_zone_id'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['zone_id'] : '';
				$order_data['payment_country'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['country'] : '';
				$order_data['payment_country_id'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['country_id'] : '';
				$order_data['payment_address_format'] = isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['address_format'] : '';
				$order_data['payment_custom_field'] = (isset($this->session->data['payment_address']) ? $this->session->data['payment_address']['custom_field'] : '');

				if (isset($this->session->data['payment_method']['title'])) {
					$order_data['payment_method'] = $this->session->data['payment_method']['title'];
				} else {
					$order_data['payment_method'] = '';
				}

				if (isset($this->session->data['payment_method']['code'])) {
					$order_data['payment_code'] = $this->session->data['payment_method']['code'];
				} else {
					$order_data['payment_code'] = '';
				}

				// Shipping Details
				if ($this->cart->hasShipping()) {
					$order_data['shipping_firstname'] = $this->session->data['shipping_address']['firstname'];
					$order_data['shipping_lastname'] = $this->session->data['shipping_address']['lastname'];
					$order_data['shipping_company'] = $this->session->data['shipping_address']['company'];
					$order_data['shipping_address_1'] = $this->session->data['shipping_address']['address_1'];
					$order_data['shipping_address_2'] = $this->session->data['shipping_address']['address_2'];
					$order_data['shipping_city'] = $this->session->data['shipping_address']['city'];
					$order_data['shipping_postcode'] = $this->session->data['shipping_address']['postcode'];
					$order_data['shipping_zone'] = $this->session->data['shipping_address']['zone'];
					$order_data['shipping_zone_id'] = $this->session->data['shipping_address']['zone_id'];
					$order_data['shipping_country'] = $this->session->data['shipping_address']['country'];
					$order_data['shipping_country_id'] = $this->session->data['shipping_address']['country_id'];
					$order_data['shipping_address_format'] = $this->session->data['shipping_address']['address_format'];
					$order_data['shipping_custom_field'] = (isset($this->session->data['shipping_address']['custom_field']) ? $this->session->data['shipping_address']['custom_field'] : array());

					if (isset($this->session->data['shipping_method']['title'])) {
						$order_data['shipping_method'] = $this->session->data['shipping_method']['title'];
					} else {
						$order_data['shipping_method'] = '';
					}

					if (isset($this->session->data['shipping_method']['code'])) {
						$order_data['shipping_code'] = $this->session->data['shipping_method']['code'];
					} else {
						$order_data['shipping_code'] = '';
					}
				} else {
					$order_data['shipping_firstname'] = '';
					$order_data['shipping_lastname'] = '';
					$order_data['shipping_company'] = '';
					$order_data['shipping_address_1'] = '';
					$order_data['shipping_address_2'] = '';
					$order_data['shipping_city'] = '';
					$order_data['shipping_postcode'] = '';
					$order_data['shipping_zone'] = '';
					$order_data['shipping_zone_id'] = '';
					$order_data['shipping_country'] = '';
					$order_data['shipping_country_id'] = '';
					$order_data['shipping_address_format'] = '';
					$order_data['shipping_custom_field'] = array();
					$order_data['shipping_method'] = '';
					$order_data['shipping_code'] = '';
				}

				// Products
				$order_data['products'] = array();

				foreach ($this->cart->getProducts() as $product) {
					$option_data = array();

					foreach ($product['option'] as $option) {
						$option_data[] = array(
							'product_option_id'       => $option['product_option_id'],
							'product_option_value_id' => $option['product_option_value_id'],
							'option_id'               => $option['option_id'],
							'option_value_id'         => $option['option_value_id'],
							'name'                    => $option['name'],
							'value'                   => $option['value'],
							'type'                    => $option['type']
						);
					}

					$order_data['products'][] = array(
						'product_id' => $product['product_id'],
						'name'       => $product['name'],
						'model'      => $product['model'],
						'option'     => $option_data,
						'download'   => $product['download'],
						'status'     => $product['status'],
						'quantity'   => $product['quantity'],
						'subtract'   => $product['subtract'],
                        'booking_day'=> $product['booking_day'],
                        'booking_date'=> $product['booking_day'],
						'price'      => $product['price'],
						'total'      => $product['total'],
						'total_raw'      => $product['total'],
						'tax'        => $this->tax->getTax($product['price'], $product['tax_class_id']),
						'reward'     => $product['reward']
					);
				}

				// Gift Voucher
				$order_data['vouchers'] = array();

				if (!empty($this->session->data['vouchers'])) {
					foreach ($this->session->data['vouchers'] as $voucher) {
						$order_data['vouchers'][] = array(
							'description'      => $voucher['description'],
							'code'             => token(10),
							'to_name'          => $voucher['to_name'],
							'to_email'         => $voucher['to_email'],
							'from_name'        => $voucher['from_name'],
							'from_email'       => $voucher['from_email'],
							'voucher_theme_id' => $voucher['voucher_theme_id'],
							'message'          => $voucher['message'],
							'amount'           => $voucher['amount']
						);
					}
				}

				// Order Totals
				$this->load->model('extension/extension');

				$totals = array();
				$taxes = $this->cart->getTaxes();
				$total = 0;

				// Because __call can not keep var references so we put them into an array.
				$total_data = array(
					'totals' => &$totals,
					'taxes'  => &$taxes,
					'total'  => &$total
				);

				$sort_order = array();

				$results = $this->model_extension_extension->getExtensions('total');

				foreach ($results as $key => $value) {
					$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
				}

				array_multisort($sort_order, SORT_ASC, $results);

				foreach ($results as $result) {
					if ($this->config->get($result['code'] . '_status')) {
						$this->load->model('extension/total/' . $result['code']);

						// We have to put the totals in an array so that they pass by reference.
						$this->{'model_extension_total_' . $result['code']}->getTotal($total_data, $order_data['products']);
					}
				}

				$sort_order = array();

				foreach ($total_data['totals'] as $key => $value) {
					$sort_order[$key] = $value['sort_order'];
				}

				array_multisort($sort_order, SORT_ASC, $total_data['totals']);

				$order_data = array_merge($order_data, $total_data);

                $this->load->model('checkout/order');

				if(isset($this->request->get['user_id'])) {
                    $who_added = $this->model_checkout_order->getUserName($this->request->get['user_id']);
                } else {
                    $who_added = '';
                }

				if (isset($this->request->post['comment'])) {
					$order_data['comment'] = $this->request->post['comment'];
				} else {
					$order_data['comment'] = '';
				}

				if (isset($this->request->post['affiliate_id'])) {
					$subtotal = $this->cart->getSubTotal();

					// Affiliate
					$this->load->model('affiliate/affiliate');

					$affiliate_info = $this->model_affiliate_affiliate->getAffiliate($this->request->post['affiliate_id']);

					if ($affiliate_info) {
						$order_data['affiliate_id'] = $affiliate_info['affiliate_id'];
						$order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
					} else {
						$order_data['affiliate_id'] = 0;
						$order_data['commission'] = 0;
					}

					// Marketing
					$order_data['marketing_id'] = 0;
					$order_data['tracking'] = '';
				} else {
					$order_data['affiliate_id'] = 0;
					$order_data['commission'] = 0;
					$order_data['marketing_id'] = 0;
					$order_data['tracking'] = '';
				}

				$order_data['language_id'] = $this->config->get('config_language_id');
				$order_data['currency_id'] = $this->currency->getId($this->session->data['currency']);
				$order_data['currency_code'] = $this->session->data['currency'];
				$order_data['currency_value'] = $this->currency->getValue($this->session->data['currency']);
				$order_data['ip'] = $this->request->server['REMOTE_ADDR'];

				if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
					$order_data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
				} elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) {
					$order_data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
				} else {
					$order_data['forwarded_ip'] = '';
				}

				if (isset($this->request->server['HTTP_USER_AGENT'])) {
					$order_data['user_agent'] = $this->request->server['HTTP_USER_AGENT'];
				} else {
					$order_data['user_agent'] = '';
				}

				if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) {
					$order_data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE'];
				} else {
					$order_data['accept_language'] = '';
				}

				$json['order_id'] = $this->model_checkout_order->addOrder($order_data);

				// Set the order history
				if (isset($this->request->post['order_status_id'])) {
					$order_status_id = $this->request->post['order_status_id'];
				} else {
					$order_status_id = $this->config->get('config_order_status_id');
				}

				$this->model_checkout_order->addOrderHistory($json['order_id'], $order_status_id, sprintf($this->language->get('text_added_by'), $who_added));

				// clear cart since the order has already been successfully stored.
				//$this->cart->clear();
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function edit() {
		$this->load->language('api/order');

		$json = array();

		if (!isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$this->load->model('checkout/order');

			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				// Customer
				if (!isset($this->session->data['customer'])) {
					$json['error'] = $this->language->get('error_customer');
				}

				// Payment Address
				/*if (!isset($this->session->data['payment_address'])) {
					$json['error'] = $this->language->get('error_payment_address');
				}*/

				// Payment Method
				/*if (!$json && !empty($this->request->post['payment_method'])) {
					if (empty($this->session->data['payment_methods'])) {
						$json['error'] = $this->language->get('error_no_payment');
					} elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) {
						$json['error'] = $this->language->get('error_payment_method');
					}

					if (!$json) {
						$this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
					}
				}

				if (!isset($this->session->data['payment_method'])) {
					$json['error'] = $this->language->get('error_payment_method');
				}*/

				// Shipping
				/*if ($this->cart->hasShipping()) {
					// Shipping Address
					if (!isset($this->session->data['shipping_address'])) {
						$json['error'] = $this->language->get('error_shipping_address');
					}

					// Shipping Method
					if (!$json && !empty($this->request->post['shipping_method'])) {
						if (empty($this->session->data['shipping_methods'])) {
							$json['error'] = $this->language->get('error_no_shipping');
						} else {
							$shipping = explode('.', $this->request->post['shipping_method']);

							if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
								$json['error'] = $this->language->get('error_shipping_method');
							}
						}

						if (!$json) {
							$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
						}
					}

					if (!isset($this->session->data['shipping_method'])) {
						$json['error'] = $this->language->get('error_shipping_method');
					}
				} else {
					unset($this->session->data['shipping_address']);
					unset($this->session->data['shipping_method']);
					unset($this->session->data['shipping_methods']);
				}*/

				// Cart
				if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
					$json['error'] = $this->language->get('error_stock');
				}

				// Validate minimum quantity requirements.
				$products = $this->cart->getProducts();

				foreach ($products as $product) {
					$product_total = 0;

					foreach ($products as $product_2) {
						if ($product_2['product_id'] == $product['product_id']) {
							$product_total += $product_2['quantity'];
						}
					}

					if ($product['minimum'] > $product_total) {
						$json['error'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);

						break;
					}
				}

				if (!$json) {
					$json['success'] = $this->language->get('text_success');

					$order_data = array();

					// Store Details
					$order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
					$order_data['store_id'] = $this->config->get('config_store_id');
					$order_data['store_name'] = $this->config->get('config_name');
					$order_data['store_url'] = $this->config->get('config_url');

					// Customer Details
					$order_data['customer_id'] = $this->session->data['customer']['customer_id'];
					$order_data['customer_group_id'] = $this->session->data['customer']['customer_group_id'];
					$order_data['firstname'] = $this->session->data['customer']['firstname'];
					$order_data['lastname'] = $this->session->data['customer']['lastname'];
					$order_data['email'] = $this->session->data['customer']['email'] ? $this->session->data['customer']['email'] : 'there-is@no.email';
					$order_data['telephone'] = $this->session->data['customer']['telephone'];
					$order_data['fax'] = $this->session->data['customer']['fax'];
					$order_data['custom_field'] = $this->session->data['customer']['custom_field'];

					// Payment Details
					$order_data['payment_firstname'] = isset($this->session->data['payment_address']['firstname']) ? $this->session->data['payment_address']['firstname'] : '';
					$order_data['payment_lastname'] = isset($this->session->data['payment_address']['lastname']) ? $this->session->data['payment_address']['lastname'] : '';
					$order_data['payment_company'] = isset($this->session->data['payment_address']['company']) ? $this->session->data['payment_address']['company'] : '';
					$order_data['payment_address_1'] = isset($this->session->data['payment_address']['address_1']) ? $this->session->data['payment_address']['address_1'] : '';
					$order_data['payment_address_2'] = isset($this->session->data['payment_address']['address_2']) ? $this->session->data['payment_address']['address_2'] : '';
					$order_data['payment_city'] = isset($this->session->data['payment_address']['city']) ? $this->session->data['payment_address']['city'] : '';
					$order_data['payment_postcode'] = isset($this->session->data['payment_address']['postcode']) ? $this->session->data['payment_address']['postcode'] : '';
					$order_data['payment_zone'] = isset($this->session->data['payment_address']['zone']) ? $this->session->data['payment_address']['zone'] : '';
					$order_data['payment_zone_id'] = isset($this->session->data['payment_address']['zone_id']) ? $this->session->data['payment_address']['zone_id'] : '';
					$order_data['payment_country'] = isset($this->session->data['payment_address']['country']) ? $this->session->data['payment_address']['country'] : '';
					$order_data['payment_country_id'] = isset($this->session->data['payment_address']['country_id']) ? $this->session->data['payment_address']['country_id'] : '';
					$order_data['payment_address_format'] = isset($this->session->data['payment_address']['address_format']) ? $this->session->data['payment_address']['address_format'] : '';
					$order_data['payment_custom_field'] = isset($this->session->data['payment_address']['custom_field']) ? $this->session->data['payment_address']['custom_field'] : '';

					if (isset($this->session->data['payment_method']['title'])) {
						$order_data['payment_method'] = $this->session->data['payment_method']['title'];
					} else {
						$order_data['payment_method'] = '';
					}

					if (isset($this->session->data['payment_method']['code'])) {
						$order_data['payment_code'] = $this->session->data['payment_method']['code'];
					} else {
						$order_data['payment_code'] = '';
					}

					// Shipping Details
					if ($this->cart->hasShipping()) {
						$order_data['shipping_firstname'] = '';//$this->session->data['shipping_address']['firstname'];
						$order_data['shipping_lastname'] = '';//$this->session->data['shipping_address']['lastname'];
						$order_data['shipping_company'] = '';//$this->session->data['shipping_address']['company'];
						$order_data['shipping_address_1'] =''; //$this->session->data['shipping_address']['address_1'];
						$order_data['shipping_address_2'] =''; //$this->session->data['shipping_address']['address_2'];
						$order_data['shipping_city'] = '';//$this->session->data['shipping_address']['city'];
						$order_data['shipping_postcode'] = '';//$this->session->data['shipping_address']['postcode'];
						$order_data['shipping_zone'] = '';//$this->session->data['shipping_address']['zone'];
						$order_data['shipping_zone_id'] = '';//$this->session->data['shipping_address']['zone_id'];
						$order_data['shipping_country'] = '';//$this->session->data['shipping_address']['country'];
						$order_data['shipping_country_id'] =''; //$this->session->data['shipping_address']['country_id'];
						$order_data['shipping_address_format'] =''; //$this->session->data['shipping_address']['address_format'];
						$order_data['shipping_custom_field'] = '';//$this->session->data['shipping_address']['custom_field'];

						if (isset($this->session->data['shipping_method']['title'])) {
							$order_data['shipping_method'] = $this->session->data['shipping_method']['title'];
						} else {
							$order_data['shipping_method'] = '';
						}

						if (isset($this->session->data['shipping_method']['code'])) {
							$order_data['shipping_code'] = $this->session->data['shipping_method']['code'];
						} else {
							$order_data['shipping_code'] = '';
						}
					} else {
						$order_data['shipping_firstname'] = '';
						$order_data['shipping_lastname'] = '';
						$order_data['shipping_company'] = '';
						$order_data['shipping_address_1'] = '';
						$order_data['shipping_address_2'] = '';
						$order_data['shipping_city'] = '';
						$order_data['shipping_postcode'] = '';
						$order_data['shipping_zone'] = '';
						$order_data['shipping_zone_id'] = '';
						$order_data['shipping_country'] = '';
						$order_data['shipping_country_id'] = '';
						$order_data['shipping_address_format'] = '';
						$order_data['shipping_custom_field'] = array();
						$order_data['shipping_method'] = '';
						$order_data['shipping_code'] = '';
					}

					// Products
					$order_data['products'] = array();

					foreach ($this->cart->getProducts() as $product) {
						$option_data = array();

						foreach ($product['option'] as $option) {
							$option_data[] = array(
								'product_option_id'       => $option['product_option_id'],
								'product_option_value_id' => $option['product_option_value_id'],
								'option_id'               => $option['option_id'],
								'option_value_id'         => $option['option_value_id'],
								'name'                    => $option['name'],
								'value'                   => $option['value'],
								'type'                    => $option['type']
							);
						}

						/*$statuses = $this->model_checkout_order->getStatuses($order_id, $product['product_id'], $product['booking_day'], $product['quantity']);

						$get_customer = $statuses['customer'];
						$get_confirm = $statuses['confirm'];
						$get_status = $statuses['status'];*/

						$order_data['products'][] = array(
							'product_id' => $product['product_id'],
							'name'       => $product['name'],
							'model'      => $product['model'],
                            'booking_day'=> $product['booking_day'],
                            'booking_date'=> $product['booking_day'],
                            'customer'   => $product['customer'],
                            'confirm'    => $product['confirm'],
                            'status'     => $product['status'],
							'option'     => $option_data,
							'download'   => $product['download'],
							'quantity'   => $product['quantity'],
							'subtract'   => $product['subtract'],
							'price'      => $product['price'],
							'total'      => $product['total'],
							'total_raw'      => $product['total'],
							'tax'        => $this->tax->getTax($product['price'], $product['tax_class_id']),
							'reward'     => $product['reward']
						);
					}

					// Gift Voucher
					$order_data['vouchers'] = array();

					if (!empty($this->session->data['vouchers'])) {
						foreach ($this->session->data['vouchers'] as $voucher) {
							$order_data['vouchers'][] = array(
								'description'      => $voucher['description'],
								'code'             => token(10),
								'to_name'          => $voucher['to_name'],
								'to_email'         => $voucher['to_email'],
								'from_name'        => $voucher['from_name'],
								'from_email'       => $voucher['from_email'],
								'voucher_theme_id' => $voucher['voucher_theme_id'],
								'message'          => $voucher['message'],
								'amount'           => $voucher['amount']
							);
						}
					}

					// Order Totals
					$this->load->model('extension/extension');

					$totals = array();
					$taxes = $this->cart->getTaxes();
					$total = 0;

					// Because __call can not keep var references so we put them into an array.
					$total_data = array(
						'totals' => &$totals,
						'taxes'  => &$taxes,
						'total'  => &$total
					);

					$sort_order = array();

					$results = $this->model_extension_extension->getExtensions('total');

					foreach ($results as $key => $value) {
						$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
					}

					array_multisort($sort_order, SORT_ASC, $results);

					foreach ($results as $result) {
						if ($this->config->get($result['code'] . '_status')) {
							$this->load->model('extension/total/' . $result['code']);

							// We have to put the totals in an array so that they pass by reference.
							$this->{'model_extension_total_' . $result['code']}->getTotal($total_data, $order_data['products']);
						}
					}

					$sort_order = array();

					foreach ($total_data['totals'] as $key => $value) {
						$sort_order[$key] = $value['sort_order'];
					}

					array_multisort($sort_order, SORT_ASC, $total_data['totals']);

					$order_data = array_merge($order_data, $total_data);

					if (isset($this->request->post['comment']) && !empty($this->request->post['comment'])) {
						$order_data['comment'] = $this->request->post['comment'];
					} else {
						$order_data['comment'] = '';
					}

					if (isset($this->request->post['affiliate_id'])) {
						$subtotal = $this->cart->getSubTotal();

						// Affiliate
						$this->load->model('affiliate/affiliate');

						$affiliate_info = $this->model_affiliate_affiliate->getAffiliate($this->request->post['affiliate_id']);

						if ($affiliate_info) {
							$order_data['affiliate_id'] = $affiliate_info['affiliate_id'];
							$order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
						} else {
							$order_data['affiliate_id'] = 0;
							$order_data['commission'] = 0;
						}
					} else {
						$order_data['affiliate_id'] = 0;
						$order_data['commission'] = 0;
					}

					$this->model_checkout_order->editOrder($order_id, $order_data);

					// Set the order history
					if (isset($this->request->post['order_status_id'])) {
						$order_status_id = $this->request->post['order_status_id'];
					} else {
						$order_status_id = $this->config->get('config_order_status_id');
					}

                    if(isset($this->request->get['user_id'])) {
                        $who_modify = $this->model_checkout_order->getUserName($this->request->get['user_id']);
                    } else {
                        $who_modify = '';
                    }

					$this->model_checkout_order->addOrderHistory($order_id, $order_status_id, sprintf($this->language->get('text_modify_by'), $who_modify));
				}
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function recalc_total() {
		$this->load->language('api/order');

		$json = array();

		if (false AND !isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$this->load->model('checkout/order');

			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}
			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {

				// Validate minimum quantity requirements.
				$products = $this->model_checkout_order->getOrderProducts($order_id);

				foreach ($products as $product) {
					$product_total = 0;

					foreach ($products as $product_2) {
						if ($product_2['product_id'] == $product['product_id']) {
							$product_total += $product_2['quantity'];
						}
					}
				}

					$order_data = array();

					$order_data['products'] = array();

					foreach ($products as $product) {
						$option_data = array();


						$order_data['products'][] = array(
							'product_id' => $product['product_id'],
							'name'       => $product['name'],
							'model'      => $product['model'],
                            'booking_date'=> $product['booking_date'],
                            'customer'   => $product['customer'],
                            'confirm'    => $product['confirm'],
                            'status'     => $product['status'],
							//'download'   => $product['download'],
							'quantity'   => $product['quantity'],
							//'subtract'   => $product['subtract'],
							'price'      => $product['price'],
							'total'      => $product['total'],
							'total_raw'      => $product['total'],
							'tax'        => $this->tax->getTax($product['price'], 0),
							//'reward'     => $product['reward']
						);
					}

					// Order Totals
					$this->load->model('extension/extension');
					//$this->load->model('extension/total');

					$totals = array();
					$taxes = [];
					$total = 0;

					// Because __call can not keep var references so we put them into an array.
					$total_data = array(
						'totals' => &$totals,
						'taxes'  => &$taxes,
						'total'  => &$total
					);

					$sort_order = array();

					$results = $this->model_extension_extension->getExtensions('total');

					foreach ($results as $key => $value) {
						$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
					}

					array_multisort($sort_order, SORT_ASC, $results);

					foreach ($results as $result) {
						if ($this->config->get($result['code'] . '_status')) {
							$this->load->model('extension/total/' . $result['code']);

							// We have to put the totals in an array so that they pass by reference.
							$this->{'model_extension_total_' . $result['code']}->getTotal($total_data, $order_data['products']);
						}
					}

					$sort_order = array();
					foreach ($total_data['totals'] as $key => $value) {
						$sort_order[$key] = $value['sort_order'];

					}
					array_multisort($sort_order, SORT_ASC, $total_data['totals']);

                    $this->model_extension_total_total->delteTotal($order_id,$total_data['totals']);
                    $this->model_extension_total_total->updateTotalInOrder($order_id);
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function delete() {
		$this->load->language('api/order');

		$json = array();

		if (!isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$this->load->model('checkout/order');

			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				$this->model_checkout_order->deleteOrder($order_id);

				$json['success'] = $this->language->get('text_success');
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function info() {
		$this->load->language('api/order');

		$json = array();

		if (!isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			$this->load->model('checkout/order');

			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				$json['order'] = $order_info;

				$json['success'] = $this->language->get('text_success');
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function history() {
		$this->load->language('api/order');

		$json = array();

		if (!isset($this->session->data['api_id'])) {
			$json['error'] = $this->language->get('error_permission');
		} else {
			// Add keys for missing post vars
			$keys = array(
				'order_status_id',
				'notify',
				'override',
				'comment'
			);
			foreach ($keys as $key) {
				if (!isset($this->request->post[$key])) {
					$this->request->post[$key] = '';
				}
			}

			$this->load->model('checkout/order');

			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				$this->model_checkout_order->addOrderHistory($order_id, $this->request->post['order_status_id'], $this->request->post['comment'], $this->request->post['notify'], $this->request->post['override']);

				$json['success'] = $this->language->get('text_success');
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
		}

		if (isset($this->request->server['HTTP_ORIGIN'])) {
			$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
			$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
			$this->response->addHeader('Access-Control-Max-Age: 1000');
			$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function payment(){
        $json = [];
        $this->load->language('api/order');

        if (isset($this->request->post['order_id']) AND
            isset($this->request->post['total']) AND
            isset($this->request->post['payment_id'])
        ) {
            $order_id=$this->request->post['order_id'];
            $total=$this->request->post['total'];
            $payment_id=$this->request->post['payment_id'];
            if(in_array($payment_id,$this->config->get('config_payment_auto'))) {
              $this->request->post['order_id']=$order_id;
              $this->request->post['amount']=$total;
              $json = $this->load->controller('payment/liqpay');
            }
        } else{
            $this->load->language('api/order');
            $json['error'] = $this->language->get('error_not_param');
        }


        $this->response->setOutput(json_encode($json));
    }
    public function addProduct(){
        $json = [];
        $this->load->language('api/order');
        if (isset($this->request->post['product_id']) AND
            isset($this->request->post['date']) AND
            isset($this->request->post['quantity']) AND
            isset($this->request->post['order_id'])
        ){$this->load->model('checkout/order');
            $order_id=$this->request->post['order_id'];
            $quantity=$this->request->post['quantity'];
            $product_id=$this->request->post['product_id'];
            $date=$this->request->post['date'];
            $this->model_checkout_order->addOrderProducts($order_id, $quantity, $date, $product_id);
            $this->request->get['order_id']=$order_id;
            $this->recalc_total();
        }else{
            $json['error'] = $this->language->get('error_not_param');
            $this->response->setOutput(json_encode($json));
        }
        $this->response->setOutput(json_encode($json));
    }
     public function deleteProduct(){
        $json = [];
        if (isset($this->request->post['user']) AND
            isset($this->request->post['user_confirm']) AND
            isset($this->request->post['order_product_id'])
           ) {
            $this->load->model('checkout/order');
            $this->load->language('api/order');

            $user_name = $this->model_checkout_order->getUserName($this->request->post['user']);
            $user_confirm_name = $this->model_checkout_order->getUserName($this->request->post['user_confirm']);
            $order_product_ids = explode(',', $this->request->post['order_product_id']);

                    foreach($order_product_ids as $order_product_id){
                         $order = $this->model_checkout_order->getOrderFromOrderProductID($order_product_id);
                        $product= $this->model_checkout_order->getProductFromProductId($order_product_id);
                        $this->model_checkout_order->deleteProductID($order_product_id);
                        $this->request->get['order_id']=$order['order_id'];
                        $this->recalc_total();

                        if($user_name == $user_confirm_name){

                        $comment_delete= $this->language->get('text_product_delete').$product['name'] . $this->language->get('text_date').$product['booking_date']." - ". $user_confirm_name;
                        }
                    else{
                        $comment_delete= $this->language->get('text_product_delete').$product['name'] . $this->language->get('text_date').$product['booking_date']." - ".$user_name.$this->language->get('text_administrator'). $user_confirm_name;
                    }
                $this->model_checkout_order->addOrderHistory($order['order_id'], $order['order_status_id'], $comment_delete,1);
            }
        }else{
            $json['error'] = $this->language->get('error_not_param');
        }

        $this->response->setOutput(json_encode($json));
    }
    public function confirm(){
        $json = [];
        if (isset($this->request->post['user']) AND
            isset($this->request->post['user_confirm']) AND
            isset($this->request->post['order_product_id'])
           )
        {
            $this->load->model('checkout/order');
            $this->load->language('api/order');

            $user_name = $this->model_checkout_order->getUserName($this->request->post['user']);

            $user_confirm_name = $this->model_checkout_order->getUserName($this->request->post['user_confirm']);


            $order_product_ids = explode(',', $this->request->post['order_product_id']);

                    foreach($order_product_ids as $order_product_id){

                        $order = $this->model_checkout_order->getOrderFromOrderProductID($order_product_id);
                        $this->model_checkout_order->updateStatusOrderProductID($order_product_id);
                        $product= $this->model_checkout_order->getProductFromProductId($order_product_id);

                        $this->request->get['order_id']=$order['order_id'];
                        $this->recalc_total();
                        if($user_name == $user_confirm_name){


                            $comment_confirm= $this->language->get('text_confirm').$product['name'] . $this->language->get('text_date').$product['booking_date']." - ". $user_confirm_name;
                        }
                        else{

                            $comment_confirm= $this->language->get('text_confirm').$product['name'] . $this->language->get('text_date').$product['booking_date']." - ".$user_name.$this->language->get('text_administrator'). $user_confirm_name;
                        }
            $this->model_checkout_order->addOrderHistory($order['order_id'], $order['order_status_id'], $comment_confirm);

            }
        }else{
            $json['error'] = $this->language->get('error_not_param');
            $this->response->setOutput(json_encode($json));
        }
        $this->response->setOutput(json_encode($json));
    }
    public function getHome(){
        $data = array();
        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $category_id = $this->request->post['category_id'];
        $data['token'] = $this->request->post['token'];
        $data['category_id'] = $this->request->post['category_id'];
        $x = strtotime($this->request->post['date']);
        if($x === false ) {
            $day_come = date_create_from_format('d/m/Y', $this->request->post['date'])->format('Y-m-d');
        } else {
            $day_come = date('Y-m-d', $x);
        }
        $today = $day_come;
        $today_day = date('N', strtotime($this->request->post['date']));
        $new_today = date('Y-m-d', strtotime($today.' -'.((int)$today_day - 1).' day'));

        $data['next_date'] = date('Y-m-d',strtotime("$today +31 day"));
        $data['prev_date'] = date('Y-m-d',strtotime("$today -31 day"));
        $data['today'] = $today;
        $data['today2'] = date('d/m/Y',strtotime($today));
        $data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_SERVER : HTTP_SERVER;

		$data['week'][1]="Пн"; 
		$data['week'][2]="Вт"; 
		$data['week'][3]="Ср"; 
		$data['week'][4]="Чт"; 
		$data['week'][5]="Пт"; 
		$data['week'][6]="Сб"; 
		$data['week'][7]="Вс";
		$prise = array();
		for($i=(1-(int)$today_day); $i<=(31-(int)$today_day); $i++)
        {
            $data['days'][]=array(
                'date_'  => date('d/m/Y',strtotime("$today +$i day")),
                'datpmc'  => date('d/m',strtotime("$today +$i day")),
                'date'  => date('Y-m-d',strtotime("$today +$i day")),
                'day'   => date('d',strtotime("$today +$i day")),
                'week'  => date('N',strtotime("$today +$i day"))
            );			
        }
        $products=$this->model_catalog_category->getCategoryHome($category_id, $new_today);
	//	print_r($products);
        foreach ($products as $product) {
             $data['products'][]=array(
                'product_id'    => $product['product_id'],
                'name'          => $product['name'],
				'booking'       => $this->model_catalog_category->getBooking($product['product_id']),
                'day1'          =>$product['day0'],
                'day2'          =>$product['day1'],
                'day3'          =>$product['day2'],
                'day4'          =>$product['day3'],
                'day5'          =>$product['day4'],
                'day6'          =>$product['day5'],
                'day7'          =>$product['day6'],
                'day8'          =>$product['day0'],
                'day9'          =>$product['day1'],
                'day10'         =>$product['day2'],
                'day11'         =>$product['day3'],
                'day12'         =>$product['day4'],
                'day13'         =>$product['day5'],
                'day14'         =>$product['day6'],
                'day15'         =>$product['day0'],
                'day16'         =>$product['day1'],
                'day17'         =>$product['day2'],
                'day18'         =>$product['day3'],
                'day19'         =>$product['day4'],
                'day20'         =>$product['day5'],
                'day21'         =>$product['day6'],
                'day22'         =>$product['day0'],
                'day23'         =>$product['day1'],
                'day24'         =>$product['day2'],
                'day25'         =>$product['day3'],
                'day26'         =>$product['day4'],
                'day27'         =>$product['day5'],
                'day28'         =>$product['day6'],
                'day29'         =>$product['day0'],
                'day30'         =>$product['day1'],
                'day31'         =>$product['day2'],
                'price0'          =>$this->currency->format((($product['special0']==0)?(float)$product['price']:$product['special0']), $this->session->data['currency'],'', true,true),
                'price1'          =>$this->currency->format((($product['special1']==0)?(float)$product['price']:$product['special1']), $this->session->data['currency'],'', true,true),
                'price2'          =>$this->currency->format((($product['special2']==0)?(float)$product['price']:$product['special2']), $this->session->data['currency'],'', true,true),
                'price3'          =>$this->currency->format((($product['special3']==0)?(float)$product['price']:$product['special3']), $this->session->data['currency'],'', true,true),
                'price4'          =>$this->currency->format((($product['special4']==0)?(float)$product['price']:$product['special4']), $this->session->data['currency'],'', true,true),
                'price5'          =>$this->currency->format((($product['special5']==0)?(float)$product['price']:$product['special5']), $this->session->data['currency'],'', true,true),
                'price6'          =>$this->currency->format((($product['special6']==0)?(float)$product['price']:$product['special6']), $this->session->data['currency'],'', true,true),
                'price7'          =>$this->currency->format((($product['special0']==0)?(float)$product['price']:$product['special0']), $this->session->data['currency'],'', true,true),
                'price8'          =>$this->currency->format((($product['special1']==0)?(float)$product['price']:$product['special1']), $this->session->data['currency'],'', true,true),
                'price9'          =>$this->currency->format((($product['special2']==0)?(float)$product['price']:$product['special2']), $this->session->data['currency'],'', true,true),
                'price10'         =>$this->currency->format((($product['special3']==0)?(float)$product['price']:$product['special3']), $this->session->data['currency'],'', true,true),
                'price11'         =>$this->currency->format((($product['special4']==0)?(float)$product['price']:$product['special4']), $this->session->data['currency'],'', true,true),
                'price12'         =>$this->currency->format((($product['special5']==0)?(float)$product['price']:$product['special5']), $this->session->data['currency'],'', true,true),
                'price13'         =>$this->currency->format((($product['special6']==0)?(float)$product['price']:$product['special6']), $this->session->data['currency'],'', true,true),
                'price14'         =>$this->currency->format((($product['special0']==0)?(float)$product['price']:$product['special0']), $this->session->data['currency'],'', true,true),
                'price15'         =>$this->currency->format((($product['special1']==0)?(float)$product['price']:$product['special1']), $this->session->data['currency'],'', true,true),
                'price16'         =>$this->currency->format((($product['special2']==0)?(float)$product['price']:$product['special2']), $this->session->data['currency'],'', true,true),
                'price17'         =>$this->currency->format((($product['special3']==0)?(float)$product['price']:$product['special3']), $this->session->data['currency'],'', true,true),
                'price18'         =>$this->currency->format((($product['special4']==0)?(float)$product['price']:$product['special4']), $this->session->data['currency'],'', true,true),
                'price19'         =>$this->currency->format((($product['special5']==0)?(float)$product['price']:$product['special5']), $this->session->data['currency'],'', true,true),
                'price20'         =>$this->currency->format((($product['special6']==0)?(float)$product['price']:$product['special6']), $this->session->data['currency'],'', true,true),
                'price21'         =>$this->currency->format((($product['special0']==0)?(float)$product['price']:$product['special0']), $this->session->data['currency'],'', true,true),
                'price22'         =>$this->currency->format((($product['special1']==0)?(float)$product['price']:$product['special1']), $this->session->data['currency'],'', true,true),
                'price23'         =>$this->currency->format((($product['special2']==0)?(float)$product['price']:$product['special2']), $this->session->data['currency'],'', true,true),
                'price24'         =>$this->currency->format((($product['special3']==0)?(float)$product['price']:$product['special3']), $this->session->data['currency'],'', true,true),
                'price25'         =>$this->currency->format((($product['special4']==0)?(float)$product['price']:$product['special4']), $this->session->data['currency'],'', true,true),
                'price26'         =>$this->currency->format((($product['special5']==0)?(float)$product['price']:$product['special5']), $this->session->data['currency'],'', true,true),
                'price27'         =>$this->currency->format((($product['special6']==0)?(float)$product['price']:$product['special6']), $this->session->data['currency'],'', true,true),
                'price28'         =>$this->currency->format((($product['special0']==0)?(float)$product['price']:$product['special0']), $this->session->data['currency'],'', true,true),
                'price29'         =>$this->currency->format((($product['special1']==0)?(float)$product['price']:$product['special1']), $this->session->data['currency'],'', true,true),
                'price30'         =>$this->currency->format((($product['special2']==0)?(float)$product['price']:$product['special2']), $this->session->data['currency'],'', true,true),
                'price31'         =>$this->currency->format((($product['special3']==0)?(float)$product['price']:$product['special3']), $this->session->data['currency'],'', true,true),


            );
        }

        $html = $this->load->view('product/calendar_home', $data);

        $this->response->setOutput(json_encode(['html'=>'<div class="close" onclick="$(\'#view\').hide();"><i class="fa fa-times" aria-hidden="true"></i></div>'.$html]));
    }

    public  function  getDay(){
        $data = [];
        $this->load->language('api/order');
        $data['column_name']=$this->language->get('column_name');
        $data['column_status']=$this->language->get('column_status');
        $data['column_total']=$this->language->get('column_total');
        $data['column_quantity']=$this->language->get('column_quantity');
        $data['column_edit']=$this->language->get('column_edit');
        $this->load->model('catalog/product');

        $product_id=$this->request->post['product_id'];
        $day=$this->request->post['day'];
        $data['token']=$this->request->post['token'];
        $data['booking_info']=$this->model_catalog_product->getProductFromDate($product_id,$day);
        $html = $this->load->view('product/table', $data);


        $this->response->setOutput(json_encode(['html'=>'<div class="close" onclick="$(\'#view\').hide();"><i class="fa fa-times" aria-hidden="true"></i></div>'.$html]));
    }

    public function getdates() {
        $data=[];
        $json = array();
        $this->load->language('api/order');

        $data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_SERVER : HTTP_SERVER;

        if (!empty($this->request->post['product_id']) && !empty($this->request->post['month'])){

                $this->load->model('catalog/product');

                $starting = '01';
                $ending = date("t", strtotime($this->request->post['month']));
                $month = date("m", strtotime($this->request->post['month']));
                $year = date("Y", strtotime($this->request->post['month']));

                if($this->request->post['data_end']==$this->request->post['data_start']){

                    $this->request->post['data_end']=date('Y-m-d',strtotime($this->request->post['data_end'].' +1 day'));
                }
                $data_start = date($year.'-'.$month.'-'.$starting);
                $data_end = date($year.'-'.$month.'-'.$ending);
                $data['mes'] = [array_fill(1, 7, [])];
                $shift = date("N", strtotime($data_start)) - 2;

                switch ($month) {
                    case 1:
                        $m = $this->language->get('text_january'); break;
                    case 2:
                        $m = $this->language->get('text_february'); break;
                    case 3:
                        $m = $this->language->get('text_march'); break;
                    case 4:
                        $m = $this->language->get('text_april'); break;
                    case 5:
                        $m = $this->language->get('text_may'); break;
                    case 6:
                        $m = $this->language->get('text_june'); break;
                    case 7:
                        $m = $this->language->get('text_july'); break;
                    case 8:
                        $m = $this->language->get('text_august'); break;
                    case 9:
                        $m = $this->language->get('text_september'); break;
                    case 10:
                        $m = $this->language->get('text_october'); break;
                    case 11:
                        $m = $this->language->get('text_november'); break;
                    case 12:
                        $m = $this->language->get('text_december'); break;
                }
                $data['year'] = $m.' '.$year;
                $data['next'] = date('Y-m-d', strtotime($data_start.' +1 month'));
                $data['prev'] = date('Y-m-d', strtotime($data_start.' -1 month'));
                $data['today'] = date('Y-m-d');

                $data['sunday'] = $this->language->get('text_sunday');
                $data['monday'] = $this->language->get('text_monday');
                $data['tuesday'] = $this->language->get('text_tuesday');
                $data['wednesday'] = $this->language->get('text_wednesday');
                $data['thursday'] = $this->language->get('text_thursday');
                $data['friday'] = $this->language->get('text_friday');
                $data['saturday'] = $this->language->get('text_saturday');

                $data['period'] = $this->language->get('text_period');
                $data['reserved'] = $this->language->get('text_reserved');

                if(isset($this->request->get['customer_group_id'])) {
                    $customer_gr_id = $this->request->get['customer_group_id'];
                } else {
                    $customer_gr_id = (int)$this->config->get('config_customer_group_id');
                }

                for($i = $starting; $i <= $ending; $i++ ) {
					$day = ($i < 10 ) ? '0'.(int)$i : $i;
                    $quantity = $this->model_catalog_product->getProduct((int)$this->request->post['product_id'], date($year.'-'.$month.'-'.$day), $customer_gr_id);
                    $today = strtotime($year.'-'.$month.'-'.$day);
                    $column = date('N', $today);
                    $row = floor(($i + $shift) / 7);

                    $data['mes'][$row][$column] = array(
                        'day' => $day,
                        'date' => date($year.'-'.$month.'-'.$day),
                        'next_day' => date('Y-m-d',strtotime($year.'-'.$month.'-'.$day." +1 day")),
                        'spots' => (isset($this->request->post['another']) && (date($year.'-'.$month.'-'.$day) < $data['today']) || (isset($this->request->post['another']) && (date($year.'-'.$month.'-'.$day) >  date("Y-m-d", strtotime(date("Y-m-d", strtotime($data['today'])) . " + 365 day"))))) ? 0 : $quantity['quantity'],
                        'is_pay' => false,
                        'is_start' => date($year.'-'.$month.'-'.$day)==date('Y-m-d',strtotime($this->request->post['data_start'])),
                        'is_end' => date($year.'-'.$month.'-'.$day)==date('Y-m-d',strtotime($this->request->post['data_end'])),
                        'quantity' => isset($this->request->post['quantity']) ? $this->request->post['quantity'] : 1,
                        'input' => (strtotime($this->request->post['data_start'])<=$today AND strtotime($this->request->post['data_end'])>=$today),
                        'price' => $this->currency->format((($quantity['special']==0)?(float)$quantity['price']:$quantity['special']), $this->session->data['currency'])
                    );
                }

                $booking_info = $this->model_catalog_product->getBookingDays((int)$this->request->post['product_id'], $data_start, $data_end);

                if($booking_info) {
                    foreach ($booking_info as $key => $day) {
                        $column = date('N', strtotime($day['days']));
                        $row = floor((date('j', strtotime($day['days'])) + $shift) / 7);
                        $data['mes'][$row][$column]['spots'] -= $day['total'];
                        $data['mes'][$row][$column]['is_pay'] =true;
                        $data['mes'][$row][$column]['next_day'] =date('Y-m-d',strtotime($day['days']." +1 day"));
                        $data['mes'][$row][$column]['is_start'] = $day['days']==date('Y-m-d',strtotime($this->request->post['data_start']));
                        $data['mes'][$row][$column]['is_end'] = $day['days']==date('Y-m-d',strtotime($this->request->post['data_end']));
                    }
                }

                if (isset($this->request->post['another_calendar'])) {
                    return $this->load->view('product/calendar2', $data);
                }elseif (isset($this->request->post['another'])) {
                    $json['calendar'] = $this->load->view('product/calendar2', $data);
                } else {
                    $json['calendar'] = $this->load->view('product/calendar', $data);
                }
        }

        $this->response->setOutput(json_encode($json));
    }
}
