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

	
	function checking_debet_kredit($account_subtype){
		if($account_subtype == 'Hutang Dagang' || $account_subtype == 'Akumulasi Penyusutan Aktiva Tetap' || $account_subtype == 'Hutang Bahan Baku' || $account_subtype == 'Hutang Bahan Bakar' || 
			$account_subtype == 'Hutang Bahan Bakar Penolong' || $account_subtype == 'Hutang Afiliasi' || $account_number == 'Hutang Ekspedisi' || 
			$account_subtype == 'Hutang Lain-Lain' || $account_subtype == 'Hutang Pajak' || $account_subtype == 'Hutang Yg Belum di Tagih' || 
			$account_subtype == 'Hutang Barang Migrasi' || $account_subtype == 'Hutang Expense' || $account_subtype == 'Hutang Advance Payment' || 
			$account_subtype == 'Hutang PPn Fiscal' || $account_subtype == 'Hutang Aktiva Tetap' || $account_subtype == 'Uang Muka Penjualan' || 
			$account_subtype == 'Bank Garansi Customer' || $account_subtype == 'Hutang Pemegang Saham' || $account_subtype == 'Hutang Bank' || 
			$account_subtype == 'Hutang Leasing' || $account_subtype == 'Kewajiban Pajak Tangguhan' || $account_subtype == 'Modal' || 
			$account_subtype == 'Prive' || $account_subtype == 'Laba (Rugi) Ditahan' || $account_subtype == 'Laba (Rugi) Berjalan' || 
			$account_subtype == 'Laba (Rugi) Bulan Berjalan' || $account_subtype == 'Dividen' || $account_subtype == 'Revenue' || 
			$account_subtype == 'Retur Pembelian' || $account_subtype == 'Other Revenue' || $account_subtype == 'Selisih Kurs Akhir Transaksi' || 
			$account_subtype == 'Selisih Kurs Akhir Transaksi'){  
			
			$kredit_debet = "Kredit";
		}
		else{
			$kredit_debet = "Debet";
		}
	}
	
	//function journal voucher opening balance
	function insert_journal_voucher($account_number, $branch_code, $opening_balance, $account_subtype, $account_name, $cbg_currency, $jv_number, $currency_rate, $employee_code){
		$jv_model =	&get_instance();
		$jv_model->load->model('../modules_core/activities/m_journalvoucher');
		
		$check_subtype = $this->checking_debet_kredit($account_subtype);
		
		//kondisi opening balance jika dia lebih kecil atau lebih besar
		$amount_currency 	= $opening_balance * $currency_rate;
		
		$data_group = array(
			'BranchCode' 		=> $branch_code,
			'JVchDate' 			=> date('Y-m-d'),
			'JVchNumber' 		=> $jv_number,
			'JVchDesc' 			=> "Opening Balance",
			'JVchTotalDebet' 	=> 0,
			'JVchTotalCredit' 	=> $opening_balance,
			'JVchType' 			=> 2,
			'AppLevel1Status' 	=> 1,
			'AppLevel1Code' 	=> $employee_code,
			'AppLevel1Date' 	=> date('Y-m-d'),
			'AppLevel2Status' 	=> 1,
			'AppLevel2Code' 	=> $employee_code,
			'AppLevel2Date' 	=> date('Y-m-d'),
			'TransStatus' 		=> 2,
			'LastUser' 			=> $employee_code
		);
		
		$result_insert = $jv_model->m_journalvoucher->insert($data_group);
				
		if($opening_balance < 0 && $check_subtype == "Kredit"){
			
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_credit);
			
			$jaournal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_kredit);
			
			$jaournal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> $opening_balance,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_debet);
			
		}
		else if($opening_balance < 0 && $check_subtype == "Debet"){
			
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_credit);
			
			$jaournal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> $opening_balance,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_kredit);
			
			$jaournal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->m_journalvoucher->insert_journaljvc_voucher($jaournal_debet);
		}
		else if($opening_balance > 0 && $check_subtype == "Kredit"){
			
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_credit);
			
			$jaournal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_kredit);
			
			$jaournal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> $opening_balance,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_debet);
		}
		else{
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_detail_journal_voucher($data_detail_credit);
			
			$jaournal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> $opening_balance,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_kredit);
			
			$jaournal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $jv_model->m_journalvoucher->insert_journaljvc_voucher($jaournal_debet);
		}
		//end
	}
	//end function journal voucher
?>