<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class sr_report extends MY_Controller{
	public function index(){
		$this->load->view($this->router->fetch_class()."/index");
	}

	public function load_data(){
		$this->load->model("invoice_model");
		$this->load->model("purchase_model");
		$this->load->model("tradein_model");
		$this->load->model("adj_model");
		$this->load->model("monthly_report_model");
		$this->load->model("company_profile_model");
		
		$post = $this->input->post();
		//print_r($post);return;
		
		if($this->input->post('CATEGORY') != "" && count($post['CATEGORY']) > 0){
			$condition = "";
			$condition1 = "";
			$filter1 = "";

			//Params---
			if($this->input->post('CATEGORY') == "monthly_report"){
				$condition = "";

				if($this->input->post('DATE_FROM') != "" && $this->input->post('DATE_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.DATE BETWEEN '".date("Y-m-d", strtotime($post['DATE_FROM']))."' AND '".date("Y-m-d", strtotime($post['DATE_TO']))."'");
						
					$data['Date'] = date("d M Y", strtotime($post['DATE_FROM']))." TO ".date("d M Y", strtotime($post['DATE_TO']));
					$data['params']['DATE'] = date("d M Y", strtotime($post['DATE_FROM']))." - ".date("d M Y", strtotime($post['DATE_TO']));
				}

				if($this->input->post('TIME_FROM') != "" && $this->input->post('TIME_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.TIME BETWEEN '".date("H:i:s", strtotime($post['TIME_FROM']))."' AND '".date("H:i:s", strtotime($post['TIME_TO']))."'");
						
					$data['Time'] = date("H:i:s", strtotime($post['TIME_FROM']))." TO ".date("H:i:s", strtotime($post['TIME_TO']));
					$data['params']['TIME'] = date("H:i:s", strtotime($post['TIME_FROM']))." - ".date("H:i:s", strtotime($post['TIME_TO']));
				}

        		$additional_join = ""; 
		        $data['list'] = $this->monthly_report_model->get_data(array(
					'sort' => "<T>.REFNO DESC",
					'condition' => $condition,
					'additional_select' => "",
					'additional_join' => $additional_join, 	
					'group_by' => ""

				));
			}else if($this->input->post('CATEGORY') == "sales_report"){
				$condition = "";

				if($this->input->post('DATE_FROM') != "" && $this->input->post('DATE_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.DATE BETWEEN '".date("Y-m-d", strtotime($post['DATE_FROM']))."' AND '".date("Y-m-d", strtotime($post['DATE_TO']))."'");
						
					$data['Date'] = date("d M Y", strtotime($post['DATE_FROM']))." TO ".date("d M Y", strtotime($post['DATE_TO']));
					$data['params']['DATE'] = date("d M Y", strtotime($post['DATE_FROM']))." - ".date("d M Y", strtotime($post['DATE_TO']));
				}

				if($this->input->post('TIME_FROM') != "" && $this->input->post('TIME_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.TIME BETWEEN '".date("H:i:s", strtotime($post['TIME_FROM']))."' AND '".date("H:i:s", strtotime($post['TIME_TO']))."'");
						
					$data['Time'] = date("H:i:s", strtotime($post['TIME_FROM']))." TO ".date("H:i:s", strtotime($post['TIME_TO']));
					$data['params']['TIME'] = date("H:i:s", strtotime($post['TIME_FROM']))." - ".date("H:i:s", strtotime($post['TIME_TO']));
				}

        		$additional_join = ""; 
        		$additional_join .= "LEFT JOIN TRADEIN ON TRADEIN.RECEIPT = <T>.REFNO";
        		$additional_join .= " LEFT JOIN DEBTOR ON DEBTOR.CODE = <T>.DEBTOR";
		        $data['list'] = $this->invoice_model->get_data(array(
					'sort' => "<T>.DATE DESC",
					'condition' => $condition,
					'additional_select' => "TRADEIN.TRADEIN_W AS TRADEIN, DEBTOR.NAME AS DEBTOR_NAME",
					'additional_join' => $additional_join, 	
					'group_by' => ""

				));
			}else if($this->input->post('CATEGORY') == "purchase_report"){
				$condition = "";

				if($this->input->post('DATE_FROM') != "" && $this->input->post('DATE_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.DATE BETWEEN '".date("Y-m-d", strtotime($post['DATE_FROM']))."' AND '".date("Y-m-d", strtotime($post['DATE_TO']))."'");
						
					$data['Date'] = date("d M Y", strtotime($post['DATE_FROM']))." TO ".date("d M Y", strtotime($post['DATE_TO']));
					$data['params']['DATE'] = date("d M Y", strtotime($post['DATE_FROM']))." - ".date("d M Y", strtotime($post['DATE_TO']));
				}

				if($this->input->post('TIME_FROM') != "" && $this->input->post('TIME_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.TIME BETWEEN '".date("H:i:s", strtotime($post['TIME_FROM']))."' AND '".date("H:i:s", strtotime($post['TIME_TO']))."'");
						
					$data['Time'] = date("H:i:s", strtotime($post['TIME_FROM']))." TO ".date("H:i:s", strtotime($post['TIME_TO']));
					$data['params']['TIME'] = date("H:i:s", strtotime($post['TIME_FROM']))." - ".date("H:i:s", strtotime($post['TIME_TO']));
				}

        		$additional_join = ""; 
        		$additional_join .= "LEFT JOIN CREDITOR ON CREDITOR.CODE = <T>.CREDITOR";
		        $data['list'] = $this->purchase_model->get_data(array(
					'sort' => "<T>.DATE DESC",
					'condition' => $condition,
					'additional_select' => "CREDITOR.NAME AS CREDITOR_NAME",
					'additional_join' => $additional_join, 	
					'group_by' => ""

				));
			}else if($this->input->post('CATEGORY') == "tradein_report"){
				$condition = "";

				if($this->input->post('DATE_FROM') != "" && $this->input->post('DATE_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.DATE BETWEEN '".date("Y-m-d", strtotime($post['DATE_FROM']))."' AND '".date("Y-m-d", strtotime($post['DATE_TO']))."'");
						
					$data['Date'] = date("d M Y", strtotime($post['DATE_FROM']))." TO ".date("d M Y", strtotime($post['DATE_TO']));
					$data['params']['DATE'] = date("d M Y", strtotime($post['DATE_FROM']))." - ".date("d M Y", strtotime($post['DATE_TO']));
				}

				if($this->input->post('TIME_FROM') != "" && $this->input->post('TIME_TO') != "" ){
					$condition = $this->fm->append($condition, "<T>.TIME BETWEEN '".date("H:i:s", strtotime($post['TIME_FROM']))."' AND '".date("H:i:s", strtotime($post['TIME_TO']))."'");
						
					$data['Time'] = date("H:i:s", strtotime($post['TIME_FROM']))." TO ".date("H:i:s", strtotime($post['TIME_TO']));
					$data['params']['TIME'] = date("H:i:s", strtotime($post['TIME_FROM']))." - ".date("H:i:s", strtotime($post['TIME_TO']));
				}

        		$additional_join = ""; 
        		$additional_join .= "LEFT JOIN DEBTOR ON DEBTOR.CODE = <T>.DEBTOR";
        		$additional_join .= " LEFT JOIN INVOICE ON INVOICE.TRADEIN_REFNO = <T>.REFNO";
		        $data['list'] = $this->tradein_model->get_data(array(
					'sort' => "<T>.DATE DESC",
					'condition' => $condition,
					'additional_select' => "DEBTOR.NAME AS DEBTOR_NAME, INVOICE.REFNO AS INVOICE_REFNO",
					'additional_join' => $additional_join, 	
					'group_by' => ""

				));
			}
			
			$data['fontsize'] = $this->input->post('FONTSIZE');
			$data['category'] = $this->input->post('CATEGORY');
			
			//Company Profile
	        $company = $this->company_profile_model->get_data(array(
	            'condition' => "",
	        ));
	        $data['company'] = $company[0];
			$user = $this->fm->get_current_user();
	        $data['username'] = $user->NAME;
	        $data['userid'] = $user->CODE;
	        $data['condition1'] = $filter1;
			
			//Store Footprint---
			if($this->record_footprint) $this->footprint_model->record($this->router->fetch_class()."/".$this->router->fetch_method(), isset($_SERVER['QUERY_STRING'])? $_SERVER['QUERY_STRING']:"", isset($_POST)? $_POST:array());
			//------------------
			
			//Set parameter to avoid Filter Data lost---
			$this->session->set_flashdata($this->router->fetch_class().'_page_filter', $_SERVER['QUERY_STRING']);
			//------------------------------------------
			
			
			if($this->input->post('CATEGORY') == "monthly_report"){
				$this->load->view($this->router->fetch_class()."/load_data_monthly_report", $data);}
			else if($this->input->post('CATEGORY') == "sales_report"){
				$this->load->view($this->router->fetch_class()."/load_data_sales_report", $data);}
			else if($this->input->post('CATEGORY') == "purchase_report"){
				$this->load->view($this->router->fetch_class()."/load_data_purchase_report", $data);}
			else if($this->input->post('CATEGORY') == "tradein_report"){
				$this->load->view($this->router->fetch_class()."/load_data_tradein_report", $data);}
	
		}
	}
		

}

