<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Fachri / 08-06-2016	
*/
 
class Purchase_dp extends CI_Controller {
    public $menu_id     	= 399;
    public $code        	= 'Purchase Dp Number';
    public $name        	= 'Purchase DP.';
    public $app1        	= array('MGR','SPV');
    public $app2       	 	= array('DIR');
    public $app_level   	= array('DIR','MGR','SPV');
	public $app_level_new   = array(0,1);
	
    public function __construct() {
        parent::__construct();
		$this->load->helper('message');	
		$this->load->helper('status');			
		$this->load->helper('convert_date');
		$this->load->helper('approval');
		$this->load->helper('regex');
		$this->load->helper('lables');
		$this->load->helper('custom_string_helper');

		$this->load->model('setup/m_branchinfo');
		$this->load->model('m_purchase_dp');
		$this->load->model('m_purchase_payment');
		$this->load->model('m_vendors');
		$this->load->model('m_employee');
		$this->load->model('m_account_journal');
		$this->load->model('custom/m_custom');
		
		$this->load->model('login/m_login');
	
		$is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
		if($is_login){

		}else{
			$username       = $this->session->userdata('username');
			$session_id     = $this->session->userdata('session_id');
			$this->m_login->delete_session($username,$session_id);
			$this->session->sess_destroy();

			$uri_func = $this->uri->segment(3);
			if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
				redirect(base_url() . 'login', 'refresh');
			}else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
				$result['status']   = 'session';
				$result['message']  = get_notification('session_destroy',0);
				echo json_encode($result);
				die();
			}
		}
    }
	
    public function index(){
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					$group_menu 	 = $this->session->userdata('group_menu');		
					// set data
					$data['menu_id'] =  $this->menu_id;

					// get link from tbl_menu 
					$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
					$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					$this->load->view('v_header');
					$this->load->view('purchase/purchase_dp/v_purchasedp', $data);	
					$this->load->view('v_footer');	
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
	
	//Function Form Edit
    public function add($SInvNumber='',$CustomerCode='',$CurrencyCode='') {
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
			/*
				untuk di index/list = get_menu_by_group, 
				untuk di add = get_menu_by_group_add
				untuk di edit = get_menu_by_group_edit
			*/
            $arr_group_id           = $this->m_menu->get_menu_by_group_add($group_menu, $this->menu_id);
			//--------------------------end-----------------------------------------------------------
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					/*
						modul untuk akses add masing masing modul berebda 
						jangan lupa copy $data nya (jika dia bener langsung ke add)
					*/
					$group_menu 	 = $this->session->userdata('group_menu');
					// set data
					$data['menu_id'] =  $this->menu_id;

					// get link from tbl_menu 
					$data['menu_label'] 	= $this->m_menu->get_menulabel($this->menu_id);
					$data['menu_link']  	= $this->m_menu->get_menulink($this->menu_id);
					$data['SInvNumber'] 	= $SInvNumber;
					$data['CustomerCode'] 	= $CustomerCode; 
					$data['CurrencyCode'] 	= $CurrencyCode;

					$data['pdpay_date']  = date('d-m-Y');
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					// set for view
					$this->load->view('v_header');
					$this->load->view('purchase/purchase_dp/v_new_purchase_dp', $data);
					$this->load->view('v_footer');
					//----------------------end--------------------------------------------------------		
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
    }
	
	function insert(){
		/* =====================================jcos number========================================== */
		$PDpayDate 	 	= $this->input->post('txt_payment_date');
		$BranchPaidFor  = $this->input->post('cbg_branch');
		$PDpayNumber 	= $this->m_purchase_dp->create_sdp_number($PDpayDate, $BranchPaidFor);
		/* ========================================================================================== */
		$BranchCode  = $this->session->userdata('branch');
		$POrdNumber  = $this->input->post('txt_dp_number');
		$Pay_CurrencyCode   = $this->input->post('txt_currency_code');
		$Pay_CurrencyRate   = $this->input->post('txt_rate');
		$VendorCode  		= $this->input->post('cbg_vendor');
		$PDpayDesc 			= $this->input->post('txa_memo');
		$PDpayAmount 		= $this->input->post('owing_amount');
		$detail_post 		= $this->input->post('detail');
		$txt_amount 		= $this->input->post('txt_amount');
		
		$detail 			= json_decode($detail_post);
		
		if(count($detail) > 0) {
			$this->db->trans_begin();
			$data_group = array(
				'BranchCode' 	=> $BranchCode,
				'BranchPaidFor' => $BranchPaidFor,
				'PDpayDate' 	=> date("Y-m-d", strtotime($PDpayDate)),
				'PDpayNumber' 	=> $PDpayNumber,
				'POrdNumber' 	=> $POrdNumber,
				'PDpayAmount' 	=> $PDpayAmount,
				'PayAmount' 	=> $txt_amount,
				'VendorCode' 	=> $VendorCode,
				'CurrencyCode' 	=> $Pay_CurrencyCode,
				'CurrencyRate' 	=> $Pay_CurrencyRate,
				'PDpayDesc' 	=> $PDpayDesc,
				'TransStatus' 	=> 5,
				'CreatedBy' 	=> $this->session->userdata('username'),
				'CreatedAt' 	=> date('Y-m-d'),
				'LastUser' 		=> $this->session->userdata('username'),
			);
			
			$this->m_purchase_dp->insert($data_group);
			
			foreach($detail as $row_detail){
				$AccontNumber 	= $row_detail->AccountNumber;
				$Amount 		= $row_detail->Amount;
				$Memo 			= $row_detail->Memo;
				$CurrencyRate 	= $row_detail->Rate;
				
				$data_detail = array(
					'PDpayNumber' 		 => $PDpayNumber,
					'PDpayAccountNumber' => $AccontNumber,
					'PDpayAmount'	 	=> $Amount,
					'PDpayMemo' 		=> $Memo,
					'CurrencyRate' 		=> $CurrencyRate,
					'LastUser' 			=> $this->session->userdata('username')
				);
				$this->m_purchase_dp->insert_detail($data_detail);
				@$SumAmount += $Amount;
			}
			
			$data_vendor = array(
				'LockStatus' => 1
			);
			$this->m_vendors->update($data_vendor, $VendorCode, $BranchPaidFor);
			
			$this->insert_journal($BranchCode, $VendorCode, $POrdNumber, $SumAmount, $PDpayDate, $PDpayDesc, $Pay_CurrencyCode, $Pay_CurrencyRate, $PDpayNumber, $detail);
			
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] 		= FALSE;
				$result['message']   = get_notification_transaksi($this->name, 'insert', 0,$PDpayNumber);
			}
			else {
				$this->db->trans_commit();
				$result['status'] 		= TRUE;
				$result['message']   = get_notification_transaksi($this->name, 'insert', 1,$PDpayNumber);
			}
		}
		else{
			$result['status']   = false;
            $result['message']  = "Sales DP gagal disimpan. Tidak ada Account Name pada Sales DP.";
		}
		
		 echo json_encode($result);
	}
	
	function insert_OLD(){
		try{
			/* =====================================jcos number========================================== */
			$SDpayDate 	 = $this->input->post('txt_payment_date');
			$SDpayNumber = $this->m_purchase_dp->create_sdp_number($SDpayDate);
			/* ========================================================================================== */
			$BranchCode  = $this->session->userdata('branch');
			$SOrdNumber  = $this->input->post('txt_dp_number');
			$Pay_CurrencyCode   = $this->input->post('txt_currency_code');
			$Pay_CurrencyRate   = $this->input->post('txt_rate');
			$CustomerCode  		= $this->input->post('cmb_customer');
			$SDpayDesc 			= $this->input->post('txa_memo');
			$SDpayAmount 		= $this->input->post('owing_amount');
			$detail_post 		= $this->input->post('detail');
			$txt_amount 		= $this->input->post('txt_amount');
			
        	$detail 			= json_decode($detail_post);
		
			$data_group = array(
				'BranchCode' 	=> $BranchCode,
				'SDpayDate' 	=> date("Y-m-d", strtotime($SDpayDate)),
				'SDpayNumber' 	=> $SDpayNumber,
				'SOrdNumber' 	=> $SOrdNumber,
				'SDpayAmount' 	=> $SDpayAmount,
				'CustomerCode' 	=> $CustomerCode,
				'CurrencyCode' 	=> $Pay_CurrencyCode,
				'CurrencyRate' 	=> $Pay_CurrencyRate,
				'SDpayDesc' 	=> $SDpayDesc,
				'TransStatus' 	=> 5,
				'CreatedBy' 	=> $this->session->userdata('username'),
            	'CreatedAt' 	=> date('Y-m-d'),
				'LastUser' 		=> $this->session->userdata('username'),
			);
			
			$this->trans_data['insert_group']['data'] = $data_group;
			
			foreach($detail as $row_detail){
				$AccontNumber 	= $row_detail->AccountNumber;
				$Amount 		= $row_detail->Amount;
				$Memo 			= $row_detail->Memo;
				
				$data_detail = array(
					'SDpayNumber' 		 => $SDpayNumber,
					'SDpayAccountNumber' => $AccontNumber,
					'SDpayAmount'	 	=> $Amount,
					'SDpayMemo' 		=> $Memo,
					'LastUser' 			=> $this->session->userdata('username')
				);
				$this->trans_data['insert_detail']['data'][] = $data_detail;
				
				@$SumAmount += $Amount;
			}
			
			$data_customer = array(
				'LockStatus' => 1
			);
			$this->trans_data['update_customer']['data'] 		= $data_customer;
			$this->trans_data['update_customer']['CustomerCode']  = $CustomerCode;
			
			$this->insert_journal($BranchCode, $CustomerCode, $SOrdNumber, $SumAmount, $SDpayDate, $SDpayDesc, $Pay_CurrencyCode, $Pay_CurrencyRate, $SDpayNumber);
			
			$result_insert = $this->m_purchase_dp->insert_transaction($this->trans_data);
			$this->trans_data = [];
			
			
			if ($result_insert == 1) {
				$result['status'] = true;
				$result['message'] = get_notification_transaksi($this->name, 'insert', 1, $SDpayNumber);
			} else {
				$result['status'] = false;
				$result['message'] = get_notification_transaksi($this->name, 'insert', 0, $SDpayNumber);
			}
			/* ==================================end====================================================== */
			echo json_encode($result);
				
		}
		catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
	}
	
	function insert_journal($BranchCode, $VendorCode, $POrdNumber, $PDpayAmount,$PDpayDate,$PDpayJDesc, $Bank_Curr_Code, $Bank_Curr_Rate, $PDpayNumber, $detail){
		
		//$this->trans_data['delete_journal']['TransNumber'] = $SDpayNumber;
		$total_dp 				= 0;
		$total_dp_no_currency 	= 0;
		$Select_DP 				= $POrdNumber;
		$payment_dp 			= $PDpayAmount;
		
		/*===================Get Pordetail DP==========================================*/
		$sordetail_dp 			= $this->m_purchase_payment->get_saldo_detail_dp($VendorCode, $Select_DP, 1);
		
		$saldo_dp 		 		= $payment_dp;
		$saldo_dp_new 		 	= $payment_dp;
		
		$jurnal_dp 				= 0;
		$jurnal_dp_currency 	= 0;
		$jurnal_dp_no_currency 	= 0;
		$currency_rate_detail 	= 0;
		$is_edit 				= "";
		$PaymentBefore 			= 0;
		$PaymentBefore_Sum 		= 0;
		$PaymentPlus			= 0;
		$PaymentPlus_Sum 		= 0;
		
		$relize_debet 			= 0;
		$relize_kredit 			= 0;
		$total_dp 				= 0;
		
		foreach($sordetail_dp as $row_detail_dp){
			$DPUsed					= $row_detail_dp->DPUsed;
			$JVoucherBalance 		= $row_detail_dp->JVoucherBalance; 
			$SaldoDPBalance 		= $row_detail_dp->SaldoDPBalance;
			$currency_rate_detail 	= $row_detail_dp->CurrencyRate;
			
			$DPUsed					= $DPUsed + $JVoucherBalance;
			if($DPUsed != $SaldoDPBalance){
				$dp_used_book 			= 0;
				$saldo_balance_detail 	=  $row_detail_dp->PaymentAmount ;
				$SOrdDetailDPID 		=  $row_detail_dp->POrdDetailDPID;
				$SaldoDPBalance 		=  $row_detail_dp->SaldoDPBalance;
				
				if($saldo_dp > 0){
					//echo $saldo_dp;
					$saldo_dp 			= doubleval($saldo_dp_new - ($PaymentPlus_Sum + $saldo_balance_detail));
					
					if($saldo_dp < 0){
						$dp_used_old 			= $DPUsed;
						$dp_used_book 			= doubleval($DPUsed + ($saldo_dp_new - $PaymentBefore_Sum));
						$jurnal_dp 				= (doubleval(($saldo_dp_new - $PaymentBefore_Sum) * $currency_rate_detail));
						$jurnal_dp_currency 	= (doubleval($saldo_dp_new - $PaymentBefore_Sum / $currency_rate_detail));
						$jurnal_dp_no_currency 	= doubleval($saldo_dp_new - $PaymentBefore_Sum);
						$update_sordetail = array(
							'DPUsed' 				=> $dp_used_book
						);
						/*$this->trans_data['update_dp']['data'][] 		= $update_sordetail;
						$this->trans_data['update_dp']['CustomerCode'][]  = $CustomerCode;
						$this->trans_data['update_dp']['SOrdDetailDPID'][]  = $SOrdDetailDPID;
						$this->trans_data['update_dp']['Select_DP'][]  = $Select_DP;*/
						$this->m_purchase_payment->update_po_detail_dp_by_vendor($update_sordetail, $VendorCode, $SOrdDetailDPID, $Select_DP);
						
						$DPAccount = $this->m_account_journal->get_account_name('Uang Muka Pembelian', $Bank_Curr_Code); // Uang Muka Category -> 1-4-03-001-00
						if ($DPAccount == false) {
							$this->trans_rollback('journal', 'Uang Muka Pembelian', '');
						}
						
						$dp_currency  	= $Bank_Curr_Code == 'IDR' ? $jurnal_dp_no_currency : $jurnal_dp_no_currency;
						
						$journal_dp          = array(
							'PDpayDate'           => date("Y-m-d", strtotime($PDpayDate)),
							'PDpayNumber'         => $PDpayNumber,
							'PDpayAccountNumber'  => $DPAccount->AccountNumber,
							'PDpayJDebet'         => 0,
							'PDpayJCredit'        => $jurnal_dp,
							'AmountCurrency'      => $dp_currency,
							'PDpayJDesc'          => $PDpayJDesc,
							'PDpayJSignStatus'    => 1, 
							'POrdDetailDPID' 	  => $SOrdDetailDPID,
							'BranchTransCode' 	  => $BranchCode,
							'CurrencyRate' 		  => $currency_rate_detail,
							'LastUser'            => $this->session->userdata("username")
						);
						 
						if($jurnal_dp > 0){
							//$this->trans_data['insert_journal']['data'][] = $journal_dp;
							$this->m_purchase_dp->insert_journal($journal_dp);
						}
						
						@$total_dp 				+= $jurnal_dp;
						$total_dp_no_currency 	+= $jurnal_dp_no_currency;
					}
					else{
						$PaidStatus 			= $DPUsed;
						$dp_used_book 			= doubleval(($saldo_balance_detail - $DPUsed) + $PaidStatus);
						$jurnal_dp 				= (doubleval($saldo_balance_detail - $DPUsed) * doubleval($currency_rate_detail));
						$jurnal_dp_currency 	= (doubleval($saldo_balance_detail - $DPUsed) / doubleval($currency_rate_detail));
						$jurnal_dp_no_currency 	= doubleval($saldo_balance_detail - $DPUsed);
						
						$update_sordetail = array(
							'DPUsed' 				=> $dp_used_book
						);
						/*$this->trans_data['update_dp']['data'][] 		= $update_sordetail;
						$this->trans_data['update_dp']['CustomerCode'][]  = $CustomerCode;
						$this->trans_data['update_dp']['SOrdDetailDPID'][]  = $SOrdDetailDPID;
						$this->trans_data['update_dp']['Select_DP'][]  = $Select_DP;*/
						$this->m_purchase_payment->update_po_detail_dp_by_vendor($update_sordetail, $VendorCode, $SOrdDetailDPID, $Select_DP);
						$PaymentBefore 			= doubleval($saldo_balance_detail - $DPUsed);
						$PaymentBefore_Sum 		+= $PaymentBefore;
						$PaymentPlus 			=  doubleval($saldo_balance_detail + $DPUsed);
						$PaymentPlus_Sum 		+= $PaymentPlus;
						
						$DPAccount = $this->m_account_journal->get_account_name('Uang Muka Pembelian', $Bank_Curr_Code); // Uang Muka Category -> 1-4-03-001-00
						if ($DPAccount == false) {
							$this->trans_rollback('journal', 'Uang Muka Pembelian', '');
						}
						
						$dp_currency  	= $Bank_Curr_Code == 'IDR' ? $jurnal_dp_no_currency : $jurnal_dp_no_currency;
						
						$journal_dp          = array(
							'PDpayDate'           => date("Y-m-d", strtotime($PDpayDate)),
							'PDpayNumber'         => $PDpayNumber,
							'PDpayAccountNumber'  => $DPAccount->AccountNumber,
							'PDpayJDebet'         => 0,
							'PDpayJCredit'        => $jurnal_dp,
							'AmountCurrency'      => $dp_currency,
							'PDpayJDesc'          => $PDpayJDesc,
							'PDpayJSignStatus'    => 1, 
							'POrdDetailDPID' 	  => $SOrdDetailDPID,
							'BranchTransCode' 	  => $BranchCode,
							'CurrencyRate' 		  => $currency_rate_detail,
							'LastUser'            => $this->session->userdata("username")
						);
						 
						if($jurnal_dp > 0){
							//$this->trans_data['insert_journal']['data'][] = $journal_dp;
							$this->m_purchase_dp->insert_journal($journal_dp);
						}
						
						@$total_dp 			+= $jurnal_dp;
						$total_dp_no_currency 	+= $jurnal_dp_no_currency;
					}
				}
			
				$update_isedit = array(
					'IsEdit' 				=> 1
				);
				/*$this->trans_data['update_dp_']['data'][] 		= $update_isedit;
				$this->trans_data['update_dp']['CustomerCode'][]  = $CustomerCode;
				$this->trans_data['update_dp']['SOrdDetailDPID'][]  = $SOrdDetailDPID;
				$this->trans_data['update_dp']['Select_DP'][]  = $Select_DP;*/
				$this->m_purchase_payment->update_po_detail_dp_by_vendor($update_isedit, $VendorCode, $SOrdDetailDPID, $Select_DP);
			}
		}
		/*========================End===================================================*/
					
		foreach($detail as $row_detail){
			/*
			  Function Nature Credit/Debit
			 */
			$nature_journal = $this->m_account_journal->debet_kredit($row_detail->TypeName, $row_detail->SubtypeName);
			if ($nature_journal == "Debet") {
				$Sign_Status = 0;
			} else {
				$Sign_Status = 1;
			}
			/* ======================End================================== */
		
			$AccountNumber  = $row_detail->AccountNumber;
			$Amount 	 	= $row_detail->Amount;
			$Rate 			= $row_detail->Rate; 
			$CurrencyCode 	= $row_detail->CurrencyCode;
			
			if($Bank_Curr_Code == 'IDR'){
				$amount_total     	= $CurrencyCode == 'IDR' ? $Amount : $Amount * $Rate;
				$Amount_currency   	= $CurrencyCode == 'IDR' ? $Amount : $Amount * $Rate;
			}
			else{
				$amount_total    		= $CurrencyCode == 'IDR' ? $Amount : $Amount * $Rate;	
				$Amount_currency  		= $CurrencyCode == 'IDR' ? $Amount / $Rate : $Amount;
			}
			
			//$amount_total  = $Bank_Curr_Code == 'IDR' ? doubleval($Amount) : doubleval($Amount) * $Rate;
			
			$journal_debet           = array(
				'PDpayDate'           => date("Y-m-d", strtotime($PDpayDate)),
				'PDpayNumber'         => $PDpayNumber,
				'PDpayAccountNumber'  => $AccountNumber,
				'PDpayJDebet'         => $amount_total,
				'PDpayJCredit'        => 0,
				'AmountCurrency'      => $Amount_currency,
				'PDpayJDesc'          => $PDpayJDesc,
				'PDpayJSignStatus'    => $Sign_Status, 
				'BranchTransCode' 	  => $BranchCode,
				'CurrencyRate' 		  => $Rate,
				'LastUser'            => $this->session->userdata("username")
			);
			
			$this->m_purchase_dp->insert_journal($journal_debet);
			@$sum_total_amount += $amount_total;
		}
		
		/*=============================Selisih Kurs===============================*/
		
		if($Bank_Curr_Code != 'IDR'){
			$inv_dp_selisih_kurs	= doubleval($total_dp) - doubleval($sum_total_amount);
			if($inv_dp_selisih_kurs < 0){
				$RealizedAccount  	 	= $this->m_account_journal->get_sys_sales_account('RealizedGainedLoss', $Bank_Curr_Code);                     
				if($RealizedAccount == false) {$this->trans_rollback('journal','Realized, Sales',$Bank_Curr_Code);}
	
				$realized_amount    = abs($inv_dp_selisih_kurs);
				//$realized_currency  = $Bank_Curr_Code == 'IDR' ? doubleval($inv_dp_selisih_kurs) : doubleval($inv_dp_selisih_kurs) / $Bank_Curr_Rate;
				
				$journal_RealizedAccount           = array(
					'PDpayDate'           => date("Y-m-d", strtotime($PDpayDate)),
					'PDpayNumber'         => $PDpayNumber,
					'PDpayAccountNumber'  => $RealizedAccount->AccountNumber,
					'PDpayJDebet'         => 0,
					'PDpayJCredit'        => $realized_amount,
					'AmountCurrency'      => 0,
					'PDpayJDesc'          => $PDpayJDesc,
					'PDpayJSignStatus'    => 1, 
					'BranchTransCode' 	  => $BranchCode,
					'CurrencyRate' 		  => 1,
					'LastUser'            => $this->session->userdata("username")
				);
				//$this->trans_data['insert_journal']['data'][] = $journal_RealizedAccount;
				
				$this->m_purchase_dp->insert_journal($journal_RealizedAccount);
			} 
			else if($inv_dp_selisih_kurs > 0){
				$RealizedAccount  	 	= $this->m_account_journal->get_sys_sales_account('RealizedGainedLoss', $Bank_Curr_Code);                     
				if($RealizedAccount == false) {$this->trans_rollback('journal','Realized, Sales',$Bank_Curr_Code);}
	
				$realized_amount    = abs($inv_dp_selisih_kurs);
				//$realized_currency  = $Bank_Curr_Code == 'IDR' ? doubleval($inv_dp_selisih_kurs) : doubleval($inv_dp_selisih_kurs) / $Bank_Curr_Rate;
				
				$journal_RealizedAccount           = array(
					'PDpayDate'           => date("Y-m-d", strtotime($PDpayDate)),
					'PDpayNumber'         => $PDpayNumber,
					'PDpayAccountNumber'  => $RealizedAccount->AccountNumber,
					'PDpayJDebet'         => $realized_amount,
					'PDpayJCredit'        => 0,
					'AmountCurrency'      => 0,
					'PDpayJDesc'          => $PDpayJDesc,
					'PDpayJSignStatus'    => 0, 
					'BranchTransCode' 	  => $BranchCode,
					'CurrencyRate' 		  => 1,
					'LastUser'            => $this->session->userdata("username")
				);
				//$this->trans_data['insert_journal']['data'][] = $journal_RealizedAccount;
				
				$this->m_purchase_dp->insert_journal($journal_RealizedAccount);
			}
		}
		/*==================================End=========================*/	
	}
	
	//Function Form Edit
	public function edit(){	
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
			/*
				untuk di index/list = get_menu_by_group, 
				untuk di add = get_menu_by_group_add
				untuk di edit = get_menu_by_group_edit
			*/
            $arr_group_id           = $this->m_menu->get_menu_by_group_edit($group_menu, $this->menu_id);
			//--------------------------end-----------------------------------------------------------
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
				}
				else{
					/*
						modul untuk akses add masing masing modul berebda 
						jangan lupa copy $data nya (jika dia bener langsung ke add)
					*/
					$group_menu 	 		= $this->session->userdata('group_menu');		
					// set data
					$data['menu_id'] 	=  $this->menu_id;
					
					// get link from tbl_menu 
					$data['menu_label'] =  $this->m_menu->get_menulabel($this->menu_id);
					$data['menu_link'] 	= $this->m_menu->get_menulink($this->menu_id);
					
					// inisialisasi additional parameter
					$id 				= str_replace($data['menu_link'].'/edit/',"",$this->uri->uri_string());
					$data['detail'] 	= $this->m_purchase_dp->get_detail($id);
					
					$get_dp_for_equal 	= $this->m_purchase_dp->get_down_payment_for_row($data['detail']->VendorCode, $data['detail']->POrdNumber);
					
					
					if($data['detail']->VendorCode == 'IDR'){
						$data['summing_data'] = $get_dp_for_equal->SumDp_norate;
					}
					else{
						$data['summing_data'] = $get_dp_for_equal->SumDp_rate;
					}
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					if($data['detail'] != false){
						// set for view
						$this->load->view('v_header');
						$this->load->view('purchase/purchase_dp/v_edit_purchase_dp',$data);
						$this->load->view('v_footer');
					}
					//----------------------end--------------------------------------------------------		
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	function update(){
		
		/* =====================================jcos number========================================== */
		$PDpayDate 	 = $this->input->post('txt_payment_date');
		$PDpayNumber = $this->input->post('txt_dp_no');
		/* ========================================================================================== */
		$BranchCode  = $this->session->userdata('branch');
		$POrdNumber  = $this->input->post('txt_dp_number');
		$Pay_CurrencyCode   = $this->input->post('txt_currency_code');
		$Pay_CurrencyRate   = $this->input->post('txt_rate');
		$VendorCode  		= $this->input->post('cbg_vendor');
		$PDpayDesc 			= $this->input->post('txa_memo');
		$txt_amount 		= $this->input->post('txt_amount');
		$PDpayAmount 		= $this->input->post('owing_amount');
		$detail_post 		= $this->input->post('detail');
		$detail 			= json_decode($detail_post);
		$BranchPaidFor  	= $this->input->post('cbg_branch');
		
		$detail_id = $this->input->post('detail_id');
        $detail_id = json_decode($detail_id);
		
		if(count($detail) > 0) {
			$this->db->trans_begin();
			
			$DPAccount  		= $this->m_account_journal->get_account_name('Uang Muka Pembelian', $Pay_CurrencyCode);
			$sordetail_dp_new 	= $this->m_purchase_dp->get_receipt_detail_dp_update($VendorCode, $DPAccount->AccountNumber);
			
			foreach($sordetail_dp_new as $row_dp_suspend){
				$DPUsed = $row_dp_suspend->DPUsed;
				$POrdDetailDPID = $row_dp_suspend->POrdDetailDPID;
				$POrdNumber_DP = $row_dp_suspend->POrdNumber;
				$total_pordetail = $row_dp_suspend->AmountCurrency - $DPUsed;
				//$total_pordetail_new = $row_dp_suspend->PPaySaldoCredit - $DPUsed;
				
				$update_sordstatus = array('SOrdStatus' => 1, 'DPUsed' => abs($total_pordetail));
				$this->m_purchase_payment->update_po_detail_dp_for_pp($update_sordstatus, $VendorCode, $POrdDetailDPID);
			}
			
			$this->m_purchase_dp->delete_journal($PDpayNumber);
			
			$data_group = array(
				'PDpayAmount' 	=> $PDpayAmount,
				'CurrencyRate' 	=> $Pay_CurrencyRate,
				'PDpayDesc' 	=> $PDpayDesc,
				'PayAmount' 	=> $txt_amount,
				'AppFinished' 	=> 2,
				'LastUser' 		=> $this->session->userdata('username'),
			);	
			
			$this->m_purchase_dp->update($data_group, $PDpayNumber);
			
			foreach($detail as $row_detail){
				$AccontNumber 	= $row_detail->AccountNumber;
				$Amount 		= $row_detail->Amount;
				$Memo 			= $row_detail->Memo;
				$PDpayDetailID  = $row_detail->PDpayDetailID;
				$CurrencyRate   = $row_detail->Rate;
				
				$data_detail = array(
					'PDpayAccountNumber' => $AccontNumber,
					'PDpayAmount'	 	=> $Amount,
					'PDpayMemo' 		=> $Memo,
					'CurrencyRate' 		=> $CurrencyRate,
					'LastUser' 			=> $this->session->userdata('username')
				);
				$this->m_purchase_dp->update_detail($data_detail, $PDpayDetailID);
				
				@$SumAmount += $Amount;
			}
			
			/* ===========================delete detail================================================================= */
            if (count($detail_id) > 0) {
                foreach ($detail_id as $SDpayDetailID) {
                    $this->m_purchase_dp->delete_data_grid($PDpayDetailID);
                }
            }
            /* =============================end========================================================================== */
			
			$this->insert_journal($BranchPaidFor, $VendorCode, $POrdNumber, $SumAmount, $PDpayDate, $PDpayDesc, $Pay_CurrencyCode, $Pay_CurrencyRate, $PDpayNumber, $detail);
			
			if ($this->db->trans_status() === FALSE) {
				$this->db->trans_rollback();
				$result['status'] 	 = FALSE;
				$result['message']   = get_notification_transaksi($this->name, 'update', 1,$PDpayNumber); 
			}
			else {
				$this->db->trans_commit();
				$result['status'] 	 = TRUE;
				$result['message']   = get_notification_transaksi($this->name, 'update', 1,$PDpayNumber);
			}
			/* ==================================end====================================================== */
		}
		else{
			$result['status']   = false;
            $result['message']  = "Sales DP gagal disimpan. Tidak ada Account Name pada Sales DP.";
		}
		echo json_encode($result);
	}
	
	function update_Old(){
		try{
			/* =====================================jcos number========================================== */
			$SDpayDate 	 = $this->input->post('txt_payment_date');
			$SDpayNumber = $this->input->post('txt_dp_no');
			/* ========================================================================================== */
			$BranchCode  = $this->session->userdata('branch');
			$SOrdNumber  = $this->input->post('txt_dp_number');
			$Pay_CurrencyCode   = $this->input->post('txt_currency_code');
			$Pay_CurrencyRate   = $this->input->post('txt_rate');
			$CustomerCode  		= $this->input->post('cmb_customer');
			$SDpayDesc 			= $this->input->post('txa_memo');
			$SDpayAmount 		= $this->input->post('owing_amount');
			$detail_post 		= $this->input->post('detail');
			$detail 			= json_decode($detail_post);
			
			if(count($detail) > 0) {
				$DPAccount  		= $this->m_account_journal->get_account_name('Uang Muka Penjualan', $Pay_CurrencyCode);
				$sordetail_dp_new 	= $this->m_purchase_dp->get_receipt_detail_dp_update($CustomerCode, $DPAccount->AccountNumber);
				
				foreach($sordetail_dp_new as $row_dp_suspend){
					$DPUsed 		 		=  $row_dp_suspend->DPUsed;
					$SOrdDetailDPID 		=  $row_dp_suspend->SOrdDetailDPID;
					$SOrdNumber_DP 			=  $row_dp_suspend->SOrdNumber; 
					$total_pordetail 		=  $row_dp_suspend->AmountCurrency - $DPUsed;
					//$total_pordetail_new 	=  $row_dp_suspend->SRecSaldoDebet - $DPUsed;
					
					$update_sordstatus = array('SOrdStatus' => 1, 'DPUsed' => abs($total_pordetail));
					
					$this->trans_data['update_dpused']['data'][] 		= $update_sordstatus;
					$this->trans_data['update_dpused']['CustomerCode'][]  = $CustomerCode;
					$this->trans_data['update_dpused']['SOrdDetailDPID'][]  = $SOrdDetailDPID;
					
					//$this->m_sales_receipt->update_so_detail_dp_for_sr($update_sordstatus, $Customer_Code, $SOrdDetailDPID);
				}
			
				$data_group = array(
					'SDpayAmount' 	=> $SDpayAmount,
					'CurrencyRate' 	=> $Pay_CurrencyRate,
					'SDpayDesc' 	=> $SDpayDesc,
					'LastUser' 		=> $this->session->userdata('username'),
				);	
				
				$this->trans_data['update_group']['TransNumber'] = $SDpayNumber;
				$this->trans_data['update_group']['data'] = $data_group;
				
				foreach($detail as $row_detail){
					$AccontNumber 	= $row_detail->AccountNumber;
					$Amount 		= $row_detail->Amount;
					$Memo 			= $row_detail->Memo;
					$SDpayDetailID  = $row_detail->SDpayDetailID;
					
					$data_detail = array(
						'SDpayAccountNumber' => $AccontNumber,
						'SDpayAmount'	 	=> $Amount,
						'SDpayMemo' 		=> $Memo,
						'LastUser' 			=> $this->session->userdata('username')
					);
					$this->trans_data['update_detail']['data'][] = $data_detail;
					$this->trans_data['update_detail']['TransNumber'][] = $SDpayDetailID;
					
					@$SumAmount += $Amount;
				}
				
				
				$this->insert_journal($CustomerCode, $SOrdNumber, $SumAmount, $SDpayDate, $SDpayDesc, $Pay_CurrencyCode, $Pay_CurrencyRate, $SDpayNumber);
				
				$result_update = $this->m_purchase_dp->update_transaction($this->trans_data);
				$this->trans_data = [];
				
				
				if ($result_update == 1) {
					$result['status'] = true;
					$result['message'] = get_notification_transaksi($this->name, 'update', 1, $SDpayNumber);
				} else {
					$result['status'] = false;
					$result['message'] = get_notification_transaksi($this->name, 'update', 0, $SDpayNumber);
				}
				/* ==================================end====================================================== */
				echo json_encode($result);
			}
			else{
				throw new Exception("Sales DP gagal disimpan. Tidak ada Account pada Sales DP.");
			}
		}
		catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
	}
	
	public function suspend_data() {
		try{
			$id 			 = $this->input->post('id');
			$get_table_group = $this->m_purchase_dp->get_detail($id);
			$VendorCode 	= $get_table_group->VendorCode;
			$CurrencyCode 	= $get_table_group->CurrencyCode;
			$POrdNumber 	= $get_table_group->POrdNumber;
			$BranchPaidFor 	= $get_table_group->BranchPaidFor;
			/*
				0=SI
				1=SO
			*/
			$DPAccount  		= $this->m_account_journal->get_account_name('Uang Muka Pembelian', $CurrencyCode);
			$sordetail_dp_new 	= $this->m_purchase_dp->get_payment_detail_dp_suspend($VendorCode, $POrdNumber, $DPAccount->AccountNumber);
				
			foreach($sordetail_dp_new as $row_dp_suspend){
				$DPUsed 		 		=  $row_dp_suspend->DPUsed;
				$POrdDetailDPID 		=  $row_dp_suspend->POrdDetailDPID;
				$POrdNumber_DP 			=  $row_dp_suspend->POrdNumber; 
				$total_pordetail 		=  $row_dp_suspend->AmountCurrency - $DPUsed;
				//$total_pordetail_new 	=  $row_dp_suspend->SDpayJCredit - $DPUsed;
				
				//$dp_total_amount    = $CurrencyCode == 'IDR' ? $total_pordetail : $total_pordetail_new;
		
				//if($Select_DP != $POrdNumber_DP){
					$update_sordstatus = array('SOrdStatus' => 1, 'DPUsed' => abs($total_pordetail), 'IsEdit'=>0);
					$this->trans_data['update_dp']['data'][] 		= $update_sordstatus;
					$this->trans_data['update_dp']['VendorCode'][]  = $VendorCode;
					$this->trans_data['update_dp']['POrdNumber'][]  = $POrdNumber;
					$this->trans_data['update_dp']['POrdDetailDPID'][]  = $POrdDetailDPID;
			
					//$this->m_sales_receipt->update_sr_detail_dp_rec_before($update_sordstatus, $data->CustomerCode, $row_detail->Select_DP, $POrdDetailDPID);
				//}
			}
		
			$data_customer = array(
				'LockStatus' => 0
			);
			$this->trans_data['update_vendor']['data'] 		= $data_customer;
			$this->trans_data['update_vendor']['VendorCode']  = $VendorCode;
			$this->trans_data['update_vendor']['BranchPaidFor']  = $BranchPaidFor;
			
			$data_group = array(
				'TransStatus' 	=> 3,
				'LastUser' 		=> $this->session->userdata('username'),
			);	
				
			$this->trans_data['update_group']['TransNumber'] = $id;
			$this->trans_data['update_group']['data'] = $data_group;
			
			$result_suspend = $this->m_purchase_dp->suspend_transaction($this->trans_data);
			$this->trans_data = [];
			
			
			if ($result_suspend == 1) {
				$result['status'] = true;
				$result['message'] = get_notification_transaksi($this->name, 'suspend', 1, $id);
			} else {
				$result['status'] = false;
				$result['message'] = get_notification_transaksi($this->name, 'suspend', 0, $id);
			}
			/* ==================================end====================================================== */
			echo json_encode($result);
		}
		catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
			
	}
	
	public function approval_mgr(){
		try{
			$txt_dp_no 		= $this->input->post('txt_dp_no');
			$VendorCode 	= $this->input->post('cbg_vendor');
			$BranchPaidFor 	= $this->input->post('cbg_branch');
			
			$data_group = array(
				'TransStatus' 		=> 2,
				'AppFinished' 		=> 1,
				'AppLevel1Code' 	=> 1,
				'AppLevel1Status' 	=> $this->session->userdata('username'),
				'AppLevel1Date' 	=> date("Y-m-d"),
				'LastUser' 			=> $this->session->userdata('username'),
			);	
				
			$this->trans_data['update_group']['TransNumber'] = $txt_dp_no;
			$this->trans_data['update_group']['data'] = $data_group;
			
			$data_customer = array(
				'LockStatus' => 0
			);
			$this->trans_data['update_vendor']['data'] 		= $data_customer;
			$this->trans_data['update_vendor']['VendorCode']  = $VendorCode;
			$this->trans_data['update_vendor']['BranchPaidFor']  = $BranchPaidFor;
			
			$result_suspend = $this->m_purchase_dp->approval_transaction($this->trans_data);
			$this->trans_data = [];
			
			if ($result_suspend == 1) {
				$result['status'] = true;
				$result['message'] = get_notification('approval', 1);  
			} else {
				$result['status'] = false;
				$result['message'] = get_notification('approval', 0);  
			}
			/* ==================================end====================================================== */
			echo json_encode($result);
		}
		catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
	}
	
	function reverse_data_detail() {
		try{
			$txt_dp_no  		= $this->input->post('txt_dp_no');
			$txt_trans_dp_no  	= $this->input->post('txt_trans_dp_no');
			$VendorCode  		= $this->input->post('cbg_vendor');
			
			$detail_post 		= $this->input->post('detail');
			
            $detail 			= json_decode($detail_post);
			$BranchPaidFor 		= $this->input->post('cbg_branch');
			
			$date_transaction 	= check_end_periode($txt_dp_no);
			 
			$get_end_periode = $this->m_custom->check_period_end_custom($date_transaction);//Check Periode End
			$get_customer 	 = $this->m_purchase_dp->get_vendor_code($VendorCode);
			 
			if ($get_end_periode) {
				throw new Exception(get_notification('reverse_in_period', 0)); //sudah di end periode atau belum
			} else if($get_customer->LockStatus == 1){
				throw new Exception("Maaf Vendor ini mempunya Transaksi yang belum di selesaikan di Purchase Payment atau Purchase Journal, SIlahkan proses terlebih dahulu"); //sudah di end periode atau belum
			}
			
			$data_group = array(
				'TransStatus' 		=> 5,
				'AppFinished' 		=> 0,
				'AppLevel1Code' 	=> 0,
				'AppLevel1Status' 	=> NULL,
				'AppLevel1Date' 	=> NULL,
				'LastUser' 			=> $this->session->userdata('username'),
			);	
				
			$this->trans_data['update_group']['TransNumber'] = $txt_dp_no;
			$this->trans_data['update_group']['data'] = $data_group;
			
			$data_customer = array(
				'LockStatus' => 1
			);
			$this->trans_data['update_vendor']['data'] 		= $data_customer;
			$this->trans_data['update_vendor']['VendorCode']  = $VendorCode;
			$this->trans_data['update_vendor']['BranchPaidFor']  = $BranchPaidFor;
			
			$result_reverse = $this->m_purchase_dp->reverse_transaction($this->trans_data);
			$this->trans_data = [];
			
			if ($result_reverse == 0) {
                $result['status'] = FALSE;
                $result['message'] = get_notification_transaksi($this->name, 'reverse', 0, $txt_dp_no);
            } else {
                $result['status'] = TRUE;
                $result['message'] = get_notification_transaksi($this->name, 'reverse', 1, $txt_dp_no);
            }
			/* ==================================end====================================================== */
			echo json_encode($result);
		}
		catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
	}
	
	public function dp_filter(){
		$vend = $this->input->post('vend_code');
		
		if(!empty($vend)){
           echo json_encode($this->m_purchase_dp->get_down_payment($vend));
		}
		else{
			echo '[]';
		}
		
	}
	
	function get_akun($Currency,$Branch_code) {
		$branch_replace = str_replace("_","-",$Branch_code);
		
        $result = $this->m_purchase_dp->get_akun($Currency,$branch_replace);
        echo json_encode($result);
    }
	
	public function ajax_item_for_edit(){
        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
        $id = str_replace($data['menu_link'].'/ajax_item_for_edit/',"",$this->uri->uri_string());

        if(!empty($id)){
            echo json_encode($this->m_purchase_dp->get_detail_item($id));
        }
        else{
            echo '[]';
        }
        
//        echo $this->db->last_query(); die();
    }
	
	public function ajax(){
		$page	= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows	= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$hasil = array();
		/*===========rows*page tidak boleh melebihi dari jumlah data======= */
		
		/*=================================post dari find di kiri====================================================*/
		$param 			= $this->input->post('param');
		$Number			= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : ''; 
		$Desc			= isset($param['txt_Description']) ? $param['txt_Description'] : ''; 
		
		$DateFrom		= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo			= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$TransStatus	= isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
		$BranchCode		= isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
		
		$cmb_VendorCode	= isset($param['cmb_VendorCode']) ? $param['cmb_VendorCode'] : '';
		/*============================================end============================================================*/
		/*========================jika kondisi tidak sama dengan kosong di find lakukan proses di if=================*/
		if($BranchCode != ""){
			$this->db->where('BranchCode', $BranchCode);
		}
		else{
			$this->db->where('BranchCode', $this->session->userdata('branch'));
		}
		
		if($Number != ""){$this->db->like('PDpayNumber', $Number);} 
		if($Desc != ""){$this->db->like('PDpayDesc', $Desc);}
		if($cmb_VendorCode != ""){$this->db->where('VendorCode', $cmb_VendorCode);} 
		 
		if($TransStatus!=''){ 
			$this->db->where_in('TransStatus', $TransStatus);
		}
		else{
			/*
				default adalah 2,6. Finished dan Need Approval
			*/
			$this->db->where_in('at_purchasedp.TransStatus',array(0,1,4,5,6,7));
		}
		
		
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('at_purchasedp.SDpayDate >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('at_purchasedp.SDpayDate <=', date("Y-m-d", strtotime($DateTo))); 
			}
        }
		/*==========================end=================================================================*/
		
		$count_data = $this->db->count_all_results('at_purchasedp'); 
		$offset 	= $count_data - ($rows * $page);
		if($offset > 0){
			$rows = $rows;
		}
		else{
			$rows = $count_data - ($rows*($page-1));
		}
		/*========================end=======================================*/
		$result = $this->m_purchase_dp->get_all(); 
		
		for($i=0; $i<=($rows-1); $i++){
			// kalau master ambil dari get_status_master(), kalau transaksi get_status()
			$TransStatus = get_status($result[$i]->TransStatus);

            $hasil[] = array(
				'TransNumber' 	=>	$result[$i]->PDpayNumber,
				'PDpayDate' 	=>	$result[$i]->PDpayDate, 
				'BranchName' 	=>	$result[$i]->BranchName, 
				'VendorName' 	=>	$result[$i]->VendorName, 
				'PDpayDesc' 	=>	$result[$i]->PDpayDesc, 
				'PDpayAmount' 	=>	$result[$i]->PayAmount, 
				'TransStatus' 	=>	$TransStatus,
				'CreatedBy' 	=> $result[$i]->CreatedBy 
			);
		}

		if(empty($hasil)){
			$emptyVar = array();
			echo json_encode($emptyVar);
		}
		else{
			$result = array();
			$result['total'] = $count_data;
			$result['rows'] = $hasil;
			echo json_encode($result);
		}
	}
	
	/*======================Function preview journal============================*/
    function to_preview_journal(){	
        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
		
        // set data 
        $id = str_replace($data['menu_link'].'/to_preview_journal/',"",$this->uri->uri_string());
        $data['detail'] = $this->m_purchase_dp->get_detail_journal($id);
		
        $data['list']   = $this->m_purchase_dp->get_journal_by_number($id);		

        $this->load->view('v_header');
        $this->load->view('purchase/purchase_dp/v_preview_journal_purchasedp',$data);	
        $this->load->view('v_footer');	
    }
	/*======================end============================*/
	
	public function trans_rollback($modus, $AccounName, $Currency) {
        $this->db->trans_rollback();
        $result['status']   = false;

        if($modus == 'approval'){
            $result['message']  = get_notification($modus, 0);
        }
        else{
             $result['message']  = get_notification_preference($modus, 0, $AccounName, $Currency);               
        }
                
        echo json_encode($result);
        die();
    }
	
	public function to_print_detail() {

        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

        $BranchCode = $this->session->userdata('branch');

        $payment_no = str_replace($data['menu_link'] . '/to_print_detail/', "", $this->uri->uri_string());

        if ($payment_no != "") {

            $data['detail'] = $this->m_purchase_dp->get_detail_item_print($payment_no);
            $data['list'] = $this->m_purchase_dp->get_detail_item_print_journal($payment_no, $BranchCode);

            $this->load->view('v_header');
            $this->load->view('purchase/purchase_dp/v_print_detail_purchase_dp', $data);
            $this->load->view('v_footer');
        }
    }
	
	function to_preview($obj_string = null) {
        $data['menu_id']        = $this->menu_id;
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id);
//        $data['list'] = $this->m_purchase_requisition->get_all($this->ho);
        $data['lables']         = get_lables_eng(); //tambah dhidhik, 050022016, untuk labelisasi, penamaan biar seperti ini saja biar enak copy-paste

        $data_filter            = arrayParamInUrlToStr($obj_string);
		$data['branch_code'] = $data_filter['cmb_branch'];
		$data['company_code'] = $data_filter['cmb_company'];
        $data['list']           = $this->m_purchase_dp->get_all_to_print($data_filter);
        
        $this->load->view('v_header');
        $this->load->view('purchase/purchase_dp/v_preview_purchase_dp', $data);
        $this->load->view('v_footer');
    }
}