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

/*	
 *	Author 	: Ladur
 *	Tanggal : 20-07-2014	
 *	Revisi 	: Fachri
 */ 

class Journal_voucher extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 100;
	public $menu_model = 'm_journalvoucher';
	
	// 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->helper('message');
		$this->load->helper('convert_date');	
		$this->load->model('m_journalvoucher');	
		$this->load->model('list/m_vendors');
		$this->load->model('list/m_customer');	
        date_default_timezone_set('Asia/Jakarta');		
	}	
	
	// Function Index	
	public function index() {
		/* // pengecekan otoritas
		if($this->session->userdata("checking") == "") {
			redirect(base_url() . 'custom/cek/'.$this->menu_id, 'refresh');	
		}
		
		// reset session 
		$this->session->unset_userdata('checking'); */
		
		// 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);
		
		$data['journal_number']      = $this->m_journalvoucher->create_jv_number(date('d-m-Y'));
		$data['date_now'] = date('d-m-Y');
		// set for view
		$this->load->view('v_header');
		$this->load->view('general_ledger/journal_voucher/v_journalvoucher', $data);
		$this->load->view('v_footer');
	}
	
	public function get_akun(){
		$BranchCode = $this->session->userdata('branch');
		$list_akun = $this->m_journalvoucher->get_akun($BranchCode);
		echo"$list_akun";
	}
	
	public function get_department(){
		$CompanyCode = $this->session->userdata('company');
		$list_department = $this->m_journalvoucher->get_department($CompanyCode);
		echo"$list_department";
	}
	
	public function get_type_journal(){
	    $result ='[{"TypeID":"0","TypeName":"Periode End"},{"TypeID":"1","TypeName":"Voucher"},{"TypeID":"2","TypeName":"Depreciation Assest"}, {"TypeID":"3","TypeName":"Roll Over"}]';
		echo $result;
	}
	
	public function get_jv_number() {
        $preg_date      = $this->input->post('jv_af_date') == '' ? date('d-m-Y') : $this->input->post('jv_af_date');
		
        $jv_number    = $this->m_journalvoucher->create_jv_number($preg_date);
        
//        print_r($data['sq_number1']);
        
        $result['status']   = true;
        $result['number']   = $jv_number;
        echo json_encode($result);
    }
	
	// Function Insert
	public function insert() {
	    $BranchCode 	= $this->session->userdata('branch');
		
		$JVchDate 				= date("Y-m-d", strtotime($this->input->post('txt_voucher_date')));	
		$JVchDesc 				= $this->input->post('txa_description');
		$cek_data 				= $this->input->post('cek_data');
		$txt_vendor_customer 	= $this->input->post('txt_vendor_customer');
		$cmb_intenity 			= $this->input->post('cmb_intenity');
		$JVchNumber 			= $this->input->post('txt_voucher_no');
		
		$this->db->trans_begin();
		$data_journal = array(
			'BranchCode' 		=> $BranchCode,
			'JVchDate' 			=> $JVchDate,
			'JVchNumber' 		=> $JVchNumber,
			'JVchDesc' 			=> $JVchDesc,
			'TransStatus' 		=> 6,
			'LastUser' 			=> $this->session->userdata('username')
		);
		
		$hasil = $this->m_journalvoucher->insert_journal_voucher($data_journal);
	    if($hasil == 0) {$this->trans_rollback('insert');}
		
		$data_jv 					= $this->input->post('detail_jv');
		$txt_vendor_customer 		= $this->input->post('txt_vendor_customer');
		$detail_jv					= json_decode($data_jv);
		$total_rate_new 			= 0;
		$total_rupiah 				= 0;
		
		foreach($detail_jv as $details){
		
			$AccountNumber 	= $details->AccountNumber;
			$CurrencyCode 	= $details->CurrencyCode;
			$Debit_detail 	= $details->Debit;
			$Credit_detail 	= $details->Credit;
			$Vendor 		= $details->VendorCode;
			$Customer 		= $details->CustomerCode;
			$SubTypeID 		= $details->AccountSubTypeID;
			$Debit_journal 	= $details->TotRateDebet;
			$Credit_journal = $details->TotRateCredit;
			
			if($Debit_detail != 0){
				$sign_status = 0;
				if($SubTypeID == 5 || $SubTypeID == 6 || $SubTypeID == 62 || $SubTypeID == 7){
					$amoun_currency = $Debit_detail;
				}
				else{
					$amoun_currency = $Debit_journal;
				}
			}
			else if($Credit_detail != 0){
				$sign_status = 1;
				if($SubTypeID == 5 || $SubTypeID == 6 || $SubTypeID == 62 || $SubTypeID == 7){
					$amoun_currency = $Credit_detail;
				}
				else{
					$amoun_currency = $Credit_journal;
				}
			}
			
			//set masuk ke jurnal
			
			
			$Memo = $details->Memo;
			
			$detail_voucher = array(
				'JVchNumber' 		=>  $JVchNumber,
				'JVchAccount' 		=> $AccountNumber,
				'JVchDebetSaldo' 	=> $Debit_detail,
				'JVchCreditSaldo'  	=> $Credit_detail,
				'JVchDetailMemo' 	=> $Memo,
				'VendorCode' 		=> $Vendor,
				'CustomerCode' 		=> $Customer,
				'JVchRate' 			=> $details->Rate,
				'CurrencyCode' 		=> $CurrencyCode,
				'LastUser' 			=> $this->session->userdata('username')
			);
		
			$hasil = $this->m_journalvoucher->insert_detail_journal_voucher($detail_voucher);
			if($hasil == 0) {$this->trans_rollback('insert');}
			
			$jaournal = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $JVchNumber,
				'JVchAccountNumber' 	=> $AccountNumber,
				'JVchSaldoDebet' 		=> $Debit_journal,
				'JVchSaldoCredit'  		=> $Credit_journal,
				'JVchSignStatus' 		=> $sign_status,
				'AmountCurrency' 		=> $amoun_currency,
			    'JVchDesc' 				=> $Memo,
				'LastUser' 				=> $this->session->userdata('username')
			);
			
			$hasil = $this->m_journalvoucher->insert_journaljvc_voucher($jaournal);
			if($hasil == 0) {$this->trans_rollback('insert');}
		}
		
		if($hasil == 0) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = get_notification_transaksi('Journal Voucher', 'insert', 0);
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = get_notification_transaksi('Journal Voucher', 'insert', 1);
		}	

		echo json_encode($result);
	}
	
	function vendor($currency){
        $data = $this->m_journalvoucher->get_vendor($currency);
        echo json_encode($data);
    }
	
	function customer($currency){
        $data = $this->m_journalvoucher->get_customer($currency);
        echo json_encode($data);
    }
	
	function vendor_edit($branch){
		$branhc_replace = str_replace('_', '-', $branch);
        $data = $this->m_journalvoucher->get_vendor_edit($branhc_replace);
        echo json_encode($data);
    }
	
	function customer_edit($branch){
		$branhc_replace = str_replace('_', '-', $branch);
        $data = $this->m_journalvoucher->get_customer_edit($branhc_replace);
        echo json_encode($data);
    }
	
	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['list'] = $this->m_journalvoucher->get_list($id);

		// set for view
		$this->load->view('v_header');
		$this->load->view('general_ledger/journal_voucher/v_edit_journalvoucher', $data);
		$this->load->view('v_footer');
	}
	
	public function get_detail_jvoucher(){
		$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
	    $jvc = str_replace($data['menu_link'].'/get_detail_jvoucher/',"",$this->uri->uri_string());
	    $result = $this->m_journalvoucher->get_detail_jvoucher($jvc);
		//echo $this->db->last_query();
		foreach($result as $row) {
		    $id = $row->JVchNumber;	
			$total_debet 	= $row->JVchDebetSaldo * $row->JVchRate;
			$total_credit 	= $row->JVchCreditSaldo * $row->JVchRate;	
			$hasil[] = array(
			    'id' 				=> $row->JVchDetailID,
			    'AccountName' 		=> $row->AccountName,
				'AccountNumber' 	=> $row->JVchAccount,
				'Debit' 			=> $row->JVchDebetSaldo,
				'Credit' 			=> $row->JVchCreditSaldo,
				'Customer' 			=> $row->CustomerName,
				'Vendor' 			=> $row->VendorName,
				'CustomerCode' 		=> $row->CustomerCode,
				'VendorCode' 		=> $row->VendorCode,
				'Rate' 				=> $row->JVchRate,
				'TotRateDebet' 		=> $total_debet,
				'TotRateCredit' 	=> $total_credit,
				'AccountSubTypeID' 	=> $row->AccountSubTypeID,
				'CurrencyCode' 		=> $row->CurrencyCode,
				'Memo' 				=> $row->JVchDetailMemo
			); 
		}
		
		if(!empty($hasil)){
			echo json_encode($hasil);
		}
		else{
			$notFound = array();
			echo json_encode($notFound);
		}
	}
	
	public function update() {
	    $BranchCode  = $this->session->userdata('branch');
		$JVchNumber  = $this->input->post('txt_voucher_no');
		$JVchDate 	 = date("Y-m-d", strtotime($this->input->post('txt_voucher_date')));	
		$JVchDesc    = $this->input->post('txa_description');
		
		$data_journal = array(
			'BranchCode' 	=> $BranchCode,
			'JVchNumber' 	=> $JVchNumber,
			'JVchDate' 		=> $JVchDate,
			'JVchDesc' 		=> $JVchDesc,
			'LastUser' 		=> $this->session->userdata('username'),
		);
		
		$this->m_journalvoucher->update_journal_voucher($data_journal, $JVchNumber);
	    
		$data_jv=$this->input->post('detail_jv');
		$detail_jv=json_decode($data_jv);
		$journal_id = $this->m_journalvoucher->get_journal_id($JVchNumber);
		
		$result_update = $this->m_journalvoucher->delete_jv_journal($JVchNumber);
		if($result_update == 0) {$this->trans_rollback('update');}	
				
		foreach($detail_jv as $details){
		
			$AccountNumber 	= $details->AccountNumber;
			$Debit 			= $details->Debit;
			$Credit 		= $details->Credit;
			$Memo 			= $details->Memo;
			$id 			= $details->id;
			$Debit_journal 	= $details->TotRateDebet;
			$Credit_journal = $details->TotRateCredit;
			
			if($Debit != 0){
				$sign_status = 0;
				
				$amoun_currency = $Debit;
			}
			else if($Credit != 0){
				$sign_status = 1;
				$amoun_currency = $Credit;
			}
			
			$cek_on_detail = $this->m_journalvoucher->get_id_on_detail_jvch($id);
			if(!empty($cek_on_detail)){ 
			
				$detail_voucher = array(
					'JVchDebetSaldo' 	=> $Debit,
					'JVchCreditSaldo'  	=> $Credit,
					'JVchDetailMemo' 	=> $Memo,
					'JVchRate' 			=> $details->Rate,
					'LastEdited' 		=> $this->session->userdata('username')
				);
			
				$hasil = $this->m_journalvoucher->update_detail_journal_voucher($detail_voucher, $id);	
				if($hasil == 0) {$this->trans_rollback('update');}
				
				$jaournal = array(
					'JVchDate' 				=> $JVchDate,
					'JVchNumber' 			=> $JVchNumber,
					'JVchAccountNumber' 	=> $AccountNumber,
					'JVchSaldoDebet' 		=> $Debit_journal,
					'JVchSaldoCredit'  		=> $Credit_journal,
					'JVchSignStatus' 		=> $sign_status,
					'AmountCurrency' 		=> $amoun_currency,
					'JVchDesc' 				=> $Memo,
					'LastUser' 				=> $this->session->userdata('username')
				);
				
				$hasil = $this->m_journalvoucher->insert_journaljvc_voucher($jaournal);
				if($hasil == 0) {$this->trans_rollback('insert');}
				
			} 
		}
		
		if($this->db->trans_status() == 0) {
			$this->db->trans_rollback();
			$result['status'] = false;
			$result['message'] = get_notification_transaksi('Journal Voucher', 'update', 0);
		}
		else {
			$this->db->trans_commit();
			$result['status'] = true;
			$result['message'] = get_notification_transaksi('Journal Voucher', 'update', 1);
		}	

		echo json_encode($result);
	}
	
	public function to_journal(){
	    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
		
	    $CompanyCode = $this->session->userdata('company');
		$BranchCode 	= $this->session->userdata('branch');	
        $journal_number = str_replace($data['menu_link'].'/to_journal/',"",$this->uri->uri_string());		
		
		$data['date_journal'] = $this->m_journalvoucher->get_date_journal($journal_number);
		$data['list_jurnal']  = $this->m_journalvoucher->get_all_journal($journal_number, $BranchCode);
		
		$this->load->view('v_header');
        $this->load->view('general_ledger/journal_voucher/v_print_journal', $data);		
		$this->load->view('v_footer');	
	}
	
	public function approval1() {
		if($this->session->userdata("jobtitle_status")==0){
			$voucher_no					= $this->input->post('txt_voucher_no');
			$cek_data 					= $this->input->post('cek_data');
			$txt_vendor_customer 		= $this->input->post('txt_vendor_customer');
			$detail_jv 					= $this->input->post('detail_jv');
			$total_rate_new 			= 0;
			$total_rupiah 				= 0;
			$detail_jv					= json_decode($detail_jv);
			
			$cek_app_status_mgr = $this->m_journalvoucher->cek_status_app_mgr($voucher_no);
            if(!empty($cek_app_status_mgr)){
				$TransStatus 	= 2; 
				$AppFinis 		= 1; 
				foreach($detail_jv as $details){
						$customer = $details->CustomerCode;
						$vendor = $details->VendorCode;
						
						if($customer != ""){
							if($details->AccountSubTypeID == 9 || $details->AccountSubTypeID == 10 || $details->AccountSubTypeID == 11 || $details->AccountSubTypeID == 12 || $details->AccountSubTypeID == 13 || $details->AccountSubTypeID == 14){
								
								
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									$get_customer_outstanding = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$oustanding = $get_customer_outstanding->OutStanding;
									$curren_rate = $get_customer_outstanding->CurrencyRate;
									
									$total_dollar 		= $oustanding + $nilai_debit_credit;
									$total_out_customer	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_customer_jv = $total_out_customer + $total_rupiah;
									
									$total_rate_new = $total_outstanding_customer_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) + doubleval($nilai_debit_credit);
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									
									$rate_jv = $details->Rate;
									$get_customer_outstanding = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$oustanding = $get_customer_outstanding->OutStanding;
									$curren_rate = $get_customer_outstanding->CurrencyRate;
									
									$total_dollar 		= $nilai_debit_credit - $oustanding;
									$total_out_customer	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_customer_jv = $total_out_customer + $total_rupiah;
									
									$total_rate_new = $total_outstanding_customer_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) - doubleval($nilai_debit_credit);
								}
								
								$update_customer = array(
									'OutStanding' 	=> $outstanding_new,
									'CurrencyRate' 	=> $total_rate_new
								);
								$hasil = $this->m_customer->update($update_customer, $customer);
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
							else if($details->AccountSubTypeID == 39 || $details->AccountSubTypeID == 40 || $details->AccountSubTypeID == 41){
								if($details->Debit != 0){
									
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									$get_customer_dp = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$SaldoDPBalance = $get_customer_dp->SaldoDPBalance;
									$curren_rate 	= $get_customer_dp->SaldoDPRate;
									
									$total_dollar 		= $SaldoDPBalance + $nilai_debit_credit;
									$total_out_customer	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_customer_jv = $total_out_customer + $total_rupiah;
									$total_rate_new = $total_dp_customer_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) + doubleval($nilai_debit_credit);
								
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
									$get_customer_dp = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$SaldoDPBalance = $get_customer_dp->SaldoDPBalance;
									$curren_rate 	= $get_customer_dp->SaldoDPRate;
									
									$total_dollar 		= $nilai_debit_credit - $SaldoDPBalance;
									$total_out_customer	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_customer_jv = $total_out_customer + $total_rupiah;
									$total_rate_new = $total_dp_customer_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) - doubleval($nilai_debit_credit);
								}
								$update_customer = array(
									'SaldoDPBalance' 	=> $dp_new,
									'SaldoDPRate' 		=> $total_rate_new
								);
								
								$hasil = $this->m_customer->update($update_customer, $customer);
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
						}
						else if($vendor != ""){
							
							if($details->AccountSubTypeID == 31 || $details->AccountSubTypeID == 32 || $details->AccountSubTypeID == 33 || $details->AccountSubTypeID == 34 || $details->AccountSubTypeID == 35 || $details->AccountSubTypeID == 36 || $details->AccountSubTypeID == 37 || $details->AccountSubTypeID == 44 || $details->AccountSubTypeID == 46 || $details->AccountSubTypeID == 47 || $details->AccountSubTypeID == 63){
								
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									
									$rate_jv = $details->Rate;
								
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
								
									$oustanding = $get_vendor_outstanding->OutStanding;
									$curren_rate = $get_vendor_outstanding->CurrencyRate;
									
									$total_dollar 		= $oustanding + $nilai_debit_credit;
									$total_out_vendor	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_outstanding_vendor_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) + doubleval($nilai_debit_credit);
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
								
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
								
									$oustanding = $get_vendor_outstanding->OutStanding;
									$curren_rate = $get_vendor_outstanding->CurrencyRate;
									
									$total_dollar 		= $nilai_debit_credit - $oustanding;
									$total_out_vendor	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_outstanding_vendor_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) - doubleval($nilai_debit_credit);
								}
								
								$update_vendor = array(
									'OutStanding' 	=> $outstanding_new,
									'CurrencyRate' 	=> $total_rate_new
								);
								$hasil = $this->m_vendors->update($update_vendor, $vendor, $this->session->userdata('branch'));
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
							else if($details->AccountSubTypeID == 25 || $details->AccountSubTypeID == 26 || $details->AccountSubTypeID == 27){
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
									$SaldoDPBalance = $get_vendor_outstanding->SaldoDPBalance;
									$curren_rate 	= $get_vendor_outstanding->SaldoDPRate;
									
									$total_dollar 		= $SaldoDPBalance + $nilai_debit_credit;
									$total_out_vendor	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_dp_vendor_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) + doubleval($nilai_debit_credit);
									
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
									
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
									$SaldoDPBalance = $get_vendor_outstanding->SaldoDPBalance;
									$curren_rate 	= $get_vendor_outstanding->SaldoDPRate;
									
									$total_dollar 		= $nilai_debit_credit - $SaldoDPBalance;
									$total_out_vendor	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_dp_vendor_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) - doubleval($nilai_debit_credit);
								}
								
								$update_vendor = array(
									'SaldoDPBalance' 	=> $dp_new,
									'SaldoDPRate' 		=> $total_rate_new
								);
								$hasil = $this->m_vendors->update($update_vendor, $vendor, $this->session->userdata('branch'));
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
						}
					}
				
			} else {
				$TransStatus = 4;
				$AppFinis 	 = 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_journalvoucher->approval_status($data,$voucher_no);
			
			$app_finished = array('AppFinished' => $AppFinis);
			$cek_app = $this->m_journalvoucher->cek_app($voucher_no);
			
			if(!empty($cek_app)){
				$this->m_journalvoucher->status_finished($app_finished, $voucher_no);
			}
				
			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){
			$voucher_no					= $this->input->post('txt_voucher_no');
			$cek_data 					= $this->input->post('cek_data');
			$txt_vendor_customer 		= $this->input->post('txt_vendor_customer');
			$detail_jv 					= $this->input->post('detail_jv');
			$total_rate_new 			= 0;
			$total_rupiah 				= 0;
			$detail_jv					= json_decode($detail_jv);
			
			$cek_app_status_dir = $this->m_journalvoucher->cek_status_app_dir($voucher_no);
            if(!empty($cek_app_status_dir)){
				$TransStatus = 2; 
				$AppFinis 	 = 1;
					foreach($detail_jv as $details){
						$customer = $details->CustomerCode;
						$vendor = $details->VendorCode;
						
						if($customer != ""){
							if($details->AccountSubTypeID == 9 || $details->AccountSubTypeID == 10 || $details->AccountSubTypeID == 11 || $details->AccountSubTypeID == 12 || $details->AccountSubTypeID == 13 || $details->AccountSubTypeID == 14){
								
								
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									$get_customer_outstanding = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$oustanding = $get_customer_outstanding->OutStanding;
									$curren_rate = $get_customer_outstanding->CurrencyRate;
									
									$total_dollar 		= $oustanding + $nilai_debit_credit;
									$total_out_customer	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_customer_jv = $total_out_customer + $total_rupiah;
									
									$total_rate_new = $total_outstanding_customer_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) + doubleval($nilai_debit_credit);
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									
									$rate_jv = $details->Rate;
									$get_customer_outstanding = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$oustanding = $get_customer_outstanding->OutStanding;
									$curren_rate = $get_customer_outstanding->CurrencyRate;
									
									$total_dollar 		= $nilai_debit_credit - $oustanding;
									$total_out_customer	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_customer_jv = $total_out_customer + $total_rupiah;
									
									$total_rate_new = $total_outstanding_customer_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) - doubleval($nilai_debit_credit);
								}
								
								$update_customer = array(
									'OutStanding' 	=> $outstanding_new,
									'CurrencyRate' 	=> $total_rate_new
								);
								$hasil = $this->m_customer->update($update_customer, $customer);
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
							else if($details->AccountSubTypeID == 39 || $details->AccountSubTypeID == 40 || $details->AccountSubTypeID == 41){
								if($details->Debit != 0){
									
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									$get_customer_dp = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$SaldoDPBalance = $get_customer_dp->SaldoDPBalance;
									$curren_rate 	= $get_customer_dp->SaldoDPRate;
									
									$total_dollar 		= $SaldoDPBalance + $nilai_debit_credit;
									$total_out_customer	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_customer_jv = $total_out_customer + $total_rupiah;
									$total_rate_new = $total_dp_customer_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) + doubleval($nilai_debit_credit);
								
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
									$get_customer_dp = $this->m_journalvoucher->get_cust_outstanding($customer, $this->session->userdata('branch'));
								
									$SaldoDPBalance = $get_customer_dp->SaldoDPBalance;
									$curren_rate 	= $get_customer_dp->SaldoDPRate;
									
									$total_dollar 		= $nilai_debit_credit - $SaldoDPBalance;
									$total_out_customer	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_customer_jv = $total_out_customer + $total_rupiah;
									$total_rate_new = $total_dp_customer_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) - doubleval($nilai_debit_credit);
								}
								$update_customer = array(
									'SaldoDPBalance' 	=> $dp_new,
									'SaldoDPRate' 		=> $total_rate_new
								);
								
								$hasil = $this->m_customer->update($update_customer, $customer);
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
						}
						else if($vendor != ""){
							
							if($details->AccountSubTypeID == 31 || $details->AccountSubTypeID == 32 || $details->AccountSubTypeID == 33 || $details->AccountSubTypeID == 34 || $details->AccountSubTypeID == 35 || $details->AccountSubTypeID == 36 || $details->AccountSubTypeID == 37 || $details->AccountSubTypeID == 44 || $details->AccountSubTypeID == 46 || $details->AccountSubTypeID == 47 || $details->AccountSubTypeID == 63){
								
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									
									$rate_jv = $details->Rate;
								
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
								
									$oustanding = $get_vendor_outstanding->OutStanding;
									$curren_rate = $get_vendor_outstanding->CurrencyRate;
									
									$total_dollar 		= $oustanding + $nilai_debit_credit;
									$total_out_vendor	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_outstanding_vendor_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) + doubleval($nilai_debit_credit);
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
								
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
								
									$oustanding = $get_vendor_outstanding->OutStanding;
									$curren_rate = $get_vendor_outstanding->CurrencyRate;
									
									$total_dollar 		= $nilai_debit_credit - $oustanding;
									$total_out_vendor	= $oustanding * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_outstanding_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_outstanding_vendor_jv /$total_dollar;
									
									$outstanding_new = doubleval($oustanding) - doubleval($nilai_debit_credit);
								}
								
								$update_vendor = array(
									'OutStanding' 	=> $outstanding_new,
									'CurrencyRate' 	=> $total_rate_new
								);
								$hasil = $this->m_vendors->update($update_vendor, $vendor, $this->session->userdata('branch'));
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
							else if($details->AccountSubTypeID == 25 || $details->AccountSubTypeID == 26 || $details->AccountSubTypeID == 27){
								if($details->Debit != 0){
									$nilai_debit_credit = $details->Debit;
									$rate_jv = $details->Rate;
									
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
									$SaldoDPBalance = $get_vendor_outstanding->SaldoDPBalance;
									$curren_rate 	= $get_vendor_outstanding->SaldoDPRate;
									
									$total_dollar 		= $SaldoDPBalance + $nilai_debit_credit;
									$total_out_vendor	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_dp_vendor_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) + doubleval($nilai_debit_credit);
									
								}
								else if($details->Credit != 0){
									$nilai_debit_credit = $details->Credit;
									$rate_jv = $details->Rate;
									
									$get_vendor_outstanding = $this->m_journalvoucher->get_vend_outstanding($vendor, $this->session->userdata('branch'));
									$SaldoDPBalance = $get_vendor_outstanding->SaldoDPBalance;
									$curren_rate 	= $get_vendor_outstanding->SaldoDPRate;
									
									$total_dollar 		= $nilai_debit_credit - $SaldoDPBalance;
									$total_out_vendor	= $SaldoDPBalance * $curren_rate;
									$total_rupiah 		= $nilai_debit_credit * $rate_jv;
									
									$total_dp_vendor_jv = $total_out_vendor + $total_rupiah;
									
									$total_rate_new = $total_dp_vendor_jv /$total_dollar;
									
									$dp_new = doubleval($SaldoDPBalance) - doubleval($nilai_debit_credit);
								}
								
								$update_vendor = array(
									'SaldoDPBalance' 	=> $dp_new,
									'SaldoDPRate' 		=> $total_rate_new
								);
								$hasil = $this->m_vendors->update($update_vendor, $vendor, $this->session->userdata('branch'));
								if($hasil == 0) {$this->trans_rollback('insert');}
							}
						}
					}
				
				
			} else {
				$TransStatus = 5;
				$AppFinis 	 = 0;
			}
			
			$data = array (		
				'AppLevel1Status' 		=> 1,
				'TransStatus'           => $TransStatus,
				'AppLevel1Code'         => $this->session->userdata("username"),
				'AppLevel1Date'			=> date("Y-m-d"),	
				'LastUser' 				=> $this->session->userdata("username")
			);
			// transaction begin
			$this->db->trans_begin();
			$this->m_journalvoucher->approval_status($data,$voucher_no);
			
			$app_finished = array('AppFinished' => $AppFinis);
			$cek_app = $this->m_journalvoucher->cek_app($voucher_no);
			
			if(!empty($cek_app)){
				$this->m_journalvoucher->status_finished($app_finished, $voucher_no);
			}
				
			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 suspend_data(){
        $id = $this->input->post('id');
        $result_insert = 0;
        $data = array(
                'TransStatus' 		=> 3,
                'LastUser'          => $this->session->userdata('username')
		);
		$this->db->trans_begin();
        $result_insert = $this->m_journalvoucher->update_trans_status($id, $data);
		if($result_insert == 0){ $this->trans_rollback('suspend'); }
		
		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 trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status']   = false;

        if ($modus == 'journal') {
            $result['message']  = 'Pengaturan Preference Journal Template belum lengkap, silahkan atur ulang';
        }
		else {
            $result['message']  = get_notification_transaksi('Journal Voucher', $modus, 0);            
        }
        
		echo json_encode($result);
        die();
    }
	
	function delete(){
        $id  = $this->input->post('id');
		
		$get_jurnal = $this->m_journalvoucher->get_jurnal($id);
		$nomor_akun = $get_jurnal->JVchAccount;
		$nomor_transaksi = $get_jurnal->JVchNumber;
		
        if (!empty($id)) {
            $this->db->trans_begin();
            $result_delete = $this->m_journalvoucher->delete_detail_by_id($id);
			$result_delete = $this->m_journalvoucher->delete_detail_jurnal_by_id($nomor_transaksi,$nomor_akun);
            if ($result_delete == 0) {
                $this->db->trans_rollback();
                $result['status']   = false;
                $result['message']  = get_notification('delete', 0);            
            }		
            else {
                $this->db->trans_commit();
                $result['status']   = true;
                $result['message']  = get_notification('delete', 1);            
            }

            echo json_encode($result);
        }
    }
	
}