<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * Author   : Dhidhik / Tanggal : 13-10-2015
 * Revisi   : 
 */

class Index_cash_bank extends CI_Controller {
    public $menu_model  = array('m_index_to_report', 'm_index_to_report', 'm_index_cash_bank', 'file/m_new_company', 'setup/m_branchinfo','custom/m_custom','list/m_deposit','list/m_ibank_transfer');
    public $menu_id_payment     = 292;
    public $menu_id_deposit     = 293;
    public $menu_id_sweep_bank  = 295;
    public $menu_id_afiliasi    = 296;
    public $menu_id_pitang_karyawan         = 298;
    public $menu_id_transf_to_petty_cash    = 300;
    public $menu_id_ibank_transfer          = 301;
    public $menu_id_other_income            = 302;
    public $menu_id_other_payment           = 360;
    public $menu_id_bank_book               = 376;
    
    public function __construct() {
        parent::__construct();
        $this->load->model($this->menu_model);

        $this->load->helper('convert_date_helper');
        $this->load->helper('status');
        $this->load->helper('message');
        $this->load->helper('number');

        if ($this->session->userdata("login") != TRUE) {
            redirect("login", "refresh");
            die;
        }
    }

    public function payment_report() {        
        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_payment);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_payment, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_payment);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_payment, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_payment_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_reloadDataPayment(){  
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_payment();
        count($resultData)>0?null:$resultData=[0];            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataPayment(){      
        $resultData = $this->m_index_cash_bank->get_all_payment();
        count($resultData)>0?null:$resultData=[0];    
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    function get_payment_from() {
        $CompanyCode    = $this->uri->segment(4); 
        $BranchCode     = $this->uri->segment(5); 
        $result         = $this->m_index_cash_bank->get_petyy_cash($CompanyCode, $BranchCode);
        //echo $this->db->last_query();
        $result[]       = array('AccountNumber'=>"All",'AccountName'=>"All");
        $rst            = json_encode($result);
        echo "$rst";
    }
    
    public function deposit_report() {        
        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_deposit);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_deposit, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_deposit);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_deposit, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_deposit_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
    
    public function pre_ajax_reloadDataDeposit(){   
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_deposit();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataDeposit(){  
        $resultData = $this->m_index_cash_bank->get_all_deposit();        
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    function get_deposit_from(){        
        $CompanyCode        = $this->uri->segment(4);
        $BranchCode         = $this->uri->segment(5);
        $list_petty_cash    = $this->m_index_cash_bank->get_deposit_from($CompanyCode, $BranchCode);
        //echo $this->db->last_query();
        //die();
        $list_petty_cash    = json_decode($list_petty_cash);
        $list_petty_cash[]  = array('CompanyCode'=>'All','BranchCode'=>'All','AccountNumber'=>"All",'AccountName'=>"All",'CurrencyCode'=>'All');
        $list_petty_cash    = json_encode($list_petty_cash);
        echo"$list_petty_cash";
    }
    
    function get_deposit_to(){
        $CompanyCode        = $this->uri->segment(4);
        $BranchCode         = $this->uri->segment(5);
        $list_petty_cash    = $this->m_index_cash_bank->get_deposit_to($CompanyCode, $BranchCode);
        //echo $this->db->last_query();
        //die();
        $list_petty_cash    = json_decode($list_petty_cash);
        $list_petty_cash[]  = array('CompanyCode'=>'All','BranchCode'=>'All','AccountNumber'=>"All",'AccountName'=>"All",'CurrencyCode'=>'All');
        $list_petty_cash    = json_encode($list_petty_cash);
        echo"$list_petty_cash";
    }
    
    public function sweep_bank_report() {        
        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_sweep_bank);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sweep_bank, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_sweep_bank);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sweep_bank, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_sweep_bank_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
    public function pre_ajax_reloadDataSweepBank(){  
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_sweep_bank();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSweepBank(){ 
        $resultData = $this->m_index_cash_bank->get_all_sweep_bank();
        //echo $this->db->last_query();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function get_sweep_bank_to(){       
        $CompanyCode    = $this->uri->segment(4); 
        $list_bank      = $this->m_index_cash_bank->get_sweep_bank_to($CompanyCode);
        echo"$list_bank";
    }    
    
    public function afiliasi_report() {        
        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_afiliasi);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_afiliasi, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_afiliasi);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_afiliasi, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_afiliasi_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }    
    public function pre_ajax_reloadDataAfiliasi(){       
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_afiliasi();
	count($resultData)>0?null:$resultData=[0];
            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataAfiliasi(){        
        $resultData = $this->m_index_cash_bank->get_all_afiliasi();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
        
    public function piutang_karyawan_report() {        
        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_pitang_karyawan);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_pitang_karyawan, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_pitang_karyawan);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_pitang_karyawan, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_piutang_karyawan_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
    public function pre_ajax_reloadDataPiutangKaryawan(){   
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_piutang_karyawan();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataPiutangKaryawan(){    
        $resultData = $this->m_index_cash_bank->get_all_piutang_karyawan();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
        
    public function transfer_to_petty_cash_report() {        
        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_transf_to_petty_cash);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_transf_to_petty_cash, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_transf_to_petty_cash);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_transf_to_petty_cash, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_transfer_to_petty_cash_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataTransferToPettyCash(){   
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_transfer_to_petty_cash();
	count($resultData)>0?null:$resultData=[0];            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataTransferToPettyCash(){   
        $resultData = $this->m_index_cash_bank->get_all_transfer_to_petty_cash();
        count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
        
    public function inter_bank_transfer_report() {        
        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_ibank_transfer);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_ibank_transfer, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_ibank_transfer);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_ibank_transfer, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_inter_bank_transfer_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
    public function pre_ajax_reloadDataInterBankTransfer(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_inter_bank_transfer();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataInterBankTransfer(){     
        $resultData = $this->m_index_cash_bank->get_all_inter_bank_transfer();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function get_ibank_from(){        
        $CompanyCode    = $this->uri->segment(4);
        $BranchCode     = $this->uri->segment(5);
        $list_bank      = $this->m_index_cash_bank->get_ibank_from($BranchCode);
        $list_bank      = json_decode($list_bank);
        $list_bank[]    = array("AccountNumber"=>"All","AccountName"=>"All","CurrencyCode"=>"All","BranchName"=>"All","BranchCode"=>"All");
        echo json_encode($list_bank);
    }
    
    public function get_ibank_to(){
        $company_code   = $this->uri->segment(4);
        $currency_idr   = $this->uri->segment(5);
        $currency_code  = $this->uri->segment(6);
        $list_bank      =  $this->m_ibank_transfer->get_coa_bank_to_ho_add($company_code, $currency_idr, $currency_code);
        $list_bank      = json_decode($list_bank);
        $list_bank[]    = array('CompanyCode'=>'All','BranchCode'=>'All','AccountNumber'=>"All",'AccountName'=>"All",'CurrencyCode'=>'All');
        echo json_encode($list_bank);
    }
        
    public function other_income_report(){
        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_other_income);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_other_income, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_other_income);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_other_income, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_other_income_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
        
    public function pre_ajax_reloadDataOIncome(){    
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_cash_bank->pre_get_all_other_income();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataOIncome(){      
        $resultData = $this->m_index_cash_bank->get_all_other_income();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function get_oincome_to(){        
        $CompanyCode    = $this->uri->segment(4);
        $BranchCode     = $this->uri->segment(5);
        $list_bank      = $this->m_index_cash_bank->get_oincome_to($CompanyCode,$BranchCode);
        //$list_bank      = json_decode($list_bank);
        $list_bank[]    = array("AccountNumber"=>"All","AccountName"=>"All","CurrencyCode"=>"All","BranchName"=>"All","BranchCode"=>"All");
        echo json_encode($list_bank);
    }
    
    public function other_payment_report(){
        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_other_payment);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_other_payment, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_other_payment);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_other_payment, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_other_payment_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }    
    public function pre_ajax_reloadDataOPayment(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            

        $resultData = $this->m_index_cash_bank->pre_get_all_other_payment();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataOPayment(){        
        $resultData = $this->m_index_cash_bank->get_all_other_payment();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function bank_book_report(){
        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_bank_book);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_bank_book, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_bank_book);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_bank_book, $group_menu);

                    $this->load->view('v_header');
                    $this->load->view('report/cash_bank_to_report/v_bank_book_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }  
    public function pre_ajax_reloadDataBankBook(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $BankAccountNo  = "";
        $NominalBalaces  = 0;
        $messagesWarning= "";$CurrencySymbol= "";$BankCode= "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            $BankAccountNo  = ($this->input->post('cmb_bank')!=null)?$this->input->post('cmb_bank'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
            
            //======= cek end Period=========//
            $v_yearmonth_min1 = $this->intPeriodYearMonth($txt_DateFrom,"ind","min",1);//Month_Filter-1, result 20165
            $v_yearmonth = $this->intPeriodYearMonth($txt_DateFrom,"ind","same","");//DateFrom Filter hanya diambil tahun dan bulanya saja(YearMonth)20165
            $rstCekPeriod_0 = $this->cekEndPeriod_0($CompCode,$BranchCode,$BankAccountNo,$v_yearmonth_min1);
            $CurrencySymbol = $rstCekPeriod_0[0]->CurrencySymbol;
            $BankCode = $rstCekPeriod_0[0]->BankCode;
            
            $paramSaldo = array(
                "dateFrom" => ""
                , "dateParam" => ""
                , "BranchCode" => $BranchCode
                , "CompanyCode" => $CompCode
                , "BankAccountNo" => $BankAccountNo
                , "txt_dateFrom" => $txt_DateFrom
                , "transactionOnly" => ""
                , "oldDateTransaction" => ""
            );
            if($rstCekPeriod_0[0]->PeriodEnd_search!=null){//periodEnd sesuai Month_Filter-1 ditemukan
                //tinggal ditambah transaksi di jurnal smp dateFrom-1day
                $paramSaldo["dateFrom"] = $rstCekPeriod_0[0]->PeriodEnd_search;
                $paramSaldo["dateParam"] = "int";
                $paramSaldo["transactionOnly"] = "no";
                $NominalBalaces = $this->saldoAwal($paramSaldo);
            }else if($rstCekPeriod_0[0]->PeriodEnd!=null){//awal periodEnd ditemukan
                $dateYM_PE  = $this->stringYearMonth("int", $rstCekPeriod_0[0]->PeriodEnd);//result 201605
                $dateYM_From= $this->stringYearMonth("int", $v_yearmonth);//result 201605
                
                if($dateYM_From <= $dateYM_PE ){//sebenarnya ini ga akan mungkin,karena pasti ditemukan di if diatas (month-1 ada datanya),kecuali pencarian sebelum periodEnd pertama
                    if($rstCekPeriod_0[0]->oldDateTransaction != null){//jika ditemukan TglTransaksiTertua
                        if(convert_to_ymd($txt_DateFrom) < $rstCekPeriod_0[0]->oldDateTransaction){//tgl filter lebih kecil dr TglTransaksiTertua 
                            $messagesWarning = "Invalid Date";
                        }else{
                            $paramSaldo["dateFrom"] = $this->intPeriodYearMonth($rstCekPeriod_0[0]->oldDateTransaction,"eng", "same", "");
                            $paramSaldo["dateParam"] = "int";
                            $paramSaldo["transactionOnly"] = "yes";
                            $paramSaldo["oldDateTransaction"] = $rstCekPeriod_0[0]->oldDateTransaction;
                            $NominalBalaces = $this->saldoAwal($paramSaldo);
                        }
                    }else{
                        $messagesWarning = "Invalid Date";
                    }
                }else{//Month_Filter > PeriodEnd, brarti ada PeriodEnd hanya saja masih di bawah di Month_Filter-1
                    $paramSaldo["dateFrom"] = $rstCekPeriod_0[0]->PeriodEnd;
                    $paramSaldo["dateParam"] = "int";
                    $paramSaldo["transactionOnly"] = "no";
                    $NominalBalaces = $this->saldoAwal($paramSaldo);
                }
            }else{//awal PeriodEnd tidak Ditemukan,kemungkinan company belum berdiri ato mgkn sudah berdiri tp belum ada emperiod
                //cek tgl tua transaksi di jurnal, jika tgl tua transaksi>tglFilter brati invalid
                //else,  tinggal ditambah transaksi di jurnal smp dateFrom-1day
                if($rstCekPeriod_0[0]->oldDateTransaction != null){//jika ditemukan TglTransaksiTertua
                    if($txt_DateFrom < $rstCekPeriod_0[0]->oldDateTransaction){//tgl filter lebih kecil dr TglTransaksiTertua 
                        $messagesWarning = "Invalid Date ";
                    }else{
                        $paramSaldo["dateFrom"] = $this->intPeriodYearMonth($rstCekPeriod_0[0]->oldDateTransaction,"eng", "same", "");
                        $paramSaldo["dateParam"] = "int";
                        $paramSaldo["transactionOnly"] = "yes";
                        $paramSaldo["oldDateTransaction"] = $rstCekPeriod_0[0]->oldDateTransaction;
                        $NominalBalaces = $this->saldoAwal($paramSaldo);
                    }
                }else{
                    $messagesWarning = "Invalid Date";
                }
            }
            //======= cek end Period=========//
        }
        if($messagesWarning!=""){
            $resultData=[0];
        }else{
            $resultData = $this->m_index_cash_bank->pre_get_all_bank_book();
            count($resultData)>0?null:$resultData=[0];
        }
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName
                ,'nominalbalance'=>$NominalBalaces, "messagesWarning"=>$messagesWarning, "CurrencySymbol"=>$CurrencySymbol, "BankCode"=>$BankCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataBankBook(){     
        $resultData = $this->m_index_cash_bank->get_all_bank_book();
	count($resultData)>0?null:$resultData=[0];
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    function intPeriodYearMonth($txt_DateFrom,$formatDate, $param, $numbering){
        switch($param){
            case "min" : $yearMonth = date('Y-m', strtotime(date($this->dateToYM($txt_DateFrom, $formatDate)) . "- ".$numbering." month"));break;
            case "plus" : $yearMonth = date('Y-m', strtotime(date($this->dateToYM($txt_DateFrom, $formatDate)) . "+ ".$numbering." month"));break;
            default : $yearMonth = date('Y-m', strtotime(date($this->dateToYM($txt_DateFrom, $formatDate))));break;
        }
        $arYearMonth = $this->arYearMonth("str",$yearMonth);
        $v_year = $arYearMonth[0] * 1;//tahun dari bulan filter - 1
        $v_month = $arYearMonth[1]* 1;//bulan dari bulan filter - 1
        $v_yearmonth = $v_year.$v_month;//2016 . 5
        return $v_yearmonth; //20165
    }
    function cekEndPeriod_0($CompCode,$BranchCode,$BankAccountNo,$v_yearmonth){
        $rstStartPeriod = $this->m_index_cash_bank->cek_start_period($CompCode,$BranchCode,$BankAccountNo,$v_yearmonth);
        //contoh result : {PeriodEnd_search: null, PeriodEnd: "20165"}; 
        //PeriodEnd_search=period filter bulan-1, PeriodEnd=Emperiod tahunBulan yang paling baru.
//            echo "rstLastPeriod = ".  json_encode($rstStartPeriod);
        return $rstStartPeriod;
    }
    function saldoAwal($param){
        $arYearMonth = $this->arYearMonth($param['dateParam'],$param['dateFrom']);
        $v_year = $arYearMonth[0]*1;
        $v_month = $arYearMonth[1]*1;
        $columnName = $this->saldoColumnName($v_month);
        ($param['transactionOnly']=="yes")?$dateStartSum = $param['oldDateTransaction']:$dateStartSum = $this->startDateSumPeriod($param['dateParam'], $param['dateFrom']);
        $dateEndSum = $this->endDateSumPeriod($param['txt_dateFrom']);
        $setParamBalance = array(
            "v_year"=>$v_year
            ,"v_month"=>$v_month
            ,"CompanyCode"=>$param['CompanyCode'], "BranchCode"=>$param['BranchCode']
            ,"BankAccountNo"=>$param['BankAccountNo'], "columnName"=>$columnName
            , "dateStartSum" =>$dateStartSum, "dateEndSum"=>$dateEndSum
            , "transactionOnly" => $param['transactionOnly']
        );
        $rstBalacePeriod = $this->m_index_cash_bank->get_balance_period($setParamBalance);
        $saldo = ($rstBalacePeriod[0]->$columnName *1)+($rstBalacePeriod[0]->sumTransaction *1);
        return $saldo;
    }
    function saldoColumnName($v_month){
        $columnName = "GLSaldoBalance".$v_month;
        return $columnName;
    }
    function startDateSumPeriod($param, $dateFrom){//tanggal awal sesudah emperiod,parameter $param =
        switch($param){
            case 'str' :$next_YearMonth = (date('Y-m', strtotime(date($dateFrom. '+ 1 month'))));
                    $dateStartSum   = $next_YearMonth."-01";
                    break;
            case "int" : $str_YearMonth  = substr($dateFrom,0,4).'-'.substr($dateFrom,4,2);//ambil tahun bulan terakhir yang di emperiod
                    $next_YearMonth = date('Y-m', strtotime(date($str_YearMonth) . '+ 1 month'));
                    $dateStartSum   = $next_YearMonth."-01";
                    break;
        }
        return $dateStartSum;
    }
    function endDateSumPeriod($dateFrom){
        $dateEndSum     = date('Y-m-d', strtotime(date(($dateFrom)) . '- 1 day'));//tanggal akhir ,sebelum tanggal start pencarian FILTER
        return $dateEndSum;
    }
    function arYearMonth($param,$strDate){
        switch($param){
            case "str" : $arYearMonth = explode("-", $strDate);break;
            case "int" : $arYearMonth = array(substr($strDate,0,4), substr($strDate,4,2));break;
        }
        return $arYearMonth;//{2016 , 12, 31}
    }
    function stringYearMonth($param, $value){
        switch ($param){
            case "int" : $value_year = substr($value, 0,4);
                $value_month = substr($value, 4,2);
                strlen($value_month)<=1?$value_month = "0".$value_month:"";
                $result = $value_year.$value_month;
                break;
            case "str" : $arValue = explode("-", $value);
                $result = $arValue[0].$arValue[1];
                break;
        }
        return $result;
    }
    function get_list_bank(){
        $CompanyCode    = $this->uri->segment(4);
        $BranchCode     = $this->uri->segment(5);
        $list_bank      = $this->m_index_cash_bank->get_list_bank($CompanyCode,$BranchCode);
        echo json_encode($list_bank);
    }
    
    function get_opayment_from(){   
        $CompanyCode    = $this->uri->segment(4);
        $BranchCode     = $this->uri->segment(5);
        $list_bank      = $this->m_index_cash_bank->get_opayment_from($CompanyCode,$BranchCode);
        //$list_bank      = json_decode($list_bank);
        $list_bank[]    = array("AccountNumber"=>"All","AccountName"=>"All","CurrencyCode"=>"All","BranchName"=>"All","BranchCode"=>"All");
        echo json_encode($list_bank);
    }
        
    //untuk mendapatkan tanggal 1 di bulan yang bersangkutan
    function stringStartDateInd() {
        //$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
        $datestring = "01-%m-%Y";
        $time = time();
        return mdate($datestring, $time);
    }

    function dateSqlToIndo($txtDate) {
        $indoDate ="-";
        if($txtDate!=null && $txtDate!=""){
            $arDate     = explode("-", $txtDate);
            $indoDate   = $arDate[2]."-".$arDate[1]."-".$arDate[0];
        }
        return $indoDate;
    }

    function dateToYM($txtDate, $formatDate) {
        $arDate     = explode("-", $txtDate);
        $formatDate=="ind"?$indoDate   = $arDate[2]."-".$arDate[1]:$indoDate   = $arDate[0]."-".$arDate[1];;
        return $indoDate;
    }

    function get_branch_select_all(){        
        $data = $this->m_custom->get_branch();
        $data[]=array('BranchCode'=>"All",'BranchName'=>"All");
        echo json_encode($data);
    }
}

?>
