<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*	
 *	Author 	: Ladur
 *	Tanggal : 15-12-2014	
 *	Revisi 	: 
 */ 

class Account_history extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 97;
    public $code = "Item Code";
    public $name = "Item Name";
	public $menu_model      = 'm_account_history';
	
    public function __construct() {
        parent::__construct();
		
		$this->load->model('m_menu');
		$this->load->model('m_account_history');	
		$this->load->model('custom/m_custom');	
		$this->load->model('setup/m_branchinfo');	
		$this->load->model('login/m_login');
	
		$is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
		if($is_login){

		}else{
			$username       = $this->session->userdata('username');
			$session_id     = $this->session->userdata('session_id');
			$this->m_login->delete_session($username,$session_id);
			$this->session->sess_destroy();

			$uri_func = $this->uri->segment(3);
			if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
				redirect(base_url() . 'login', 'refresh');
			}else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
				$result['status']   = 'session';
				$result['message']  = get_notification('session_destroy',0);
				echo json_encode($result);
				die();
			}
		}		
	}
	
	//Function Index
    public function index(){	
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
			
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					//untuk ambil data dari session dan hak akses
					$group_menu 		= $this->session->userdata('group_menu');
					
					$data['menu_id'] 	=  $this->menu_id;
					$data['menu_model'] = $this->menu_model;

					$data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id);
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					 $this->load->view('v_header');
					$this->load->view('account_history/v_account_history', $data);	
					$this->load->view('v_footer');
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	
	
	//Function preview data
    public function to_preview(){
	     // set data
		$data['menu_id'] =  $this->menu_id;
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		// set data
		$data['list'] = json_encode($this->m_account_history->get_all());
		
		$this->load->view('v_header');
		$this->load->view('account_history/v_preview_account_history', $data);	
		$this->load->view('v_footer');	
	}
	
	public function cmb_menu(){
       echo json_encode($this->m_account_history->get_account_history());
    }	
	
	public function to_print(){
	     // set data
		$data['menu_id'] =  $this->menu_id;
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
		// set data
		$data['list'] = json_encode($this->m_account_history->get_all());
		
		$this->load->view('v_header');
		$this->load->view('account_history/v_print_account_history', $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'] : ''; 
		
		$Type			= isset($param['cmb_AccountType']) ? $param['cmb_AccountType'] : ''; 
		$DateFrom		= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo			= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$Menu			= isset($param['cmb_menu']) ? $param['cmb_menu'] : ''; 
		
		$sess_branch	= $this->session->userdata('branch');
		/*============================================end============================================================*/
		
		if($Number != ""){$this->db->like('av_account_history.AccountNo', $Number);}
		if($Desc != ""){$this->db->like('av_account_history.AccountName', $Desc);}
		
		if($Menu != ""){
			if($Menu == 'All'){
				
			}
			else{
				$this->db->like('av_account_history.Source', $Menu);
			}
		} //change
		
		if($Type != ""){$this->db->where('am_accountsubtype.AccountTypeID', $Type);}
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('av_account_history.Date >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('av_account_history.Date <=', date("Y-m-d", strtotime($DateTo))); 
			}
        }
		
		$this->db->join('am_accountgl','am_accountgl.AccountNumber = av_account_history.AccountNo','INNER');
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID = am_accountgl.AccountSubTypeID','INNER');	
		$this->db->like('av_account_history.BranchCode', $sess_branch);
		
		$count_data = $this->db->count_all_results('av_account_history'); //change
		$offset 	= $count_data - ($rows * $page);
		if($offset > 0){
			$rows = $rows;
		}
		else{
			$rows = $count_data - ($rows*($page-1));
		}
		/*========================end=======================================*/
		$result = $this->m_account_history->get_akun(); //change
		
		
		for($i=0; $i<=($rows-1); $i++){
			// kalau master ambil dari get_status_master(), kalau transaksi get_status()
			
            $hasil[] = array(
				'AccountNo' 	=>	$result[$i]->AccountNo,
				'AccountName' 	=>	$result[$i]->AccountName,
				'Debet' 		=>	$result[$i]->Debet,
				'Credit' 		=>	$result[$i]->Credit,
				'Description' 	=>	$result[$i]->Description,
				'Date' 			=>	$result[$i]->Date,
				'Source'		=>	$result[$i]->Source,
				'SourceNo' 		=> 	$result[$i]->SourceNo,
				'TransLink' 	=> 	$result[$i]->TransLink
			);
		}

		if(empty($hasil)){
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
		else{
			$result = array();
			$result['total'] = $count_data;
			$result['rows'] = $hasil;
			echo json_encode($result);
		}
	}
}