<?php
class ControllerReportAdvSaleOrder extends Controller { 
	public function index() {  
		// Insert DB columns
		$query = $this->db->query("DESC ".DB_PREFIX."product report");
			if (!$query->num_rows) {
				$this->db->query("ALTER TABLE `" . DB_PREFIX . "product` ADD `report` varchar(12) COLLATE utf8_bin NOT NULL DEFAULT '';");
			}
			
		$this->load->language('report/adv_sale_order');

		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('report/adv_sale_order');
		
		if (isset($this->request->post['filter_date_start'])) {
			$filter_date_start = $this->request->post['filter_date_start'];
		} else {
			$filter_date_start = '';
		}

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

		$this->data['ranges'] = array();
		
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_custom'),
			'value' => 'custom',
		);			
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_week'),
			'value' => 'week',
		);		
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_month'),
			'value' => 'month',
		);		
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_quarter'),
			'value' => 'quarter',
		);
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_year'),
			'value' => 'year',
		);
		$this->data['ranges'][] = array(
			'text'  => $this->language->get('stat_all'),
			'value' => 'all',
		);
		
		if (isset($this->request->post['filter_range'])) {
			$filter_range = $this->request->post['filter_range'];
		} else {
			$filter_range = 'year'; //show Year in Statistics Range by default
		}

		$this->data['groups'] = array();

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_no_grouping'),
			'value' => 'all',
		);
		
		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_year'),
			'value' => 'year',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_month'),
			'value' => 'month',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_week'),
			'value' => 'week',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_day'),
			'value' => 'day',
		);
		
		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_order'),
			'value' => 'order',
		);
		
		if (isset($this->request->post['filter_group'])) {
			$filter_group = $this->request->post['filter_group'];
		} else {
			$filter_group = 'month'; //show Month in Group Report by default
		}

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

		if (isset($this->request->post['filter_details'])) {
			$filter_details = $this->request->post['filter_details'];
		} else {
			$filter_details = 0;
		}
		
		$this->data['order_statuses'] = $this->model_report_adv_sale_order->getOrderStatuses(); 			
		if (isset($this->request->post['filter_order_status_id']) && is_array($this->request->post['filter_order_status_id'])) {
			$filter_order_status_id = array_flip($this->request->post['filter_order_status_id']);
		} else {
			$filter_order_status_id = '';
		}

		$this->data['stores'] = $this->model_report_adv_sale_order->getOrderStores();						
		if (isset($this->request->post['filter_store_id']) && is_array($this->request->post['filter_store_id'])) {
			$filter_store_id = array_flip($this->request->post['filter_store_id']);
		} else {
			$filter_store_id = '';			
		}
		
		$this->data['currencies'] = $this->model_report_adv_sale_order->getOrderCurrencies();	
		if (isset($this->request->post['filter_currency']) && is_array($this->request->post['filter_currency'])) {
			$filter_currency = array_flip($this->request->post['filter_currency']);
		} else {
			$filter_currency = '';		
		}

		$this->data['taxes'] = $this->model_report_adv_sale_order->getOrderTaxes();					
		if (isset($this->request->post['filter_taxes']) && is_array($this->request->post['filter_taxes'])) {
			$filter_taxes = array_flip($this->request->post['filter_taxes']);
		} else {
			$filter_taxes = '';		
		}
		
		$this->data['customer_groups'] = $this->model_report_adv_sale_order->getOrderCustomerGroups();		
		if (isset($this->request->post['filter_customer_group_id']) && is_array($this->request->post['filter_customer_group_id'])) {
			$filter_customer_group_id = array_flip($this->request->post['filter_customer_group_id']);
		} else {
			$filter_customer_group_id = '';
		}

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

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

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

		$this->data['locations'] = $this->model_report_adv_sale_order->getOrderLocation();			
		if (isset($this->request->post['filter_location']) && is_array($this->request->post['filter_location'])) {
			$filter_location = array_flip($this->request->post['filter_location']);
		} else {
			$filter_location = '';		
		}
		
		$this->data['shippings'] = $this->model_report_adv_sale_order->getOrderShipping();			
		if (isset($this->request->post['filter_shipping']) && is_array($this->request->post['filter_shipping'])) {
			$filter_shipping = array_flip($this->request->post['filter_shipping']);
		} else {
			$filter_shipping = '';		
		}

		$this->data['payments'] = $this->model_report_adv_sale_order->getOrderPayment();	
		if (isset($this->request->post['filter_payment']) && is_array($this->request->post['filter_payment'])) {
			$filter_payment = array_flip($this->request->post['filter_payment']);
		} else {
			$filter_payment = '';		
		}

		$this->data['shipping_zones'] = $this->model_report_adv_sale_order->getShippingZones();			
		if (isset($this->request->post['filter_shipping_zone']) && is_array($this->request->post['filter_shipping_zone'])) {
			$filter_shipping_zone = array_flip($this->request->post['filter_shipping_zone']);
		} else {
			$filter_shipping_zone = '';		
		}
		
		$this->data['shipping_countries'] = $this->model_report_adv_sale_order->getShippingCountries();			
		if (isset($this->request->post['filter_shipping_country']) && is_array($this->request->post['filter_shipping_country'])) {
			$filter_shipping_country = array_flip($this->request->post['filter_shipping_country']);
		} else {
			$filter_shipping_country = '';		
		}
		
		$this->data['payment_countries'] = $this->model_report_adv_sale_order->getPaymentCountries();	
		if (isset($this->request->post['filter_payment_country']) && is_array($this->request->post['filter_payment_country'])) {
			$filter_payment_country = array_flip($this->request->post['filter_payment_country']);
		} else {
			$filter_payment_country = '';		
		}		

		if (isset($this->request->post['page'])) {
			$page = $this->request->post['page'];
		} else {
			$page = 1;
		}
		
   		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),       		
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('report/adv_sale_order', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['orders'] = array();
		
		$data = array(
			'filter_date_start'	     	=> $filter_date_start, 
			'filter_date_end'	     	=> $filter_date_end, 
			'filter_range'           	=> $filter_range,
			'filter_group'           	=> $filter_group,			
			'filter_order_status_id'	=> $filter_order_status_id,
			'filter_store_id' 	 	 	=> $filter_store_id,
			'filter_currency' 	 	 	=> $filter_currency,
			'filter_taxes' 	 	 		=> $filter_taxes,			
			'filter_customer_group_id'  => $filter_customer_group_id,
			'filter_company' 	 		=> $filter_company,
			'filter_customer_id' 	 	=> $filter_customer_id,			
			'filter_email' 	 			=> $filter_email,	
			'filter_product_id' 	 	=> $filter_product_id,
			'filter_location'  			=> $filter_location,			
			'filter_shipping'  			=> $filter_shipping,
			'filter_payment'  			=> $filter_payment,
			'filter_shipping_zone'  	=> $filter_shipping_zone,			
			'filter_shipping_country'  	=> $filter_shipping_country,
			'filter_payment_country'  	=> $filter_payment_country,	
			'filter_sort'  				=> $filter_sort,	
			'filter_details'  			=> $filter_details,				
			'start'                 	=> ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                  	=> $this->config->get('config_admin_limit')
		);
		
		//$order_total = $this->model_report_adv_sale_order->getTotalSaleOrders($data);
		
		$results = $this->model_report_adv_sale_order->getSaleOrders($data);

			foreach ($results as $result) {
				$this->data['orders'][] = array(
				'date_start' 					=> date($this->language->get('date_format_short'), strtotime($result['date_start'])),
				'date_end'   					=> date($this->language->get('date_format_short'), strtotime($result['date_end'])),	
				'order_id'   					=> $result['order_id'],	
				'orders'     					=> $result['orders'],
				'customers'   					=> $result['customers'],				
				'products'   					=> $result['products'],	
				'sub_total'        				=> $this->currency->format($result['sub_total'], $this->config->get('config_currency')),
				'reward'      					=> $this->currency->format($result['reward'], $this->config->get('config_currency')),
				'shipping'        				=> $this->currency->format($result['shipping'], $this->config->get('config_currency')),
				'coupon'      					=> $this->currency->format($result['coupon'], $this->config->get('config_currency')),
				'tax'        					=> $this->currency->format($result['tax'], $this->config->get('config_currency')),
				'credit'      					=> $this->currency->format($result['credit'], $this->config->get('config_currency')),
				'voucher'        				=> $this->currency->format($result['voucher'], $this->config->get('config_currency')),
				'total'      					=> $this->currency->format($result['total'], $this->config->get('config_currency')),
				'order_ord_id'     				=> $result['order_ord_id'],
				'order_ord_idc'     			=> $result['order_ord_idc'],					
				'order_order_date'    			=> get('date_format_short'), strtotime($result['order_order_date']),
				'order_inv_prefix'    			=> $result['order_inv_prefix'],
				'order_inv_id'     				=> $result['order_inv_id'],
				'order_name'   					=> $result['order_name'],
				'order_email'   				=> $result['order_email'],
				'order_group'   				=> $result['order_group'],
				'order_shipping_method' 		=> $result['order_shipping_method'],
				'order_payment_method'  		=> $result['order_payment_method'],					
				'order_status'  				=> $result['order_status'],
				'order_store'      				=> $result['order_store'],					
				'order_products' 				=> $result['order_products'],
				'order_currency' 				=> $result['order_currency'],
				'order_sub_total'  				=> $result['order_sub_total'],
				'order_shipping'  				=> $result['order_shipping'],
				'order_tax'  					=> $result['order_tax'],					
				'order_value'  					=> $result['order_value'],				
				'product_ord_id'  				=> $result['product_ord_id'],
				'product_order_idc'  			=> $result['product_ord_idc'],
				'product_order_date'    		=> $result['product_order_date'],
				'product_inv_prefix'   			=> $result['product_inv_prefix'],
				'product_inv_id'     			=> $result['product_inv_id'],					
				'product_sku'  					=> $result['product_sku'],				
				'product_name'  				=> $result['product_name'],	
				'product_model'  				=> $result['product_model'],					
				'product_manu'  				=> $result['product_manu'],
				'product_currency'  			=> $result['product_currency'],
				'product_price'  				=> $result['product_price'],
				'product_quantity'  			=> $result['product_quantity'],				
				'product_total'  				=> $result['product_total'],
				'product_tax'  					=> $result['product_tax']
				);
			}

		$this->data['text_no_details'] = $this->language->get('text_no_details');
		$this->data['text_order_list'] = $this->language->get('text_order_list');
		$this->data['text_product_list'] = $this->language->get('text_product_list');
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_no_grouping'] = $this->language->get('text_no_grouping');
		$this->data['text_all_status'] = $this->language->get('text_all_status');		
		$this->data['text_all_stores'] = $this->language->get('text_all_stores');
		$this->data['text_all_currencies'] = $this->language->get('text_all_currencies');
		$this->data['text_all_taxes'] = $this->language->get('text_all_taxes');		
		$this->data['text_all_groups'] = $this->language->get('text_all_groups');
		$this->data['text_all_locations'] = $this->language->get('text_all_locations');	
		$this->data['text_all_shippings'] = $this->language->get('text_all_shippings');			
		$this->data['text_all_payments'] = $this->language->get('text_all_payments');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');			
		$this->data['text_all_countries'] = $this->language->get('text_all_countries');	
		$this->data['text_none_selected'] = $this->language->get('text_none_selected');
		$this->data['text_selected'] = $this->language->get('text_selected');		
		$this->data['text_detail'] = $this->language->get('text_detail');
		$this->data['text_export_xls'] = $this->language->get('text_export_xls');
		$this->data['text_export_xls_orders_details'] = $this->language->get('text_export_xls_orders_details');
		$this->data['text_export_xls_products_details'] = $this->language->get('text_export_xls_products_details');		
		$this->data['text_export_html'] = $this->language->get('text_export_html');
		$this->data['text_export_html_orders_details'] = $this->language->get('text_export_html_orders_details');
		$this->data['text_export_html_products_details'] = $this->language->get('text_export_html_products_details');
		$this->data['text_filter_total'] = $this->language->get('text_filter_total');
		
		$this->data['column_date'] = $this->language->get('column_date');
		$this->data['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
    	$this->data['column_orders'] = $this->language->get('column_orders');
    	$this->data['column_customers'] = $this->language->get('column_customers');		
		$this->data['column_products'] = $this->language->get('column_products');		
		$this->data['column_sub_total'] = $this->language->get('column_sub_total');
		$this->data['column_points'] = $this->language->get('column_points');
		$this->data['column_shipping'] = $this->language->get('column_shipping');		
		$this->data['column_coupon'] = $this->language->get('column_coupon');
		$this->data['column_tax'] = $this->language->get('column_tax');		
		$this->data['column_credit'] = $this->language->get('column_credit');	
		$this->data['column_voucher'] = $this->language->get('column_voucher');	
		$this->data['column_total'] = $this->language->get('column_total');	
		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['column_order_date_added'] = $this->language->get('column_order_date_added');
		$this->data['column_order_order_id'] = $this->language->get('column_order_order_id');
		$this->data['column_inv_date'] = $this->language->get('column_inv_date');
		$this->data['column_order_inv_id'] = $this->language->get('column_order_inv_id');
		$this->data['column_order_customer'] = $this->language->get('column_order_customer');		
		$this->data['column_order_email'] = $this->language->get('column_order_email');		
		$this->data['column_order_customer_group'] = $this->language->get('column_order_customer_group');		
		$this->data['column_order_shipping_method'] = $this->language->get('column_order_shipping_method');
		$this->data['column_order_payment_method'] = $this->language->get('column_order_payment_method');		
		$this->data['column_order_status'] = $this->language->get('column_order_status');
		$this->data['column_order_store'] = $this->language->get('column_order_store');
		$this->data['column_order_quantity'] = $this->language->get('column_order_quantity');	
		$this->data['column_order_currency'] = $this->language->get('column_order_currency');
		$this->data['column_order_sub_total'] = $this->language->get('column_order_sub_total');
		$this->data['column_order_shipping'] = $this->language->get('column_order_shipping');
		$this->data['column_order_tax'] = $this->language->get('column_order_tax');			
		$this->data['column_order_value'] = $this->language->get('column_order_value');	
		$this->data['column_prod_order_id'] = $this->language->get('column_prod_order_id');		
		$this->data['column_prod_date_added'] = $this->language->get('column_prod_date_added');	
		$this->data['column_prod_inv_id'] = $this->language->get('column_prod_inv_id');			
		$this->data['column_prod_sku'] = $this->language->get('column_prod_sku');
		$this->data['column_prod_model'] = $this->language->get('column_prod_model');		
		$this->data['column_prod_name'] = $this->language->get('column_prod_name');	
		$this->data['column_prod_option'] = $this->language->get('column_prod_option');		
		$this->data['column_prod_manu'] = $this->language->get('column_prod_manu');
		$this->data['column_prod_currency'] = $this->language->get('column_prod_currency');
		$this->data['column_prod_price'] = $this->language->get('column_prod_price');
		$this->data['column_prod_quantity'] = $this->language->get('column_prod_quantity');
		$this->data['column_prod_total'] = $this->language->get('column_prod_total');
		$this->data['column_prod_tax'] = $this->language->get('column_prod_tax');	

		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_range'] = $this->language->get('entry_range');
		$this->data['entry_status'] = $this->language->get('entry_status');		
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_currency'] = $this->language->get('entry_currency');	
		$this->data['entry_tax'] = $this->language->get('entry_tax');		
		$this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
		$this->data['entry_company'] = $this->language->get('entry_company');
		$this->data['entry_customer'] = $this->language->get('entry_customer');		
		$this->data['entry_email'] = $this->language->get('entry_email'); 
		$this->data['entry_product'] = $this->language->get('entry_product');		
		$this->data['entry_location'] = $this->language->get('entry_location');
		$this->data['entry_shipping'] = $this->language->get('entry_shipping');		
		$this->data['entry_payment'] = $this->language->get('entry_payment');
		$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_shipping_country'] = $this->language->get('entry_shipping_country');
		$this->data['entry_payment_country'] = $this->language->get('entry_payment_country');
		$this->data['entry_group'] = $this->language->get('entry_group');		
		$this->data['entry_sort_by'] = $this->language->get('entry_sort_by');
		$this->data['entry_show_details'] = $this->language->get('entry_show_details');	
		
		$this->data['button_export'] = $this->language->get('button_export');
		$this->data['button_filter'] = $this->language->get('button_filter');
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['token'] = $this->session->data['token'];

		//$pagination = new Pagination();
		//$pagination->total = $order_total;
		//$pagination->page = $page;
		//$pagination->limit = $this->config->get('config_admin_limit');		
		//$pagination->limit = 9999;
		//$pagination->text = $this->language->get('text_pagination');
		//$pagination->url = $this->url->link('report/adv_sale_order', 'token=' . $this->session->data['token'] . '&page={page}', 'SSL');
			
		//$this->data['pagination'] = $pagination->render();		

		$this->data['filter_date_start'] = $filter_date_start;
		$this->data['filter_date_end'] = $filter_date_end;		
		$this->data['filter_group'] = $filter_group;
		$this->data['filter_range'] = $filter_range;
		$this->data['filter_order_status_id'] = $filter_order_status_id;
		$this->data['filter_store_id'] = $filter_store_id;
		$this->data['filter_currency'] = $filter_currency;
		$this->data['filter_taxes'] = $filter_taxes;		
		$this->data['filter_customer_group_id'] = $filter_customer_group_id;
		$this->data['filter_company'] = $filter_company; 
		$this->data['filter_customer_id'] = $filter_customer_id; 		
		$this->data['filter_email'] = $filter_email; 	
		$this->data['filter_product_id'] = $filter_product_id; 	
		$this->data['filter_location'] = $filter_location;		
		$this->data['filter_shipping'] = $filter_shipping;
		$this->data['filter_payment'] = $filter_payment;
		$this->data['filter_shipping_zone'] = $filter_shipping_zone;		
		$this->data['filter_shipping_country'] = $filter_shipping_country;
		$this->data['filter_payment_country'] = $filter_payment_country;		
		$this->data['filter_sort'] = $filter_sort;	
		$this->data['filter_details'] = $filter_details;
		
		$this->template = 'report/adv_sale_order.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());

    	if (isset($this->request->post['export']) && $this->request->post['export'] == 1) { // export_xls
    			$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
				$xls_output ="<html><head>";
				$xls_output .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				$xls_output .="</head>";
				$xls_output .="<body>";				
				$xls_output .="<table border='1'>";	
				$xls_output .="<tr>";
				$xls_output .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_start')."</td>";
				$xls_output .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_end')."</td>";	
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_orders')."</td>";				
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_customers')."</td>";
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_products')."</td>";
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_sub_total')."</td>";
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_points')."</td>";				
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_shipping')."</td>";
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_coupon')."</td>";				
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_tax')."</td>";
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_credit')."</td>";				
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_voucher')."</td>";				
				$xls_output .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_total')."</td>";				
				$xls_output .="</tr>";
				foreach ($results as $result) {						
					$xls_output .="<tr>";
					$xls_output .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$xls_output .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";	
					$xls_output .= "<td align='right'>".$result['orders']."</td>";					
					$xls_output .= "<td align='right'>".$result['customers']."</td>";
					$xls_output .= "<td align='right'>".$result['products']."</td>";
					$xls_output .= "<td align='right'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";
					$xls_output .= "<td align='right'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$xls_output .= "<td align='right'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$xls_output .= "<td align='right'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$xls_output .= "<td align='right'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$xls_output .= "<td align='right'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$xls_output .= "<td align='right'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$xls_output .= "<td align='right'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";					
					$xls_output .="</tr>";				
				}
				$xls_output .="</body></html>";

			$filename = "sale_order_report_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Type: application/vnd.ms-excel; charset=UTF-8; encoding=UTF-8');			
			header('Content-Disposition: attachment; filename='.$filename.".xls");
			header('Content-Transfer-Encoding: UTF-8');	
			print $xls_output;			
			exit;
			
		} elseif (isset($this->request->post['export']) && $this->request->post['export'] == 2) { // export_xls_detail
    			$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
				$export_xls_orders_details ="<html><head>";
				$export_xls_orders_details .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				$export_xls_orders_details .="</head>";
				$export_xls_orders_details .="<body>";
				foreach ($results as $result) {					
				$export_xls_orders_details .="<table border='1'>";		
				$export_xls_orders_details .="<tr>";
				$export_xls_orders_details .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_start')."</td>";
				$export_xls_orders_details .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_end')."</td>";	
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_orders')."</td>";				
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_customers')."</td>";
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_products')."</td>";
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_sub_total')."</td>";
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_points')."</td>";				
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_shipping')."</td>";
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_coupon')."</td>";				
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_tax')."</td>";
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_credit')."</td>";				
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_voucher')."</td>";				
				$export_xls_orders_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_total')."</td>";				
				$export_xls_orders_details .="</tr>";				
					$export_xls_orders_details .="<tr>";
					$export_xls_orders_details .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$export_xls_orders_details .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";
					$export_xls_orders_details .= "<td align='right'>".$result['orders']."</td>";					
					$export_xls_orders_details .= "<td align='right'>".$result['customers']."</td>";
					$export_xls_orders_details .= "<td align='right'>".$result['products']."</td>";
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$export_xls_orders_details .= "<td align='right'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";				
					$export_xls_orders_details .="</tr>";
					$export_xls_orders_details .="<tr>";
					$export_xls_orders_details .= "<td colspan='2'></td>";
					$export_xls_orders_details .= "<td colspan='11' align='center'>";
						$export_xls_orders_details .="<table border='1'>";
						$export_xls_orders_details .="<tr>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_order_id')."</td>";					
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_date_added')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_inv_id')."</td>";									
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_customer')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_email')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_customer_group')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_shipping_method')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_payment_method')."</td>";						
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_status')."</td>";
						$export_xls_orders_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_store')."</td>";
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_quantity')."</td>";	
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_currency')."</td>";
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_sub_total')."</td>";
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_shipping')."</td>";
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_tax')."</td>";							
						$export_xls_orders_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_order_value')."</td>";							
						$export_xls_orders_details .="</tr>";
						$export_xls_orders_details .="<tr>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_ord_idc']."</td>";					
						$export_xls_orders_details .= "<td align='left'>".$result['order_order_date']."</td>";
						$export_xls_orders_details .= "<td align='left'><table border='0'>";
						$export_xls_orders_details .="<tr>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_inv_prefix']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_inv_id']."</td>";
						$export_xls_orders_details .="</tr>";					
						$export_xls_orders_details .="</table></td>";					
						$export_xls_orders_details .= "<td align='left'>".$result['order_name']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_email']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_group']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_shipping_method']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_payment_method']."</td>";						
						$export_xls_orders_details .= "<td align='left'>".$result['order_status']."</td>";
						$export_xls_orders_details .= "<td align='left'>".$result['order_store']."</td>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_products']."</td>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_currency']."</td>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_sub_total']."</td>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_shipping']."</td>";
						$export_xls_orders_details .= "<td align='right'>".$result['order_tax']."</td>";							
						$export_xls_orders_details .= "<td align='right'>".$result['order_value']."</td>";					
						$export_xls_orders_details .="</tr>";					
						$export_xls_orders_details .="</table>";
				$export_xls_orders_details .="</td>";
				$export_xls_orders_details .="</tr>";					
			}
				$export_xls_orders_details .="</body></html>";

			$filename = "sale_order_report_orders_details_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Type: application/vnd.ms-excel; charset=UTF-8; encoding=UTF-8');			
			header('Content-Disposition: attachment; filename='.$filename.".xls");
			header('Content-Transfer-Encoding: UTF-8');
			print $export_xls_orders_details;			
			exit;

		} elseif (isset($this->request->post['export']) && $this->request->post['export'] == 3) { // export_xls_products_details
    			$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
				$export_xls_products_details ="<html><head>";
				$export_xls_products_details .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				$export_xls_products_details .="</head>";
				$export_xls_products_details .="<body>";	
				foreach ($results as $result) {					
				$export_xls_products_details .="<table border='1'>";		
				$export_xls_products_details .="<tr>";
				$export_xls_products_details .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_start')."</td>";
				$export_xls_products_details .= "<td align='left' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_date_end')."</td>";	
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_orders')."</td>";				
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_customers')."</td>";
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_products')."</td>";
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_sub_total')."</td>";
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_points')."</td>";				
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_shipping')."</td>";
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_coupon')."</td>";				
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_tax')."</td>";
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_credit')."</td>";				
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_voucher')."</td>";				
				$export_xls_products_details .= "<td align='right' style='background-color:#D8D8D8; font-weight:bold;'>".$this->language->get('column_total')."</td>";					
				$export_xls_products_details .="</tr>";				
					$export_xls_products_details .="<tr>";
					$export_xls_products_details .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$export_xls_products_details .= "<td align='left'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";
					$export_xls_products_details .= "<td align='right'>".$result['orders']."</td>";					
					$export_xls_products_details .= "<td align='right'>".$result['customers']."</td>";
					$export_xls_products_details .= "<td align='right'>".$result['products']."</td>";
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$export_xls_products_details .= "<td align='right'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";						
					$export_xls_products_details .="</tr>";
					$export_xls_products_details .="<tr>";
					$export_xls_products_details .= "<td colspan='2'></td>";
					$export_xls_products_details .= "<td colspan='11' align='center'>";
						$export_xls_products_details .="<table border='1'>";
						$export_xls_products_details .="<tr>";
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_order_id')."</td>";					
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_date_added')."</td>";
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_inv_id')."</td>";									
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_sku')."</td>";
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_name')."</td>";
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_model')."</td>";
						$export_xls_products_details .= "<td align='left' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_manu')."</td>";
						$export_xls_products_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_currency')."</td>";						
						$export_xls_products_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_price')."</td>";
						$export_xls_products_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_quantity')."</td>";
						$export_xls_products_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_total')."</td>";	
						$export_xls_products_details .= "<td align='right' style='background-color:#EFEFEF; font-weight:bold;'>".$this->language->get('column_prod_tax')."</td>";						
						$export_xls_products_details .="</tr>";
						$export_xls_products_details .="<tr>";
						$export_xls_products_details .= "<td align='left'>".$result['product_ord_idc']."</td>";					
						$export_xls_products_details .= "<td align='left'>".$result['product_order_date']."</td>";
						$export_xls_products_details .= "<td align='left'><table border='0'>";
						$export_xls_products_details .="<tr>";
						$export_xls_products_details .= "<td align='right'>".$result['product_inv_prefix']."</td>";
						$export_xls_products_details .= "<td align='left'>".$result['product_inv_id']."</td>";
						$export_xls_products_details .="</tr>";					
						$export_xls_products_details .="</table></td>";					
						$export_xls_products_details .= "<td align='left'>".$result['product_sku']."</td>";
						$export_xls_products_details .= "<td align='left'>".$result['product_name']."</td>";
						$export_xls_products_details .= "<td align='left'>".$result['product_model']."</td>";
						$export_xls_products_details .= "<td align='left'>".$result['product_manu']."</td>";
						$export_xls_products_details .= "<td align='right'>".$result['product_currency']."</td>";						
						$export_xls_products_details .= "<td align='right'>".$result['product_price']."</td>";
						$export_xls_products_details .= "<td align='right'>".$result['product_quantity']."</td>";
						$export_xls_products_details .= "<td align='right'>".$result['product_total']."</td>";
						$export_xls_products_details .= "<td align='right'>".$result['product_tax']."</td>";						
						$export_xls_products_details .="</tr>";					
						$export_xls_products_details .="</table>";
				$export_xls_products_details .="</td>";
				$export_xls_products_details .="</tr>";				
			}
				$export_xls_products_details .="</body></html>";

			$filename = "sale_order_report_products_details_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Type: application/vnd.ms-excel; charset=UTF-8; encoding=UTF-8');			
			header('Content-Disposition: attachment; filename='.$filename.".xls");
			header('Content-Transfer-Encoding: UTF-8');
			print $export_xls_products_details;			
			exit;

		} elseif (isset($this->request->post['export']) && $this->request->post['export'] == 4) { // export_html
    			$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
			$export_html ="<html><head>";
			$export_html .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
			$export_html .="</head>";
			$export_html .="<body>";
			$export_html .="<style type='text/css'>
.list_main {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;	
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.list_main td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;	
}
.list_main thead td {
	background-color: #E5E5E5;
	padding: 3px;
	font-weight: bold;
}
.list_main tbody a {
	text-decoration: none;
}
.list_main tbody td {
	vertical-align: middle;
	padding: 3px;
}
.list_main .left {
	text-align: left;
	padding: 7px;
}
.list_main .right {
	text-align: right;
	padding: 7px;
}
.list_main .center {
	text-align: center;
	padding: 3px;
}
</style>";
				$export_html .="<table class='list_main'>";
				$export_html .="<thead>";
				$export_html .="<tr>";
				$export_html .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_start')."</td>";
				$export_html .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_end')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_orders')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_customers')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_products')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_sub_total')."</td>";	
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_points')."</td>";				
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_shipping')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_coupon')."</td>";				
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_tax')."</td>";
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_credit')."</td>";					
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_voucher')."</td>";				
				$export_html .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_total')."</td>";					
				$export_html .="</tr>";
				$export_html .="</thead><tbody>";
				foreach ($results as $result) {
					$export_html .="<tr>";
					$export_html .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$export_html .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";
					$export_html .= "<td align='right' nowrap='nowrap'>".$result['orders']."</td>";					
					$export_html .= "<td align='right' nowrap='nowrap'>".$result['customers']."</td>";
					$export_html .= "<td align='right' nowrap='nowrap'>".$result['products']."</td>";
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";	
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$export_html .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";					
					$export_html .="</tr>";				
				}
				$export_html .="</tbody></table>";
				$export_html .="</body></html>";

			$filename = "sale_order_report_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Disposition: attachment; filename='.$filename.".html");
			print $export_html;			
			exit;
			
		} elseif (isset($this->request->post['export']) && $this->request->post['export'] == 5) { // export_html_detail
    			$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
			$export_html_orders_details ="<html><head>";
			$export_html_orders_details .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
			$export_html_orders_details .="</head>";
			$export_html_orders_details .="<body>";
			$export_html_orders_details .="<style type='text/css'>
.list_main {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.list_main td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;	
}
.list_main thead td {
	background-color: #E5E5E5;
	padding: 3px;
}
.list_main thead td a, .list_main thead td {
	text-decoration: none;
	font-weight: bold;
}
.list_main tbody a {
	text-decoration: none;
}
.list_main tbody td {
	vertical-align: middle;
	padding: 3px;
}
.list_main .left {
	text-align: left;
	padding: 7px;
}
.list_main .right {
	text-align: right;
	padding: 7px;
}
.list_main .center {
	text-align: center;
	padding: 3px;
}

.list_detail {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	margin-top: 10px;
	margin-bottom: 10px;
}
.list_detail td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;
}
.list_detail thead td {
	background-color: #F0F0F0;
	padding: 0px 3px;
	font-size: 11px;
}
.list_detail tbody td {
	padding: 0px 3px;
	font-size: 11px;	
}
.list_detail .left {
	text-align: left;
	padding: 3px;
}
.list_detail .right {
	text-align: right;
	padding: 3px;
}
.list_detail .center {
	text-align: center;
	padding: 3px;
}
</style>";
				foreach ($results as $result) {	
				$export_html_orders_details .="<table class='list_main'>";
				$export_html_orders_details .="<thead>";
				$export_html_orders_details .="<tr>";
				$export_html_orders_details .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_start')."</td>";
				$export_html_orders_details .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_end')."</td>";				
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_customers')."</td>";
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_orders')."</td>";
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_products')."</td>";
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_sub_total')."</td>";	
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_points')."</td>";				
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_shipping')."</td>";
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_coupon')."</td>";				
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_tax')."</td>";
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_credit')."</td>";					
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_voucher')."</td>";				
				$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_total')."</td>";					
				$export_html_orders_details .="</tr>";
				$export_html_orders_details .="</thead><tbody>";				
					$export_html_orders_details .="<tr>";
					$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";				
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['customers']."</td>";
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['orders']."</td>";
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['products']."</td>";
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";	
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";
					$export_html_orders_details .="</tr>";
					$export_html_orders_details .="<tr>";
					$export_html_orders_details .= "<td colspan='13' align='center'>";
						$export_html_orders_details .="<table class='list_detail'>";
						$export_html_orders_details .="<thead>";
						$export_html_orders_details .="<tr>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_order_id')."</td>";					
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_date_added')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_inv_id')."</td>";									
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_customer')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_email')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_customer_group')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_shipping_method')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_payment_method')."</td>";						
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_status')."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_order_store')."</td>";
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_quantity')."</td>";	
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_currency')."</td>";	
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_sub_total')."</td>";	
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_shipping')."</td>";	
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_tax')."</td>";							
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_order_value')."</td>";						
						$export_html_orders_details .="</tr>";
						$export_html_orders_details .="</thead><tbody>";
						$export_html_orders_details .="<tr>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_ord_idc']."</td>";					
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_order_date']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'><div style='display:inline-block; float:left;'>".$result['order_inv_prefix']."</div><div style='display:inline-block; float:left;'>".$result['order_inv_id']."</div></td>";			
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_name']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_email']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_group']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_shipping_method']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_payment_method']."</td>";						
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_status']."</td>";
						$export_html_orders_details .= "<td align='left' nowrap='nowrap'>".$result['order_store']."</td>";
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_products']."</td>";
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_currency']."</td>";	
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_sub_total']."</td>";
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_shipping']."</td>";
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_tax']."</td>";						
						$export_html_orders_details .= "<td align='right' nowrap='nowrap'>".$result['order_value']."</td>";					
						$export_html_orders_details .="</tr>";					
						$export_html_orders_details .="</tbody></table>";
						$export_html_orders_details .="</td>";
						$export_html_orders_details .="</tr>";								
				}
				$export_html_orders_details .="</tbody></table>";
				$export_html_orders_details .="</body></html>";

			$filename = "sale_order_report_orders_details_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Disposition: attachment; filename='.$filename.".html");
			print $export_html_orders_details;			
			exit;			
		
		} elseif (isset($this->request->post['export']) && $this->request->post['export'] == 6) { // export_html_products_details
    		$results = $this->model_report_adv_sale_order->getSaleOrderExport($data);
				
			$export_html_products_details ="<html><head>";
			$export_html_products_details .="<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
			$export_html_products_details .="</head>";
			$export_html_products_details .="<body>";
			$export_html_products_details .="<style type='text/css'>
.list_main {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.list_main td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;	
}
.list_main thead td {
	background-color: #E5E5E5;
	padding: 3px;
}
.list_main thead td a, .list_main thead td {
	text-decoration: none;
	font-weight: bold;
}
.list_main tbody a {
	text-decoration: none;
}
.list_main tbody td {
	vertical-align: middle;
	padding: 3px;
}
.list_main .left {
	text-align: left;
	padding: 7px;
}
.list_main .right {
	text-align: right;
	padding: 7px;
}
.list_main .center {
	text-align: center;
	padding: 3px;
}

.list_detail {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	margin-top: 10px;
	margin-bottom: 10px;
}
.list_detail td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;
}
.list_detail thead td {
	background-color: #F0F0F0;
	padding: 0px 3px;
	font-size: 11px;
}
.list_detail tbody td {
	padding: 0px 3px;
	font-size: 11px;	
}
.list_detail .left {
	text-align: left;
	padding: 3px;
}
.list_detail .right {
	text-align: right;
	padding: 3px;
}
.list_detail .center {
	text-align: center;
	padding: 3px;
}
</style>";
				foreach ($results as $result) {	
				$export_html_products_details .="<table class='list_main'>";
				$export_html_products_details .="<thead>";
				$export_html_products_details .="<tr>";
				$export_html_products_details .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_start')."</td>";
				$export_html_products_details .= "<td align='left' width='80' nowrap='nowrap'>".$this->language->get('column_date_end')."</td>";				
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_customers')."</td>";
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_orders')."</td>";
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_products')."</td>";
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_sub_total')."</td>";	
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_points')."</td>";				
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_shipping')."</td>";
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_coupon')."</td>";				
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_tax')."</td>";
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_credit')."</td>";					
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_voucher')."</td>";				
				$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_total')."</td>";				
				$export_html_products_details .="</tr>";
				$export_html_products_details .="</thead><tbody>";				
					$export_html_products_details .="<tr>";
					$export_html_products_details .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_start']))."</td>";
					$export_html_products_details .= "<td align='left' nowrap='nowrap'>".date($this->language->get('date_format_short'), strtotime($result['date_end']))."</td>";				
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['customers']."</td>";
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['orders']."</td>";
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['products']."</td>";
					$export_html_products_details .= "<td align='right' nowrap='nowrap' style='background-color:#DCFFB9;'>".$this->currency->format($result['sub_total'], $this->config->get('config_currency'))."</td>";	
					$export_html_products_details .= "<td align='right' nowrap='nowrap' style='background-color:#ffd7d7;'>".$this->currency->format($result['reward'], $this->config->get('config_currency'))."</td>";					
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['shipping'], $this->config->get('config_currency'))."</td>";
					$export_html_products_details .= "<td align='right' nowrap='nowrap' style='background-color:#ffd7d7;'>".$this->currency->format($result['coupon'], $this->config->get('config_currency'))."</td>";					
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['tax'], $this->config->get('config_currency'))."</td>";
					$export_html_products_details .= "<td align='right' nowrap='nowrap' style='background-color:#ffd7d7;'>".$this->currency->format($result['credit'], $this->config->get('config_currency'))."</td>";					
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['voucher'], $this->config->get('config_currency'))."</td>";				
					$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->currency->format($result['total'], $this->config->get('config_currency'))."</td>";
					$export_html_products_details .="</tr>";
					$export_html_products_details .="<tr>";
					$export_html_products_details .= "<td colspan='13' align='center'>";
						$export_html_products_details .="<table class='list_detail'>";
						$export_html_products_details .="<thead>";
						$export_html_products_details .="<tr>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_order_id')."</td>";					
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_date_added')."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_inv_id')."</td>";									
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_sku')."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_name')."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_model')."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$this->language->get('column_prod_manu')."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_prod_currency')."</td>";						
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_prod_price')."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_prod_quantity')."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_prod_total')."</td>";	
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$this->language->get('column_prod_tax')."</td>";					
						$export_html_products_details .="</tr>";
						$export_html_products_details .="</thead><tbody>";
						$export_html_products_details .="<tr>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_ord_idc']."</td>";					
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_order_date']."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'><div style='display:inline-block; float:left;'>".$result['product_inv_prefix']."</div><div style='display:inline-block; float:left;'>".$result['product_inv_id']."</div></td>";			
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_sku']."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_name']."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_model']."</td>";
						$export_html_products_details .= "<td align='left' nowrap='nowrap'>".$result['product_manu']."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['product_currency']."</td>";						
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['product_price']."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['product_quantity']."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['product_total']."</td>";
						$export_html_products_details .= "<td align='right' nowrap='nowrap'>".$result['product_tax']."</td>";						
						$export_html_products_details .="</tr>";					
						$export_html_products_details .="</tbody></table>";
						$export_html_products_details .="</td>";
						$export_html_products_details .="</tr>";								
				}
				$export_html_products_details .="</tbody></table>";
				$export_html_products_details .="</body></html>";

			$filename = "sale_order_report_products_details_".date("Y-m-d",time());
			header('Expires: 0');
			header('Cache-control: private');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Description: File Transfer');			
			header('Content-Disposition: attachment; filename='.$filename.".html");
			print $export_html_products_details;			
			exit;			
		}	
	}
	
	public function customer_autocomplete() {
		$json = array();

		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->get['filter_customer_id']) || isset($this->request->get['filter_email']) || isset($this->request->get['filter_company'])) {
			$this->load->model('report/adv_sale_order');
					
		if (isset($this->request->get['filter_company'])) {
			$filter_company = $this->request->get['filter_company'];
		} else {
			$filter_company = '';
		}
		
		if (isset($this->request->get['filter_customer_id'])) {
			$filter_customer_id = $this->request->get['filter_customer_id'];
		} else {
			$filter_customer_id = '';
		}

		if (isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		} else {
			$filter_email = '';
		}	
		
		$data = array(		
			'filter_company' 	 		=> $filter_company,
			'filter_customer_id' 	 	=> $filter_customer_id,			
			'filter_email' 	 			=> $filter_email			
		);
						
		$results = $this->model_report_adv_sale_order->getCustomerAutocomplete($data);
			
			foreach ($results as $result) {
				$json[] = array(
					'customer_id'     		=> $result['customer_id'],
					'cust_company'     		=> html_entity_decode($result['cust_company'], ENT_QUOTES, 'UTF-8'),					
					'cust_name'     		=> html_entity_decode($result['cust_name'], ENT_QUOTES, 'UTF-8'),
					'cust_email'     		=> $result['cust_email']					
				);
			}
		}
		
		$this->response->setOutput(json_encode($json));
	}	
	
	public function product_autocomplete() {
		$json = array();

		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->get['filter_product_id'])) {
			$this->load->model('report/adv_sale_order');
					
		if (isset($this->request->get['filter_product_id'])) {
			$filter_product_id = $this->request->get['filter_product_id'];
		} else {
			$filter_product_id = '';
		}
		
		$data = array(				
			'filter_product_id' 	 	=> $filter_product_id			
		);
						
		$results = $this->model_report_adv_sale_order->getProductAutocomplete($data);
			
			foreach ($results as $result) {
				$json[] = array(
					'product_id'     		=> $result['product_id'],
					'prod_name'     		=> html_entity_decode($result['prod_name'], ENT_QUOTES, 'UTF-8')					
				);
			}
		}
		
		$this->response->setOutput(json_encode($json));
	}	
}
?>