<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class reports extends MY_Controller{

public function index(){
	$this->load->view($this->router->fetch_class()."/index");
}
	
public function load_data(){
	$this->load->model("debtor_model");
	$this->load->model("creditor_model");
	$this->load->model("invoice_model");
	$this->load->model("invitem_model");
	$this->load->model("tradein_model");
	$this->load->model("tradeitem_model");
	$this->load->model("purchase_model");
	$this->load->model("puritem_model");
	$this->load->model("company_profile_model");

	$post = $this->input->post();
	// print_r($post);return;

	$data['list'] = array();
	//$data['logo'] = $this->fm->get_logo();
	

	if($this->input->post('CATEGORY') != "" && count($post['CATEGORY']) > 0 && $this->input->post('CATEGORY_REPORT') != "" && count($post['CATEGORY_REPORT']) > 0){

		$condition = "";
		$condition1 = "";
		$filter1 = "";
		
		//Params---
		if($this->input->post('CATEGORY') == "detail_invoice_listing"){
			$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('DEBTOR') != "" && count($post['DEBTOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['DEBTOR'], "<T>.DEBTOR"));
		
				$data['params']['DEBTOR'] = "";
				$debtor_array = $this->debtor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['DEBTOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($debtor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->DEBTOR)."] ";
					$temp .= $v->NAME;
					$data['params']['DEBTOR'] = $this->fm->append($data['params']['DEBTOR'], $temp, ", ");
				}
				$filter1 = "Debtor: ".$data['params']['DEBTOR'];
			}

			$additional_join = ""; 
	        $additional_join .= "LEFT JOIN DEBTOR ON DEBTOR.CODE = <T>.DEBTOR";
	        $additional_join .= " LEFT JOIN INVITEM ON INVITEM.REFNO = <T>.REFNO";
	        $invoice_array = $this->invoice_model->get_data(array(
				'sort' => "<T>.REFNO",
				'condition' => $condition,
				'additional_select' => "DEBTOR.NAME AS DEBTOR_NAME, INVITEM.ITEM AS INVITEM_ITEM, INVITEM.DESCRIPT AS INVITEM_DESCRIPT, INVITEM.INCH AS INVITEM_INCH, INVITEM.PCS AS INVITEM_PCS, INVITEM.QTY AS INVITEM_QTY, INVITEM.UNIT AS INVITEM_UNIT, INVITEM.PRICE AS INVITEM_PRICE, INVITEM.LABOUR AS INVITEM_LABOUR, INVITEM.TAXCODE AS INVITEM_TAXCODE, INVITEM.PERCEN AS INVITEM_PERCEN, INVITEM.GST AS INVITEM_GST, INVITEM.AMT AS INVITEM_AMT, INVITEM.LENGTH AS INVITEM_LENGTH, INVITEM.NEWTIME AS INVITEM_NEWTIME",
				'additional_join' => $additional_join, 	
			));

			if(count($invoice_array) > 0){
		        $data['invoice'] = $invoice_array[0];
				$data['list'] = array();
				foreach($invoice_array as $v){
					$data['list'][$v->REFNO][$v->INVITEM_LABOUR][$v->INVITEM_NEWTIME] = $v;
				}
			}

			//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;

		}elseif($this->input->post('CATEGORY') == "invoice_summary"){
			$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('DEBTOR') != "" && count($post['DEBTOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['DEBTOR'], "<T>.DEBTOR"));
				$data['params']['DEBTOR'] = "";
				$debtor_array = $this->debtor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['DEBTOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($debtor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->NAME)."] ";
					$temp .= $v->NAME;
					$data['params']['DEBTOR'] = $this->fm->append($data['params']['DEBTOR'], $temp, ", ");
				}
				$filter1 = "Debtor: ".$data['params']['DEBTOR'];
			}

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

			//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;

		}elseif($this->input->post('CATEGORY') == "detail_tradein_listing"){
			$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('DEBTOR') != "" && count($post['DEBTOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['DEBTOR'], "<T>.DEBTOR"));
		
				$data['params']['DEBTOR'] = "";
				$debtor_array = $this->debtor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['DEBTOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($debtor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->DEBTOR)."] ";
					$temp .= $v->NAME;
					$data['params']['DEBTOR'] = $this->fm->append($data['params']['DEBTOR'], $temp, ", ");
				}
				$filter1 = "Debtor: ".$data['params']['DEBTOR'];
			}

			$additional_join = ""; 
	        $additional_join .= "LEFT JOIN DEBTOR ON DEBTOR.CODE = <T>.DEBTOR";
	        $additional_join .= " LEFT JOIN TRADEITEM ON TRADEITEM.REFNO = <T>.REFNO";
	        $tradein_array = $this->tradein_model->get_data(array(
				'sort' => "<T>.REFNO",
				'condition' => $condition,
				'additional_select' => "DEBTOR.NAME AS DEBTOR_NAME, TRADEITEM.GOLD AS TRADEITEM_GOLD, TRADEITEM.DESCRIPT AS TRADEITEM_DESCRIPT, TRADEITEM.WEIGHT AS TRADEITEM_WEIGHT, TRADEITEM.UNIT_W AS TRADEITEM_UNIT_W, TRADEITEM.RATE AS TRADEITEM_RATE, TRADEITEM.GOLDPRICE AS TRADEITEM_GOLDPRICE, TRADEITEM.NEWTIME AS TRADEITEM_NEWTIME",
				'additional_join' => $additional_join, 	
			));
			$data['list'] = array();
			foreach($tradein_array as $v){
				$data['list'][$v->REFNO][$v->TRADEITEM_GOLDPRICE][$v->TRADEITEM_NEWTIME] = $v;
			}

			//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;

		}elseif($this->input->post('CATEGORY') == "tradein_summary"){
			$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('DEBTOR') != "" && count($post['DEBTOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['DEBTOR'], "<T>.DEBTOR"));
				$data['params']['DEBTOR'] = "";
				$debtor_array = $this->debtor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['DEBTOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($debtor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->NAME)."] ";
					$temp .= $v->NAME;
					$data['params']['DEBTOR'] = $this->fm->append($data['params']['DEBTOR'], $temp, ", ");
				}
				$filter1 = "Debtor: ".$data['params']['DEBTOR'];
			}

			$additional_join = ""; 
	        $additional_join .= "LEFT JOIN DEBTOR ON DEBTOR.CODE = <T>.DEBTOR";
	        $additional_join .= " LEFT JOIN STAFF ON STAFF.CODE = <T>.STAFF";
	        $data['list'] = $this->tradein_model->get_data(array(
				'sort' => "<T>.REFNO",
				'condition' => $condition,	
				'additional_select' => "DEBTOR.NAME AS DEBTOR_NAME, STAFF.NAME AS STAFF_NAME",
				'additional_join' => $additional_join,
			));

			//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;

		}elseif($this->input->post('CATEGORY') == "detail_purchase_listing"){
			$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('CREDITOR') != "" && count($post['CREDITOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['CREDITOR'], "<T>.CREDITOR"));
		
				$data['params']['CREDITOR'] = "";
				$creditor_array = $this->creditor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['CREDITOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($creditor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->CREDITOR)."] ";
					$temp .= $v->NAME;
					$data['params']['CREDITOR'] = $this->fm->append($data['params']['CREDITOR'], $temp, ", ");
				}
				$filter1 = "Creditor: ".$data['params']['CREDITOR'];
			}

			$additional_join = ""; 
	        $additional_join .= "LEFT JOIN CREDITOR ON CREDITOR.CODE = <T>.CREDITOR";
	        $additional_join .= " LEFT JOIN PURITEM ON PURITEM.REFNO = <T>.REFNO";
	        $tradein_array = $this->purchase_model->get_data(array(
				'sort' => "<T>.REFNO",
				'condition' => $condition,
				'additional_select' => "CREDITOR.NAME AS CREDITOR_NAME, PURITEM.GOLD AS PURITEM_GOLD, PURITEM.DESCRIPT AS PURITEM_DESCRIPT, PURITEM.WEIGHT AS PURITEM_WEIGHT, PURITEM.UNIT_W AS PURITEM_UNIT_W, PURITEM.RATE AS PURITEM_RATE, PURITEM.GOLDPRICE AS PURITEM_GOLDPRICE, PURITEM.NEWTIME AS PURITEM_NEWTIME",
				'additional_join' => $additional_join, 	
			));
			$data['list'] = array();
			foreach($tradein_array as $v){
				$data['list'][$v->REFNO][$v->PURITEM_GOLDPRICE][$v->PURITEM_NEWTIME] = $v;
			}

			//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;

		}elseif($this->input->post('CATEGORY') == "purchase_summary"){
			$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('CREDITOR') != "" && count($post['CREDITOR']) > 0){
				$condition = $this->fm->append($condition, $this->fm->build_condition($post['CREDITOR'], "<T>.CREDITOR"));
		
				$data['params']['CREDITOR'] = "";
				$creditor_array = $this->creditor_model->get_data(array(
					'condition' => $this->fm->build_condition($post['CREDITOR'], "<T>.<PK>"),
					'' => "",
				));
				foreach($creditor_array as $v){
					$temp = "";
					//if($v->CODE != "") $temp .= "[".trim($v->CREDITOR)."] ";
					$temp .= $v->NAME;
					$data['params']['CREDITOR'] = $this->fm->append($data['params']['CREDITOR'], $temp, ", ");
				}
				$filter1 = "Creditor: ".$data['params']['CREDITOR'];
			}

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

			//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;
		}
	}
			
		$data['fontsize'] = $this->input->post('FONTSIZE');
		$data['category'] = $this->input->post('CATEGORY');
		
		$user = $this->fm->get_current_user();
		$data['username'] = $user->NAME;
		$data['userid'] = $user->CODE;
		
		//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']);
		//------------------------------------------
	
		// $this->load->view($this->router->fetch_class()."/load_data", $data);
		if($this->input->post('CATEGORY') == "detail_invoice_listing"){
			$this->load->view($this->router->fetch_class()."/load_data_detail_invoice_listing", $data);}
		elseif($this->input->post('CATEGORY') == "invoice_summary"){
			$this->load->view($this->router->fetch_class()."/load_data_invoice_summary", $data);}
		elseif($this->input->post('CATEGORY') == "detail_tradein_listing"){
			$this->load->view($this->router->fetch_class()."/load_data_detail_tradein_listing", $data);}
		elseif($this->input->post('CATEGORY') == "tradein_summary"){
			$this->load->view($this->router->fetch_class()."/load_data_tradein_summary", $data);}
		elseif($this->input->post('CATEGORY') == "detail_purchase_listing"){
			$this->load->view($this->router->fetch_class()."/load_data_detail_purchase_listing", $data);}
		elseif($this->input->post('CATEGORY') == "purchase_summary"){
			$this->load->view($this->router->fetch_class()."/load_data_purchase_summary", $data);}

	}
}