<modification>
	<id>tshirtecommerce</id>
	<version>4.2.1</version>
	<vqmver>2.4.0</vqmver>
	<author>Tshirtecommerce Team</author>
	
	<!-- Send Orders -->
	<file error="skip" name="admin/controller/sale/order.php,system/storage/modification/admin/controller/sale/order.php">
		<operation error="skip">
			<search position="before"><![CDATA[
				$this->response->setOutput($this->load->view('sale/order_list', $data));
			]]></search>
			<add><![CDATA[
				/* vqmod/xml/tshirtecommerce_product.xml */
				if($this->config->get('tshirtecommerce_send_order') == 1)
				{
					$this->db->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."tshirt_send_orders` (
						`id` int(11) NOT NULL AUTO_INCREMENT,
						`order_id` int(11) NOT NULL,
						`options` text,  
						PRIMARY KEY (`id`)
					)");
					
					
					$tsquery = $this->db->query("SELECT * FROM `".DB_PREFIX."tshirt_send_orders`");
					if ($tsquery->num_rows)
					{
						$listsend = array();
						foreach($tsquery->rows as $t_res)
						{
							$listsend[] = $t_res['order_id'];
						}
						
						foreach($data['orders'] as $t_key=>$t_order)
						{
							if(in_array($t_order['order_id'], $listsend))
								$data['orders'][$t_key]['sended'] = 1;
							else
								$data['orders'][$t_key]['sended'] = 0;
						}
					}else
					{
						foreach($data['orders'] as $t_key=>$t_order)
						{
							$data['orders'][$t_key]['sended'] = 0;
						}
					}
				}
			]]></add>
		</operation>
	</file>
	
	<file error="skip" name="admin/controller/sale/order.php,system/storage/modification/admin/controller/sale/order.php">
		<operation error="skip">
			<search position="after"><![CDATA[
				protected function getList() {
			]]></search>
			<add><![CDATA[
				/* vqmod/xml/tshirtecommerce_product.xml */
				if (isset($this->request->get['send_order_id']) && $this->request->get['send_order_id'] != '')
				{
					$result = array();
					$result['error'] = '1';
					$result['data'] = 'Data not found';
					$send_order_id 	= (int) $this->request->get['send_order_id'];
					$api = '';
					$path = dirname(dirname(dirname(__FILE__))).'/tshirtecommerce/data/settings.json';
					if(file_exists($path))
					{
						$settings = json_decode(file_get_contents($path), true);
						if(isset($settings['store']['enable']) && $settings['store']['enable'] == 1)
						{
							if(isset($settings['store']['api']))
								$api = $settings['store']['api'];
						}
					}
					
					if ($this->request->server['HTTPS']) {
						$site_url = HTTPS_CATALOG;
					} else {
						$site_url = HTTP_CATALOG;
					}
					$site_url = trim($site_url, '/');
					
					$order_url 	= 'index.php?route=tshirtecommerce/orders';
					$client_url = base64_encode( $site_url .'||'. $order_url );

					$config = dirname(dirname(dirname(dirname(__FILE__)))). DIRECTORY_SEPARATOR .'tshirtecommerce'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'config.php';
					if(file_exists($config))
						include_once($config);
					else
						define( 'MAIN_STORE_URL', 'https://9file.net/');
						
					$store_url 	= MAIN_STORE_URL. 'index.php/order/index/'.$api.'/'.$send_order_id.'/'.$client_url;
					
					$storedata 	= '';
					if( function_exists('curl_exec') )
					{
						$ch = curl_init();
						curl_setopt($ch, CURLOPT_URL, $store_url);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$storedata = curl_exec($ch);
						curl_close($ch);
					}
					
					if( $storedata == false && function_exists('file_get_contents') )
					{
						$storedata = file_get_contents($store_url);
					}
					
					if($storedata !== false)
					{
						$array 	= json_decode($storedata, true);
						if(isset($array['error']) && $array['error'] == 0)
						{
							$result['error'] 	= 0;
							$result['data'] 	= 'Send order success.';
						}
					}
					
					$this->db->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."tshirt_send_orders` (
						`id` int(11) NOT NULL AUTO_INCREMENT,
						`order_id` int(11) NOT NULL,
						`options` text,  
						PRIMARY KEY (`id`)
					)");
					
					$tsquery = $this->db->query("SELECT `order_id` FROM `".DB_PREFIX."tshirt_send_orders` WHERE `order_id`='".(int)$send_order_id."'");
					if (!$tsquery->num_rows)
						$this->db->query("INSERT INTO `".DB_PREFIX."tshirt_send_orders` SET `order_id`='".(int)$send_order_id."',`options`=''");

					return $result;
				}
			]]></add>
		</operation>
	</file>
	
	<file error="skip" name="catalog/model/checkout/order.php,system/storage/modification/catalog/model/checkout/order.php">
		<operation error="skip">
			<search position="after"><![CDATA[
				$order_info = $this->getOrder($order_id);
			]]></search>
			<add><![CDATA[
				if($order_status_id == $this->config->get('tshirtecommerce_list_send_status'))
				{
					$api = '';
					$path = dirname(dirname(dirname(__FILE__))).'/tshirtecommerce/data/settings.json';
					if(file_exists($path))
					{
						$settings = json_decode(file_get_contents($path), true);
						if(isset($settings['store']['enable']) && $settings['store']['enable'] == 1)
						{
							if(isset($settings['store']['api']))
								$api = $settings['store']['api'];
						}
					}
					
					if ($this->request->server['HTTPS']) {
						$site_url = HTTPS_SERVER;
					} else {
						$site_url = HTTP_SERVER;
					}
					$site_url = trim($site_url, '/');
					
					$order_url 	= 'index.php?route=tshirtecommerce/orders';
					$client_url = base64_encode( $site_url .'||'. $order_url );

					$config = dirname(dirname(dirname(dirname(__FILE__)))). DIRECTORY_SEPARATOR .'tshirtecommerce'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'config.php';
					if(file_exists($config))
						include_once($config);
					else
						define( 'MAIN_STORE_URL', 'https://9file.net/');
						
					$store_url 	= MAIN_STORE_URL .'index.php/order/index/'.$api.'/'.$order_id.'/'.$client_url;
					
					$storedata 	= '';
					if( function_exists('curl_exec') )
					{
						$ch = curl_init();
						curl_setopt($ch, CURLOPT_URL, $store_url);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$storedata = curl_exec($ch);
						curl_close($ch);
					}
					
					if( $storedata == false && function_exists('file_get_contents') )
					{
						file_get_contents($store_url);
					}
					
					$this->db->query("CREATE TABLE IF NOT EXISTS `".DB_PREFIX."tshirt_send_orders` (
						`id` int(11) NOT NULL AUTO_INCREMENT,
						`order_id` int(11) NOT NULL,
						`options` text,  
						PRIMARY KEY (`id`)
					)");
					
					$tsquery = $this->db->query("SELECT `order_id` FROM `".DB_PREFIX."tshirt_send_orders` WHERE `order_id`='".(int)$order_id."'");
					if (!$tsquery->num_rows)
						$this->db->query("INSERT INTO `".DB_PREFIX."tshirt_send_orders` SET `order_id`='".(int)$order_id."',`options`=''");
				}
			]]></add>
		</operation>
	</file>
	
	<file error="skip" name="admin/view/template/sale/order_list.twig,system/storage/modification/admin/view/template/sale/order_list.twig">
		<operation error="skip">
			<search position="after"><![CDATA[
				<td class="text-right"><div style="min-width: 120px;">
			]]></search>
			<add><![CDATA[
				{% if order.sended is defined %} 
					{% if order.sended == 1 %}
						{{ 'Done! <a href="javascript:void(0)" onclick="send_order(this,' }} {{ order.order_id }} {{ ')">Re-send?</a> ' }}
					{% else %}
						{{ '<button type="button" class="btn btn-default" onclick="send_order(this,' }} {{ order.order_id }} {{ ')">Send Order</button> ' }}
					{% endif %}
				{% endif %}
				
			]]></add>
		</operation>
	</file>
	
	<file error="skip" name="admin/view/template/sale/order_list.twig,system/storage/modification/admin/view/template/sale/order_list.twig">
		<operation error="skip">
			<search position="after"><![CDATA[
				</form>
			]]></search>
			<add><![CDATA[
				<script type="text/javascript">
					function send_order(e, id)
					{
						var btn = jQuery(e);
						if( btn.hasClass('disabled') ) return;

						var check = confirm('You sure want send this oder?');
						if(check == true)
						{
							var send_url = 'index.php?route=sale/order&user_token={{ user_token }}&send_order_id='+id;
							jQuery(e).html('Sending...').addClass('disabled');
							jQuery.ajax({
								url: send_url,
							}).done(function( msg ) {
								btn.html('Completed');
							});
						}
					}
				</script>
			]]></add>
		</operation>
	</file>

</modification>