<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Ferry // Tanggal : 06-08-2014	
 *	Revisi 	: 
 */
 
class Account_balance extends CI_Controller {
    public $menu_id = 98;
	public $menu_model      = 'm_general_account_balance';
	
	
	public function __construct(){
        parent::__construct();
            $this->load->model('m_general_account_balance');
			$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();
				}
			}		   	
	}
	
	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('general_ledger/v_account_balance', $data);	
					$this->load->view('v_footer');
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
    
	//function get data to grid
	public function get_akun(){
		$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['account_type']) ? $param['account_type'] : ''; 
		/*============================================end============================================================*/
		/*========================jika kondisi tidak sama dengan kosong di find lakukan proses di if=================*/
		
		if($Number != ""){$this->db->like('at_compaccountgl.AccountNumber', $Number);} //change
		if($Desc != ""){$this->db->like('am_accountgl.AccountName', $Desc);} //change
		if($Type != ""){
			if($Type == 'All'){
				
			}
			else{
				$this->db->like('am_accountgl.AccountSubTypeID', $Type);
			}
		} //change
		
		$this->db->where_in('at_compaccountgl.TransStatus',array(2)); 
		$this->db->where('at_compaccountgl.GLSaldoYears',date('Y'));
		$this->db->where('at_compaccountgl.BranchCode',$this->session->userdata('branch'));  
		/*==========================end=================================================================*/
		$this->db->join('am_accountgl','am_accountgl.AccountNumber = at_compaccountgl.AccountNumber','INNER');	
		$count_data = $this->db->count_all_results('at_compaccountgl'); //change
		$offset 	= $count_data - ($rows * $page);
		if($offset > 0){
			$rows = $rows;
		}
		else{
			$rows = $count_data - ($rows*($page-1));
		}
		/*========================end=======================================*/
		$result = $this->m_general_account_balance->get_akun(); //change
		
		for($i=0; $i<=($rows-1); $i++){
			// kalau master ambil dari get_status_master(), kalau transaksi get_status()
			$hasil[] = array(
				'AccountNumber' 	=>	$result[$i]->AccountNumber,
				'AccountName' 		=>	$result[$i]->AccountName,
				'GLSaldoYears' 		=>	$result[$i]->GLSaldoYears,
				'GLSaldoOpening' 	=>	$result[$i]->GLSaldoOpening,
				'GLSaldoBSheet' 	=>	$result[$i]->GLSaldoBSheet,
				
				'GLSaldoBalance1' 	=>	$result[$i]->GLSaldoBalance1,
				'GLSaldoBalance2' 	=>	$result[$i]->GLSaldoBalance2,
				'GLSaldoBalance3' 	=>	$result[$i]->GLSaldoBalance3,
				'GLSaldoBalance4' 	=>	$result[$i]->GLSaldoBalance4,
				'GLSaldoBalance5' 	=>	$result[$i]->GLSaldoBalance5,
				'GLSaldoBalance6' 	=>	$result[$i]->GLSaldoBalance6,
				'GLSaldoBalance7' 	=>	$result[$i]->GLSaldoBalance7,
				'GLSaldoBalance8' 	=>	$result[$i]->GLSaldoBalance8,
				'GLSaldoBalance9' 	=>	$result[$i]->GLSaldoBalance9,
				'GLSaldoBalance10' 	=>	$result[$i]->GLSaldoBalance10,
				'GLSaldoBalance11' 	=>	$result[$i]->GLSaldoBalance11,
				'GLSaldoBalance12' 	=>	$result[$i]->GLSaldoBalance12,
				'AccountLevel' 		=>	$result[$i]->AccountLevel,
				'BranchCode' 		=>	$result[$i]->BranchCode,
				'BranchName' 		=>	$result[$i]->BranchName
			);
		}

		if(empty($hasil)){
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
		else{
			$result = array();
			$result['total'] = $count_data;
			$result['rows'] = $hasil;
			echo json_encode($result);
		}
	}
	
    public function get_saldo_akun(){
        $CompanyCode    = $this->session->userdata('company');
        $result         = $this->m_general_account_balance->get_saldo_akun($CompanyCode);
		
        foreach($result as $row) {
            $hasil[]    = array(
                            'GLSaldoActual'     => $row->GLSaldoActual,
                            'GLSaldoBalance1'   => $row->GLSaldoBalance1,
                            'GLSaldoBalance2'   => $row->GLSaldoBalance2,
                            'GLSaldoBalance3'   => $row->GLSaldoBalance3,
                            'GLSaldoBalance4'   => $row->GLSaldoBalance4,
                            'GLSaldoBalance5'   => $row->GLSaldoBalance5,
                            'GLSaldoBalance6'   => $row->GLSaldoBalance6,
                            'GLSaldoBalance7'   => $row->GLSaldoBalance7,
                            'GLSaldoBalance8'   => $row->GLSaldoBalance8,
                            'GLSaldoBalance9'   => $row->GLSaldoBalance9,
                            'GLSaldoBalance10'  => $row->GLSaldoBalance10,
                            'GLSaldoBalance11'  => $row->GLSaldoBalance11,
                            'GLSaldoBalance12'  => $row->GLSaldoBalance12
                        ); 
        }
		
        if(!empty($hasil)){
            echo json_encode($hasil);
        }
        else{
            $notFound = array();
            echo json_encode($notFound);
        }
    }
	
    public function to_preview(){
        $CompanyCode    = $this->session->userdata('company');
        $result         = $this->m_general_account_balance->get_akun_print();
		
        foreach($result as $row) {
            $hasil[]    = array(
				'AccountNumber' 	=> $row->AccountNumber,
				'AccountName' 		=> $row->AccountName,
				'Year' 				=> $row->GLSaldoYears,
				//'GLSaldoActual' 	=> $row->GLSaldoActual,
				'GLSaldoBalance1' 	=> $row->GLSaldoBalance1,
				'GLSaldoBalance2' 	=> $row->GLSaldoBalance2,
				'GLSaldoBalance3' 	=> $row->GLSaldoBalance3,
				'GLSaldoBalance4' 	=> $row->GLSaldoBalance4,
				'GLSaldoBalance5' 	=> $row->GLSaldoBalance5,
				'GLSaldoBalance6' 	=> $row->GLSaldoBalance6,
				'GLSaldoBalance7' 	=> $row->GLSaldoBalance7,
				'GLSaldoBalance8' 	=> $row->GLSaldoBalance8,
				'GLSaldoBalance9' 	=> $row->GLSaldoBalance9,
				'GLSaldoBalance10' 	=> $row->GLSaldoBalance10,
				'GLSaldoBalance11' 	=> $row->GLSaldoBalance11,
				'GLSaldoBalance12' 	=> $row->GLSaldoBalance12
			); 
        }
		
        $data['list'] = $hasil;

        $this->load->view('v_header');
        $this->load->view('general_ledger/v_preview_account_balance', $data);	
        $this->load->view('v_footer');	
    }
	
    function detail(){
        $id         = $this->uri->segment(4);
        $nojournal  = str_replace(".", "/", $id);

        $this->session->set_userdata('id_detail', $nojournal);
        $this->load->view('v_header');
        $this->load->view('general_ledger/v_detail_account_balance');
        $this->load->view('v_footer');
    }
	
    function ajax_detail(){
        $id     = $this->session->userdata('id_detail');
        echo $this->m_general_account_balance->get_by_id($id);
    }
}
	