<?php
static $config = NULL;
static $log = NULL;

// Error Handler
function error_handler_for_export($errno, $errstr, $errfile, $errline) {
	global $config;
	global $log;
	
	switch ($errno) {
		case E_NOTICE:
		case E_USER_NOTICE:
			$errors = "Notice";
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$errors = "Warning";
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$errors = "Fatal Error";
			break;
		default:
			$errors = "Unknown";
			break;
	}
		
	if (($errors=='Warning') || ($errors=='Unknown')) {
		return true;
	}

	if ($config->get('config_error_display')) {
		echo '<b>' . $errors . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
	}
	
	if ($config->get('config_error_log')) {
		$log->write('PHP ' . $errors . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
	}

	return true;
}


function fatal_error_shutdown_handler_for_export()
{
	$last_error = error_get_last();
	if ($last_error['type'] === E_ERROR) {
		// fatal error
		error_handler_for_export(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
	}
}





class ControllerReportStock extends Controller {
	public function index() {
		
		$this->data['url_product'] = (HTTPS_SERVER . 'index.php?route=catalog/product/update&product_id=');
	     
		$this->load->language('report/stock');

		$this->document->setTitle($this->language->get('heading_title'));
		
		
 		if (isset($this->session->data['warning'])) {
			$this->data['error_warning'] = $this->session->data['warning'];
			
			unset($this->session->data['warning']);
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		if (isset($this->request->get['filter_category'])) {
			$this->data['filter_category'] = $this->request->get['filter_category'];
		} else {
			$this->data['filter_category'] = '0';
		}
		
		if (isset($this->request->get['filter_calc'])) {
			$this->data['filter_calc'] = $this->request->get['filter_calc'];
		} else {
			$this->data['filter_calc'] = '0';
		}
		
		if (isset($this->request->get['filter_store'])) {
			$this->data['filter_store'] = $this->request->get['filter_store'];
		} else {
			$this->data['filter_store'] = '0';
		}
		
		
		if (isset($this->request->get['sort'])) {
			$this->data['sort'] = $this->request->get['sort'];
			$this->data['direction'] = substr($this->request->get['sort'], -1) == 'a' ? 'b' : 'a';
		} else {
			$this->data['sort'] = '';
			$this->data['direction'] = 'a';
		}
		
		if (isset($this->request->get['option2'])) {
			$option2 = $this->request->get['option2'] ;
		}else{
			$option2 = 'filter' ;
		}

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

		$url = '';
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
						
		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => (HTTPS_SERVER . 'index.php?route=common/home'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => (HTTPS_SERVER . 'index.php?route=report/stock' . $url),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);		
		
		$this->data['action'] = $this->url->link('report/stock', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->load->model('report/stock');
		
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			if ((isset( $this->request->files['upload'] )) && (is_uploaded_file($this->request->files['upload']['tmp_name']))) {
				$file = $this->request->files['upload']['tmp_name'];
				if ($this->model_report_stock->upload($file)) {
					$this->session->data['success'] = $this->language->get('text_success');
					$this->redirect($this->url->link('report/stock', 'token=' . $this->session->data['token'], 'SSL'));
				}
				 else {
				
					$this->session->data['warning'] = $this->language->get('error_upload');
					$this->redirect($this->url->link('report/stock', 'token=' . $this->session->data['token'], 'SSL'));
					
 				}
			}
				
		}
		
		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
				
		
		if ($option2!='csv'){
		$count = 0;
		
		$this->data['products'] = $this->model_report_stock->getProductStockReport($page , 15, $count, $this->data['filter_category'],$this->data['sort'],$this->data['filter_calc'],$this->data['filter_store'] );
		 
 		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_model'] = $this->language->get('column_model');
		$this->data['column_subtract'] = $this->language->get('column_subtract');
		$this->data['column_sku'] = $this->language->get('column_sku');
		$this->data['column_stock'] = $this->language->get('column_stock');
		$this->data['column_pricesell'] = $this->language->get('column_pricesell');
		$this->data['column_pricebuy'] = $this->language->get('column_pricebuy');
		$this->data['column_stockval'] = $this->language->get('column_stockval');
		$this->data['column_margin'] = $this->language->get('column_margin');
		
		$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_calc'] = $this->language->get('entry_calc');
		$this->data['entry_calc1'] = $this->language->get('entry_calc1');
		$this->data['entry_store'] = $this->language->get('entry_store');
		
		
	
		$this->data['button_csv'] = $this->language->get('button_csv');
		$this->data['button_import'] = $this->language->get('button_import');
		$this->data['button_filter'] = $this->language->get('button_filter');
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('catalog/category');
				
		$this->data['categories'] = $this->model_catalog_category->getCategories(0);
	
				
		$pagination = new Pagination();
		$pagination->total = $count;
		$pagination->page = $page;
		$pagination->limit = 15;
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('report/stock', 'token=' . $this->session->data['token'] . $url . '&page={page}&filter_category='.$this->data['filter_category'].'&sort='.$this->data['sort'].'&filter_store='.$this->data['filter_store'],$this->data['filter_calc']);
			
		$this->data['pagination'] = $pagination->render();
		 
		$this->template = 'report/stock.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	elseif ($option2 =='csv')
	
	{
		$this->model_report_stock->createXLS($this->data['filter_category'],$this->data['filter_store']);
		
	
	}
	
}	
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'report/stock')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
}
?>