<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Ferry / Tanggal : 15-12-2014 / Penambahan Account P/L
 *      Revisi  :
 */
 
class Preference_account extends CI_Controller {
    
	//edited by Prawira Tgl 18 Agustus 2015
	public $menu_id = 90;
	public function __construct() {
        parent::__construct();
		
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					//  $this->load->model('m_preference');
						$this->load->model('list/m_compaccountgl');
						$this->load->model('setup/m_branchinfo');
					//	$this->load->model('list/m_coa');	
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
		
    }

    //
	public function index(){	
        $data['jobtitle'] = $this->session->userdata('jobtitle');	
        
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_account', $data);
        $this->load->view('v_footer');
    }
    
    //Function menampilkan COA untuk Branch P/L
    public function ajax_pl_account(){
        $data=$this->m_compaccountgl->get_comp_gl_account(1);
        header('Content-Type: text/json');
		
        ob_clean();
        flush();       
        echo json_encode($data);
    }
    
    //Update PL Status as P/L Account
    public function update_pl_account(){
        if ($this->session->userdata('branch') == 0) {
            $company_code       = $this->session->userdata('company');
            $branch_ho_code     = $this->session->userdata('branch');

            $pl_post            = $this->input->post('selected_pl_account');
            $pl_selected        = json_decode($pl_post);
            $result_edit        = 0;
            
            foreach($pl_selected as $comp_row) {
                if(($comp_row->ModusCode == 2) && ($comp_row->AccountChild == 1)) {
                    $account_number     = $comp_row->AccountNumber;
                    $pl_status          = $comp_row->PLStatus == 'Yes' ? 1 : 0;
                   
				    $this->db->trans_begin();
					//cek data hanya branch bukan HO apakah balance sudah 0 semua ata tidak
					$get_branch = $this->m_branchinfo->get_branch_by_company($company_code);
                    foreach($get_branch as $branch_row) {
                        $branch_code    = $branch_row->BranchCode;
                                
                        $check_account = $this->m_compaccountgl->check_branch_pl_account_new($account_number, $company_code, $branch_code);
						if($check_account->GLSaldoBalance == 0){
							//1. Update status PL jika balance 0
							$result_edit = $this->m_compaccountgl->update_pl_active($account_number, $company_code, $branch_code, $pl_status);
						}
						else{
							$result['status']   = FALSE;
							$result['message']  = 'Data gagal disimpan, masih ada Opening Blance yang belum 0(Kosong)';
							
							echo json_encode($result);
                			die;	
						}
                    }
					//--------------------------------------------end cek data------------------------------------------------------------//
					
                    //2. Update PL Status into atcompaccountgl
                    $comp_pl    = array(
                                    'PLStatus'      => $pl_status,
                                    'LastUserPL'    => $this->session->userdata("username")
                                    );

                   $result_edit = $this->m_compaccountgl->update_branch($comp_pl, $account_number, $branch_ho_code);
                    //--------------------------------------------------------------------------------------------------------------//
                }
            }

            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();			
                $result['status']   = false;
                $result['message']  = 'Data gagal disimpan, silahkan pilih P/L Account';
            }
            else {
                $this->db->trans_commit();			
                $result['status']   = true;
                $result['message']  = 'P/L Account Status sudah berhasil disimpan';            
            }		
        }
        else {
            $result['status']   = false;
            $result['message']  = 'Pengaturan P/L Status hanya dapat dilakukan oleh Kantor Pusat';
        }
        
        echo json_encode($result);
    }    
}