<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu
 *	Tanggal : 24-11-2014	
 *	Revisi 	: Dhidhik, 18 Maret 2016
 */
 
class Preference_others extends CI_Controller {

     public function __construct()
    {
	    parent::__construct();
	    $this->load->model('m_preference');
	}

    public function index(){
        $CompanyCode = $this->session->userdata('company');	
        $categoriID = 1;
        $categoriID2 = 2;
        
        $ExpenseCategoryID = $this->m_preference->get_sysothercategory_id($CompanyCode, $categoriID);	
        $ExpenseCategoryID2 = $this->m_preference->get_sysothercategory_id2($CompanyCode, $categoriID2);		
	
        $data['akun_payment_other'] = $this->m_preference->get_akun_payment_other($ExpenseCategoryID);	
        $data['akun_receipt_other'] = $this->m_preference->get_akun_receipt_other($ExpenseCategoryID);

        $data['akun_fix_asset1']    = $this->m_preference->get_akun_asset1_other($ExpenseCategoryID2);	
        $data['akun_fix_asset2']    = $this->m_preference->get_akun_asset2_other($ExpenseCategoryID2);		
		
        $this->load->view('v_header');
        $this->load->view('preference/view_preference_other', $data);
        $this->load->view('v_footer');
    }
	
	
	//PROCESS POST DATA	
    function proses_post_data_afiliasi(){
        try{            
            $categoriID = $this->input->post('categoriID');
            $CompanyCode = $this->session->userdata('company');
            
            $dataSend   = [];
            $dataSend['data_category_insert']   = [];     
            $dataSend['data_payment_insert']    = [];     
            $dataSend['data_payment_update']    = [];   
            $dataSend['data_receipt_insert']    = [];  
            $dataSend['data_receipt_update']    = [];
            $dataSend['categoriID']             = $categoriID;
            $dataSend['CompanyCode']            = $CompanyCode;
            
            $cek_categori = $this->m_preference->cek_categori_afiliasi($categoriID);
            if(empty($cek_categori)){
                $dataSend['data_category_insert'][] = array(
                        'CompanyCode' => $CompanyCode, 
                        'OtherCategoryID' => $categoriID, 
                        'LastUser' => $this->session->userdata('username')
                ); 
//                $this->m_preference->insert_category_afiliasi($data_category);
            }
            $ExpenseCategoryID = $this->m_preference->get_sysothercategory_id($CompanyCode, $categoriID);//masuk juga ke model

            $PaymentAfiliasi = $this->input->post('select_afiliasi_payment');
            $code_payment ="PaymentAfiliasi";

            $ReceiptAfiliasi = $this->input->post('select_afiliasi_receipt');
            $code_receipt ="ReceiptAfiliasi";

            $cek_akun_pay = $this->m_preference->cek_kode_on_payment_afiliasi($code_payment, $ExpenseCategoryID);
            if(empty($cek_akun_pay)){
                $dataSend['data_payment_insert'][] = array(
                    'SysOtherCategoryID' => $ExpenseCategoryID, 
                    'AccountNumber' => $PaymentAfiliasi,
                    'AccountCode' => $code_payment,				 
                    'LastUser' => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysother_payment_account($data_payment);
            } else {
                $dataSend['data_payment_update'][] = array(
                    'SysOtherCategoryID' => $ExpenseCategoryID, 
                    'AccountNumber' => $PaymentAfiliasi,
                    'AccountCode' => $code_payment,				 
                    'LastUser' => $this->session->userdata('username'),
                    'idcode'    => $code_payment
                );
//                $this->m_preference->update_sysother_payment_account($data_payment, $ExpenseCategoryID, $code_payment);
            }

            $cek_akun_receipt = $this->m_preference->cek_kode_on_receipt_afiliasi($code_receipt, $ExpenseCategoryID);
            if(empty($cek_akun_receipt)){
                $dataSend['data_receipt_insert'][] = array(
                    'SysOtherCategoryID' => $ExpenseCategoryID, 
                    'AccountNumber' => $ReceiptAfiliasi,
                    'AccountCode' => $code_receipt,				 
                    'LastUser' => $this->session->userdata('username')
                );
//                $this->m_preference->insert_sysother_receipt_account($data_receipt);
            }else {
                $dataSend['data_receipt_update'][] = array(
                    'SysOtherCategoryID' => $ExpenseCategoryID, 
                    'AccountNumber' => $ReceiptAfiliasi,
                    'AccountCode' => $code_receipt,				 
                    'LastUser' => $this->session->userdata('username'),
                    'idcode'    => $code_receipt
                );
//                $this->m_preference->update_sysother_receipt_account($data_receipt, $ExpenseCategoryID, $code_receipt);
            }

            //================ approval ======================================// //yang di remark masuk ke model semua
            $jobtitlecode = $this->input->post('jobtitle');
            $dataSend['jobtitlecode'] = $jobtitlecode;
//            $cek_approval = $this->m_preference->cek_approval_on_sysother_account($jobtitlecode, $ExpenseCategoryID);
//            $date_approval = date('Y-m-d'); 
//
//            if($jobtitlecode == 'MGR'){
//                    $data_approval = array(
//                            'AppLevel1Status' => 1,
//                            'AppLevel1Code' => $jobtitlecode,
//                            'AppLevel1Date' => $date_approval
//                    );
//
//                    $this->m_preference->update_approval_sysother_account($data_approval, $ExpenseCategoryID);
//            }
            //================ end approval ==================================//

//            $data['akun_payment_other'] = $this->m_preference->get_akun_payment_other($ExpenseCategoryID);	
//            $data['akun_receipt_other'] = $this->m_preference->get_akun_receipt_other($ExpenseCategoryID);	       
//
//            $data['akun_fix_asset1'] = $this->m_preference->get_akun_asset1_other($ExpenseCategoryID);	
//            $data['akun_fix_asset2'] = $this->m_preference->get_akun_asset2_other($ExpenseCategoryID);	
//
//            $result['status'] 	= true;
//            $result['message']	= "Preferences Berhasil Disimpan";
//            echo json_encode($result);        
            
            $rstDataSend = $this->m_preference->data_send_preference_other_afiliasi($dataSend);
            if($rstDataSend['status']){
                $data['akun_payment_other'] = $rstDataSend['akun_payment_other'];	
                $data['akun_receipt_other'] = $rstDataSend['akun_receipt_other'];	       
                $data['akun_fix_asset1']    = $rstDataSend['akun_fix_asset1'];	
                $data['akun_fix_asset2']    = $rstDataSend['akun_fix_asset2'];
                
                $result['status'] 	= true;
                $result['message']	= "Preferences Berhasil Disimpan";                
            }else{
                $result['status'] 	= false;
                $result['message']	= "Preferences Gagal Disimpan";
            }
            echo json_encode($result);
        }catch (Exception $e){            
            $result['status']   = false;
            $result['message']  = $e->getMessage();
            echo json_encode($result);
        }	
    }
	function proses_post_data_afiliasi_LAMA(){
	    $categoriID = $this->input->post('categoriID');
		$CompanyCode = $this->session->userdata('company');	
		
	    $data_category = array(
			'CompanyCode' => $CompanyCode, 
			'OtherCategoryID' => $categoriID, 
			'LastUser' => $this->session->userdata('username')
		); 
		
		$cek_categori = $this->m_preference->cek_categori_afiliasi($categoriID);
		if(empty($cek_categori)){
			$this->m_preference->insert_category_afiliasi($data_category);
		}
		
		$ExpenseCategoryID = $this->m_preference->get_sysothercategory_id($CompanyCode, $categoriID);
		
		$PaymentAfiliasi = $this->input->post('select_afiliasi_payment');
		$code_payment ="PaymentAfiliasi";
		
		$ReceiptAfiliasi = $this->input->post('select_afiliasi_receipt');
		$code_receipt ="ReceiptAfiliasi";
		
		$cek_akun_pay = $this->m_preference->cek_kode_on_payment_afiliasi($code_payment, $ExpenseCategoryID);
		
		if(empty($cek_akun_pay)){
		
			$data_payment = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $PaymentAfiliasi,
                 'AccountCode' => $code_payment,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->insert_sysother_payment_account($data_payment);
			
		} else {
		
			$data_payment = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $PaymentAfiliasi,
                 'AccountCode' => $code_payment,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->update_sysother_payment_account($data_payment, $ExpenseCategoryID, $code_payment);
			
		}
		
		$cek_akun_receipt = $this->m_preference->cek_kode_on_receipt_afiliasi($code_receipt, $ExpenseCategoryID);
		
		if(empty($cek_akun_receipt)){
			
			$data_receipt = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $ReceiptAfiliasi,
                 'AccountCode' => $code_receipt,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->insert_sysother_receipt_account($data_receipt);
			
		} else {
			
			$data_receipt = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $ReceiptAfiliasi,
                 'AccountCode' => $code_receipt,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->update_sysother_receipt_account($data_receipt, $ExpenseCategoryID, $code_receipt);
			
		}
		
		//approval
		$jobtitlecode = $this->input->post('jobtitle');
		$cek_approval = $this->m_preference->cek_approval_on_sysother_account($jobtitlecode, $ExpenseCategoryID);
		$date_approval = date('Y-m-d'); 
		
		if($jobtitlecode == 'MGR'){
			$data_approval = array(
				'AppLevel1Status' => 1,
				'AppLevel1Code' => $jobtitlecode,
				'AppLevel1Date' => $date_approval
			);
			
			$this->m_preference->update_approval_sysother_account($data_approval, $ExpenseCategoryID);
		}
		
        $data['akun_payment_other'] = $this->m_preference->get_akun_payment_other($ExpenseCategoryID);	
        $data['akun_receipt_other'] = $this->m_preference->get_akun_receipt_other($ExpenseCategoryID);	       
       
        $data['akun_fix_asset1'] = $this->m_preference->get_akun_asset1_other($ExpenseCategoryID);	
        $data['akun_fix_asset2'] = $this->m_preference->get_akun_asset2_other($ExpenseCategoryID);	
		
        $result['status'] 	= true;
        $result['message']	= "Preferences Berhasil Disimpan";
        echo json_encode($result);	
    }
//        $data['akun_payment_other'] = $this->m_preference->get_coa_account('Hutang Afiliasi', '2');
//        $data['akun_receipt_other'] = $this->m_preference->get_coa_account('Piutang Afiliasi', '2');
       
		
    function get_afiliasi_payment(){
        $company        = $this->session->userdata('company');		
        $afiliasi       = $this->m_preference->get_akun($company, $id='39');
        $afiliasi_data  = json_encode($afiliasi);
        
        echo "$afiliasi_data";
    }
	
    function get_afiliasi_receipt(){
        $company        = $this->session->userdata('company');		
        $receipt        =  $this->m_preference->get_akun($company, $id='11');
        $receipt_data   = json_encode($receipt);
        
        echo "$receipt_data";
    }
	
	//PROCESS POST DATA	
	function proses_post_data_asset(){
	    $categoriID = $this->input->post('categoriID');
		$CompanyCode = $this->session->userdata('company');	
		
	    $data_category = array(
			'CompanyCode' => $CompanyCode, 
			'OtherCategoryID' => $categoriID, 
			'LastUser' => $this->session->userdata('username')
		); 
		
		$cek_categori = $this->m_preference->cek_categori_asset($categoriID);
		if(empty($cek_categori)){
			$this->m_preference->insert_category_asset($data_category);
		}
		
		$ExpenseCategoryID = $this->m_preference->get_sysothercategory_id($CompanyCode, $categoriID);
		
		$asset1 = $this->input->post('select_fix_asset1');
		$code_asset1 ="Asset1";
		
		$asset2 = $this->input->post('select_fix_asset2');
		$code_asset2 ="Asset2";
		
		$cek_akun_asset = $this->m_preference->cek_kode_on_fix_asset($code_asset1, $ExpenseCategoryID);
		
		if(empty($cek_akun_asset)){
		
			$data_asset = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $asset1,
                 'AccountCode' => $code_asset1,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->insert_sysother_asset1_account($data_asset);
			
		} else {
		
			$data_asset = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $asset1,
                 'AccountCode' => $code_asset1,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->update_sysother_asset1_account($data_asset, $ExpenseCategoryID, $code_asset1);
			
		}
		
		$cek_akun_asset2 = $this->m_preference->cek_kode_on_fix_asset2($code_asset2, $ExpenseCategoryID);
		
		if(empty($cek_akun_asset2)){
			
			$data_asset2 = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $asset2,
                 'AccountCode' => $code_asset2,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->insert_sysother_asset2_account($data_asset2);
			
		} else {
			
			$data_asset2 = array(
				 'SysOtherCategoryID' => $ExpenseCategoryID, 
				 'AccountNumber' => $asset2,
                 'AccountCode' => $code_asset2,				 
				 'LastUser' => $this->session->userdata('username')
			);
			
			$this->m_preference->update_sysother_asset2_account($data_asset2, $ExpenseCategoryID, $code_asset2);
			
		}
		
		//approval
		$jobtitlecode = $this->input->post('jobtitle');
		$cek_approval = $this->m_preference->cek_approval_on_sysother_account($jobtitlecode, $ExpenseCategoryID);
		$date_approval = date('Y-m-d'); 
		
		if($jobtitlecode == 'MGR'){
			$data_approval = array(
				'AppLevel1Status' => 1,
				'AppLevel1Code' => $jobtitlecode,
				'AppLevel1Date' => $date_approval
			);
			
			$this->m_preference->update_approval_sysother_account($data_approval, $ExpenseCategoryID);
		}
		
		$data['akun_payment_other'] = $this->m_preference->get_akun_payment_other($ExpenseCategoryID);	
        $data['akun_receipt_other'] = $this->m_preference->get_akun_receipt_other($ExpenseCategoryID);	     
		
		$data['akun_fix_asset1'] = $this->m_preference->get_akun_asset1_other($ExpenseCategoryID);	
        $data['akun_fix_asset2'] = $this->m_preference->get_akun_asset2_other($ExpenseCategoryID);		
		
		$this->load->view('v_header');
		$this->load->view('preference/view_preference_other', $data);
		$this->load->view('v_footer');
	}
	
	function get_fix_asser1(){
		 $company = $this->session->userdata('company');		
		 $asset1 =  $this->m_preference->get_akun($company, $id='34');
		 $asset_data = json_encode($asset1);
		 echo"$asset_data";
	}
	
	function get_fix_asser2(){
		 $company = $this->session->userdata('company');		
		 $asset2 =  $this->m_preference->get_akun($company, $id='11');
		 $asset2_data = json_encode($asset2);
		 echo"$asset2_data";
	}
	
}

/* End of file preference_other.php */
/* Location: ./application/controllers/preference_other.php */