<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* 
 * Author   : Ferry / Tanggal : 22-06-2015
 * Revisi   :
 */

class Index_financial_statement extends CI_Controller {
    public $menu_model          = array('m_index_to_report','list/m_account_journal','m_index_financial_statement','file/m_new_company','setup/m_branchinfo','login/m_login');
    public $menu_id_standard    = 290; 

    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');

        $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 financial_statements(){
        $this->load->view('v_header');
        $this->load->view('report/index_to_report/view_financial_statement');
        $this->load->view('v_footer');
    }

    public function preview_profit_loss_standard(){
        $uri_data               = str_replace('report/index_financial_statement/preview_profit_loss_standard/', "", $this->uri->uri_string());
        $date_period            = explode('/', $uri_data);

        $begin_date             = date("Y-m-d", strtotime($date_period[0]));
        $end_date               = date("Y-m-d", strtotime($date_period[1]));
        $branch                 = $date_period[2];
        $branch                 = str_replace('_', '-', $branch);
        $data['BeginDate']      = $begin_date;
        $data['EndDate']        = $end_date;

        //$branch                 = $this->session->userdata('branch');
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id_standard);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id_standard);      

       
        $plus                   = 1;
        $min                    = -1;
        $all_acc_type           = array('Revenue','COGS / COGM (Cost Of Goods Made)','Expense','Other Revenue','Other Expense');
        $arr_trans              = array();
        $arr_index              = -1;
        $arr_revenue            = array();
        $arr_cogm               = array();
        $arr_expense            = array();
        $arr_other_expense      = array();
        $arr_other_revenue      = array();

        $revenue_group          = 0;
        $cogm_group             = 0;
        $expense_group          = 0;
        $other_revenue_group    = 0;
        $other_expense_group    = 0;

        $all_transaction        = $this->m_index_financial_statement->get_all_transaction_by_date($branch,$begin_date,$end_date,$all_acc_type);
        //echo $this->db->last_query();

        foreach($all_transaction as $row){
            $Account_Number         = $row->Account_Number;
            $Sub_Type_Name          = $row->Sub_Type_Name;
            $Acc_Type_Name          = $row->Acc_Type_Name;
            $Account_Name           = $row->Account_Name;
            $balance                = $row->balance;
            $type_trans             = $row->type_trans;

            /*============================================= get detail transaction ===============================================*/
            if($Acc_Type_Name == 'Revenue'){

                /*================================== revenue group =================================================*/
                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }
                /*==================================================================================================*/

                /*================================== group detail revenue ======================================*/
                if(count($arr_revenue) == 0){
                    array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                }else{

                    $key    = $this->search_key($Account_Number,'Account_Number',$arr_revenue);
                    if($key === false){
                        array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                    }else{
                         $arr_revenue[$key]['balance'] += $balance;
                    }
                    
                }
                /*================================== end group detail revenue ======================================*/

                if($Sub_Type_Name =='Retur Penjualan'){
                    $revenue_group          -= $balance;
                }else{
                    $revenue_group          += $balance;
                }

            }elseif($Acc_Type_Name == 'COGS / COGM (Cost Of Goods Made)'){

                 /*================================== cogm group =================================================*/
                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $cogm_group          += $balance;
                /*==================================================================================================*/

                /*================================== group detail cogm ======================================*/
                if(count($arr_cogm) == 0){
                    array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                }else{

                    $key    = $this->search_key($Account_Number,'Account_Number',$arr_cogm);
                    if($key === false){
                        array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                    }else{
                         $arr_cogm[$key]['balance'] += $balance;
                    }
                    
                }
                /*================================== end group detail cogm ======================================*/
               

            }elseif($Acc_Type_Name == 'Expense'){

                /*================================== expense group =================================================*/
                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $expense_group          += $balance;
                /*==================================================================================================*/

                /*================================== group detail expense ======================================*/
                if(count($arr_expense) == 0){
                    array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                }else{
                    $key    = $this->search_key($Account_Number,'Account_Number',$arr_expense);
                    if($key === false){
                        array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                    }else{
                         $arr_expense[$key]['balance'] += $balance;
                    }
                    
                }
                /*================================== end group detail expense ======================================*/

                
            }elseif($Acc_Type_Name == 'Other Revenue'){

                 /*================================== other revenue group =================================================*/
                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Debet'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_revenue_group          += $balance;
                /*==================================================================================================*/

                /*================================== group detail other revenue ======================================*/
                if(count($arr_other_revenue) == 0){
                    array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                }else{
                    $key    = $this->search_key($Account_Number,'Account_Number',$arr_other_revenue);
                    if($key === false){
                        array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                    }else{
                         $arr_other_revenue[$key]['balance'] += $balance;
                    }
                    
                }
                /*================================== end group detail other revenue ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Expense'){

                /*================================== other expense group =================================================*/
                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_expense_group          += $balance;
                /*==================================================================================================*/

                /*================================== group detail other expense ======================================*/
                if(count($arr_other_expense) == 0){
                    array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                }else{
                    $key    = $this->search_key($Account_Number,'Account_Number',$arr_other_expense);
                    if($key === false){
                        array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'Sub_Type_Name'=>$Sub_Type_Name,'Acc_Type_Name'=>$Acc_Type_Name));
                    }else{
                         $arr_other_expense[$key]['balance'] += $balance;
                    }
                    
                }
                /*================================== end group detail other expense ======================================*/
            }

        }
        /*=================================================== end get detail transaction =======================================================*/

        $data['PL']                 = $revenue_group - $cogm_group - $expense_group + $other_revenue_group - $other_expense_group;
        $data['arr_revenue']        = $arr_revenue;
        $data['arr_cogm']           = $arr_cogm;
        $data['arr_expense']        = $arr_expense;
        $data['arr_other_expense']  = $arr_other_expense;
        $data['arr_other_revenue']  = $arr_other_revenue;
        $data['revenue_group']      = $revenue_group;
        $data['cogm_group']         = $cogm_group;
        $data['expense_group']      = $expense_group;
        $data['other_revenue_group'] = $other_revenue_group;
        $data['other_expense_group'] = $other_expense_group;

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_profit_lost_standard', $data);  
        $this->load->view('v_footer'); 
    }

    public function preview_profit_loss_multi_period($month_begin,$year_begin,$month_end,$year_end,$branch){
        //$branch                 = $this->session->userdata('branch');
        $branch                 = str_replace('_', '-', $branch);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id_standard);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id_standard);      

        $data['month_begin']    = change_month_indonesia($month_begin);
        $data['month_end']      = change_month_indonesia($month_end);
        $data['year_begin']     = $year_begin;
        $data['year_end']       = $year_end;

        $from_date              = date('Y-m-d',strtotime($year_begin.'-'.$month_begin.'-01'));
        $day_month              = date('t', strtotime($year_end.'-'.$month_end));
        $last_date              = date('Y-m-d',strtotime($year_end.'-'.$month_end.'-'.$day_month));

        $plus                   = 1;
        $min                    = -1;

        $arr_PL                     = array();
        $arr_revenue_group          = array();
        $arr_cogm_group             = array();
        $arr_expense_group          = array();
        $arr_other_revenue_group    = array();
        $arr_other_expense_group    = array();

        $arr_trans              = array();
        $arr_revenue            = array();
        $arr_cogm               = array();
        $arr_expense            = array();
        $arr_other_expense      = array();
        $arr_other_revenue      = array();
        $arr_index              = -1;
        $ind_revenue            = -1;
        $ind_cogm               = -1;
        $ind_expense            = -1;
        $ind_other_expense      = -1;
        $ind_other_revenue      = -1;
        $all_acc_type           = array('Revenue','COGS / COGM (Cost Of Goods Made)','Expense','Other Revenue','Other Expense');

        $interval_date          = $this->get_interval_month($month_begin,$year_begin,$month_end,$year_end);
        $arr_date               = array();
        $arr_balance            = array();

        for($x=0;$x<$interval_date;$x++){
            $arr_trans[$x]              = array();
            if($month_begin == 13){
                $month_begin        = 1;
                $year_begin         = $year_begin + 1;
                array_push($arr_date,array('month'=>$month_begin,'year'=>$year_begin));
            }else{
                array_push($arr_date,array('month'=>$month_begin,'year'=>$year_begin));
            }

            $arr_balance['balance'.$x]          = 0;

            $total_revenue[$x]                  = 0;
            $total_retur_penjualan[$x]          = 0;
            $total_retur_pembelian[$x]          = 0;
            $revenue_group[$x]                  = 0;    

            $total_cogm[$x]                     = 0;
            $total_biaya_pabrik[$x]             = 0;
            $total_biaya_marketing[$x]          = 0;
            $cogm_group[$x]                     = 0;   

            $total_expense[$x]                  = 0;
            $total_biaya_freight_out[$x]        = 0;
            $total_biaya_freight_in[$x]         = 0;
            $total_biaya_penyusutan[$x]         = 0;
            $expense_group[$x]                  = 0;

            $total_other_revenue[$x]            = 0;
            $other_revenue_group[$x]            = 0;

            $total_other_expense[$x]            = 0;
            $total_adjustment_expense[$x]       = 0;
            $total_kurs_transaction[$x]         = 0;
            $total_kurs_period[$x]              = 0;
            $other_expense_group[$x]            = 0;

            $arr_PL[$x]                         = 0;



            $month_begin++;
        }

        $all_transaction            = $this->m_index_financial_statement->get_all_transaction_by_month_year($branch,$from_date,$last_date,$all_acc_type,$arr_date);
        //echo $this->db->last_query();

        foreach($all_transaction as $row){
            $Account_Number         = $row->Account_Number;
            $Sub_Type_Name          = $row->Sub_Type_Name;
            $Acc_Type_Name          = $row->Acc_Type_Name;
            $Account_Name           = $row->Account_Name;
            $balance                = $row->balance;
            $type_trans             = $row->type_trans;
            $month                  = $row->month;
            $year                   = $row->year;

            /*============================================= get detail transaction ===============================================*/
            if($Acc_Type_Name == 'Revenue'){

                 $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail revenue ======================================*/
                if(count($arr_revenue) == 0){
                    array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                    $ind_revenue += 1;
                    $arr_revenue[$ind_revenue] = array_merge($arr_revenue[$ind_revenue],$arr_balance);
                    for($n=0;$n<count($arr_date);$n++){
                        if(($arr_revenue[$ind_revenue]['month'] == $arr_date[$n]['month']) and ($arr_revenue[$ind_revenue]['year'] == $arr_date[$n]['year'])){
                            if($Sub_Type_Name =='Retur Penjualan'){
                                $arr_revenue[$ind_revenue]['balance'.$n] -=  $balance;
                            }else{
                                $arr_revenue[$ind_revenue]['balance'.$n] +=  $balance;
                            }
                        }
                    }
                }else{
                    if($arr_revenue[$ind_revenue]['Account_Number'] == $Account_Number){
                        for($n=0;$n<count($arr_date);$n++){
                            if(($month == $arr_date[$n]['month']) and ($year == $arr_date[$n]['year'])){
                                if($Sub_Type_Name =='Retur Penjualan'){
                                    $arr_revenue[$ind_revenue]['balance'.$n] -=  $balance;
                                }else{
                                    $arr_revenue[$ind_revenue]['balance'.$n] +=  $balance;
                                }
                            }
                        }
                    }else{
                        array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                        $ind_revenue += 1;
                        $arr_revenue[$ind_revenue] = array_merge($arr_revenue[$ind_revenue],$arr_balance);
                        for($n=0;$n<count($arr_date);$n++){
                            if(($arr_revenue[$ind_revenue]['month'] == $arr_date[$n]['month']) and ($arr_revenue[$ind_revenue]['year'] == $arr_date[$n]['year'])){
                                if($Sub_Type_Name =='Retur Penjualan'){
                                    $arr_revenue[$ind_revenue]['balance'.$n] -=  $balance;
                                }else{
                                    $arr_revenue[$ind_revenue]['balance'.$n] +=  $balance;
                                }
                            }
                        }
                    }
                }
                /*================================== end group detail revenue ======================================*/

            }elseif($Acc_Type_Name == 'COGS / COGM (Cost Of Goods Made)'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail cogm ======================================*/
                if(count($arr_cogm) == 0){
                    array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                    $ind_cogm += 1;
                    $arr_cogm[$ind_cogm] = array_merge($arr_cogm[$ind_cogm],$arr_balance);
                    for($n=0;$n<count($arr_date);$n++){
                        if(($arr_cogm[$ind_cogm]['month'] == $arr_date[$n]['month']) and ($arr_cogm[$ind_cogm]['year'] == $arr_date[$n]['year'])){
                            $arr_cogm[$ind_cogm]['balance'.$n] +=  $balance;
                        }
                    }
                }else{
                    if($arr_cogm[$ind_cogm]['Account_Number'] == $Account_Number){
                        for($n=0;$n<count($arr_date);$n++){
                            if(($month == $arr_date[$n]['month']) and ($year == $arr_date[$n]['year'])){
                                $arr_cogm[$ind_cogm]['balance'.$n] +=  $balance;
                            }
                        }
                    }else{
                        array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                        $ind_cogm += 1;
                        $arr_cogm[$ind_cogm] = array_merge($arr_cogm[$ind_cogm],$arr_balance);
                        for($n=0;$n<count($arr_date);$n++){
                            if(($arr_cogm[$ind_cogm]['month'] == $arr_date[$n]['month']) and ($arr_cogm[$ind_cogm]['year'] == $arr_date[$n]['year'])){
                                $arr_cogm[$ind_cogm]['balance'.$n] +=  $balance;
                            }
                        }
                    }
                }
                /*================================== end group detail cogm ======================================*/

            }elseif($Acc_Type_Name == 'Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail expense ======================================*/
                if(count($arr_expense) == 0){
                    array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                    $ind_expense += 1;
                    $arr_expense[$ind_expense] = array_merge($arr_expense[$ind_expense],$arr_balance);
                    for($n=0;$n<count($arr_date);$n++){
                        if(($arr_expense[$ind_expense]['month'] == $arr_date[$n]['month']) and ($arr_expense[$ind_expense]['year'] == $arr_date[$n]['year'])){
                            $arr_expense[$ind_expense]['balance'.$n] +=  $balance;
                        }
                    }
                }else{
                    if($arr_expense[$ind_expense]['Account_Number'] == $Account_Number){
                        for($n=0;$n<count($arr_date);$n++){
                            if(($month == $arr_date[$n]['month']) and ($year == $arr_date[$n]['year'])){
                                $arr_expense[$ind_expense]['balance'.$n] +=  $balance;
                            }
                        }
                    }else{
                        array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                        $ind_expense += 1;
                        $arr_expense[$ind_expense] = array_merge($arr_expense[$ind_expense],$arr_balance);
                        for($n=0;$n<count($arr_date);$n++){
                            if(($arr_expense[$ind_expense]['month'] == $arr_date[$n]['month']) and ($arr_expense[$ind_expense]['year'] == $arr_date[$n]['year'])){
                                $arr_expense[$ind_expense]['balance'.$n] +=  $balance;
                            }
                        }
                    }
                }
                /*================================== end group detail expense ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Revenue'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Debet'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail other revenue ======================================*/
                if(count($arr_other_revenue) == 0){
                    array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                    $ind_other_revenue += 1;
                    $arr_other_revenue[$ind_other_revenue] = array_merge($arr_other_revenue[$ind_other_revenue],$arr_balance);
                    for($n=0;$n<count($arr_date);$n++){
                        if(($arr_other_revenue[$ind_other_revenue]['month'] == $arr_date[$n]['month']) and ($arr_other_revenue[$ind_other_revenue]['year'] == $arr_date[$n]['year'])){
                            $arr_other_revenue[$ind_other_revenue]['balance'.$n] +=  $balance;
                        }
                    }
                }else{
                    if($arr_other_revenue[$ind_other_revenue]['Account_Number'] == $Account_Number){
                        for($n=0;$n<count($arr_date);$n++){
                            if(($month == $arr_date[$n]['month']) and ($year == $arr_date[$n]['year'])){
                                $arr_other_revenue[$ind_other_revenue]['balance'.$n] +=  $balance;
                            }
                        }
                    }else{
                        array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                        $ind_other_revenue += 1;
                        $arr_other_revenue[$ind_other_revenue] = array_merge($arr_other_revenue[$ind_other_revenue],$arr_balance);
                        for($n=0;$n<count($arr_date);$n++){
                            if(($arr_other_revenue[$ind_other_revenue]['month'] == $arr_date[$n]['month']) and ($arr_other_revenue[$ind_other_revenue]['year'] == $arr_date[$n]['year'])){
                                $arr_other_revenue[$ind_other_revenue]['balance'.$n] +=  $balance;
                            }
                        }
                    }
                }
                /*================================== end group detail other revenue ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail other expense ======================================*/
                 if(count($arr_other_expense) == 0){
                    array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                    $ind_other_expense += 1;
                    $arr_other_expense[$ind_other_expense] = array_merge($arr_other_expense[$ind_other_expense],$arr_balance);
                    for($n=0;$n<count($arr_date);$n++){
                        if(($arr_other_expense[$ind_other_expense]['month'] == $arr_date[$n]['month']) and ($arr_other_expense[$ind_other_expense]['year'] == $arr_date[$n]['year'])){
                            $arr_other_expense[$ind_other_expense]['balance'.$n] +=  $balance;
                        }
                    }
                }else{
                    if($arr_other_expense[$ind_other_expense]['Account_Number'] == $Account_Number){
                        for($n=0;$n<count($arr_date);$n++){
                            if(($month == $arr_date[$n]['month']) and ($year == $arr_date[$n]['year'])){
                                $arr_other_expense[$ind_other_expense]['balance'.$n] +=  $balance;
                            }
                        }
                    }else{
                        array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'month'=>$month,'year'=>$year,'Acc_Type_Name'=>$Acc_Type_Name,'Sub_Type_Name'=>$Sub_Type_Name));
                        $ind_other_expense += 1;
                        $arr_other_expense[$ind_other_expense] = array_merge($arr_other_expense[$ind_other_expense],$arr_balance);
                        for($n=0;$n<count($arr_date);$n++){
                            if(($arr_other_expense[$ind_other_expense]['month'] == $arr_date[$n]['month']) and ($arr_other_expense[$ind_other_expense]['year'] == $arr_date[$n]['year'])){
                                $arr_other_expense[$ind_other_expense]['balance'.$n] +=  $balance;
                            }
                        }
                    }
                }
                /*================================== end group detail other expense ======================================*/  
            }  
        }
        /*=================================================== end get detail transaction =======================================================*/

        for($x=0;$x<count($arr_date);$x++){
            foreach($all_transaction as $row){

                $Account_Number         = $row->Account_Number;
                $Sub_Type_Name          = $row->Sub_Type_Name;
                $Acc_Type_Name          = $row->Acc_Type_Name;
                $Account_Name           = $row->Account_Name;
                $balance                = $row->balance;
                $type_trans             = $row->type_trans;
                $month                  = $row->month;
                $year                   = $row->year;

                if($Acc_Type_Name == 'Revenue'){
                    if(($month == $arr_date[$x]['month']) and ($year == $arr_date[$x]['year'])){

                        $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                        if($debet_credit['plus'] == 'Debet'){
                            $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                        }else{
                            $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                        }

                        if($Sub_Type_Name =='Retur Penjualan'){
                            $revenue_group[$x]      -= $balance;
                        }else{
                            $revenue_group[$x]      += $balance;
                        }
                        
                    }
                }elseif($Acc_Type_Name == 'COGS / COGM (Cost Of Goods Made)'){
                    if(($month == $arr_date[$x]['month']) and ($year == $arr_date[$x]['year'])){

                        $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                        if($debet_credit['plus'] == 'Debet'){
                            $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                        }else{
                            $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                        }

                        $cogm_group[$x]         += $balance;
                    } 
                }elseif($Acc_Type_Name == 'Expense'){
                    if(($month == $arr_date[$x]['month']) and ($year == $arr_date[$x]['year'])){

                        $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                        if($debet_credit['plus'] == 'Debet'){
                            $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                        }else{
                            $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                        }

                        $expense_group[$x]         += $balance;
                    }  
                }elseif($Acc_Type_Name == 'Other Revenue'){
                    if(($month == $arr_date[$x]['month']) and ($year == $arr_date[$x]['year'])){

                        $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                        if($debet_credit['plus'] == 'Debet'){
                            $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Debet'))  ? $balance : $balance * $min;
                        }else{
                            $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                        }

                        $other_revenue_group[$x]      += $balance;
                    }
                }elseif($Acc_Type_Name == 'Other Expense'){
                    if(($month == $arr_date[$x]['month']) and ($year == $arr_date[$x]['year'])){

                        $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                        if($debet_credit['plus'] == 'Debet'){
                            $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                        }else{
                            $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                        }

                        $other_expense_group[$x]         += $balance;
                    }
                }  
            }

            $arr_PL[$x]                     = $revenue_group[$x] - $cogm_group[$x] - $expense_group[$x] + $other_revenue_group[$x] - $other_expense_group[$x];
        }

        $data['arr_PL']                     = $arr_PL;
        $data['revenue_group']              = $revenue_group;
        $data['cogm_group']                 = $cogm_group;
        $data['expense_group']              = $expense_group;
        $data['other_revenue_group']        = $other_revenue_group;
        $data['other_expense_group']        = $other_expense_group;

        $data['arr_revenue']                = $arr_revenue;
        $data['arr_cogm']                   = $arr_cogm;
        $data['arr_expense']                = $arr_expense;
        $data['arr_other_revenue']          = $arr_other_revenue;
        $data['arr_other_expense']          = $arr_other_expense;
        $data['arr_date']                   = $arr_date;

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_profit_lost_multi_period', $data);  
        $this->load->view('v_footer'); 
    }

    Public function preview_profit_loss_compare_period($begin_date1,$end_date1,$begin_date2,$end_date2,$branch){
        //$branch                 = $this->session->userdata('branch');
        $branch                 = str_replace('_', '-', $branch);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id_standard);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id_standard);  

        $begin_date1            = str_replace('_', '-',$begin_date1);
        $end_date1              = str_replace('_', '-',$end_date1);
        $begin_date2            = str_replace('_', '-',$begin_date2);
        $end_date2              = str_replace('_', '-',$end_date2);

        $begin_date1            = date("Y-m-d", strtotime($begin_date1));
        $end_date1              = date("Y-m-d", strtotime($end_date1));
        $data['BeginDate1']     = $begin_date1;
        $data['EndDate1']       = $end_date1;

        $begin_date2            = date("Y-m-d", strtotime($begin_date2));
        $end_date2              = date("Y-m-d", strtotime($end_date2));
        $data['BeginDate2']     = $begin_date2;
        $data['EndDate2']       = $end_date2;
        
        $get_transaction1       = $this->get_all_transaction_by_date($branch,$begin_date1,$end_date1,1);
        $get_transaction2       = $this->get_all_transaction_by_date($branch,$begin_date2,$end_date2,2);

        for($i=0;$i<count($get_transaction2['arr_revenue']);$i++){
             array_push($get_transaction1['arr_revenue'],$get_transaction2['arr_revenue'][$i]);
        }

        $merge_arr_revenue              = $get_transaction1['arr_revenue'];
        $arr_revenue                    = array();
        for($x=0;$x<count($merge_arr_revenue);$x++){
            if(count($arr_revenue) == 0){
                array_push($arr_revenue,array(
                                        'Account_Number'=>$merge_arr_revenue[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_revenue[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_revenue[$x]['ordinal'] == 1 ? $merge_arr_revenue[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_revenue[$x]['ordinal'] == 2 ? $merge_arr_revenue[$x]['balance'] : 0));
            }else{

                $key_merge = $this->search_key($merge_arr_revenue[$x]['Account_Number'],'Account_Number',$arr_revenue);
                if($key_merge !== false){
                    if($merge_arr_revenue[$x]['ordinal'] == 1){
                        $arr_revenue[$key_merge]['balance1'] = $merge_arr_revenue[$x]['balance'];
                    }else{
                        $arr_revenue[$key_merge]['balance2'] = $merge_arr_revenue[$x]['balance'];
                    }
                }else{

                    array_push($arr_revenue,array(
                                        'Account_Number'=>$merge_arr_revenue[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_revenue[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_revenue[$x]['ordinal'] == 1 ? $merge_arr_revenue[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_revenue[$x]['ordinal'] == 2 ? $merge_arr_revenue[$x]['balance'] : 0));
                }

            }
        }

        for($i=0;$i<count($get_transaction2['arr_cogm']);$i++){
             array_push($get_transaction1['arr_cogm'],$get_transaction2['arr_cogm'][$i]);
        }

        $merge_arr_cogm              = $get_transaction1['arr_cogm'];
        $arr_cogm                    = array();
        for($x=0;$x<count($merge_arr_cogm);$x++){
            if(count($arr_cogm) == 0){
                array_push($arr_cogm,array(
                                        'Account_Number'=>$merge_arr_cogm[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_cogm[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_cogm[$x]['ordinal'] == 1 ? $merge_arr_cogm[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_cogm[$x]['ordinal'] == 2 ? $merge_arr_cogm[$x]['balance'] : 0));
            }else{

                $key_merge = $this->search_key($merge_arr_cogm[$x]['Account_Number'],'Account_Number',$arr_cogm);
                if($key_merge !== false){
                    if($merge_arr_cogm[$x]['ordinal'] == 1){
                        $arr_cogm[$key_merge]['balance1'] = $merge_arr_cogm[$x]['balance'];
                    }else{
                        $arr_cogm[$key_merge]['balance2'] = $merge_arr_cogm[$x]['balance'];
                    }
                }else{

                    array_push($arr_cogm,array(
                                        'Account_Number'=>$merge_arr_cogm[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_cogm[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_cogm[$x]['ordinal'] == 1 ? $merge_arr_cogm[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_cogm[$x]['ordinal'] == 2 ? $merge_arr_cogm[$x]['balance'] : 0));
                }

            }
        }

        for($i=0;$i<count($get_transaction2['arr_expense']);$i++){
             array_push($get_transaction1['arr_expense'],$get_transaction2['arr_expense'][$i]);
        }

        $merge_arr_expense              = $get_transaction1['arr_expense'];
        $arr_expense                    = array();
        for($x=0;$x<count($merge_arr_expense);$x++){
            if(count($arr_expense) == 0){
                array_push($arr_expense,array(
                                        'Account_Number'=>$merge_arr_expense[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_expense[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_expense[$x]['ordinal'] == 1 ? $merge_arr_expense[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_expense[$x]['ordinal'] == 2 ? $merge_arr_expense[$x]['balance'] : 0));
            }else{

                $key_merge = $this->search_key($merge_arr_expense[$x]['Account_Number'],'Account_Number',$arr_expense);
                if($key_merge !== false){
                    if($merge_arr_expense[$x]['ordinal'] == 1){
                        $arr_expense[$key_merge]['balance1'] = $merge_arr_expense[$x]['balance'];
                    }else{
                        $arr_expense[$key_merge]['balance2'] = $merge_arr_expense[$x]['balance'];
                    }
                }else{

                    array_push($arr_expense,array(
                                        'Account_Number'=>$merge_arr_expense[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_expense[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_expense[$x]['ordinal'] == 1 ? $merge_arr_expense[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_expense[$x]['ordinal'] == 2 ? $merge_arr_expense[$x]['balance'] : 0));
                }

            }
        }

        for($i=0;$i<count($get_transaction2['arr_other_revenue']);$i++){
             array_push($get_transaction1['arr_other_revenue'],$get_transaction2['arr_other_revenue'][$i]);
        }

        $merge_arr_other_revenue        = $get_transaction1['arr_other_revenue'];
        $arr_other_revenue              = array();
        for($x=0;$x<count($merge_arr_other_revenue);$x++){
            if(count($arr_other_revenue) == 0){
                array_push($arr_other_revenue,array(
                                        'Account_Number'=>$merge_arr_other_revenue[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_other_revenue[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_other_revenue[$x]['ordinal'] == 1 ? $merge_arr_other_revenue[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_other_revenue[$x]['ordinal'] == 2 ? $merge_arr_other_revenue[$x]['balance'] : 0));
            }else{

                $key_merge = $this->search_key($merge_arr_other_revenue[$x]['Account_Number'],'Account_Number',$arr_other_revenue);
                if($key_merge !== false){
                    if($merge_arr_other_revenue[$x]['ordinal'] == 1){
                        $arr_other_revenue[$key_merge]['balance1'] = $merge_arr_other_revenue[$x]['balance'];
                    }else{
                        $arr_other_revenue[$key_merge]['balance2'] = $merge_arr_other_revenue[$x]['balance'];
                    }
                }else{

                    array_push($arr_other_revenue,array(
                                        'Account_Number'=>$merge_arr_other_revenue[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_other_revenue[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_other_revenue[$x]['ordinal'] == 1 ? $merge_arr_other_revenue[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_other_revenue[$x]['ordinal'] == 2 ? $merge_arr_other_revenue[$x]['balance'] : 0));
                }

            }
        }

        for($i=0;$i<count($get_transaction2['arr_other_expense']);$i++){
             array_push($get_transaction1['arr_other_expense'],$get_transaction2['arr_other_expense'][$i]);
        }

        $merge_arr_other_expense        = $get_transaction1['arr_other_expense'];
        $arr_other_expense              = array();
        for($x=0;$x<count($merge_arr_other_expense);$x++){
            if(count($arr_other_expense) == 0){
                array_push($arr_other_expense,array(
                                        'Account_Number'=>$merge_arr_other_expense[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_other_expense[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_other_expense[$x]['ordinal'] == 1 ? $merge_arr_other_expense[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_other_expense[$x]['ordinal'] == 2 ? $merge_arr_other_expense[$x]['balance'] : 0));
            }else{

                $key_merge = $this->search_key($merge_arr_other_expense[$x]['Account_Number'],'Account_Number',$arr_other_expense);
                if($key_merge !== false){
                    if($merge_arr_other_expense[$x]['ordinal'] == 1){
                        $arr_other_expense[$key_merge]['balance1'] = $merge_arr_other_expense[$x]['balance'];
                        //$arr_other_expense[$key_merge]['balance1'] = $merge_arr_other_expense[$x]['balance'];
                    }else{
                        $arr_other_expense[$key_merge]['balance2'] = $merge_arr_other_expense[$x]['balance'];
                        //$arr_other_expense[$key_merge]['balance1'] = 
                    }
                }else{

                    array_push($arr_other_expense,array(
                                        'Account_Number'=>$merge_arr_other_expense[$x]['Account_Number'],
                                        'Account_Name'=>$merge_arr_other_expense[$x]['Account_Name'],
                                        'balance1'=>$merge_arr_other_expense[$x]['ordinal'] == 1 ? $merge_arr_other_expense[$x]['balance'] : 0,
                                        'balance2'=>$merge_arr_other_expense[$x]['ordinal'] == 2 ? $merge_arr_other_expense[$x]['balance'] : 0));
                }

            }
        }

        $data['PL1']                    = $get_transaction1['PL'];
        $data['revenue_group1']         = $get_transaction1['revenue_group'];
        $data['cogm_group1']            = $get_transaction1['cogm_group'];
        $data['expense_group1']         = $get_transaction1['expense_group'];
        $data['other_revenue_group1']   = $get_transaction1['other_revenue_group'];
        $data['other_expense_group1']   = $get_transaction1['other_expense_group'];

        $data['PL2']                    = $get_transaction2['PL'];
        $data['revenue_group2']         = $get_transaction2['revenue_group'];
        $data['cogm_group2']            = $get_transaction2['cogm_group'];
        $data['expense_group2']         = $get_transaction2['expense_group'];
        $data['other_revenue_group2']   = $get_transaction2['other_revenue_group'];
        $data['other_expense_group2']   = $get_transaction2['other_expense_group'];

        $data['arr_revenue']           = $arr_revenue;
        $data['arr_cogm']              = $arr_cogm;
        $data['arr_expense']           = $arr_expense;
        $data['arr_other_expense']     = $arr_other_expense;
        $data['arr_other_revenue']     = $arr_other_revenue;

        //print_r($arr_revenue);

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_profit_lost_compare_period', $data);  
        $this->load->view('v_footer'); 
    }

    function get_all_transaction_by_date($branch,$begin_date,$end_date,$ordinal){

        $plus                   = 1;
        $min                    = -1;
        $all_acc_type           = array('Revenue','COGS / COGM (Cost Of Goods Made)','Expense','Other Revenue','Other Expense');
        $arr_trans              = array();
        $arr_index              = -1;

        $arr_revenue            = array();
        $arr_cogm               = array();
        $arr_expense            = array();
        $arr_other_expense      = array();
        $arr_other_revenue      = array();
        $ind_revenue            = -1;
        $ind_cogm               = -1;
        $ind_expense            = -1;
        $ind_other_expense      = -1;
        $ind_other_revenue      = -1;

        $revenue_group          = 0;
        $cogm_group             = 0;
        $expense_group          = 0;
        $other_revenue_group    = 0;
        $other_expense_group    = 0;

        $all_transaction        = $this->m_index_financial_statement->get_all_transaction_by_date($branch,$begin_date,$end_date,$all_acc_type);
        //echo $this->db->last_query();
        foreach($all_transaction as $row){
            $Account_Number         = $row->Account_Number;
            $Sub_Type_Name          = $row->Sub_Type_Name;
            $Acc_Type_Name          = $row->Acc_Type_Name;
            $Account_Name           = $row->Account_Name;
            $balance                = $row->balance;
            $type_trans             = $row->type_trans;

            /*============================================= get detail transaction ===============================================*/
            if($Acc_Type_Name == 'Revenue'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail revenue ======================================*/
                if(count($arr_revenue) == 0){
                    array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                    $ind_revenue += 1;
                }else{
                    if($arr_revenue[$ind_revenue]['Account_Number'] == $Account_Number){
                        $arr_revenue[$ind_revenue]['balance'] += $balance;
                    }else{
                        array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                        $ind_revenue += 1;
                    }
                }
                /*================================== end group detail revenue ======================================*/

                if($Sub_Type_Name =='Retur Penjualan'){
                    $revenue_group          -= $balance;
                }else{
                    $revenue_group          += $balance;
                }

            }elseif($Acc_Type_Name == 'COGS / COGM (Cost Of Goods Made)'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                 $cogm_group         += $balance;

                /*================================== group detail cogm ======================================*/
                if(count($arr_cogm) == 0){
                    array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                    $ind_cogm += 1;
                }else{
                    if($arr_cogm[$ind_cogm]['Account_Number'] == $Account_Number){
                        $arr_cogm[$ind_cogm]['balance'] += $balance;
                    }else{
                        array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                        $ind_cogm += 1;
                    }
                }
                /*================================== end group detail cogm ======================================*/

            }elseif($Acc_Type_Name == 'Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $expense_group         += $balance;

                /*================================== group detail expense ======================================*/
                if(count($arr_expense) == 0){
                    array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                    $ind_expense += 1;
                }else{
                    if($arr_expense[$ind_expense]['Account_Number'] == $Account_Number){
                        $arr_expense[$ind_expense]['balance'] += $balance;
                    }else{
                        array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                        $ind_expense += 1;
                    }
                }
                /*================================== end group detail expense ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Revenue'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Debet'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_revenue_group      += $balance;

                /*================================== group detail other revenue ======================================*/
                if(count($arr_other_revenue) == 0){
                    array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                    $ind_other_revenue += 1;
                }else{
                    if($arr_other_revenue[$ind_other_revenue]['Account_Number'] == $Account_Number){
                        $arr_other_revenue[$ind_other_revenue]['balance'] += $balance;
                    }else{
                        array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                        $ind_other_revenue += 1;
                    }
                }
                /*================================== end group detail other revenue ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_expense_group    += $balance;

                /*================================== group detail other expense ======================================*/
                if(count($arr_other_expense) == 0){
                    array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                    $ind_other_expense += 1;
                }else{
                    if($arr_other_expense[$ind_other_expense]['Account_Number'] == $Account_Number){
                        $arr_other_expense[$ind_other_expense]['balance'] += $balance;
                    }else{
                        array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance,'ordinal'=>$ordinal));
                        $ind_other_expense += 1;
                    }
                }
                /*================================== end group detail other expense ======================================*/  
            }

        }
        /*=================================================== end get detail transaction =======================================================*/

        $data['PL']                 = $revenue_group - $cogm_group - $expense_group + $other_revenue_group - $other_expense_group;
        $data['arr_revenue']        = $arr_revenue;
        $data['arr_cogm']           = $arr_cogm;
        $data['arr_expense']        = $arr_expense;
        $data['arr_other_expense']  = $arr_other_expense;
        $data['arr_other_revenue']  = $arr_other_revenue;
        $data['revenue_group']      = $revenue_group;
        $data['cogm_group']         = $cogm_group;
        $data['expense_group']       = $expense_group;
        $data['other_revenue_group'] = $other_revenue_group;
        $data['other_expense_group'] = $other_expense_group;

        return $data;

    }

    public function preview_profit_loss_consolidation_period($begin_date,$end_date){
        $branch                 = $this->session->userdata('branch');
        $company_code           = $this->session->userdata('company');
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id_standard);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id_standard);    

        $begin_date             = str_replace('_', '-', $begin_date);
        $end_date               = str_replace('_', '-', $end_date);

        $begin_date             = date("Y-m-d", strtotime($begin_date));
        $end_date               = date("Y-m-d", strtotime($end_date));

        $data['BeginDate']      = $begin_date;
        $data['EndDate']        = $end_date;

        $plus                   = 1;
        $min                    = -1;
        $all_acc_type           = array('Revenue','COGS / COGM (Cost Of Goods Made)','Expense','Other Revenue','Other Expense');
        $arr_trans              = array();
        $arr_index              = -1;
        $arr_revenue            = array();
        $arr_cogm               = array();
        $arr_expense            = array();
        $arr_other_expense      = array();
        $arr_other_revenue      = array();
        $ind_revenue            = -1;
        $ind_cogm               = -1;
        $ind_expense            = -1;
        $ind_other_expense      = -1;
        $ind_other_revenue      = -1;

        $revenue_group          = 0;
        $cogm_group             = 0;
        $expense_group          = 0;
        $other_revenue_group    = 0;
        $other_expense_group    = 0;

        $all_transaction        = $this->m_index_financial_statement->get_all_transaction_by_company_date($company_code,$begin_date,$end_date,$all_acc_type);
        //echo $this->db->last_query();

        foreach($all_transaction as $row){
            $Account_Number         = $row->Account_Number;
            $Sub_Type_Name          = $row->Sub_Type_Name;
            $Acc_Type_Name          = $row->Acc_Type_Name;
            $Account_Name           = $row->Account_Name;
            $balance                = $row->balance;
            $type_trans             = $row->type_trans;

            /*============================================= get detail transaction ===============================================*/
            if($Acc_Type_Name == 'Revenue'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                /*================================== group detail revenue ======================================*/
                if(count($arr_revenue) == 0){
                    array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                    $ind_revenue += 1;
                }else{
                    if($arr_revenue[$ind_revenue]['Account_Number'] == $Account_Number){
                        $arr_revenue[$ind_revenue]['balance'] += $balance;
                    }else{
                        array_push($arr_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                        $ind_revenue += 1;
                    }
                }
                /*================================== end group detail revenue ======================================*/

                if($Sub_Type_Name =='Retur Penjualan'){
                    $revenue_group          -= $balance;
                }else{
                    $revenue_group          += $balance;
                }
                
            }elseif($Acc_Type_Name == 'COGS / COGM (Cost Of Goods Made)'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $cogm_group             += $balance;

                /*================================== group detail cogm ======================================*/
                if(count($arr_cogm) == 0){
                    array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                    $ind_cogm += 1;
                }else{
                    if($arr_cogm[$ind_cogm]['Account_Number'] == $Account_Number){
                        $arr_cogm[$ind_cogm]['balance'] += $balance;
                    }else{
                        array_push($arr_cogm, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                        $ind_cogm += 1;
                    }
                }
                /*================================== end group detail cogm ======================================*/

            }elseif($Acc_Type_Name == 'Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $expense_group    += $balance;

                /*================================== group detail expense ======================================*/
                if(count($arr_expense) == 0){
                    array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                    $ind_expense += 1;
                }else{
                    if($arr_expense[$ind_expense]['Account_Number'] == $Account_Number){
                        $arr_expense[$ind_expense]['balance'] += $balance;
                    }else{
                        array_push($arr_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                        $ind_expense += 1;
                    }
                }
                /*================================== end group detail expense ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Revenue'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Debet'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_revenue_group      += $balance;

                /*================================== group detail other revenue ======================================*/
                if(count($arr_other_revenue) == 0){
                    array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                    $ind_other_revenue += 1;
                }else{
                    if($arr_other_revenue[$ind_other_revenue]['Account_Number'] == $Account_Number){
                        $arr_other_revenue[$ind_other_revenue]['balance'] += $balance;
                    }else{
                        array_push($arr_other_revenue, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                        $ind_other_revenue += 1;
                    }
                }
                /*================================== end group detail other revenue ======================================*/
                
            }elseif($Acc_Type_Name == 'Other Expense'){

                $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);
                if($debet_credit['plus'] == 'Debet'){
                    $balance            = (($type_trans == 'Debet') and ($debet_credit['position'] == 'Credit'))  ? $balance : $balance * $min;
                }else{
                    $balance            = (($type_trans == 'Credit') and ($debet_credit['position'] == 'Credit')) ? $balance : $balance * $min;
                }

                $other_expense_group    += $balance;

                /*================================== group detail other expense ======================================*/
                if(count($arr_other_expense) == 0){
                    array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                    $ind_other_expense += 1;
                }else{
                    if($arr_other_expense[$ind_other_expense]['Account_Number'] == $Account_Number){
                        $arr_other_expense[$ind_other_expense]['balance'] += $balance;
                    }else{
                        array_push($arr_other_expense, array('Account_Number'=>$Account_Number,'Account_Name'=>$Account_Name,'balance'=>$balance));
                        $ind_other_expense += 1;
                    }
                }
                /*================================== end group detail other expense ======================================*/
               
            }

        }
        /*=================================================== end get detail transaction =======================================================*/

        $data['PL']                 = $revenue_group - $cogm_group - $expense_group + $other_revenue_group - $other_expense_group;
        $data['arr_revenue']        = $arr_revenue;
        $data['arr_cogm']           = $arr_cogm;
        $data['arr_expense']        = $arr_expense;
        $data['arr_other_expense']  = $arr_other_expense;
        $data['arr_other_revenue']  = $arr_other_revenue;
        $data['revenue_group']      = $revenue_group;
        $data['cogm_group']         = $cogm_group;
        $data['expense_group']      = $expense_group;
        $data['other_revenue_group'] = $other_revenue_group;
        $data['other_expense_group'] = $other_expense_group;

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_profit_lost_consolidation_period', $data);  
        $this->load->view('v_footer'); 
    }

    function search_key($value,$search,$array) {
        foreach($array as $key => $val) {
           if ($val[$search] === $value) {
                return $key;
           }
        }
        return FALSE;
    }

    function multi_search_key($search1,$search_str1,$search2,$search_str2,$search3,$search_str3,$array) {
       foreach ($array as $key => $val) {
           if (($val[$search_str1] === $search1) and ($val[$search_str2] === $search2) and ($val[$search_str3] === $search3)) {
               return $key;
           }
       }
        return FALSE; 
       
    }

    function get_interval_month1($from_date,$end_date){

        $temp_d1 = new DateTime(date('Y-m-d', strtotime($from_date))); // 2012-01-01
        $temp_d2 = new DateTime(date('Y-m-d', strtotime($end_date))); // 2013-02-01
        $interval = $temp_d2->diff($temp_d1); 
        $monthsAhead = $interval->m + ($interval->y * 12) + 2;

        return $monthsAhead;
    }

    function get_interval_month($month_begin,$year_begin,$month_end,$year_end){

        if($year_begin > $year_end){
            $from_year  = $year_end;
            $last_year  = $year_begin;
        }else{
            $from_year  = $year_begin;
            $last_year  = $year_end;
        }

        $interval = ((($last_year - $from_year) * 12) + $month_end + 1) - $month_begin;
        return $interval;
    }

    function open_journal_transaction($Account_Number,$begin_date,$end_date,$is_consolidation=FALSE){
        $Account_Number         = str_replace('_', '-', $Account_Number);
        $branch                 = $this->session->userdata('branch');
        $data['Account_Number'] = $Account_Number;
        $data['begin_date']     = str_replace('_', '-', $begin_date);
        $data['end_date']       = str_replace('_', '-', $end_date);
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($Account_Number);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['all_journal']    = $this->m_index_financial_statement->get_transaction_by_account($branch,$Account_Number,$begin_date,$end_date,$is_consolidation);
        //echo $this->db->last_query();

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_journal_profit_lost_standard', $data);  
        $this->load->view('v_footer'); 
    }

    function open_journal_transaction_multi($Account_Number){
        $Account_Number         = str_replace('_', '-', $Account_Number);
        $branch                 = $this->session->userdata('branch');
        $data['Account_Number'] = $Account_Number;
        // $data['begin_date']     = str_replace('_', '-', $begin_date);
        // $data['end_date']       = str_replace('_', '-', $end_date);
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($Account_Number);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['all_journal']    = $this->m_index_financial_statement->get_transaction_by_account($branch,$Account_Number);
        //echo $this->db->last_query();

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_journal_profit_lost_multi_period', $data);  
        $this->load->view('v_footer'); 
    }

    function open_journal_transaction_consolidation($Account_Number){
        $Account_Number         = str_replace('_', '-', $Account_Number);
        $branch                 = $this->session->userdata('branch');
        $company                = $this->session->userdata('company');
        $data['Account_Number'] = $Account_Number;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($Account_Number);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);
        $data['all_journal']    = $this->m_index_financial_statement->get_transaction_by_account_company($company,$Account_Number);
        //echo $this->db->last_query();

        $this->load->view('v_header');
        $this->load->view('financial_statement_report/v_print_journal_profit_lost_consolidation', $data);  
        $this->load->view('v_footer'); 
    }
    
    public function print_profit_loss_report(){
        $company_code           = $this->session->userdata('company');
        $data['company']        = $this->m_new_company->get_detail($company_code);        

        $uri_data               = str_replace('report/index_financial_statement/print_profit_loss_report/', "", $this->uri->uri_string());
        $date_period            = explode('/', $uri_data);
        
        $begin_date             = $date_period[0];
        $end_date               = $date_period[1];
        $data['BeginDate']      = $begin_date;
        $data['EndDate']        = $end_date;
        
        $data['Revenue']        = $this->m_index_to_report->get_pl_data($begin_date, $end_date, 65);//65
        $data['COGM']           = $this->m_index_to_report->get_pl_data($begin_date, $end_date, 68);//68
        $data['Expense']        = $this->m_index_to_report->get_pl_data($begin_date, $end_date, 71);//71
        $data['OtherRevenue']   = $this->m_index_to_report->get_pl_data($begin_date, $end_date, 75);//75
        $data['OtherExpense']   = $this->m_index_to_report->get_pl_data($begin_date, $end_date, 76);//76
        
        $this->load->view('v_header');
        $this->load->view('report/view_to_financial/v_print_profit_loss_statement', $data);
        $this->load->view('v_footer');	
    }
    
    public function journal_profit_loss_report(){
        $uri_data               = str_replace('report/index_financial_statement/journal_profit_loss_report/', "", $this->uri->uri_string());
        $data_uri               = explode('/', $uri_data);
        
        $trans_number           = str_replace('~', '/', $data_uri[0]);
        $branch_code            = explode('/', $trans_number)[0];
        
        $account_number         = $data_uri[1];
        $trans_type             = $this->get_trans_name($data_uri[2]);
        $begin_date             = $data_uri[3];
        $end_date               = $data_uri[4];
        
        $data['detail']         = $this->m_index_to_report->get_journal_index_report($branch_code, $account_number, $begin_date, $end_date); 
        $data['trans_name']     = $trans_type;
        $data['begin_date']     = $begin_date;
        $data['end_date']       = $end_date;
       
        $this->load->view('v_header');
        $this->load->view('report/view_to_financial/v_journal_profit_loss_statement', $data);
        $this->load->view('v_footer');	
    }
    
    public function preview_balance_sheet_report(){
        $branch_code            = $this->session->userdata('branch');
        
        $period_date            = str_replace('report/index_financial_statement/preview_balance_sheet_report/', "", $this->uri->uri_string());
        $date_period            = explode('/', $period_date);
        
        $period_month           = $date_period[0];
        $period_year            = $date_period[1];
        
//        $saldo_balance          = $this->m_index_to_report->get_coa_saldo_balance($period_month, $period_year);

//        foreach($saldo_balance as $row){
//            $data[$gl_saldo_balance[$row->AccountParent]] = $row->Saldo_Balance;
//        }
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);
        $data['month']          = date("F", strtotime('2015-' . $period_month . '-01'));
        $data['year']           = $period_year;
        
        $data['detail']         = $this->m_index_to_report->get_coa_balance_sheet($period_month, $period_year, 0);
        $data['detail_saldo']   = $this->m_index_to_report->get_coa_balance_sheet($period_month, $period_year, 1); 
//        echo $this->db->last_query();
        
        $this->load->view('v_header');
        $this->load->view('report/view_to_financial/v_preview_balance_sheet_statement', $data);
        $this->load->view('v_footer');	
    }

    public function preview_balance_sheet_standard_report($month,$year,$branch_code){
        //$branch_code            = $this->session->userdata('branch');
        $branch_code            = str_replace('_', '-', $branch_code);
        $period_month           = $month;
        $period_year            = $year;
        $period_end             = $year.abs($month);
        $data['bulan']          = $month;
        $data['tahun']          = $year;
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);
        $data['month']          = date("F", strtotime($year.'-' . $period_month . '-01'));
        $data['year']           = $period_year;
        
        $is_endperiod           = $this->m_index_financial_statement->check_endperiod($period_end);
        $is_lastendperiod       = $this->m_index_financial_statement->check_lastendperiod($period_end);

        /*=========================================== get coa =========================================================*/
        $list_account           = $this->m_index_financial_statement->get_coa_balance_sheet(abs($period_month), $period_year,$branch_code,$period_end,$is_endperiod,$is_lastendperiod);
        //echo $this->db->last_query();

        $total_asset            = 0;
        $total_liability        = 0;
        $arr_account            = array();
        $key_type               = 0;
        $key_sub_type           = 0;
        $key_account            = 0;

         for($x=0;$x<count($list_account);$x++){

            $AccountNumber      = $list_account[$x]['AccountNumber'];
            $AccountName        = $list_account[$x]['AccountName'];
            $AccountLevel       = $list_account[$x]['AccountLevel'];
            $AccountParent      = $list_account[$x]['AccountParent'];
            $Sub_Type_Name      = $list_account[$x]['Sub_Type_Name'];
            $Acc_Type_Name      = $list_account[$x]['Acc_Type_Name'];
            $GLSaldoBalance     = $list_account[$x]['GLSaldoBalance'];
            $AccountChild       = $list_account[$x]['AccountChild'];

           
            $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);

            if($debet_credit['position'] == 'Debet'){
                $total_asset        += $debet_credit['plus'] == 'Debet' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }else{
                $total_liability    += $debet_credit['plus'] == 'Credit' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }

             
            if(count($arr_account) == 0){

                $arr_account[$key_type]  = array(
                    'Acc_Type_Name'     =>  $Acc_Type_Name,
                    'total_acc_type'    =>  $GLSaldoBalance,
                    $key_sub_type       =>  array(
                                            'Sub_Type_Name'  =>  $Sub_Type_Name,
                                            'total_sub_type' =>  $GLSaldoBalance,
                                             $key_account    => array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance)));
            }else{

                $key_type = $this->search_key($Acc_Type_Name,'Acc_Type_Name',$arr_account);
                if($key_type !== false){

                    //print_r($arr_account[$key_type]);//[0]);
                    foreach ($arr_account[$key_type] as $key_array => $value_array) {
                       if(is_array($value_array)){
                            if($value_array['Sub_Type_Name'] == $Sub_Type_Name){
                                $key_sub_type = $key_array;
                            }else{
                                $key_sub_type = false;
                            }
                        }
                    }

                    if($key_sub_type !== false){
                        foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                           if(is_array($value_account_number)){
                                if($value_account_number['AccountNumber'] == $AccountNumber){
                                    $key_account = $key_account_number;
                                }else{
                                    $key_account = false;
                                }
                            }
                        }

                        if($key_account !== false){
                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account]['balance'] += $GLSaldoBalance;
                        }else{

                            $key_account = 0;
                            foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                               if(is_array($value_account_number)){
                                    $key_account++;
                                }
                            }

                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance);
                        }
                    }else{
                        $key_account    = 0;
                        $key_sub_type   = 0;
                        foreach($arr_account[$key_type] as $key_array => $value_array) {
                           if(is_array($value_array)){
                                $key_sub_type++;
                            }
                        }
                        
                        $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type]['Sub_Type_Name'] = $Sub_Type_Name;
                        $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                        'AccountNumber' => $AccountNumber,
                                                        'AccountName'   => $AccountName,
                                                        'AccountLevel'  => $AccountLevel,
                                                        'AccountChild'  => $AccountChild,
                                                        'AccountParent' => $AccountParent,
                                                        'balance'       => $GLSaldoBalance);
                    }
                }else{

                    $key_type = count($arr_account);
                    $key_sub_type   = 0;
                    $key_account    = 0;
                    $arr_account[$key_type]['Acc_Type_Name']  = $Acc_Type_Name;
                    $arr_account[$key_type]['total_acc_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type]['Sub_Type_Name']  = $Sub_Type_Name;
                    $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                    'AccountNumber' => $AccountNumber,
                                                    'AccountName'   => $AccountName,
                                                    'AccountLevel'  => $AccountLevel,
                                                    'AccountChild'  => $AccountChild,
                                                    'AccountParent' => $AccountParent,
                                                    'balance'       => $GLSaldoBalance);
                }
            }
        }

        //print_r($arr_account);

        $data['total_asset']        = $total_asset;
        $data['total_liability']    = $total_liability;
        $data['group']              = $arr_account;
       
        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_standard', $data);
        $this->load->view('v_footer');   
    }

    function open_journal_balance_sheet_standard_report($AccountNumber,$month,$year){

        $branch                 = $this->session->userdata('branch');

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        $from_date                  = date('Y-m-d',strtotime($year.'-'.$month.'-01'));
        $day_month                  = date('t',strtotime($year.'-'.$month));
        $end_date                   = date('Y-m-d',strtotime($year.'-'.$month.'-'.$day_month));

        $data['month']              = $month;
        $data['year']               = $year;

        if($month == 1){
            $month = 12;
            $year  = abs($year) - 1;
        }else{
            $month = abs($month - 1);
        }

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);
        $periodend                          = $year.abs($month);

        $is_endperiod                       = $this->m_index_financial_statement->check_endperiod($periodend);
        $is_lastendperiod                   = $this->m_index_financial_statement->check_lastendperiod($periodend);

        /*===================================== get saldo opening balance or glsaldobalancesheet ===================*/
        $data['saldo_opening_balance']      = $this->m_index_financial_statement->get_saldo_opening_balance($branch,$AccountNumber,$month,$year,$is_endperiod,$is_lastendperiod);

        /*======================================= get list journal by account and date ========================================*/
        $data['all_journal']                = $this->m_index_financial_statement->get_journal_balancesheet($branch,$AccountNumber,$from_date,$end_date,$month,$year,$is_endperiod,$is_lastendperiod);

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_report', $data);
        $this->load->view('v_footer');  

    }

    function open_journal_balance_sheet_compare_report($AccountNumber,$from_date,$end_date){
        $branch         = $this->session->userdata('branch');

        $from_date      = str_replace('_', '-', $from_date);
        $end_date       = str_replace('_', '-', $end_date);

        $begin_date_0   = $from_date;
        $day_0          = date('t',strtotime($begin_date_0));
        $month_0        = date('m',strtotime($begin_date_0));
        $year_0         = date('Y',strtotime($begin_date_0));
        $last_date_0    = date('Y-m-d',strtotime($year_0.'-'.$month_0.'-'.$day_0));

        $day_1          = date('t',strtotime($end_date));
        $month_1        = date('m',strtotime($end_date));
        $year_1         = date('Y',strtotime($end_date));
        $begin_date_1   = date('Y-m-d',strtotime($year_1.'-'.$month_1.'-01'));
        $last_date_1    = date('Y-m-d',strtotime($year_1.'-'.$month_1.'-'.$day_1));

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);

        $data['month'][0]           =  $month_0;
        $data['month_period'][0]    =  date("F", strtotime($year_0.'-' . $month_0 . '-01'));;
        $data['year'][0]            = $year_0;

        $data['month'][1]           =  $month_1;
        $data['month_period'][1]    =  date("F", strtotime($year_1.'-' . $month_1 . '-01'));;
        $data['year'][1]            = $year_1;

        if($month_0 == 1){
            $month_0 = 12;
            $year_0  = abs($year_0) - 1;
        }else{
            $month_0 = abs($month_0 - 1);
        }

        if($month_1 == 1){
            $month_1 = 12;
            $year_1  = abs($year_1) - 1;
        }else{
            $month_1 = abs($month_1 - 1);
        }

        $periodend_from              = $year_0.abs($month_0);
        $is_endperiod_from           = $this->m_index_financial_statement->check_endperiod($periodend_from);
        $is_lastendperiod_from       = $this->m_index_financial_statement->check_lastendperiod($periodend_from);
        $data['saldo_opening_balance'][0]      = $this->m_index_financial_statement->get_saldo_opening_balance($branch,$AccountNumber,$month_0,$year_0,$periodend_from,$is_endperiod_from,$is_lastendperiod_from);
        
        $periodend_end              = $year_1.abs($month_1);
        $is_endperiod_end           = $this->m_index_financial_statement->check_endperiod($periodend_end);
        $is_lastendperiod_end       = $this->m_index_financial_statement->check_lastendperiod($periodend_end);
        $data['saldo_opening_balance'][1]      = $this->m_index_financial_statement->get_saldo_opening_balance($branch,$AccountNumber,$month_1,$year_1,$periodend_end,$is_endperiod_end,$is_lastendperiod_end);

        /*======================================= get list journal by account and date ========================================*/
        $data['all_journal']    = $this->m_index_financial_statement->get_journal_balancesheet_compare($branch,$AccountNumber,$begin_date_0,$last_date_0,$begin_date_1,$last_date_1); 
        //echo $this->db->last_query();
        $data['from_date']      = date("d F Y", strtotime($from_date));
        $data['end_date']       = date("d F Y", strtotime($end_date));

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_compare_report', $data);
        $this->load->view('v_footer');  
    }

    function open_journal_balance_sheet_compare_consolidation_report($AccountNumber,$from_date,$end_date){
        $branch         = $this->session->userdata('branch');
        $company        = $this->session->userdata('company');

        $from_date      = str_replace('_', '-', $from_date);
        $end_date       = str_replace('_', '-', $end_date);

        $begin_date_0   = $from_date;
        $day_0          = date('t',strtotime($begin_date_0));
        $month_0        = date('m',strtotime($begin_date_0));
        $year_0         = date('Y',strtotime($begin_date_0));
        $last_date_0    = date('Y-m-d',strtotime($year_0.'-'.$month_0.'-'.$day_0));

        $day_1          = date('t',strtotime($end_date));
        $month_1        = date('m',strtotime($end_date));
        $year_1         = date('Y',strtotime($end_date));
        $begin_date_1   = date('Y-m-d',strtotime($year_1.'-'.$month_1.'-01'));
        $last_date_1    = date('Y-m-d',strtotime($year_1.'-'.$month_1.'-'.$day_1));

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);

        $data['month'][0]           =  $month_0;
        $data['month_period'][0]    =  date("F", strtotime($year_0.'-' . $month_0 . '-01'));;
        $data['year'][0]            = $year_0;

        $data['month'][1]           =  $month_1;
        $data['month_period'][1]    =  date("F", strtotime($year_1.'-' . $month_1 . '-01'));;
        $data['year'][1]            = $year_1;


        if($month_0 == 1){
            $month_0 = 12;
            $year_0  = abs($year_0) - 1;
        }else{
            $month_0 = abs($month_0 - 1);
        }

        if($month_1 == 1){
            $month_1 = 12;
            $year_1  = abs($year_1) - 1;
        }else{
            $month_1 = abs($month_1 - 1);
        }

        $periodend_0              = $year_0.abs($month_0);
        $is_endperiod_0           = $this->m_index_financial_statement->check_endperiod($periodend_0);
        $is_lastendperiod_0       = $this->m_index_financial_statement->check_lastendperiod($periodend_0);

        $periodend_1              = $year_1.abs($month_1);
        $is_endperiod_1           = $this->m_index_financial_statement->check_endperiod($periodend_1);
        $is_lastendperiod_1       = $this->m_index_financial_statement->check_lastendperiod($periodend_1);
        /*===================================== get saldo opening balance or glsaldobalancesheet ===================*/
        $data['saldo_opening_balance'][0]      = $this->m_index_financial_statement->get_saldo_opening_balance_consolidation($company,$AccountNumber,$month_0,$year_0,$periodend_0,$is_endperiod_0,$is_lastendperiod_0);
        $data['saldo_opening_balance'][1]      = $this->m_index_financial_statement->get_saldo_opening_balance_consolidation($company,$AccountNumber,$month_1,$year_1,$periodend_1,$is_endperiod_1,$is_lastendperiod_1);

        /*======================================= get list journal by account and date ========================================*/
        $data['all_journal']    = $this->m_index_financial_statement->get_journal_balancesheet_compare_consolidation($company,$AccountNumber,$begin_date_0,$last_date_0,$begin_date_1,$last_date_1); 
        //echo $this->db->last_query();
        $data['from_date']      = date("d F Y", strtotime($from_date));
        $data['end_date']       = date("d F Y", strtotime($end_date));

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_compare_consolidation_report', $data);
        $this->load->view('v_footer');  
    }

    function open_journal_balance_sheet_multi_report($AccountNumber,$from_date,$end_date){
        $branch                 = $this->session->userdata('branch');

        $from_date  = str_replace('_', '-', $from_date);
        $end_date   = str_replace('_', '-', $end_date);

        $month_from = date('m',strtotime($from_date));
        $year_from  = date('Y',strtotime($from_date));

        $month_end = date('m',strtotime($end_date));
        $year_end  = date('Y',strtotime($end_date));

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);

        $interval_month                     = $this->get_interval_month($month_from,$year_from,$month_end,$year_end);

        for($x=0;$x<$interval_month;$x++){

            if($month_from == 13){
                $month      = 1;
                $year       = $year_from + 1;
            }else{
                $month      = $month_from;
                $year       = $year_from;
            }

            $data['month'][$x]  = $month;
            $data['year'][$x]   = $year;

            if($month == 1){
                $month_period = 12;
                $year_period  = abs($year) - 1;
            }else{
                $month_period = abs($month - 1);
                $year_period  = $year;
            }

            $periodend              = $year_period.abs($month_period);
            $is_endperiod           = $this->m_index_financial_statement->check_endperiod($periodend);
            $is_lastendperiod       = $this->m_index_financial_statement->check_lastendperiod($periodend);

            /*===================================== get saldo opening balance or glsaldobalancesheet ===================*/
            $data['saldo_opening_balance'][$x]      = $this->m_index_financial_statement->get_saldo_opening_balance($branch,$AccountNumber,$month_period,$year_period,$periodend,$is_endperiod,$is_lastendperiod);

            $month_from++;

        }

        /*======================================= get list journal by account and date ========================================*/
        $data['all_journal']    = $this->m_index_financial_statement->get_journal_balancesheet($branch,$AccountNumber,$from_date,$end_date);

        $data['from_date']      = date("d F Y", strtotime($from_date));
        $data['end_date']       = date("d F Y", strtotime($end_date));

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_multi_report', $data);
        $this->load->view('v_footer');  
    }

    function open_journal_balance_sheet_multi_consolidation_report($AccountNumber,$from_date,$end_date){
        $company    = $this->session->userdata('company');
        $branch     = $this->session->userdata('branch');

        $from_date  = str_replace('_', '-', $from_date);
        $end_date   = str_replace('_', '-', $end_date);

        $month_from = date('m',strtotime($from_date));
        $year_from  = date('Y',strtotime($from_date));

        $month_end = date('m',strtotime($end_date));
        $year_end  = date('Y',strtotime($end_date));

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);

        $interval_month                     = $this->get_interval_month($month_from,$year_from,$month_end,$year_end);

        for($x=0;$x<$interval_month;$x++){

            if($month_from == 13){
                $month      = 1;
                $year       = $year_from + 1;
            }else{
                $month      = $month_from;
                $year       = $year_from;
            }

            $data['month'][$x]  = $month;
            $data['year'][$x]   = $year;

            if($month == 1){
                $month_period = 12;
                $year_period  = abs($year) - 1;
            }else{
                $month_period = abs($month - 1);
                $year_period  = $year;
            }

            $periodend              = $year_period.abs($month_period);
            $is_endperiod           = $this->m_index_financial_statement->check_endperiod($periodend);
            $is_lastendperiod       = $this->m_index_financial_statement->check_lastendperiod($periodend);

            /*===================================== get saldo opening balance or glsaldobalancesheet ===================*/
            $data['saldo_opening_balance'][$x]      = $this->m_index_financial_statement->get_saldo_opening_balance_consolidation($company,$AccountNumber,$month_period,$year_period,$periodend,$is_endperiod,$is_lastendperiod);

            $month_from++;

        }

        /*======================================= get list journal by account and date ========================================*/
        $data['all_journal']    = $this->m_index_financial_statement->get_journal_balancesheet_consolidation($company,$AccountNumber,$from_date,$end_date);

        $data['from_date']      = date("d F Y", strtotime($from_date));
        $data['end_date']       = date("d F Y", strtotime($end_date));

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_multi_consolidation_report', $data);
        $this->load->view('v_footer');  
    }

    function open_journal_balance_sheet_consolidation_report($AccountNumber,$month,$year){

        $branch                 = $this->session->userdata('branch');
        $company_code           = $this->session->userdata('company');

        $AccountNumber          = str_replace('_', '-', $AccountNumber);
        $data['Account_Number'] = $AccountNumber;
        $data['Account_Name']   = $this->m_index_financial_statement->get_account_name($AccountNumber);
        $data['branch']         = $this->m_branchinfo->get_detail($branch);

        $from_date                  = date('Y-m-d',strtotime($year.'-'.$month.'-01'));
        $day_month                  = date('t',strtotime($year.'-'.$month));
        $end_date                   = date('Y-m-d',strtotime($year.'-'.$month.'-'.$day_month));

        if($month == 1){
            $month = 12;
            $year  = abs($year) - 1;
        }else{
            $month = abs($month - 1);
        }

        /*====================================== get account type coa =============================*/
        $coa_type                           = $this->m_index_financial_statement->get_coa_type($AccountNumber);

        $data['debet_credit']               = $this->m_account_journal->debet_kredit_profit_loss($coa_type['Acc_Type_Name'],$coa_type['Sub_Type_Name']);

        $period_end                 = $year.abs($month);
        $is_endperiod               = $this->m_index_financial_statement->check_endperiod($period_end);
        $is_lastendperiod           = $this->m_index_financial_statement->check_lastendperiod($period_end);
        /*======================================= get list journal by account and date ========================================*/
        $data['saldo_opening_balance']      = $this->m_index_financial_statement->get_saldo_opening_balance_consolidation($company_code,$AccountNumber,abs($month),$year,$is_endperiod,$is_lastendperiod);
        $data['all_journal']                = $this->m_index_financial_statement->get_journal_balancesheet_consolidation($company_code,$AccountNumber,$from_date,$end_date);

        $data['month']              = $month;
        $data['year']               = $year;

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_print_journal_balance_sheet_consolidation_report', $data);
        $this->load->view('v_footer');  

    }

    public function preview_balancesheet_compare_period($month_from,$year_from,$month_end,$year_end,$branch_code){

        //$branch_code            = $this->session->userdata('branch');

        $branch_code            = str_replace('_', '-', $branch_code);
        $data['month_from']     = date("F", strtotime($year_from.'-' . $month_from . '-01'));
        $data['year_from']      = $year_from;

        $data['month_end']      = date("F", strtotime($year_end.'-' . $month_end . '-01'));
        $data['year_end']       = $year_end;

        $data['from_date']      = date("Y-m-d", strtotime($year_from.'-' . $month_from . '-01'));
        $day_month              = date("t", strtotime($year_end.'-' . $month_end . '-01'));
        $data['end_date']       = date("Y-m-d", strtotime($year_end.'-' . $month_end . '-'.$day_month));
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);
        
        /*=========================================== get coa =========================================================*/
        $period_from                = $year_from.abs($month_from);
        $is_endperiod_from          = $this->m_index_financial_statement->check_endperiod($period_from);
        $is_lastendperiod_from      = $this->m_index_financial_statement->check_lastendperiod($period_from);
        $list_account_from          = $this->m_index_financial_statement->get_coa_balance_sheet(abs($month_from), $year_from,$branch_code,$period_from,$is_endperiod_from,$is_lastendperiod_from);

        $period_end                 = $year_end.abs($month_end);
        $is_endperiod_end           = $this->m_index_financial_statement->check_endperiod($period_end);
        $is_lastendperiod_end       = $this->m_index_financial_statement->check_lastendperiod($period_end);
        $list_account_end           = $this->m_index_financial_statement->get_coa_balance_sheet(abs($month_end), $year_end,$branch_code,$period_end,$is_endperiod_end,$is_lastendperiod_end);

        $data['group_from']         = $this->calculate_asset_liability($list_account_from);
        $data['group_end']          = $this->calculate_asset_liability($list_account_end);

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_compare_period', $data);
        $this->load->view('v_footer');
    }

    public function preview_balancesheet_compare_consolidation_period($month_from,$year_from,$month_end,$year_end){

        $branch_code            = $this->session->userdata('branch');

        $data['month_from']     = date("F", strtotime($year_from.'-' . $month_from . '-01'));
        $data['year_from']      = $year_from;

        $data['month_end']      = date("F", strtotime($year_end.'-' . $month_end . '-01'));
        $data['year_end']       = $year_end;

        $data['from_date']      = date("Y-m-d", strtotime($year_from.'-' . $month_from . '-01'));
        $day_month              = date("t", strtotime($year_end.'-' . $month_end . '-01'));
        $data['end_date']       = date("Y-m-d", strtotime($year_end.'-' . $month_end . '-'.$day_month));
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);
        
        $period_from                = $year_from.abs($month_from);
        $is_endperiod_from          = $this->m_index_financial_statement->check_endperiod($period_from);
        $is_lastendperiod_from      = $this->m_index_financial_statement->check_lastendperiod($period_from);

        /*=========================================== get coa =========================================================*/
        $list_account_from          = $this->m_index_financial_statement->get_coa_balance_sheet_consolidation(abs($month_from), $year_from,$period_from,$is_endperiod_from,$is_lastendperiod_from);

        $period_end                 = $year_end.abs($month_end);
        $is_endperiod_end           = $this->m_index_financial_statement->check_endperiod($period_end);
        $is_lastendperiod_end       = $this->m_index_financial_statement->check_lastendperiod($period_end);

        $list_account_end           = $this->m_index_financial_statement->get_coa_balance_sheet_consolidation(abs($month_end), $year_end,$period_end,$is_endperiod_end,$is_lastendperiod_end);

        $data['group_from']     = $this->calculate_asset_liability_consolidation($list_account_from);
        $data['group_end']      = $this->calculate_asset_liability_consolidation($list_account_end);

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_compare_consolidation_period', $data);
        $this->load->view('v_footer');
    }

    function calculate_asset_liability($list_account){
        $total_asset            = 0;
        $total_liability        = 0;
        $arr_account            = array();
        $key_type               = 0;
        $key_sub_type           = 0;
        $key_account            = 0;

         for($x=0;$x<count($list_account);$x++){

            $AccountNumber      = $list_account[$x]['AccountNumber'];
            $AccountName        = $list_account[$x]['AccountName'];
            $AccountLevel       = $list_account[$x]['AccountLevel'];
            $AccountParent      = $list_account[$x]['AccountParent'];
            $Sub_Type_Name      = $list_account[$x]['Sub_Type_Name'];
            $Acc_Type_Name      = $list_account[$x]['Acc_Type_Name'];
            $GLSaldoBalance     = $list_account[$x]['GLSaldoBalance'];
            $AccountChild       = $list_account[$x]['AccountChild'];

           
            $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);

            if($debet_credit['position'] == 'Debet'){
                $total_asset        += $debet_credit['plus'] == 'Debet' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }else{
                $total_liability    += $debet_credit['plus'] == 'Credit' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }

             
            if(count($arr_account) == 0){

                $arr_account[$key_type]  = array(
                    'Acc_Type_Name'     =>  $Acc_Type_Name,
                    'total_acc_type'    =>  $GLSaldoBalance,
                    $key_sub_type       =>  array(
                                            'Sub_Type_Name'  =>  $Sub_Type_Name,
                                            'total_sub_type' =>  $GLSaldoBalance,
                                             $key_account    => array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance)));
            }else{

                $key_type = $this->search_key($Acc_Type_Name,'Acc_Type_Name',$arr_account);
                if($key_type !== false){

                    //print_r($arr_account[$key_type]);//[0]);
                    foreach ($arr_account[$key_type] as $key_array => $value_array) {
                       if(is_array($value_array)){
                            if($value_array['Sub_Type_Name'] == $Sub_Type_Name){
                                $key_sub_type = $key_array;
                            }else{
                                $key_sub_type = false;
                            }
                        }
                    }

                    if($key_sub_type !== false){
                        foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                           if(is_array($value_account_number)){
                                if($value_account_number['AccountNumber'] == $AccountNumber){
                                    $key_account = $key_account_number;
                                }else{
                                    $key_account = false;
                                }
                            }
                        }

                        if($key_account !== false){
                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account]['balance'] += $GLSaldoBalance;
                        }else{

                            $key_account = 0;
                            foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                               if(is_array($value_account_number)){
                                    $key_account++;
                                }
                            }

                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance);
                        }
                    }else{
                        $key_account    = 0;
                        $key_sub_type   = 0;
                        foreach($arr_account[$key_type] as $key_array => $value_array) {
                           if(is_array($value_array)){
                                $key_sub_type++;
                            }
                        }
                        
                        $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type]['Sub_Type_Name'] = $Sub_Type_Name;
                        $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                        'AccountNumber' => $AccountNumber,
                                                        'AccountName'   => $AccountName,
                                                        'AccountLevel'  => $AccountLevel,
                                                        'AccountChild'  => $AccountChild,
                                                        'AccountParent' => $AccountParent,
                                                        'balance'       => $GLSaldoBalance);
                    }
                }else{

                    $key_type = count($arr_account);
                    $key_sub_type   = 0;
                    $key_account    = 0;
                    $arr_account[$key_type]['Acc_Type_Name']  = $Acc_Type_Name;
                    $arr_account[$key_type]['total_acc_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type]['Sub_Type_Name']  = $Sub_Type_Name;
                    $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                    'AccountNumber' => $AccountNumber,
                                                    'AccountName'   => $AccountName,
                                                    'AccountLevel'  => $AccountLevel,
                                                    'AccountChild'  => $AccountChild,
                                                    'AccountParent' => $AccountParent,
                                                    'balance'       => $GLSaldoBalance);
                }
            }
        }

        $data['total_asset']        = $total_asset;
        $data['total_liability']    = $total_liability;
        $data['group']              = $arr_account;

        return $data;
    }

    function calculate_asset_liability_consolidation($list_account){
        $total_asset            = 0;
        $total_liability        = 0;
        $arr_account            = array();
        $key_type               = 0;
        $key_sub_type           = 0;
        $key_account            = 0;

         for($x=0;$x<count($list_account);$x++){

            $AccountNumber      = $list_account[$x]['AccountNumber'];
            $AccountName        = $list_account[$x]['AccountName'];
            $AccountLevel       = $list_account[$x]['AccountLevel'];
            $AccountParent      = $list_account[$x]['AccountParent'];
            $Sub_Type_Name      = $list_account[$x]['Sub_Type_Name'];
            $Acc_Type_Name      = $list_account[$x]['Acc_Type_Name'];
            $GLSaldoBalance     = $list_account[$x]['GLSaldoBalance'];
            $AccountChild       = $list_account[$x]['AccountChild'];

           
            $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);

            if($debet_credit['position'] == 'Debet'){
                $total_asset        += $debet_credit['plus'] == 'Debet' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }else{
                $total_liability    += $debet_credit['plus'] == 'Credit' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }

             
            if(count($arr_account) == 0){

                $arr_account[$key_type]  = array(
                    'Acc_Type_Name'     =>  $Acc_Type_Name,
                    'total_acc_type'    =>  $GLSaldoBalance,
                    $key_sub_type       =>  array(
                                            'Sub_Type_Name'  =>  $Sub_Type_Name,
                                            'total_sub_type' =>  $GLSaldoBalance,
                                             $key_account    => array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance)));
            }else{

                $key_type = $this->search_key($Acc_Type_Name,'Acc_Type_Name',$arr_account);
                if($key_type !== false){

                    //print_r($arr_account[$key_type]);//[0]);
                    foreach ($arr_account[$key_type] as $key_array => $value_array) {
                       if(is_array($value_array)){
                            if($value_array['Sub_Type_Name'] == $Sub_Type_Name){
                                $key_sub_type = $key_array;
                            }else{
                                $key_sub_type = false;
                            }
                        }
                    }

                    if($key_sub_type !== false){
                        foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                           if(is_array($value_account_number)){
                                if($value_account_number['AccountNumber'] == $AccountNumber){
                                    $key_account = $key_account_number;
                                }else{
                                    $key_account = false;
                                }
                            }
                        }

                        if($key_account !== false){
                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account]['balance'] += $GLSaldoBalance;
                        }else{

                            $key_account = 0;
                            foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                               if(is_array($value_account_number)){
                                    $key_account++;
                                }
                            }

                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance);
                        }
                    }else{
                        $key_account    = 0;
                        $key_sub_type   = 0;
                        foreach($arr_account[$key_type] as $key_array => $value_array) {
                           if(is_array($value_array)){
                                $key_sub_type++;
                            }
                        }
                        
                        $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type]['Sub_Type_Name'] = $Sub_Type_Name;
                        $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                        'AccountNumber' => $AccountNumber,
                                                        'AccountName'   => $AccountName,
                                                        'AccountLevel'  => $AccountLevel,
                                                        'AccountChild'  => $AccountChild,
                                                        'AccountParent' => $AccountParent,
                                                        'balance'       => $GLSaldoBalance);
                    }
                }else{

                    $key_type = count($arr_account);
                    $key_sub_type   = 0;
                    $key_account    = 0;
                    $arr_account[$key_type]['Acc_Type_Name']  = $Acc_Type_Name;
                    $arr_account[$key_type]['total_acc_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type]['Sub_Type_Name']  = $Sub_Type_Name;
                    $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                    'AccountNumber' => $AccountNumber,
                                                    'AccountName'   => $AccountName,
                                                    'AccountLevel'  => $AccountLevel,
                                                    'AccountChild'  => $AccountChild,
                                                    'AccountParent' => $AccountParent,
                                                    'balance'       => $GLSaldoBalance);
                }
            }
        }


        $data['total_asset']        = $total_asset;
        $data['total_liability']    = $total_liability;
        $data['group']              = $arr_account;

        return $data;
    }

    function preview_balancesheet_consolidation_period($month,$year){
        $branch_code            = $this->session->userdata('branch');

        $period_month           = $month;
        $period_year            = $year;
        $data['bulan']          = $month;
        $data['tahun']          = $year;
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);
        $data['month']          = date("F", strtotime($year.'-' . $period_month . '-01'));
        $data['year']           = $period_year;
        
        $period_end             = $year.abs($month);
        $is_endperiod           = $this->m_index_financial_statement->check_endperiod($period_end);
        $is_lastendperiod       = $this->m_index_financial_statement->check_lastendperiod($period_end);

        /*=========================================== get coa =========================================================*/
        $list_account           = $this->m_index_financial_statement->get_coa_balance_sheet_consolidation(abs($period_month), $period_year,$period_end,$is_endperiod,$is_lastendperiod);

        $total_asset            = 0;
        $total_liability        = 0;
        $arr_account            = array();
        $key_type               = 0;
        $key_sub_type           = 0;
        $key_account            = 0;

         for($x=0;$x<count($list_account);$x++){

            $AccountNumber      = $list_account[$x]['AccountNumber'];
            $AccountName        = $list_account[$x]['AccountName'];
            $AccountLevel       = $list_account[$x]['AccountLevel'];
            $AccountParent      = $list_account[$x]['AccountParent'];
            $Sub_Type_Name      = $list_account[$x]['Sub_Type_Name'];
            $Acc_Type_Name      = $list_account[$x]['Acc_Type_Name'];
            $GLSaldoBalance     = $list_account[$x]['GLSaldoBalance'];
            $AccountChild       = $list_account[$x]['AccountChild'];

           
            $debet_credit           = $this->m_account_journal->debet_kredit_profit_loss($Acc_Type_Name,$Sub_Type_Name);

            if($debet_credit['position'] == 'Debet'){
                $total_asset        += $debet_credit['plus'] == 'Debet' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }else{
                $total_liability    += $debet_credit['plus'] == 'Credit' ? $GLSaldoBalance : $GLSaldoBalance * -1;
            }

             
            if(count($arr_account) == 0){

                $arr_account[$key_type]  = array(
                    'Acc_Type_Name'     =>  $Acc_Type_Name,
                    'total_acc_type'    =>  $GLSaldoBalance,
                    $key_sub_type       =>  array(
                                            'Sub_Type_Name'  =>  $Sub_Type_Name,
                                            'total_sub_type' =>  $GLSaldoBalance,
                                             $key_account    => array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance)));
            }else{

                $key_type = $this->search_key($Acc_Type_Name,'Acc_Type_Name',$arr_account);
                if($key_type !== false){

                    //print_r($arr_account[$key_type]);//[0]);
                    foreach ($arr_account[$key_type] as $key_array => $value_array) {
                       if(is_array($value_array)){
                            if($value_array['Sub_Type_Name'] == $Sub_Type_Name){
                                $key_sub_type = $key_array;
                            }else{
                                $key_sub_type = false;
                            }
                        }
                    }

                    if($key_sub_type !== false){
                        foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                           if(is_array($value_account_number)){
                                if($value_account_number['AccountNumber'] == $AccountNumber){
                                    $key_account = $key_account_number;
                                }else{
                                    $key_account = false;
                                }
                            }
                        }

                        if($key_account !== false){
                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account]['balance'] += $GLSaldoBalance;
                        }else{

                            $key_account = 0;
                            foreach ($arr_account[$key_type][$key_sub_type] as $key_account_number => $value_account_number) {
                               if(is_array($value_account_number)){
                                    $key_account++;
                                }
                            }

                            $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type]['total_sub_type'] += $GLSaldoBalance;
                            $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                            'AccountNumber' => $AccountNumber,
                                                            'AccountName'   => $AccountName,
                                                            'AccountLevel'  => $AccountLevel,
                                                            'AccountChild'  => $AccountChild,
                                                            'AccountParent' => $AccountParent,
                                                            'balance'       => $GLSaldoBalance);
                        }
                    }else{
                        $key_account    = 0;
                        $key_sub_type   = 0;
                        foreach($arr_account[$key_type] as $key_array => $value_array) {
                           if(is_array($value_array)){
                                $key_sub_type++;
                            }
                        }
                        
                        $arr_account[$key_type]['total_acc_type'] += $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type]['Sub_Type_Name'] = $Sub_Type_Name;
                        $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                        $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                        'AccountNumber' => $AccountNumber,
                                                        'AccountName'   => $AccountName,
                                                        'AccountLevel'  => $AccountLevel,
                                                        'AccountChild'  => $AccountChild,
                                                        'AccountParent' => $AccountParent,
                                                        'balance'       => $GLSaldoBalance);
                    }
                }else{

                    $key_type = count($arr_account);
                    $key_sub_type   = 0;
                    $key_account    = 0;
                    $arr_account[$key_type]['Acc_Type_Name']  = $Acc_Type_Name;
                    $arr_account[$key_type]['total_acc_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type]['Sub_Type_Name']  = $Sub_Type_Name;
                    $arr_account[$key_type][$key_sub_type]['total_sub_type'] = $GLSaldoBalance;
                    $arr_account[$key_type][$key_sub_type][$key_account] = array(
                                                    'AccountNumber' => $AccountNumber,
                                                    'AccountName'   => $AccountName,
                                                    'AccountLevel'  => $AccountLevel,
                                                    'AccountChild'  => $AccountChild,
                                                    'AccountParent' => $AccountParent,
                                                    'balance'       => $GLSaldoBalance);
                }
            }
        }

        $data['total_asset']        = $total_asset;
        $data['total_liability']    = $total_liability;
        $data['group']              = $arr_account;
       
        // print_r($arr_account);
        // die();
        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_consolidation', $data);
        $this->load->view('v_footer'); 
    }

    function preview_balancesheet_multi_period($month_from,$year_from,$month_end,$year_end,$branch_code){

        //$branch_code            = $this->session->userdata('branch');
        $branch_code            = str_replace('_', '-', $branch_code);

        $data['month_from']     = date("F", strtotime($year_from.'-' . $month_from . '-01'));
        $data['year_from']      = $year_from;

        $data['month_end']      = date("F", strtotime($year_end.'-' . $month_end . '-01'));
        $data['year_end']       = $year_end;

        $data['from_date']      = date("Y-m-d", strtotime($year_from.'-' . $month_from . '-01'));
        $day_month              = date("t", strtotime($year_end.'-' . $month_end . '-01'));
        $data['end_date']       = date("Y-m-d", strtotime($year_end.'-' . $month_end . '-'.$day_month));
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);

        $month_from             = abs($month_from);
        $year_from              = abs($year_from);

        $interval_month         = $this->get_interval_month($month_from,$year_from,$month_end,$year_end);
        
        for($x=0;$x<$interval_month;$x++){

            if(abs($month_from) == 13){
                $month      = 1;
                $year       = abs($year_from) + 1;
                $month_from = 1;
                $year_from  += 1;
            }else{
                $month      = $month_from;
                $year       = $year_from;
            }

            $data['month'][$x]  = $month;
            $data['year'][$x]   = $year;

            $period_end         = $year.abs($month);
            $is_endperiod       = $this->m_index_financial_statement->check_endperiod($period_end);
            $is_lastendperiod   = $this->m_index_financial_statement->check_lastendperiod($period_end);

            /*=========================================== get coa =========================================================*/
            $list_account[$x]      = $this->m_index_financial_statement->get_coa_balance_sheet($month, $year,$branch_code,$period_end,$is_endperiod,$is_lastendperiod);
            //$list_account[$x]      = $this->m_index_financial_statement->get_coa_balance_sheet1($month, $year,$branch_code);
            //echo $this->db->last_query();
            // die();

            $data['group'][$x]     = $this->calculate_asset_liability($list_account[$x]);

            $month_from++;

        }

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_multi_period', $data);
        $this->load->view('v_footer');

    }

    function preview_balancesheet_multi_consolidation_period($month_from,$year_from,$month_end,$year_end){

         $branch_code            = $this->session->userdata('branch');

        $data['month_from']     = date("F", strtotime($year_from.'-' . $month_from . '-01'));
        $data['year_from']      = $year_from;

        $data['month_end']      = date("F", strtotime($year_end.'-' . $month_end . '-01'));
        $data['year_end']       = $year_end;

        $data['from_date']      = date("Y-m-d", strtotime($year_from.'-' . $month_from . '-01'));
        $day_month              = date("t", strtotime($year_end.'-' . $month_end . '-01'));
        $data['end_date']       = date("Y-m-d", strtotime($year_end.'-' . $month_end . '-'.$day_month));
                
        $data['branch']         = $this->m_branchinfo->get_detail($branch_code);

        $month_from             = abs($month_from);
        $year_from              = abs($year_from);

        $interval_month         = $this->get_interval_month($month_from,$year_from,$month_end,$year_end);

        for($x=0;$x<$interval_month;$x++){

            if($month_from == 13){
                $month      = 1;
                $year       = abs($year_from) + 1;
                $month_from = 1;
                $year_from  += 1;
            }else{
                $month      = $month_from;
                $year       = $year_from;
            }

            $data['month'][$x]  = $month;
            $data['year'][$x]   = $year;

            $period_end             = $year.(abs($month));
            $is_endperiod           = $this->m_index_financial_statement->check_endperiod($period_end);
            $is_lastendperiod       = $this->m_index_financial_statement->check_lastendperiod($period_end);

            /*=========================================== get coa =========================================================*/
            $list_account[$x]      = $this->m_index_financial_statement->get_coa_balance_sheet_consolidation($month, $year,$period_end,$is_endperiod,$is_lastendperiod);

            $data['group'][$x]     = $this->calculate_asset_liability_consolidation($list_account[$x]);

            $month_from++;

        }

        $this->load->view('v_header');
        $this->load->view('report/financial_statement_report/v_preview_balance_sheet_multi_consolidation_period', $data);
        $this->load->view('v_footer');

    }
    
    public function journal_balance_sheet_repot(){
        $uri_data               = str_replace('report/index_financial_statement/journal_profit_loss_report/', "", $this->uri->uri_string());
        $data_uri               = explode('/', $uri_data);
        
        $trans_number           = str_replace('~', '/', $data_uri[0]);
        $branch_code            = explode('/', $trans_number)[0];
        
        $account_number         = $data_uri[1];
        $trans_type             = $this->get_trans_name($data_uri[2]);
        $begin_date             = $data_uri[3];
        $end_date               = $data_uri[4];
        
        $data['detail']         = $this->m_index_to_report->get_journal_index_report($branch_code, $account_number, $begin_date, $end_date); 
        $data['trans_name']     = $trans_type;
        $data['begin_date']     = $begin_date;
        $data['end_date']       = $end_date;
       
        $this->load->view('v_header');
        $this->load->view('report/view_to_financial/v_journal_profit_loss_statement', $data);
        $this->load->view('v_footer');	
    }
    
    function get_trans_name($trans_type){
        switch($trans_type){
            case 'PInv':
                return 'Purchase Invoice';
                break;

            case 'PPay':
                return'Purchase Payment';
                break;

            case 'SInv':
                return 'Sales Invoice';
                break;

            case 'SRec':
                return 'Sales Receipt';
                break;

            case 'EBon':
                return 'Expense Cash Bon';
                break;

            case 'CPay':
                return 'Cash Payment';
                break;

            case 'CDep':
                return 'Cash Deposit';
                break;

            case 'BSweep':
                return 'Bank Sweep';
                break;

            case 'ADebt':
                return 'Piutang Afiliasi';
                break;

            case 'AIns':
                return 'Payment Afiliasi';
                break;

            case 'EDebt':
                return 'Piutang Karyawan';
                break;

            case 'EIns':
                return 'Payment Karyawan';
                break;

            case 'TCash':
                return 'Transfer Petty Cach';
                break;

            case 'JVch':
                return 'General Ledger (JV)';
                break;
        }
   }

   function list_branch_bycompany(){
        $company_code   = $this->session->userdata('company');
        $branch_status  = $this->session->userdata('branch_status');
        $list_company   = $this->m_index_financial_statement->get_list_branch($company_code,$branch_status);
        echo json_encode($list_company);
   }
}
?>
