<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class At_master extends MY_Controller{
	public function index(){
		$this->load->view($this->router->fetch_class()."/index");
	}
	
	public function load_data(){
		$this->load->model("user_model");
		$this->load->model("wfc_item_model");
		$this->load->model("wfc_item_grp_model");
		$this->load->model("wfc_gold_model");
		$this->load->model("wfc_category_model");
		$this->load->model("wfc_model_model");
		$this->load->model("wfc_type_model");
		$this->load->model("wfc_unit_model");
		$this->load->model("wfc_unit_w_model");
		$this->load->model("wfc_taxcode_model");
		$this->load->model("wfc_taxgroup_model");
		$this->load->model("wfc_staff_model");
		$this->load->model("wfc_debtor_model");
		$this->load->model("user_model");
		$post = $this->input->post();
		
		$data['user'] = $this->fm->get_current_user();
		
		if($data['user'] != NULL){
			if($this->input->post('CATEGORY') != "" && count($post['CATEGORY']) > 0){
				
				$condition = "";
					
				//Search---
				if($this->input->post('DATE_FROM') != ""){
					$condition = $this->fm->append($condition, "<T>.DATE >= '".date("Y-m-d 00:00:00.000", strtotime($post['DATE_FROM']))."'");
					
					$data['params']['Date'] = date("d/m/Y", strtotime($post['DATE_FROM']));
				}
				
				if($this->input->post('DATE_TO') != ""){
					$condition = $this->fm->append($condition, "<T>.DATE <= '".date("Y-m-d 23:59:59.999", strtotime($post['DATE_TO']))."'");
					
					$data['params']['Date'] = isset($data['params']['Date'])? $data['params']['Date']:"*";
					$data['params']['Date'] .= " - ".date("d/m/Y", strtotime($post['DATE_TO']));
				}
				elseif(isset($data['params']['Date'])){
					$data['params']['Date'] .= " - *";
				}
					
				if($this->input->post('USER') != "" && count($post['USER']) > 0){
					$condition = $this->fm->append($condition, $this->fm->build_condition($post['USER'], "WEB_USER.PKKEY"));
					
					$data['params']['User'] = "";
					$user_array = $this->user_model->get_data(array(
						'condition' => $this->fm->build_condition($post['USER'], "<T>.<PK>"),
						'' => "",
					));
					foreach($user_array as $v){
						if($data['params']['User'] != "") $data['params']['User'] .= ", ";
						if($v->CODE != "") $data['params']['User'] .= "[".$v->CODE."] ";
						$data['params']['User'] .= $v->NAME;
					}
				}
				//---
				
				//Params---
				if($this->input->post('CATEGORY') == "at_item"){
				
					//Search---
					//---------
					
					$data['list'] = $this->wfc_item_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_item_grp"){
				
					//Search---
					//---------
					
					$data['list'] = $this->wfc_item_grp_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}
				
				elseif($this->input->post('CATEGORY') == "at_gold"){
				
					//Search---
					//---------
					
					$data['list'] = $this->wfc_gold_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_category"){
				
					//Search---
					//---------
					
					$data['list'] = $this->wfc_gold_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}
				
				elseif($this->input->post('CATEGORY') == "at_model"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_model_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_type"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_type_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_unit"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_unit_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_unit_w"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_unit_w_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_taxgroup"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_taxgroup_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_taxcode"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_taxcode_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_staff"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_staff_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}

				elseif($this->input->post('CATEGORY') == "at_debtor"){
					
					//Search---
					//---------
					
					$data['list'] = $this->wfc_debtor_model->get_data(array(
						'condition' => $condition,
						'additional_select' => "WEB_USER.CODE AS WEB_USER_CODE, WEB_USER.NAME AS WEB_USER_NAME",
					));
				}
				
				//print_r($data['sub_list']);return;
				$data['font_size'] = (double)$this->input->post('FONT_SIZE') > 0? (double)$post['FONT_SIZE']:$this->report_font_size;
				
				//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') == "at_item"){
					$this->load->view($this->router->fetch_class()."/load_data_at_item", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_item_grp"){
					$this->load->view($this->router->fetch_class()."/load_data_at_item_grp", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_gold"){
					$this->load->view($this->router->fetch_class()."/load_data_at_gold", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_category"){
					$this->load->view($this->router->fetch_class()."/load_data_at_category", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_model"){
					$this->load->view($this->router->fetch_class()."/load_data_at_model", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_type"){
					$this->load->view($this->router->fetch_class()."/load_data_at_type", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_unit"){
					$this->load->view($this->router->fetch_class()."/load_data_at_unit", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_unit_w"){
					$this->load->view($this->router->fetch_class()."/load_data_at_unit_w", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_taxgroup"){
					$this->load->view($this->router->fetch_class()."/load_data_at_taxgroup", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_taxcode"){
					$this->load->view($this->router->fetch_class()."/load_data_at_taxcode", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_staff"){
					$this->load->view($this->router->fetch_class()."/load_data_at_staff", $data);
				}
				elseif($this->input->post('CATEGORY') == "at_debtor"){
					$this->load->view($this->router->fetch_class()."/load_data_at_debtor", $data);
				}
				return;
			}
			return;
		}

		header("HTTP/1.1 500 Internal Server Error");
		header("Content-Type: application/json; charset=UTF-8");
		return;
	}
}