<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*	
 *	Author 	: Wahyu
 *	Tanggal : 18-10-2014	
 *	Revisi 	: Fachri, 7-04-2015
 */ 

class purchase_expense_real extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 235;
	
	// inisialisasi otomatis
	public $add = "add";
	public $edit = "edit";
	public $delete = "delete";
	public $to_preview = "to_preview";
	public $to_pdf = "to_pdf";
	public $to_excel = "to_excel";
	
	public function __construct() {
        parent::__construct();
		$this->load->model('m_menu');
		$this->load->model('m_expensecashbon_payment');
		$this->load->helper('message');
        $this->load->model('m_expense_cashbon');
		$this->load->model('custom/m_custom');
        $this->load->helper('convert_date');	
        $this->load->helper('status');
        date_default_timezone_set('Asia/Jakarta');		
	}	
	
	// Function Index	
	public function index() {
		// pengecekan otoritas
		
		
		// reset session 
		
		
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);		
		// set data
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
		
		$CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');
		
		$data['exp_payment_number']  = $this->m_expensecashbon_payment->create_epay_number(date('d-m-Y'));
		$department = $this->m_custom->get_department_by_name('EXIM');
		$data['department_code'] = $department[0]->DepartmentCode;
		$data['date_now'] = date('d-m-Y');
		// set for view
		$this->load->view('v_header');
		$this->load->view('expense_cashbon/v_purchase_expense_real', $data);
		$this->load->view('v_footer');
	}
	
	public function ajax_list_cashbon($EmpCode){
		$replace_emp_code = str_replace('_','-', $EmpCode);
		$result =  $this->m_expensecashbon_payment->get_list_cashbon($replace_emp_code);
		
		//echo $this->db->last_query();
		$hasil = '';
	    foreach($result as $row) {
			$EBonDate    = convert_to_dmy($row->EBonDate);
			
			if($row->EBonLockStatus == 1){
				$Lock = "Lock";
			}
			else{
				$Lock = "Un Lock";
			}
			
			$hasil[] = array(
				'EBonNumber' => $row->EBonNumber,
				'EBonDate' => $EBonDate,
				'EBonAmount' => $row->EBonAmount,
				'CurrencyCode' => $row->CurrencyCode,
				'CurrencyRate' => $row->CurrencyRate,
				'BankPayment' => $row->BankPayment,
				'EBonAccountNumber' => $row->EBonAccountNumber,
				'EBonLockStatus' => $Lock
			); 
		}
		
		echo json_encode($hasil);
	}
	
	public function get_expense_group($ebon_akun){
		$replace_akun = str_replace('_','-',$ebon_akun);
	
		$expense_group =  $this->m_expensecashbon_payment->get_list_expense_group($replace_akun);
	    echo"$expense_group";
	}
	
	public function ajax_list_item_group_expense($Groupid){
		$list_item_expense_group =  $this->m_expensecashbon_payment->get_list_item_expense_group($Groupid);
	    echo"$list_item_expense_group";
	}
	
	public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status']   = false;
        $result['message']  = get_notification_transaksi($this->name, $modus, 0);
        
        echo json_encode($result);
        die();
    }
	
	public function insert(){
	    //set post data
		$BranchCode 	= $this->session->userdata('branch');		
		$curren_code 		= $this->input->post('curren_code');
		
		$expCategoryID       	= $this->input->post('ExpGroupId');
		$account_exp_payable 	= $this->m_expensecashbon_payment->get_account_exp_payable($expCategoryID/*, $curren_code*/);
		$account_adv_cashbon 	= $this->m_expensecashbon_payment->get_account_advance_cashbon($expCategoryID/*, $curren_code*/);
		
		$account_exp_relize 	= $this->m_expensecashbon_payment->get_account_exp_relize($expCategoryID/*, $curren_code*/);
		
		if(!empty($account_exp_payable) || !empty($account_adv_cashbon) || !empty($account_exp_relize) ){
			$result_insert 		= 0;
			$DepartmentCode 	= $this->input->post('cmb_departement');
			$EmployeeCode 		= $this->input->post('cmb_employee');
			$EPayDate 			= date("Y-m-d", strtotime($this->input->post('txt_payment_date')));		                   
			$EPayNumber 		= $this->input->post('txt_payment_no');
			$EBonNumber 		= $this->input->post('txt_ebon_number');
			$EPayDesc 	    	= $this->input->post('txa_memo');
			$total_payment 	    = $this->input->post('total_payment');
			$txt_cashbon_amount = $this->input->post('txt_cashbon_amount');
			$txt_rate 	    	= $this->input->post('txt_rate');
			$total_exceed 	    = $this->input->post('total_exceed');
			$txt_exceed_hidden 	= $this->input->post('txt_exceed_hidden_idr');
			$curren_code_casbon = $this->input->post('curren_code_casbon');
			$curren_code 		= $this->input->post('curren_code');
			$rate_casbon	 	= $this->input->post('rate_casbon');
			$ExpGroupId	 		= $this->input->post('ExpGroupId');
			
			$total_convert 		= $total_payment * $txt_rate;
			$total_cashbon 		= $txt_cashbon_amount * $rate_casbon;
			
			$total_exceed_convert = $this->input->post('total_exceed_convert');
			
			//perhitungan relize selisih
			$data_payment = array(
				'BranchCode' 		=> $BranchCode,
				'DepartmentCode' 	=> $DepartmentCode,
				'EmployeeCode' 		=> $EmployeeCode,
				'EPayDate' 			=> $EPayDate,
				'EPayNumber' 		=> $EPayNumber,
				'EBonNumber' 		=> $EBonNumber,
				'EPayDesc' 			=> $EPayDesc,
				'CurrencyCode' 		=> $curren_code_casbon,
				'EPayAmount' 		=> $total_payment,
				'EPayRate' 			=> $txt_rate,
				'TransStatus' 		=> 4,
				'LastUser' 			=> $this->session->userdata('username')
			);
			$this->db->trans_begin();
			$result_insert = $this->m_expensecashbon_payment->insert_payment_cashbon($data_payment);
			if($result_insert == 0) {$this->trans_rollback('insert');}
			
			$data_payment 	= $this->input->post('detail_payment');
			$detail_pay 	= json_decode($data_payment);
			
			foreach($detail_pay as $details){
			
				$ExpDetailID 	= $details->ExpDetailID;
				$ExpDetailName 	= $details->ExpDetailName;
				$ExpDetailDesc 	= $details->ExpDetailDesc;
				$ItemAmount 	= $details->ItemAmount;
				if($curren_code_casbon == "IDR"){
					$TotalAmount = $ItemAmount * 1;
				}
				else{
					$TotalAmount = $ItemAmount * $txt_rate;
				}
				
				$detail_payment = array(
					'EPayNumber' 			=> $EPayNumber,
					'ExpCategoryID' 		=> $ExpGroupId,
					'ExpCategoryName' 		=> $ExpDetailName,
					'ExpenseDesc'  			=> $ExpDetailDesc,
					'ExpenseAmount' 		=> $ItemAmount,
					'ExpenseTotalAmount' 	=> $TotalAmount,
					'LastUser' 				=> $this->session->userdata('username')
				);
			
				$result_insert = $this->m_expensecashbon_payment->insert_detail_payment_cashbon($detail_payment);
				if($result_insert == 0) {$this->trans_rollback('insert');}
			}
			
			$cek_selisih = $txt_cashbon_amount - $total_payment;
			if($cek_selisih > 0){
			   //CRV
			   $data_crv 	= $this->input->post('detail_crcv');
			   $detail_crv 	= json_decode($data_crv);
			   
			   foreach($detail_crv as $row){
					$AccountNumber = $row->BankCode;
					//$CrCvDate = date("Y-m-d", strtotime($row->CrCvDate));	
					
					$AmountCRV_Rp 	= $row->AmountDiff; 	
					$pecah_acrv   	= $AmountCRV_Rp;
					$AmountCRV    	= $pecah_acrv;
					
					$PaymentAmount 	= $AmountCRV; 				
					
					$BranchCode 	= $this->session->userdata('branch');
					$bulan 			= date('m');
					$tahun 			= date('y');
					$cek_tahun 		= date('Y');
					$kode 			= "ER";
					
					$bank_id 			= $this->m_expensecashbon_payment->get_bank_id($AccountNumber);
					$bank_code_masuk 	= $this->m_expensecashbon_payment->get_bank_code_keluar($bank_id);
					
					$crv_no 			= $this->m_expensecashbon_payment->count_crv_number($bulan, $cek_tahun);
					
					$crv_number = $this->m_expensecashbon_payment->create_crv_number($bank_code_masuk);
					
					$detail_ppaycrv = array(
						'EPRealDate' 	=> $EPayDate,
						'EPRealNumber' 	=> $crv_number,
						'EPayDate'  	=> $EPayDate,
						'EPayNumber' 	=> $EPayNumber,
						'BankReceipt' 	=> $AccountNumber,
						'EPRealAmount' 	=> $PaymentAmount,
						'CurrencyCode' 	=> $curren_code,
						'EPRealExpense' => $total_payment,
						/*'CurrencyRate' => ,*/
						'EPRealTransType' => 1,
						'LastUser' 		=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_detail_crv($detail_ppaycrv);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_exp_crv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_payable, 
						'EPRealSaldoDebet' 		=> $total_convert,
						'AmountCurrency' 		=> $total_convert,
						'EPRealSignStatus' 		=> 0,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_crv_debit($data_journal_exp_crv_debit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_bank_crv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $AccountNumber, 
						'EPRealSaldoDebet' 		=> $total_exceed_convert,
						'AmountCurrency' 		=> $total_exceed,
						'EPRealDesc' 			=> $EPayDesc,
						'EPRealSignStatus' 		=> 0,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_bank_crv_debit($data_journal_bank_crv_debit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					//----------------------------------------------------------debit
					$data_journal_crv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_adv_cashbon, 
						'EPRealSaldoCredit' 	=> $total_cashbon,
						'AmountCurrency' 		=> $txt_cashbon_amount,
						'EPRealSignStatus' 		=> 1,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_crv_credit($data_journal_crv_credit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					if($rate_casbon > $txt_rate){
						$total_amount_relize = ($total_exceed_convert + $total_convert);
						$relize_amount 		= $total_cashbon - $total_amount_relize;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $crv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoDebet' 		=> $relize_amount,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 0,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					else{
						$total_amount_relize = ($total_exceed_convert + $total_convert);
						$relize_amount 		 = $total_amount_relize - $total_cashbon;
						
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $crv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoCredit' 	=> $relize_amount,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 0,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					
					if($relize_amount != 0){
						$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_relize_credit($data_journal_relize_credit);
						if($result_insert == 0) {$this->trans_rollback('insert');}
					}
					//----------------------------------end debit--------------------------------
			   }
			}
			
			if($cek_selisih < 0) {
			   //CPV
			   $data_cpv=$this->input->post('detail_crcv');
			   $detail_cpv=json_decode($data_cpv);
			   foreach($detail_cpv as $row){
					$AccountNumber = $row->BankCode;
					//$CrCvDate = date("Y-m-d", strtotime($row->CrCvDate));
					
					$AmountCRV_Rp 	= $row->AmountDiff; 	
					$pecah_acrv   	= $AmountCRV_Rp;
					$AmountCRV    	= $pecah_acrv;
					
					$PaymentAmount 	= $AmountCRV; 	
										
					$BranchCode 	= $this->session->userdata('branch');
					$bulan 			= date('m');
					$tahun 			= date('y');
					$cek_tahun 		= date('Y');
					$kode 			= "ER";
					
					$bank_id 		= $this->m_expensecashbon_payment->get_bank_id($AccountNumber);
					$bank_code_keluar = $this->m_expensecashbon_payment->get_bank_code_keluar($bank_id);
					
					$cpv_no = $this->m_expensecashbon_payment->count_cpv_number($bulan, $cek_tahun);
					
					$cpv_number = $this->m_expensecashbon_payment->create_crv_number($bank_code_keluar);
					
					$detail_ppaycpv = array(
						'EPRealDate' 		=> $EPayDate,
						'EPRealNumber' 		=> $cpv_number,
						'EPayDate'  		=> $EPayDate,
						'EPayNumber' 		=> $EPayNumber,
						'BankReceipt' 		=> $AccountNumber,
						'EPRealAmount' 		=> $PaymentAmount,
						'EPRealExpense' 	=> $total_payment,
						'CurrencyCode' 		=> $curren_code,
						'EPRealTransType' 	=> 2,
						'LastUser' 			=> $this->session->userdata('username')
					);
				
					$result_insert = $this->m_expensecashbon_payment->insert_detail_cpv($detail_ppaycpv);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_exp_cpv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_payable, 
						'EPRealSaldoDebet' 		=> $total_convert,
						'AmountCurrency' 		=> $total_convert,
						'EPRealSignStatus' 		=> 0,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_cpv_debit($data_journal_exp_cpv_debit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_bank_cpv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $AccountNumber, 
						'EPRealSaldoCredit' 	=> $total_exceed_convert,
						'AmountCurrency' 		=> $total_exceed,
						'EPRealSignStatus' 		=> 1,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_bank_cpv_credit($data_journal_bank_cpv_credit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_cpv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_adv_cashbon, 
						'EPRealSaldoCredit' 	=> $total_cashbon,
						'EPRealSignStatus' 		=> 1,
						'AmountCurrency' 		=> $txt_cashbon_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_cpv_credit($data_journal_cpv_credit);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					if($rate_casbon > $txt_rate){
						$total_amount_relize = ($total_cashbon + $total_exceed_convert);
						$relize_amount 		 = $total_amount_relize - $total_convert;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $cpv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoDebet' 		=> $relize_amount,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
						
					}
					else{
						$total_amount_relize = ($total_cashbon + $total_exceed_convert);
						$relize_amount 		 = $total_convert - $total_amount_relize;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $cpv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoCredit' 	=> $relize_amount,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					
					if($relize_amount != 0){
						$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_crv_credit($data_journal_relize_credit);
						if($result_insert == 0) {$this->trans_rollback('insert');}
					}
			   }
			}
			
			if($cek_selisih == 0){
				//journal payment					
				$BranchCode 	= $this->session->userdata('branch');
				$bulan 			= date('m');
				$tahun 			= date('y');
				$cek_tahun 		= date('Y');
				$kode 			= "ER";
				$bank_code_masuk  = "";
				$cpv_number = $this->m_expensecashbon_payment->create_crv_number($bank_code_masuk);
				
				$detail_ppaycpv = array(
					'EPRealDate' 		=> $EPayDate,
					'EPRealNumber' 		=> $cpv_number,
					'EPayDate'  		=> $EPayDate,
					'EPayNumber' 		=> $EPayNumber,
					'BankReceipt' 		=> 0,
					'EPRealAmount' 		=> 0,
					'EPRealExpense' 	=> 0,
					'CurrencyCode' 		=> $curren_code_casbon,
					'EPRealTransType' 	=> 2,
					'LastUser' 			=> $this->session->userdata('username')
				);
			
				$result_insert = $this->m_expensecashbon_payment->insert_detail_cpv($detail_ppaycpv);
				if($result_insert == 0) {$this->trans_rollback('insert');}
				
				$data_journal_exp_debit = array(
					'EPRealDate' 			=> $EPayDate,
					'EPRealNumber' 			=> $EPayNumber,
					'EPRealNumber_BOut' 	=> $EPayNumber,
					'EPRealAccountNumber' 	=> $account_exp_payable, 
					'EPRealSaldoDebet' 		=> $total_convert,
					'AmountCurrency' 		=> $total_convert,
					'EPRealSignStatus' 		=> 0,
					'EPRealDesc' 			=> $this->input->post('txa_memo'),
					'LastUser' 				=> $this->session->userdata('username')
				);
				
				$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_payment_debit($data_journal_exp_debit);
				if($result_insert == 0) {$this->trans_rollback('insert');}
				
				$data_journal_exp_credit = array(
					'EPRealDate' 			=> $EPayDate,
					'EPRealNumber' 			=> $EPayNumber,
					'EPRealNumber_BOut' 	=> $EPayNumber,
					'EPRealAccountNumber' 	=> $account_adv_cashbon, 
					'EPRealSaldoCredit' 	=> $total_cashbon,
					'EPRealSignStatus' 		=> 1,
					'AmountCurrency' 		=> $txt_cashbon_amount,
					'EPRealDesc' 			=> $this->input->post('txa_memo'),
					'LastUser' 				=> $this->session->userdata('username')
				);
				
				$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_payment_credit($data_journal_exp_credit);	
				if($result_insert == 0) {$this->trans_rollback('insert');}	
				
				if($rate_casbon > $txt_rate){
					
					$relize_kosong =  $total_cashbon - $total_convert;
					
					$data_journal_relize_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $EPayNumber,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_relize, 
						'EPRealSaldoDebet' 		=> $relize_kosong,
						'AmountCurrency' 		=> $relize_kosong,
						'EPRealSignStatus' 		=> 1,
						'EPRealDesc' 			=> $this->input->post('txa_memo'),
						'LastUser' 				=> $this->session->userdata('username')
					);
				}
				else{
					$relize_kosong =  $total_convert - $total_cashbon;
					
					$data_journal_relize_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $EPayNumber,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_relize, 
						'EPRealSaldoCredit' 	=> $relize_kosong,
						'AmountCurrency' 		=> $relize_kosong,
						'EPRealSignStatus' 		=> 0,
						'EPRealDesc' 			=> $this->input->post('txa_memo'),
						'LastUser' 				=> $this->session->userdata('username')
					);
				}
				if($relize_kosong != 0){
					$result_insert = $this->m_expensecashbon_payment->insert_journal_expense_crv_credit($data_journal_relize_credit);
					if($result_insert == 0) {$this->trans_rollback('insert');}	
				}
			}
			
			$status_cashbon = array('EBonLockStatus' => 1);
			$this->m_expensecashbon_payment->update_status_cashbon($EBonNumber, $status_cashbon);
			
			if($this->db->trans_status() == 0) {
				$this->db->trans_rollback();
				$result['status'] = false;
				$result['message'] = get_notification_transaksi('Expense Payment Cashbon', 'insert', 0);
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = get_notification_transaksi('Expense Payment Cashbon', 'insert', 1);
			}	

			echo json_encode($result);
		} 
		  else{
			$result['status'] = false;
		    $result['message'] ="Preference Expense Payment Cashbon Belum Disetting";
			echo json_encode($result);
		}		
	}
	
	public function edit(){
		// get link from tbl_menu 
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);		
		// set data
		$CompanyCode = $this->session->userdata('company');
		$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);

		// inisialisasi additional parameter
        $id = str_replace($data['menu_link'].'/edit/',"",$this->uri->uri_string());
		$data['detail'] = $this->m_expensecashbon_payment->get_detail($id);
		$ebonNumber = $this->m_expensecashbon_payment->get_cashbon_number($id);
		$data['amount_cashbon'] = $this->m_expensecashbon_payment->get_amount_cashbon($ebonNumber);
		$data['ebon_date'] = $this->m_expensecashbon_payment->get_date_cashbon($ebonNumber);
		$data['bank_paymanet'] = $this->m_expensecashbon_payment->get_bank_cashbon($ebonNumber);
		$data['amount_expensepay'] = $this->m_expensecashbon_payment->get_amount_expense_payment($id);

		// set for view
		$this->load->view('v_header');
		$this->load->view('expense_cashbon/v_edit_purchase_expense_real', $data);
		$this->load->view('v_footer');
	}
	
	public function ajax_detail_payment($id){
	    $no_epay 		= str_replace(".","/", $id);
		$number 	= str_replace("_","-", $no_epay); 
		$list_detail = $this->m_expensecashbon_payment->get_detail_item($number);
		
		echo"$list_detail";
	}
	
	public function ajax_detail_actual($id){
	    $no_epay = str_replace(".","/", $id);
		$number 	= str_replace("_","-", $no_epay); 
		$list_actual = $this->m_expensecashbon_payment->get_detail_actual($number);
		
		foreach($list_actual as $row) {
			
			$acc_no = $row->AccountNumber;
			$acc_name = $this->m_expensecashbon_payment->get_acc_name($acc_no);
			
			if($row->EPRealTransType == 1){
				$total_exceed = floatval($row->EBonAmount) - floatval($row->EPayAmount);
			}
			else{
				$total_exceed = floatval($row->EPayAmount) - floatval($row->EBonAmount);
			}
			
			$CrCvDate = convert_to_dmy($row->CrCvDate);
			$hasil[] = array(
				'CrCvDate' 		=> $CrCvDate,
				'AmountCRV' 	=> $row->EPayAmount,
				'PaymentAmount' => $row->EBonAmount,
				'AmountDiff' 	=> $row->AmountCRV,
				'BankName' 		=> $acc_name,
				'BankCode' 		=> $acc_no
			); 
		}
		
		echo json_encode($hasil);
	}
	
	public function update(){
		
	    //set post data
		$BranchCode 		= $this->session->userdata('branch');	
		$DepartmentCode 	= $this->input->post('cmb_departement');
		$EmployeeCode 		= $this->input->post('cmb_employee');
		$EPayDate 			= date("Y-m-d", strtotime($this->input->post('txt_payment_date')));		                   
		$EPayNumber 		= $this->input->post('txt_payment_no');
		$EBonNumber 		= $this->input->post('txt_ebon_number');
		$EPayDesc 	    	= $this->input->post('txa_memo');
        $total_payment 	    = $this->input->post('total_payment');
        $txt_cashbon_amount = $this->input->post('txt_cashbon_amount');
		$txt_rate 	    	= $this->input->post('txt_rate');
		$curren_code 	    = $this->input->post('curren_code');
		$txt_total_convert 	= $this->input->post('txt_total_convert');
		
		$get_casbon 		= $this->m_expensecashbon_payment->get_rate_cashbon($EBonNumber);
		$total_casbon_amount = $txt_cashbon_amount * $get_casbon;
		$total_paymen_amount = $total_payment * $txt_rate;
		
		$data_payment = array(
			'BranchCode' 		=> $BranchCode,
			'DepartmentCode' 	=> $DepartmentCode,
			'EmployeeCode'	 	=> $EmployeeCode,
			'EPayDate' 			=> $EPayDate,
			'EBonNumber' 		=> $EBonNumber,
			'EPayAmount' 		=> $total_payment,
			'EPayRate' 			=> $txt_rate,
			'EPayDesc' 			=> $EPayDesc,
			'LastUser' 			=> $this->session->userdata('username')
		);
		
		$this->db->trans_begin();
		
		$result_update = $this->m_expensecashbon_payment->update_payment_cashbon($data_payment, $EPayNumber);
		if($result_update == 0) {$this->trans_rollback('update');}
		
		$data_payment 	= $this->input->post('detail_payment');
		$detail_pay 	= json_decode($data_payment);
		
		foreach($detail_pay as $details){
			$ExpDetailName 	= $details->ExpDetailName;
			$ExpDetailDesc 	= $details->ExpDetailDesc;
			$ItemAmount 	= $details->ItemAmount;
			$EPayDetailID 	= $details->EPayDetailID;
			
			if($curren_code == "IDR"){
				$TotalAmount = $ItemAmount * 1;
			}
			else{
				$TotalAmount = $ItemAmount * $txt_rate;
			}
				
			$detail_payment = array(
				'EPayNumber' 		=> $EPayNumber,
				'ExpCategoryID' 	=> $details->ExpDetailID,
				'ExpCategoryName' 	=> $ExpDetailName,
				'ExpenseDesc'  		=> $ExpDetailDesc,
			    'ExpenseAmount' 	=> $ItemAmount,
				'ExpenseTotalAmount' => $TotalAmount,
				'LastUser' 			=> $this->session->userdata('username')
			);
		
			$result_update = $this->m_expensecashbon_payment->update_detail_payment_cashbon($detail_payment, $EPayDetailID);
			if($result_update == 0) {$this->trans_rollback('update');}
			
			@$CategoryAmount[$details->ExpDetailID] += $ItemAmount;
		}
		
		$cek_selisih = $txt_cashbon_amount - $total_payment;
		foreach($CategoryAmount as $key => $value){
			if($cek_selisih > 0){
			   //CRV
			   $data_crv 	= $this->input->post('detail_crcv');
			   $detail_crv 	= json_decode($data_crv);
			   
			   foreach($detail_crv as $row){
					$AccountNumber 	= $row->BankCode;
					$convert_to_idr = $row->AmountDiff * $txt_rate;
					
					$AmountCRV_Rp 	= $row->AmountDiff; 	
					$pecah_acrv   	= $AmountCRV_Rp;
					$AmountCRV    	= $pecah_acrv;
					
					$PaymentAmount 	= $AmountCRV; 				
					
					$BranchCode 	= $this->session->userdata('branch');
					$bulan 			= date('m');
					$tahun 			= date('y');
					$cek_tahun 		= date('Y');
					$kode 			= "ER";
					
					$bank_id 			= $this->m_expensecashbon_payment->get_bank_id($AccountNumber);
					$bank_code_masuk 	= $this->m_expensecashbon_payment->get_bank_code_keluar($bank_id);
					
					$crv_no 			= $this->m_expensecashbon_payment->count_crv_number($bulan, $cek_tahun);
					$crv_number 		= $this->m_expensecashbon_payment->create_crv_number($bank_code_masuk);
					
					$result_update = $this->m_expensecashbon_payment->delete_detail_crv($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$detail_ppaycrv = array(
						'EPRealDate' 		=> $EPayDate,
						'EPRealNumber'	 	=> $crv_number,
						'EPayDate'  		=> $EPayDate,
						'EPayNumber' 		=> $EPayNumber,
						'BankReceipt' 		=> $AccountNumber,
						'EPRealAmount' 		=> $PaymentAmount,
						'EPRealExpense' 	=> $total_payment,
						'CurrencyCode' 		=> $curren_code,
						'EPRealTransType' 	=> 1,
						'LastUser' 			=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_detail_crv($detail_ppaycrv);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$result_update = $this->m_expensecashbon_payment->delete_journal_expense_crv($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$account_exp_payable = $this->m_expensecashbon_payment->get_account_exp_payable($key);
		
					$account_adv_cashbon = $this->m_expensecashbon_payment->get_account_advance_cashbon($key);
					$account_exp_relize 	= $this->m_expensecashbon_payment->get_account_exp_relize($key);
					
					$data_journal_exp_crv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_payable, 
						'EPRealSaldoDebet' 		=> $total_paymen_amount,
						'EPRealSaldoCredit' 	=> 0,
						'EPRealSignStatus' 		=> 0,
						'AmountCurrency' 		=> $total_paymen_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_crv_debit($data_journal_exp_crv_debit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$data_journal_bank_crv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $AccountNumber, 
						'EPRealSaldoDebet' 		=> $convert_to_idr,
						'EPRealSaldoCredit' 	=> 0,
						'EPRealSignStatus' 		=> 0,
						'AmountCurrency' 		=> $AmountCRV,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_bank_crv_debit($data_journal_bank_crv_debit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$data_journal_crv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $crv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_adv_cashbon, 
						'EPRealSaldoCredit' 	=> $total_casbon_amount,
						'EPRealSaldoDebet' 		=> 0,
						'EPRealSignStatus' 		=> 1,
						'AmountCurrency' 		=> $txt_cashbon_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_crv_credit($data_journal_crv_credit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					
					if($get_casbon > $txt_rate){
						$total_amount_relize = ($convert_to_idr + $total_paymen_amount);
						$relize_amount 		= $total_casbon_amount - $total_amount_relize;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $crv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoDebet' 		=> $relize_amount,
							'EPRealSaldoCredit' 	=> 0,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 0,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					else{
						$total_amount_relize = ($convert_to_idr + $total_paymen_amount);
						$relize_amount 		 = $total_amount_relize - $total_casbon_amount;
						
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $crv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoCredit' 	=> $relize_amount,
							'EPRealSaldoDebet' 		=> 0,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 0,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					
					if($relize_amount != 0){
						$result_update = $this->m_expensecashbon_payment->insert_journal_expense_relize_credit($data_journal_relize_credit);
						if($result_update == 0) {$this->trans_rollback('update');}
					}
			   }
			}
			
			if($cek_selisih < 0) {
			   //CPV
			   $data_cpv=$this->input->post('detail_crcv');
			   $detail_cpv=json_decode($data_cpv);
			   foreach($detail_cpv as $row){
					$AccountNumber 	= $row->BankCode;
					
					$AmountCRV_Rp 	= $row->AmountDiff;
						
					$pecah_acrv   	= $AmountCRV_Rp;
					$AmountCRV    	= $pecah_acrv;
					
					$PaymentAmount 	= $AmountCRV; 	
					$convert_to_idr = $AmountCRV * $txt_rate;
					
					$BranchCode 	= $this->session->userdata('branch');
					$bulan 			= date('m');
					$tahun 			= date('y');
					$cek_tahun 		= date('Y');
					$kode 			= "ER";
					
					$bank_id 			= $this->m_expensecashbon_payment->get_bank_id($AccountNumber);
					$bank_code_keluar 	= $this->m_expensecashbon_payment->get_bank_code_keluar($bank_id);
					
					$cpv_no 			= $this->m_expensecashbon_payment->count_cpv_number($bulan, $cek_tahun);
					
					$cpv_number 		= $this->m_expensecashbon_payment->create_crv_number($bank_code_keluar);
					
					$result_update = $this->m_expensecashbon_payment->delete_detail_cpv($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$detail_ppaycpv = array(
						'EPRealDate' 		=> $EPayDate,
						'EPRealNumber' 		=> $cpv_number,
						'EPayDate'  		=> $EPayDate,
						'EPayNumber' 		=> $EPayNumber,
						'BankReceipt' 		=> $AccountNumber,
						'EPRealAmount' 		=> $PaymentAmount,
						'EPRealExpense' 	=> $total_payment,
						'CurrencyCode' 		=> $curren_code,
						'EPRealTransType' 	=> 2,
						'LastUser' 			=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_detail_cpv($detail_ppaycpv);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$result_update = $this->m_expensecashbon_payment->delete_journal_expense_crv($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$account_exp_payable = $this->m_expensecashbon_payment->get_account_exp_payable($key);
					$account_adv_cashbon = $this->m_expensecashbon_payment->get_account_advance_cashbon($key);
					$account_exp_relize 	= $this->m_expensecashbon_payment->get_account_exp_relize($key);
					
					$data_journal_exp_cpv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_payable, 
						'EPRealSaldoDebet' 		=> $total_paymen_amount,
						'EPRealSaldoCredit' 	=> 0,
						'EPRealSignStatus' 		=> 0,
						'AmountCurrency' 		=> $total_paymen_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_cpv_debit($data_journal_exp_cpv_debit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$data_journal_bank_cpv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $AccountNumber, 
						'EPRealSaldoCredit' 	=> $convert_to_idr,
						'EPRealSaldoDebet' 		=> 0,
						'EPRealSignStatus' 		=> 1,
						'AmountCurrency' 		=> $AmountCRV,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_bank_cpv_credit($data_journal_bank_cpv_credit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$data_journal_cpv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $cpv_number,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_adv_cashbon, 
						'EPRealSaldoCredit' 	=> $total_casbon_amount,
						'EPRealSaldoDebet' 		=> 0,
						'EPRealSignStatus' 		=> 0,
						'AmountCurrency' 		=> $txt_cashbon_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_cpv_credit($data_journal_cpv_credit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					if($get_casbon > $txt_rate){
						$total_amount_relize = ($total_casbon_amount + $convert_to_idr);
						$relize_amount 		 = $total_amount_relize - $total_paymen_amount;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $cpv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoDebet' 		=> $relize_amount,
							'EPRealSaldoCredit' 	=> 0,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
						
					}
					else{
						$total_amount_relize = ($total_casbon_amount + $convert_to_idr);
						$relize_amount 		 = $total_paymen_amount - $total_amount_relize;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $cpv_number,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoCredit' 	=> $relize_amount,
							'EPRealSaldoDebet' 		=> 0,
							'AmountCurrency' 		=> $relize_amount,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					
					if($relize_amount != 0){
						$result_update = $this->m_expensecashbon_payment->insert_journal_expense_relize_credit($data_journal_relize_credit);
						if($result_update == 0) {$this->trans_rollback('update');}
					}
			   }
			}
		
				
			if($cek_selisih == 0){
					//journal payment
					$result_update = $this->m_expensecashbon_payment->delete_journal_payment($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$result_update = $this->m_expensecashbon_payment->delete_detail_cpv($EPayNumber);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$account_exp_payable = $this->m_expensecashbon_payment->get_account_exp_payable($key);
					$account_adv_cashbon = $this->m_expensecashbon_payment->get_account_advance_cashbon($key);
					$account_exp_relize 	= $this->m_expensecashbon_payment->get_account_exp_relize($key);
					
					$BranchCode 	= $this->session->userdata('branch');
					$bulan 			= date('m');
					$tahun 			= date('y');
					$cek_tahun 		= date('Y');
					$kode 			= "ER";
					$bank_code_masuk  = "";
					$real_kosong = $this->m_expensecashbon_payment->create_crv_number($bank_code_masuk);
					
					$detail_ppaycpv = array(
						'EPRealDate' 		=> $EPayDate,
						'EPRealNumber' 		=> $real_kosong,
						'EPayDate'  		=> $EPayDate,
						'EPayNumber' 		=> $EPayNumber,
						'EPRealAmount' 		=> 0,
						'BankReceipt' 		=> 0,
						'EPRealExpense' 	=> 0,
						'CurrencyCode' 		=> $curren_code,
						'EPRealTransType' 	=> 2,
						'LastUser' 			=> $this->session->userdata('username')
					);
				
					$result_insert = $this->m_expensecashbon_payment->insert_detail_cpv($detail_ppaycpv);
					if($result_insert == 0) {$this->trans_rollback('insert');}
					
					$data_journal_exp_cpv_debit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $EPayNumber,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_exp_payable, 
						'EPRealSaldoDebet' 		=> $total_paymen_amount,
						'EPRealSaldoCredit' 	=> 0,
						'AmountCurrency' 		=> $total_paymen_amount,
						'EPRealDesc' 			=> $EPayDesc,
						'EPRealSignStatus' 		=> 0,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_cpv_debit($data_journal_exp_cpv_debit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					$data_journal_crv_credit = array(
						'EPRealDate' 			=> $EPayDate,
						'EPRealNumber' 			=> $EPayNumber,
						'EPRealNumber_BOut' 	=> $EPayNumber,
						'EPRealAccountNumber' 	=> $account_adv_cashbon, 
						'EPRealSaldoCredit' 	=> $total_casbon_amount,
						'EPRealSaldoDebet' 		=> 0,
						'AmountCurrency' 		=> $txt_cashbon_amount,
						'EPRealSignStatus' 		=> 1,
						'EPRealDesc' 			=> $EPayDesc,
						'LastUser' 				=> $this->session->userdata('username')
					);
					
					$result_update = $this->m_expensecashbon_payment->insert_journal_expense_crv_credit($data_journal_crv_credit);
					if($result_update == 0) {$this->trans_rollback('update');}
					
					if($get_casbon > $txt_rate){
						
						$relize_kosong =  $total_casbon_amount - $total_paymen_amount;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $EPayNumber,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoDebet' 		=> $relize_kosong,
							'AmountCurrency' 		=> $relize_kosong,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
						
					}
					else{
						$relize_kosong =  $total_paymen_amount - $total_casbon_amount;
						
						$data_journal_relize_credit = array(
							'EPRealDate' 			=> $EPayDate,
							'EPRealNumber' 			=> $EPayNumber,
							'EPRealNumber_BOut' 	=> $EPayNumber,
							'EPRealAccountNumber' 	=> $account_exp_relize, 
							'EPRealSaldoCredit' 	=> $relize_kosong,
							'AmountCurrency' 		=> $relize_kosong,
							'EPRealSignStatus' 		=> 1,
							'EPRealDesc' 			=> $EPayDesc,
							'LastUser' 				=> $this->session->userdata('username')
						);
					}
					if($relize_kosong != 0){
						$result_update = $this->m_expensecashbon_payment->insert_journal_expense_relize_credit($data_journal_relize_credit);
						if($result_update == 0) {$this->trans_rollback('update');}	
					}
			}
		}
		
		if($this->db->trans_status() == 0) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = get_notification_transaksi('Expense Payment Cashbon', 'insert', 0);
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = get_notification_transaksi('Expense Payment Cashbon', 'insert', 1);
		}	

		echo json_encode($result);
	}
	
	public function to_preview($EPayNumber){
	    $CompanyCode = $this->session->userdata('company');
		$str_replace_no = str_replace(".","/", $EPayNumber); 
		$payment_number = str_replace("_","-", $str_replace_no);
        $EPRealNumber 	=  $this->m_expensecashbon_payment->get_actual_number($payment_number);
		$BranchCode 	=  $this->m_expensecashbon_payment->get_branch_code($payment_number);		
		if(!empty($EPRealNumber)){
			$EPRealNumber = $EPRealNumber;
		} else {
			$EPRealNumber = $payment_number; 
		}
		
		$data['cek_status_trans'] = $this->m_expensecashbon_payment->get_status_journal($payment_number);		
		$data['list_jurnal_payment']  = $this->m_expensecashbon_payment->get_journal_payment($payment_number,  $BranchCode);	
			
		$data['date_journal'] = $this->m_expensecashbon_payment->get_date_journal_payment($payment_number);
		$data['detail_payment'] = $this->m_expensecashbon_payment->get_detail_payment_description($payment_number);
		$data['detail_list'] = $this->m_expensecashbon_payment->get_detail_pembiayaan($payment_number);
	
		$this->load->view('v_header');
		$this->load->view('expense_cashbon/v_print_journal_payment_cashbon', $data);	
		$this->load->view('v_footer');	
	}
	
	public function suspend_data(){
        $id = $this->input->post('id');
		
		$get_at_payment = $this->m_expensecashbon_payment->get_at_expensepayment($id);
		$EBonNumber 	=  $get_at_payment->EBonNumber;
        $result_insert = 0;
        $data = array(
                'TransStatus' 		=> 3,
                'LastUser'           => $this->session->userdata('username')
		);
		$this->db->trans_begin();
        $result_insert = $this->m_expensecashbon_payment->update_trans_status($id, $data);
		if($result_insert == 0){ $this->trans_rollback('suspend'); }
		
		$status_cashbon = array('TransStatus' => 1, 'EBonLockStatus' => 0);
		$this->m_expensecashbon_payment->update_status_cashbon($EBonNumber, $status_cashbon);
			
		if ($result_insert == 0) {
            $this->db->trans_rollback();
			$result['status'] 	= false;
            $result['message']	= get_notification('suspend',$result_insert);
        }
        else {
            $this->db->trans_commit();
            $result['status'] 	= true;
            $result['message']	= get_notification('suspend',$result_insert);
        }
        echo json_encode($result);
    }
	
	public function approval1() {
		if($this->session->userdata("jobtitle_status")== 0){
			$payment_number= $this->input->post('txt_payment_no');
			
			$cek_app_status_mgr = $this->m_expensecashbon_payment->cek_status_app_mgr($payment_number);
            if(!empty($cek_app_status_mgr)){
				$TransStatus = 2;
				$app_finis = 1; 
			} else {
				$TransStatus = 4;
				$app_finis = 0;
			}
			
			$data = array (		
				'AppLevel2Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel2Code'         => $this->session->userdata("username"),
				'AppLevel2Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
						
			// transaction begin
			$this->db->trans_begin();
			$this->m_expensecashbon_payment->approval_status($data,$payment_number);
			
			$app_finished = array('AppFinished' => $app_finis);
			$cek_app = $this->m_expensecashbon_payment->cek_app($payment_number);
			
			if(!empty($cek_app)){
				$this->m_expensecashbon_payment->status_finished($app_finished, $payment_number);
			}
			
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] = false;
				$result['message'] = 'Approval Gaggal';
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = 'Approval Berhasil';
			}	
			
			echo json_encode($result);
		}
	}
	
	public function approval2() {
		if($this->session->userdata("jobtitle_status")== 1){
			$payment_number	= $this->input->post('txt_payment_no');
			$EBonNumber 	=  $this->input->post('txt_ebon_number');
			$app_finis = 1;
			/*$cek_app_status_dir = $this->m_expensecashbon_payment->cek_status_app_dir($payment_number);
            if(!empty($cek_app_status_dir)){
				$TransStatus = 2; 
				$app_finis = 1; 
			} else {
				$TransStatus = 5;
				$app_finis = 0;
			}*/
			
			$data = array (		
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => 2,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			
			// transaction begin
			$this->db->trans_begin();
			$this->m_expensecashbon_payment->approval_status($data,$payment_number);
			
			$status_cashbon = array('TransStatus' => 2, 'EBonLockStatus' => 0);
			$this->m_expensecashbon_payment->update_status_cashbon($EBonNumber, $status_cashbon);
			
			$app_finished = array('AppFinished' => $app_finis);
			$cek_app = $this->m_expensecashbon_payment->cek_app($payment_number);
			
			//if(!empty($cek_app)){
				$this->m_expensecashbon_payment->status_finished($app_finished, $payment_number);
			//}
			
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] = false;
				$result['message'] = 'Approval Gaggal';
			}
			else {
				$this->db->trans_commit();
				$result['status'] = true;
				$result['message'] = 'Approval Berhasil';
			}	
			
			echo json_encode($result);
		}
	}
	
	public function get_epay_number(){
		$epay_date  	= $this->input->post('EPay_Date') == '' ? date('d-m-Y') : $this->input->post('EPay_Date');
		/*$bankout_code	= $this->input->post('BankOut_Code');*/ 
	
        if(!empty($epay_date)){
            $result['status']   = true;
            $result['number']   = $this->m_expensecashbon_payment->create_epay_number($epay_date/*, $bankout_code*/);
        }
        else{
            $result['status']   = false;
            $result['number'] 	= '';
        }

        echo json_encode($result);
	}
	
	public function get_akun($cpv,$bank_payment){
		$replace_akun = str_replace('_','-', $bank_payment);
		$branch = $this->session->userdata('branch');
		
		$get_akun_gl =  $this->m_expensecashbon_payment->get_am_akun_gl($replace_akun, $branch);
		
		$CompanyCode = $this->session->userdata('company');
		$cpv_payment = "CPV";
		$crv_payment = "CRV";
		
		if($cpv == "CPV"){
			$result =  $this->m_expensecashbon_payment->get_akun($CompanyCode, $get_akun_gl->AccountSubTypeID, $get_akun_gl->CurrencyCode, $cpv_payment);
		}
		else{
			$result =  $this->m_expensecashbon_payment->get_akun($CompanyCode, $replace_akun, $get_akun_gl->CurrencyCode, $crv_payment);
		}
	    foreach($result as $row) {
			$hasil[] = array(
				'ExpBankNumber' => $row->AccountNumber,
				'ExpBankName' => $row->AccountName,
				'ExpCurrency' => $row->CurrencyCode,
				'BranchCode' => $row->BranchCode
			); 
		}
		
		echo json_encode($hasil);
	}
	
	public function ajax_item(){
        $ExpDetailID    = $this->input->post('ExpDetailID');

        if(!empty($ExpDetailID)){
            echo json_encode($this->m_expensecashbon_payment->get_detail_item_for_payment($ExpDetailID));
        }
        else{
            echo '[]';
        }        
    }
}