<?php

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

class Index_account_receivables extends CI_Controller {
    public $menu_model  = array('m_index_to_report','m_index_cash_bank', 'm_index_account_receivables','m_index_sales', 'file/m_new_company', 'setup/m_branchinfo','custom/m_custom');
    public $menu_id_outstanding_invoice              = 337;
    public $menu_id_aging_recv_detail                = 338;
    public $menu_id_customer_list                    = 374;
    public $menu_id_customer_aging_ranking_unsettled = 384;
    public $menu_id_customer_aging_ranking_settled   = 385;
    public $menu_id_customer_aging_ranking_all       = 386;
    
    public function __construct() {
        parent::__construct();
        $this->load->model($this->menu_model);

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

        if ($this->session->userdata("login") != TRUE) {
            redirect("login", "refresh");
            die;
        }
    }
    public function outstanding_invoices_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_outstanding_invoice);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_outstanding_invoice, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_outstanding_invoice);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_outstanding_invoice, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_outstanding_invoices_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataOutstandingInvoices(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_account_receivables->pre_get_all_outstanding_invoices();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataOutstandingInvoices(){   
        $resultData = $this->m_index_account_receivables->get_all_outstanding_invoices();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataOutstandingInvoices_LAMA(){        
        $txt_DateFrom   = $this->input->post('txt_DateFrom');
        $txt_DateTo     = $this->input->post('txt_DateTo');  
        
        //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        if($BranchCode!="" && $BranchCode!="All"){
            $data['company']    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
        }else{
            $data['company']    = $this->m_index_cash_bank->get_company_name($CompCode);
        }
        //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        
        $resultData = $this->m_index_account_receivables->get_all_outstanding_invoices();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                $TransStatus_SI = get_status($dtResult->TransStatus_SI);
                $resultData[$indexLoop]->TransStatus_SI = $TransStatus_SI;
                
                $resultData[$indexLoop]->InvoiceDate_IR = $this->m_index_sales->dateSqlToIndo($dtResult->InvoiceDate_IR);
                $resultData[$indexLoop]->SRetDate_SRet = $this->m_index_sales->dateSqlToIndo($dtResult->SRetDate_SRet);
                $resultData[$indexLoop]->SRecDate_SRec = $this->m_index_sales->dateSqlToIndo($dtResult->SRecDate_SRec);
                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate_IR);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn_SI, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate_IR = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate_IR = $selisih;
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$data['company'],'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    
    
    public function aging_recv_detail_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_aging_recv_detail);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_aging_recv_detail, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_aging_recv_detail);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_aging_recv_detail, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_aging_recv_detail_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataAgingReceivdetail(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
         
        $resultData = $this->m_index_account_receivables->pre_get_all_aging_recv_detail();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataAgingReceivdetail(){ 
        $resultData = $this->m_index_account_receivables->get_all_aging_recv_detail();
        if(count($resultData)>0){
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                
                $resultData[$indexLoop]->InvoiceDate_IR = $this->m_index_sales->dateSqlToIndo($dtResult->InvoiceDate_IR);
                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate_IR);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn_SI, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate_IR = $dueDate;
                        
                $selisih = ((floor(strtotime ($this->input->post('txt_DateTo')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate_IR = $selisih;
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function customer_list_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_customer_list);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_list, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_list);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_list, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_customer_list_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function ajax_reloadDataCustomerList(){        
        $txt_DateFrom   = $this->input->post('txt_DateFrom');
        $txt_DateTo     = $this->input->post('txt_DateTo');  
        
        //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        if($BranchCode!="" && $BranchCode!="All"){
            $data['company']    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
        }else{
            $data['company']    = $this->m_index_cash_bank->get_company_name($CompCode);
        }
        //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        
        $resultData = $this->m_index_account_receivables->get_all_customer_list();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$data['company'],'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    
    
    public function customer_aging_ranking_unsettled_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_customer_aging_ranking_unsettled);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_unsettled, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_unsettled);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_unsettled, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_customer_aging_ranking_unsettled_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataCustAgingRankingUnsettled(){    
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }                      
        $resultData = $this->m_index_account_receivables->pre_get_all_cust_aging_ranking_unsettled();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataCustAgingRankingUnsettled(){
        $resultData = $this->m_index_account_receivables->get_all_cust_aging_ranking_unsettled();
        if(count($resultData)>0){
            $indexLoop = 0;
            foreach($resultData as $dtResult) {                                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                
                if($resultData[$indexLoop]->lastDateRec !=null && $resultData[$indexLoop]->lastDateRec!=""){
                    $dataRec      = explode('_', $resultData[$indexLoop]->lastDateRec);
                    $resultData[$indexLoop]->SRecDetailID = $dataRec[0];
                    $resultData[$indexLoop]->SrecNumber = $dataRec[1];
                    $resultData[$indexLoop]->SRecDate = $dataRec[2];
                    $resultData[$indexLoop]->TransStatus_SR = $dataRec[3];
                    $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->SInvDate) ))/(60*60*24));
                }else{
                    $resultData[$indexLoop]->SRecDetailID = null;
                    $resultData[$indexLoop]->SrecNumber = null;
                    $resultData[$indexLoop]->SRecDate = null;
                    $resultData[$indexLoop]->TransStatus_SR = null;
                    $resultData[$indexLoop]->FinishDue = null;
                }
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function detail_aging_ranking_unsettled() {        
        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_customer_aging_ranking_unsettled);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_unsettled, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_unsettled);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_unsettled, $group_menu);
                    
                    $data['paramUrlCompany']    = urldecode($this->uri->segment(4));
                    $data['paramUrlCompanyCode']= urldecode($this->uri->segment(5));
                    $data['paramUrlBranch']     = urldecode($this->uri->segment(6));
                    $data['paramUrlCustCode']   = urldecode($this->uri->segment(7));
                    $data['paramUrlCust']       = urldecode($this->uri->segment(8));
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_detail_aging_ranking_unsettled_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function ajax_reloadDataDetailCustAgingRankingUnsettled(){        
             
        $txt_DateFrom   = $this->input->post('txt_DateFrom');
        $txt_DateTo     = $this->input->post('txt_DateTo');  
        
        //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        if($BranchCode!="" && $BranchCode!="All"){
            $data['company']    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
        }else{
            $data['company']    = $this->m_index_cash_bank->get_company_name($CompCode);
        }
        //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        
        $resultData = $this->m_index_account_receivables->get_all_detail_cust_aging_ranking_unsettled();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                                
                $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->SInvDate) ))/(60*60*24));
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$data['company'],'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
        
    
    public function customer_aging_ranking_settled_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_customer_aging_ranking_settled);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_settled, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_settled);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_settled, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_customer_aging_ranking_settled_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataCustAgingRankingSettled(){   
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_account_receivables->pre_get_all_cust_aging_ranking_settled();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataCustAgingRankingSettled(){  
        $resultData = $this->m_index_account_receivables->get_all_cust_aging_ranking_settled();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                                
                $dataRec      = explode('_', $resultData[$indexLoop]->lastDateRec);
                $resultData[$indexLoop]->SRecDetailID = $dataRec[0];
                $resultData[$indexLoop]->SrecNumber = $dataRec[1];
                $resultData[$indexLoop]->SRecDate = $dataRec[2];
                $resultData[$indexLoop]->TransStatus_SR = $dataRec[3];
                $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->DueDate) ))/(60*60*24));
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function detail_aging_ranking_settled() {        
        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_customer_aging_ranking_settled);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_settled, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_settled);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_settled, $group_menu);
                    
                    $data['paramUrlCompany']    = urldecode($this->uri->segment(4));
                    $data['paramUrlCompanyCode']= urldecode($this->uri->segment(5));
                    $data['paramUrlBranch']     = urldecode($this->uri->segment(6));
                    $data['paramUrlCustCode']   = urldecode($this->uri->segment(7));
                    $data['paramUrlCust']       = urldecode($this->uri->segment(8));
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_detail_aging_ranking_settled_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_reloadDataDetailCustAgingRankingSettled(){  
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($CompCode!="" && $CompCode!="All"){
//                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_account_receivables->pre_get_all_detail_cust_aging_ranking_settled();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataDetailCustAgingRankingSettled(){ 
        $resultData = $this->m_index_account_receivables->get_all_detail_cust_aging_ranking_settled();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                                
                $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->DueDate) ))/(60*60*24));
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function customer_aging_ranking_all_report() {        
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_customer_aging_ranking_all);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_all, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_all);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_all, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_customer_aging_ranking_all_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_reloadDataCustAgingRankingAll(){  
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $BranchCode!="All"){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
        $resultData = $this->m_index_account_receivables->pre_get_all_cust_aging_ranking_all();
        if(count($resultData)>0){
        }else{
            $resultData=[0];
        }
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataCustAgingRankingAll(){  
        $resultData = $this->m_index_account_receivables->get_all_cust_aging_ranking_all();
        if(count($resultData)>0){
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                if($resultData[$indexLoop]->lastDateRec!=null && $resultData[$indexLoop]->lastDateRec!=""){               
                    $dataRec      = explode('_', $resultData[$indexLoop]->lastDateRec);
                    $resultData[$indexLoop]->SRecDetailID = $dataRec[0];
                    $resultData[$indexLoop]->SrecNumber = $dataRec[1];
                    $resultData[$indexLoop]->SRecDate = $dataRec[2];
                    $resultData[$indexLoop]->TransStatus_SR = $dataRec[3];
                    $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->DueDate) ))/(60*60*24));
                }else{                    
                    $resultData[$indexLoop]->SRecDetailID = null;
                    $resultData[$indexLoop]->SrecNumber = null;
                    $resultData[$indexLoop]->SRecDate = null;
                    $resultData[$indexLoop]->TransStatus_SR = null;
                    $resultData[$indexLoop]->FinishDue = null;
                }   
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        } 
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function detail_aging_ranking_All() {        
        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_customer_aging_ranking_all);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_customer_aging_ranking_all, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_customer_aging_ranking_all);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_customer_aging_ranking_all, $group_menu);
                    
                    $data['paramUrlCompany']    = urldecode($this->uri->segment(4));
                    $data['paramUrlCompanyCode']= urldecode($this->uri->segment(5));
                    $data['paramUrlBranch']     = urldecode($this->uri->segment(6));
                    $data['paramUrlCustCode']   = urldecode($this->uri->segment(7));
                    $data['paramUrlCust']       = urldecode($this->uri->segment(8));
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_detail_aging_ranking_all_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_reloadDataDetailCustAgingRankingAll(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($CompCode!="" && $BranchCode!="All"){
//                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
        $resultData = $this->m_index_account_receivables->pre_get_all_detail_cust_aging_ranking_all();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    } 
    public function ajax_reloadDataDetailCustAgingRankingAll(){ 
        $resultData = $this->m_index_account_receivables->get_all_detail_cust_aging_ranking_all();
        if(count($resultData)>0){
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate = $selisih;
                                
                $resultData[$indexLoop]->FinishDue = ((floor(strtotime ($resultData[$indexLoop]->SRecDate) - strtotime ($resultData[$indexLoop]->DueDate) ))/(60*60*24));
                $indexLoop++;
            }
        }else{
            $resultData=[0];
        }
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    } 
    
    public function detail_totalamount_outstanding_invoices_report(){               
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_outstanding_invoice);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_outstanding_invoice, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_outstanding_invoice);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_outstanding_invoice, $group_menu);
                    $data['customerCode'] = $this->uri->segment(4);
                    $data['totalAmount'] = $this->uri->segment(5);
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_outstanding_invoices_report_detailtotalamount', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function ajax_detailTotalAmountSalesOutstanding(){  
               
        $callBack =array('datas'=>[0], 'startdate'=>"",'enddate'=>"", 'company'=>"",'branch'=>"");
        $resultData = $this->m_index_account_receivables->get_all_detail_amount_invoices_outstanding();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $callBack['branch']     = $resultData[0]->BranchName;
            $callBack['company']    = $resultData[0]->CompanyName;
        }else{
            $resultData=[0];
        }            
        $callBack['datas'] = $resultData;
        echo json_encode($callBack);
    }
    
    public function detail_payment_outstanding_invoices_report(){                 
        if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id_outstanding_invoice);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_outstanding_invoice, $data['available_menu'])) { 
                    redirect(base_url() . 'error', 'refresh');	
                }
                else{
                    $group_menu         = $this->session->userdata('group_menu');
                    $data['menu_model'] = $this->menu_model;
                    $data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id_outstanding_invoice);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_outstanding_invoice, $group_menu);
//                    $data['customerCode'] = $this->uri->segment(4);
//                    $data['totalAmount'] = $this->uri->segment(5);
                    
                    $data['paramUrlCompany']    = urldecode($this->uri->segment(4));
                    $data['paramUrlCompanyCode']= urldecode($this->uri->segment(5));
                    $data['paramUrlBranchCode']     = urldecode($this->uri->segment(6));
                    $data['paramUrlCustomerCode']   = urldecode($this->uri->segment(7));
                    $data['paramUrlCustomer']       = urldecode($this->uri->segment(8));
                    $data['paramUrlDateTo']       = urldecode($this->uri->segment(9));
                    
                    $this->load->view('v_header');
                    $this->load->view('report/account_receivables_report/v_outstanding_invoices_report_detailpayment', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_detailPaymentSalesOutstanding(){ 
        $txt_DateFrom   = "";
        $txt_DateTo     = "";
        $companyName    = "";
        $BranchCode     = "";
        $pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):array(pages=>1);
        if($pages['pages']<2){
            $txt_DateFrom   = $this->input->post('txt_DateFrom');
            $txt_DateTo     = $this->input->post('txt_DateTo');          
            //=======Menyiapkan nama Company dan Branch utk ditampilkan di halaman =================//
            $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
            $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
            if($BranchCode!="" && $CompCode!=null){
                $companyName    = $this->m_index_cash_bank->get_company_by_branch($BranchCode);
            }else{
                $companyName    = $this->m_index_cash_bank->get_company_name($CompCode);
            }
            //=======END Menyiapkan nama Company dan Branch utk ditampilkan di halaman =============//
        }            
          
        $resultData = $this->m_index_account_receivables->pre_get_all_detail_payment_invoices_receivables_outstanding();
        (count($resultData)>0)?"":$resultData=[0];      
        $callBack =array('datas'=>$resultData, 'startdate'=>"",'enddate'=>"", 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_detailPaymentSalesOutstanding(){  
        $resultData = $this->m_index_account_receivables->get_all_detail_payment_invoices_receivables_outstanding();
        (count($resultData)>0)?"":$resultData=[0];  
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
}
?>
