<?php

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

class Index_account_payables extends CI_Controller {
    public $menu_model  = array('m_index_to_report','m_index_cash_bank', 'm_index_account_payables','m_index_sales', 'file/m_new_company', 'setup/m_branchinfo','custom/m_custom');
    public $menu_id_outstanding_invoice_payables     = 328;
    public $menu_id_aging_pay_detail                 = 329;
    public $menu_id_vendor_list                      = 330;
    
    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_payables);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_outstanding_invoice_payables, $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_payables);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_outstanding_invoice_payables, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_payables_report/v_outstanding_invoices_payables_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function ajax_reloadDataOutstandingInvoicesPayables(){        
        $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_payables->get_all_outstanding_invoices_payables();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                $TransStatus_PI = get_status($dtResult->TransStatus_PI);
                $resultData[$indexLoop]->TransStatus_PI = $TransStatus_PI;
                
                $resultData[$indexLoop]->InvoiceDate_IP = $this->m_index_sales->dateSqlToIndo($dtResult->InvoiceDate_IP);
                $resultData[$indexLoop]->PRetDate_PRet = $this->m_index_sales->dateSqlToIndo($dtResult->PRetDate_PRet);
                $resultData[$indexLoop]->PPayDate_PPay = $this->m_index_sales->dateSqlToIndo($dtResult->PPayDate_PPay);
                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate_IP);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn_PI, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate_IP = $dueDate;
                        
                $selisih = ((floor(strtotime (date('Y-m-d')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate_IP = $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_payables_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_pay_detail);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_aging_pay_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_pay_detail);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_aging_pay_detail, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_payables_report/v_aging_payables_detail_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function ajax_reloadDataAgingPaydetail(){        
        $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_payables->get_all_aging_pay_detail();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexLoop = 0;
            foreach($resultData as $dtResult) {
                $resultData[$indexLoop]->InvoiceDate_IP = $this->m_index_sales->dateSqlToIndo($dtResult->InvoiceDate_IP);
                
                $dueDate    = "";
                $pecah      = explode('-', $resultData[$indexLoop]->DueDate_IP);
                $tahun      = $pecah[0];
                $bulan      = $pecah[1];
                $tanggal    = $pecah[2];                
                $nextN      = mktime(0,0,0, $bulan, $tanggal + $resultData[$indexLoop]->NetDueIn_PI, $tahun);
                $dueDate    = date('d-m-Y', $nextN);
                $resultData[$indexLoop]->DueDate_IP = $dueDate;
                        
                $selisih = ((floor(strtotime ($this->input->post('txt_DateTo')) - strtotime (date('Y-m-d', $nextN)) ))/(60*60*24));
                $resultData[$indexLoop]->AgeDueDate_IP = $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 vendor_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_vendor_list);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_vendor_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_vendor_list);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_vendor_list, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/account_payables_report/v_vendor_list_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function ajax_reloadDataVendorList(){        
        $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_payables->get_all_vendor_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);
    } 
    
}
?>
