<?php

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

class Index_sales extends CI_Controller {
    public $menu_model  = array('m_index_to_report', 'm_index_cash_bank','m_index_sales', 'file/m_new_company', 'setup/m_branchinfo','custom/m_custom');
    public $menu_id_sq_by_cust              = 288;
    public $menu_id_sq_by_item              = 289;
    public $menu_id_sales_invoice           = 303;
    public $menu_id_sls_amount_item         = 304;
    public $menu_id_sls_amount_item_catgr   = 305;
    public $menu_id_sls_order_for_dp        = 306;
    public $menu_id_sq_by_salesman          = 307;
    public $menu_id_sinv_by_salesman        = 308;
    public $menu_id_sls_return_by_cust      = 309;
    public $menu_id_sls_return_by_item      = 310;
    public $menu_id_sls_return_by_item_cust = 311;
    public $menu_id_sls_do_history          = 312;
    
    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 sales_quotation_by_customer_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_sq_by_cust);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sq_by_cust, $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_sq_by_cust);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sq_by_cust, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_quotation_by_customer_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSQByCustomer(){     
        $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_sales->pre_get_all_sq_by_customer();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSQByCustomer(){        
        $resultData = $this->m_index_sales->get_all_sq_by_customer();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
        
    public function sales_quotation_by_item_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_sq_by_item);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sq_by_item, $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_sq_by_item);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sq_by_item, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_quotation_by_item_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSQByItem(){     
        $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_sales->pre_get_all_sq_by_item();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSQByItem(){        
        $resultData = $this->m_index_sales->get_all_sq_by_item();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
    
    public function sales_invoice_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_sales_invoice);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sales_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_sales_invoice);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sales_invoice, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_invoice_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSalesInvoice(){           
        $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_sales->pre_get_all_sales_invoice();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSalesInvoice(){      
        $resultData = $this->m_index_sales->get_all_sales_invoice();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSalesInvoice_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_sales->get_all_sales_invoice();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            foreach($resultData as $dtResult) {
                $TransStatus_SI = get_status($dtResult->TransStatus_SI);
                $TransStatus_SR = get_status($dtResult->TransStatus_SR);

                $result[] = array(
                    'SInvNumber_SI'         => $dtResult->SInvNumber_SI, 
                    'BranchCode_SI'         => $dtResult->BranchCode_SI,
                    'CustomerCode_SI'       => $dtResult->CustomerCode_SI,
                    'SInvDate_SI'           => $this->m_index_sales->dateSqlToIndo($dtResult->SInvDate_SI), 
                    'SInvDesc_SI'           => $dtResult->SInvDesc_SI, 
                    'SInvTotalAmount_SI'    => $dtResult->SInvTotalAmount_SI, //change
                    'TransStatus_SI'        => $TransStatus_SI,
                    'CustomerName_SI'       => $dtResult->CustomerName_SI, //change
                    'QtyReturn_SI'          => $dtResult->QtyReturn_SI, //change
                    'QtyBookReturn_SI'      => $dtResult->QtyBookReturn_SI, //change
                    
                    'SRecDetailID_SRDet'       => $dtResult->SRecDetailID_SRDet, //change
                    'SrecNumber_SRDet'         => $dtResult->SrecNumber_SRDet, //change
                    //'SInvDetailID_SRDet'       => $dtResult->SInvDetailID_SRDet, //change
                    //'SInvNumber_SRDet'         => $dtResult->SInvNumber_SRDet, //change
                    //'SInvDate_SRDet'           => $this->m_index_sales->dateSqlToIndo($dtResult->SInvDate_SR), //change
                    //'SInvAmount_SRDet'         => $dtResult->SInvAmount_SRDet, //change
                    'SRecDiscount_SRDet'       => $dtResult->SRecDiscount_SRDet, //change
                    'SRecDPUsed_SRDet'         => $dtResult->SRecDPUsed_SRDet, //change
                    'SRecAmount_SRDet'         => $dtResult->SRecAmount_SRDet, //change
                    'SrecRounded_SRDet'        => $dtResult->SrecRounded_SRDet, //change
                    'SRecCurrency_SRDet'        => $dtResult->SRecCurrency_SRDet, //change
                    
                    'SRecDate_SR'              => $this->m_index_sales->dateSqlToIndo($dtResult->SRecDate_SR), //change
                    'SRecNumber_SR'            => $dtResult->SRecNumber_SR, //change
                    'SInvTotalAmount_SR'       => $dtResult->SInvTotalAmount_SR, //change
                    'CurrencyRate_SR'             => $dtResult->CurrencyRate_SR, //change
                    'TransStatus_SR'           => $TransStatus_SR   ,
                    'CurrencySymbol'           => $dtResult->CurrencySymbol     
                        
                );
            }
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$data['company'],'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    
    public function sales_amount_item_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_sls_amount_item);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_amount_item, $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_sls_amount_item);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_amount_item, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_amount_item_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSalesAmountItem(){                
        $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_sales->pre_get_all_sales_amount_item();
        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_reloadDataSalesAmountItem(){     
        $resultData = $this->m_index_sales->get_all_sales_amount_item();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();
            $indexData = 0;
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function sales_amount_item_category_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_sls_amount_item_catgr);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_amount_item_catgr, $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_sls_amount_item_catgr);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_amount_item_catgr, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_amount_item_category_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSalesAmountItemCategory(){                 
        $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_sales->pre_get_all_sales_amount_item_category();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSalesAmountItemCategory(){      
        $resultData = $this->m_index_sales->get_all_sales_amount_item_category();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            //print_r($resultData);die();            
            $indexData = 0;
            foreach($resultData as $dtResult) {
                $TransStatus = get_status($dtResult->TransStatus);
                $resultData[$indexData]->SInvDate       = $this->m_index_sales->dateSqlToIndo($dtResult->SInvDate);
                $resultData[$indexData]->itemTransStatus= $dtResult->itemTransStatus==1?"Active":"Non Active";
                $resultData[$indexData]->TransStatus    = $TransStatus;
                $indexData++;
            }
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
    
    public function sales_order_for_dp_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_sls_order_for_dp);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_order_for_dp, $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_sls_order_for_dp);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_order_for_dp, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_order_for_dp_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }            
    public function pre_ajax_reloadDataSalesOrderForDP(){                
        $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_sales->pre_get_all_sales_order_for_dp();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSalesOrderForDP(){    
        $resultData = $this->m_index_sales->get_all_sales_order_for_dp();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
        
    public function sales_quotation_by_salesman_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_sq_by_salesman);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sq_by_salesman, $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_sq_by_salesman);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sq_by_salesman, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_quotation_by_salesman_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    public function pre_ajax_reloadDataSQBySalesman(){                   
        $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_sales->pre_get_all_sq_by_salesman();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSQBySalesman(){     
        $resultData = $this->m_index_sales->get_all_sq_by_salesman();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result = $resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
    
    public function sales_invoice_by_salesman_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_sinv_by_salesman);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sinv_by_salesman, $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_sinv_by_salesman);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sinv_by_salesman, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_invoice_by_salesman_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSalesInvoiceBySalesman(){                     
        $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_sales->pre_get_all_sales_invoice_by_salesman();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $result=$resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result, 'startdate'=>$txt_DateFrom,'enddate'=>$txt_DateTo, 'company'=>$companyName,'branch'=>$BranchCode);
        echo json_encode($callBack);
    }
    public function ajax_reloadDataSalesInvoiceBySalesman(){  
        $resultData = $this->m_index_sales->get_all_sales_invoice_by_salesman();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            $num_loop = 0;
            foreach($resultData as $dtResult) {
                $TransStatus_SI = get_status($dtResult->TransStatus_SI);
                $TransStatus_SR = get_status($dtResult->TransStatus_SR);
                $resultData[$num_loop]->SInvDate_SI = $this->m_index_sales->dateSqlToIndo($dtResult->SInvDate_SI);
                $resultData[$num_loop]->TransStatus_SI = $TransStatus_SI;
                $resultData[$num_loop]->SRecDate_SR = $this->m_index_sales->dateSqlToIndo($dtResult->SRecDate_SR);
                $resultData[$num_loop]->TransStatus_SR = $TransStatus_SR;
                $num_loop++;
            }
            $result=$resultData;
        }else{
            $result=[0];
        }
            
        $callBack =array('datas'=>$result);
        echo json_encode($callBack);
    }
    
    public function sales_return_by_customer_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_sls_return_by_cust);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_return_by_cust, $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_sls_return_by_cust);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_return_by_cust, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_return_by_customer_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }
    
    public function pre_ajax_reloadDataSalesReturnByCustomer(){                   
        $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_sales->pre_get_all_sales_return_by_customer();
        //echo $this->db->last_query();
        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_reloadDataSalesReturnByCustomer(){  
        $resultData = $this->m_index_sales->get_all_sales_return_by_customer();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function sales_return_by_item_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_sls_return_by_item);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_return_by_item, $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_sls_return_by_item);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_return_by_item, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_return_by_item_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataSalesReturnByItem(){                  
        $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_sales->pre_get_all_sales_return_by_item();
        //echo $this->db->last_query();
        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_reloadDataSalesReturnByItem(){  
        $resultData = $this->m_index_sales->get_all_sales_return_by_item();
        //echo $this->db->last_query();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }
            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
        
    public function sales_return_by_item_category_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_sls_return_by_item_cust);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_return_by_item_cust, $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_sls_return_by_item_cust);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_return_by_item_cust, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_return_by_item_category_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataSalesReturnByItemCategory(){ 
        $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_sales->pre_get_all_sales_return_by_item_category();
        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_reloadDataSalesReturnByItemCategory(){   
        $resultData = $this->m_index_sales->get_all_sales_return_by_item_category();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    
    public function sales_do_history_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_sls_do_history);
            if(!empty($arr_group_id)){
                $data['available_menu'] = array_unique($arr_group_id);
                if(!in_array($this->menu_id_sls_do_history, $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_sls_do_history);
                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id_sls_do_history, $group_menu);
                    $this->load->view('v_header');
                    $this->load->view('report/sales_report/v_sales_do_history_report', $data);
                    $this->load->view('v_footer');		
                }
            }else{
                redirect(base_url() . 'error', 'refresh');
            }
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }                    
    }    
    public function pre_ajax_reloadDataSalesDOHistory(){     
        $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_sales->pre_get_all_sales_do_history();
        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_reloadDataSalesDOHistory(){  
        $resultData = $this->m_index_sales->get_all_sales_do_history();
        if(count($resultData)>0){
            
        }else{
            $resultData=[0];
        }            
        $callBack =array('datas'=>$resultData);
        echo json_encode($callBack);
    }
    //============= FUNCTION PENDUKUNG =======================================//
    public function get_customer(){
        $CompanyCode    = $this->uri->segment(4); 
        $BranchCode     = $this->uri->segment(5); 
        if($BranchCode == null || $BranchCode == '') {
            $data = $this->m_index_sales->get_customer_by_company($CompanyCode);
        }else{
            $data = $this->m_index_sales->get_customer_by_branch($BranchCode);
        }    
        $data[]=array("BranchCode"=>"All","CustomerCode"=>"All","CustomerName"=>"All");
        echo json_encode($data);
    }
    public function get_item(){
        $CompanyCode    = $this->uri->segment(4); 
        $BranchCode     = $this->uri->segment(5); 
        if($BranchCode == null || $BranchCode == '') {
            $data = $this->m_index_sales->get_item_by_company($CompanyCode);
            //echo "itemcompany =".$this->db->last_query();
        }else{
            $data = $this->m_index_sales->get_item_by_branch($BranchCode);
            //echo "itembranch =".$this->db->last_query();
        }    
        
        $data[]=array("BranchCode"=>"All","ItemCode"=>"All","ItemDesc"=>"All");
        echo json_encode($data);
    }
}

