<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Herpan / 04-08-2014	
 *	Revisi 	: Ferry
 	Revisi 	: Fachri
 */ 

class Approval extends CI_Controller {
    public function __construct() {
        parent::__construct();	
        $this->load->model('m_approval');
		$this->load->model('m_menu');	
		$this->load->helper('status');		
    }	

    //Function Index
    public function index() {
        //-----------------get model menu untuk approval list-------------------------
		$group_menu             = $this->session->userdata('group_menu');
		$data['detail'] 		= $get_detail_menu = $this->m_menu->get_menu_by_menu_order($group_menu);
		//------------------end------------------------------------------------------
		
		$this->load->view('v_header');
        $this->load->view('approval/v_approval_list', $data);	
        $this->load->view('v_footer');	
    }

	public function ajax(){
		$page	= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows	= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$hasil = array();
		/*===========rows*page tidak boleh melebihi dari jumlah data======= */
		
		/*=================================post dari find di kiri====================================================*/
		$param 			= $this->input->post('param');
		$Number			= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : ''; 
		$Desc			= isset($param['txt_Description']) ? $param['txt_Description'] : ''; 
		$Cmb_Menu		= isset($param['cmb_menu']) ? $param['cmb_menu'] : '';
		/*============================================end============================================================*/
		
		/*========================jika kondisi tidak sama dengan kosong di find lakukan proses di if=================*/
		$this->db->where('BranchCode', $this->session->userdata('branch'));
		
		if($Number != ""){$this->db->like('TransNo', $Number);}
		if($Desc != ""){$this->db->like('Description', $Desc);}
		
		if($Cmb_Menu != ""){
			if($Cmb_Menu == "All"){
				
			}
			else{
				$this->db->like('TransType', $Cmb_Menu);
			}
		}
		
		if($this->session->userdata('jobtitle_status') == 0){
			$this->db->where_in('av_approval_listing.TransStatus', array(5,6));
		}
		else{
			$this->db->where_in('av_approval_listing.TransStatus', array(4,6));
		}
		
		$count_data = $this->db->count_all_results('av_approval_listing');
		$offset 	= $count_data - ($rows * $page);
		if($offset > 0){
			$rows = $rows;
		}
		else{
			$rows = $count_data - ($rows*($page-1));
		}
		/*========================end=======================================*/
		$result = $this->m_approval->get_all();
		
		for($i=0; $i<=($rows-1); $i++){
			// kalau master ambil dari get_status_master(), kalau transaksi get_status()
			$TransStatus = get_status($result[$i]->TransStatus);
			
            $hasil[] = array(
				'BranchName' 	=>	$result[$i]->BranchName, 
				'TransNo' 		=>	$result[$i]->TransNo, 
				'TransDate' 	=>	$result[$i]->TransDate,
				'Description' 	=>	$result[$i]->Description, 
				'CreatedBy' 	=>	$result[$i]->CreatedBy, 
				'CreatedAt' 	=>	$result[$i]->CreatedAt, 
				'TransType' 	=>	$result[$i]->TransType,
				'TransLink' 	=>	$result[$i]->TransLink,
				'TransStatus' 	=>	$TransStatus
			);
		}
		
		if(empty($hasil)){
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
		else{
			$result = array();
			$result['total'] = $count_data;
			$result['rows'] = $hasil;
			echo json_encode($result);
		}
    }
	
	/*============================get menu from tbl menu=========================================*/
    function cmb_approval(){
        $data = $this->m_approval->search_menu();
		$hasil[] = array(
			'TransType' 	=>	"All",
			'TransMenu' 	=>	"All"
		);
		
		foreach($data as $row){
			$hasil[] = array(
				'TransType' 	=>	$row->TransType,
				'TransMenu' 	=>	$row->TransMenu
			);
		}
		
		
		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
    }
	/*======================================end================================================================*/
}	