<!---- 
    Author 	: Fachri
-->
<?php
//---------Untuk privellege jika dia ada tombol save----------
	if($group_user->SavePrivillage == 1){
		$cek_autority_save  = "";
	}
	else{
		$cek_autority_save 	= "disabled";
	}
//-------------end----------------------------
?>
<div class="easyui-layout" data-options="fit:true,border:false">
    <!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false">
        <div id="form-header-wrapper">
            <table cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td align="left" valign="middle">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Sales Receipt', '<?=$menu_link?>', 'icon-add', 'add')">New</a>
                	<a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload()">Refresh</a>
                </td>							
                <td align="right">								
                    <span class="form-title"><?=$menu_label?></span>
                </td>
            </tr>
            </table>	
        </div>			
    </div>
    <!-- End of top view -->
    
     <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" style="padding:5px;" fit="true">
        	<form id="frm_purchase_journal" name="frm_purchase_journal" method="post">
            <input type="hidden" name="detail" id="detail" />
            <input type="hidden" 	name="hdn_inv_type" id="hdn_inv_type" />
            <input type="hidden" name="hdn_cust_curr" id="hdn_cust_curr" />
            <input type="hidden" name="hdn_cust_curr_rate" id="hdn_cust_curr_rate" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly />
            <input type="hidden" name="hdn_fiscal_rate" id="hdn_fiscal_rate" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly />
            <input type="hidden" name="hdn_fiscal_status" id="hdn_fiscal_status" />
            <input type="hidden" name="hdn_inv_amount" id="hdn_inv_amount" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly/>
            <input type="hidden" name="hdn_inv_owing" id="hdn_inv_owing" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly/>
            <input type="hidden" value="0" name="hdn_rec_amount" id="hdn_rec_amount" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly/>
            <input type="hidden" 	name="hdn_lock_status" id="hdn_lock_status" />
            	
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                <tr>
                	<td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                            <td width="120"><b>Branch Name</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <select class="easyui-combogrid" name="cbg_branch" id="cbg_branch" style="width:252px;height:22px;" required <?=(($this->session->userdata("branch_status")==1)?"disabled":'') ?> editable="false"
                                    data-options="
                                        panelWidth:400,
                                        value:'<?= $this->session->userdata("branch") ?>',
                                        idField: 'BranchCode',
                                        textField: 'BranchName',
                                        url: '<?=base_url()?>custom/get_data/branch',
                                        method: 'get',
                                        columns: [[
                                            {field:'BranchCode',title:'Branch No',width:100,halign:'center'},
                                            {field:'BranchName',title:'Branch Name',width:161,halign:'center'}
                                        ]],
                                        fitColumns: true
                                    ">
                                </select>
                            </td>
                        </tr>
                        <tr height="20">
                        	<td width="120"><b>Paid To</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<select class="easyui-combogrid" name="cbg_vendor" id="cbg_vendor" style="width:252px;height:22px;" required 
                                    data-options="
                                        panelWidth:400,
                                        idField: 'VendorCode',
                                        textField: 'VendorName',
                                        url: '<?=base_url()?>custom/get_data/vendor_new_by_branch',
                                        method: 'get',
                                        columns: [[
                                            {field:'VendorCode',title:'Vendor No',width:100,halign:'center'},
                                            {field:'VendorName',title:'Vendor Name',width:161}
                                        ]],
                                        fitColumns: true
                                    ">
                                </select>
                                <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_purchase();">Select PInv.</a>	
                            </td>
                        </tr>
                        <tr height="20">
                        	<td valign="top" style="padding-top:4px;"><b>Address</b></td>
                            <td valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<textarea name="txa_address" id="txa_address" rows="4" style="width:246px;resize:none;" readonly></textarea>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1">
                        
                        <tr>
                        	<td colspan="2" width="350">&nbsp;</td>
                            <td width="120"><b>Payment Date</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                                <input name="txt_sr_date" id="txt_sr_date" type="text" class="easyui-datebox validBackward" value='<?=$srec_date ?>' style="width:127px;height:22px;" required editable="false" />
                            </td>
                        </tr>
                        
                        <tr height="47"valign="middle"></tr>

                                
                        </table>
                    </td>
                </tr>
                <tr><td colspan="8"><hr/></td></tr>
                <tr>
                	<td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                        	<td width="135"><b>Account Name</b></td>
                            <td width="14"><b>:</b></td>
                            <td width="495">
                            	<select class="easyui-combogrid" id="cbg_bank" name="cbg_bank" 
                                	required editable="false" 
                                	style="width:252px;height:22px;" 
                                    data-options="
                                        panelWidth: 400,
                                        idField: 'AccountNumber',
                                        textField: 'AccountName',
                                        url: '<?=base_url()?>list/purchase_expense/ajax_account_expense',
                                        method: 'get',
                                        columns: [[
                                            {field:'AccountNumber',title:'Account Number',width:110,halign:'center',hidden:true}, 
                                            {field:'AccountName',title:'Account Name',width:190,halign:'center'}, 
                                            {field:'CurrencyCode',title:'Currency',width:80,halign:'center',align:'center'}
                                        ]],
                                        fitColumns: true
                                    ">
                                </select>
                            </td>
                        </tr>
                        
                        <tr height="20">
                        	<td width="135" valign="top" style="padding-top:4px;"><b>Memo</b></td>
                            <td width="14" valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<textarea id="txa_sr_desc" name="txa_sr_desc" rows="4" style="width:246px;resize:none;"></textarea>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                    	
                    </td>
                </tr>
                </table>

                <!-- End of form header -->
                <div class="easyui-tabs" data-options="tabHeight:18" style="margin:5px 0px;">		
                    <div id="tab_item" title="Invoice">
                    	<table id="dtg_item" class="easyui-edatagrid" 
                        	url="<?=base_url() . $menu_link . '/get_data_expense_by_pj/'?>"
                            toolbar="#toolbar" idField="id"
                            rownumbers="true" fitColumns="true" 
                            singleSelect="true" style="height:236px;"
                        >
                            <thead frozen="true">
                            <tr>
                                <th rowspan="2" resizable="false" field="Invoice_Type"     width="50"  halign="center" align="center" editor="{type:'validatebox'}" hidden>Type</th>
                                <th rowspan="2" resizable="false" field="Invoice_Number"   width="255" halign='center' align="left"   editor="{type:'validatebox',options:{required:true}}" >Invoice No.</th>
                                <th rowspan="2" resizable="false" field="Invoice_Date"     width="100"  halign='center' align="center" editor="{type:'validatebox',options:{required:true}}" formatter="formatDate">Date</th>
                                <th colspan="1">Invoice Amount</th>
                                <th colspan="1">Invoice Owing</th>
                                <th colspan="1">Payment Amount</th>
                                
                                <th rowspan="2" resizable="false" field="ItemCategoryName" width="170" halign="center" align="center" editor="text" >Category Name</th>
                                <th rowspan="2" resizable="false" field="ItemCategoryID"   width="50"  halign="center" align="left" editor="text" hidden>Category ID</th>
                                <th rowspan="2" resizable="false" field="ItemCategoryCode"  width="95"  halign="center" align="left" editor="text" hidden>Category Code</th>
                                <th rowspan="2" resizable="false" field="InvoiceCurrCode"   width="50"  data-options="halign:'center',align:'right'" editor="{type:'text', disabled:'true'}" hidden>Invoice Currency Code</th>
                                <th rowspan="2" resizable="false" field="InvoiceCurrRate"   width="50"  data-options="halign:'center',align:'right'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" hidden>Invoice Currency Rate</th>
                            	<th rowspan="2" resizable="false" field="InvoiceFiscalRate" width="85"  data-options="halign:'center',align:'right'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" hidden>Fiscal Rate</th>
                                
                                <th rowspan="2" resizable="false" field="PPnFiscalStatus"   width="60" halign='center' align="right"  editor="text" hidden>PPN Fiscal Status</th>
                                <th rowspan="2" resizable="false" field="InvoiceTypeID"     width="50"  halign="center" align="center" editor="{type:'validatebox'}" hidden>Type ID</th>
                                <th rowspan="2" resizable="false" field="InvoicePaymentID"  width="50"  halign="center" align="center" editor="{type:'validatebox'}" hidden>Invoice Payment ID</th>
                                <th field="InvoiceAmountCopy"   width="215" halign='center' align="right"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" formatter="formatRp" hidden>Invoice Amount Copy</th>
                                
                            </tr>
                            <tr>
                            	<th field="Invoice_Amount"   width="245" halign='center' align="right"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" formatter="formatRp"><span class="spn_customer_curr_code">(IDR)</span></th>
                                <th field="Invoice_Owing"    width="245" halign='center' align="right"  editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0,disabled:true}}" formatter="formatRp"><span class="spn_customer_curr_code">(IDR)</span></th>
                                
                                <th field="Payment_Amount"   width="245" halign="center" align="right" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',min:0}}" formatter="formatRp"><span class="spn_customer_curr_code">(IDR)</span></th>                                

                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <!-- End Item Grid -->
            </form>
        </div>
     </div>
     <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
        <tr>
        	<!--<td>
            	<span><b>Total Owing :: </b></span>
                <span id="spn_total_owing">0</span>
                <span><b>Total Payment :: </b></span>
                <span id="spn_total_payment">0</span>
                <span><b>Overpay :: </b></span>
                <span id="overpay">0</span>
            </td>-->
            <td align="right">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" plain="true">Help</a>					
                <a id="save_button" href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="insert_data();" <?php echo $cek_autority_save;?>>Save</a> 								
                <a href="javascript:void(0)" class="easyui-linkbutton  form-button" iconCls="icon-cancel" onclick="parent.tab_close('tt')">Close</a>			
    		</td>
        </tr>
        </table>
    </div>
</div>

<div id="dialog-sq" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons-sq" style="width:860px;height:532px;top:27px;background-color:#F8F8F8;padding:5px;"> 
        <div id="tab_purchase_req" class="easyui-tabs" style="width:835px;height:485px; background-color:#F8F8F8;" data-options="fit:true,plain:true">
            <div title="Select Purchase Invoice" style="padding:10px" >
            <table id="tt_choose" class="easyui-datagrid" 
                url="<?=base_url()?>list/purchase_expense/get_detail_invoice" 
                remoteSort="false"
                pageSize="150"
                fitColumns="true" 
                rowNumbers="true"
                style="width:812px;height:400px"
                singleSelect="true"
                data-options = "border:true,view:scrollview,autoRowheight:false"
            >
                <thead>
                    <tr>
                        <th field="Invoice_Number" width="150" halign="center" align="left" sortable="true">PInv. Number</th>
                        <th field="Invoice_Date" width="80" halign="center" align="center" formatter="formatDate" sortable="true">PInv. Date</th>
                        <th field="ItemCategoryName" width="300" halign="center" align="left">Category</th>
                    </tr>
                </thead>
            </table>
    
             
            <!-- Dialog Button for Dialog Item-->
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_item_sq();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-sq');">Cancel</a>
            </div>
            <!-- End of Dialog Button for Dialog Item-->
            </div>
            <div title="Select Down Payment" style="padding:10px" >
            <table id="tt_choose_dp" class="easyui-datagrid" 
                url="<?=base_url()?>list/purchase_expense/get_data_dp" 
                remoteSort="false"
                pageSize="150"
                fitColumns="true" 
                rowNumbers="true"
                style="width:812px;height:400px"
                singleSelect="true"
                data-options = "border:true,view:scrollview,autoRowheight:false"
            >
                <thead>
                    <tr>
                        <th field="Invoice_Number" width="150" halign="center" align="left" sortable="true">PInv. Number</th>
                        <th field="Invoice_Date" width="80" halign="center" align="center" formatter="formatDate" sortable="true">PInv. Date</th>
                        <th field="ItemCategoryName" width="300" halign="center" align="left">Category</th>
                    </tr>
                </thead>
            </table>
            <!-- Dialog Button for Dialog Item-->
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_item_dp();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-sq');">Cancel</a>
            </div>
            <!-- End of Dialog Button for Dialog Item-->
            </div>
         </div>
</div>

<script type="text/javascript">

$(document).ready(function(){
	
	$('#cbg_branch').combogrid({
		onSelect:function(rowIndex, rowData){
			$('#cbg_vendor').combogrid('setValue','');
			$('#cbg_bank').combogrid('setValue','');
			$('#txa_address').val('');
												
			$('#cbg_vendor').combogrid('grid').datagrid('load',{
				BranchCode : rowData.BranchCode,
				CompanyCode : rowData.CompanyCode
			});
		}
	});	
	
	/*
		Combo grid customer
	*/
	$('#cbg_vendor').combogrid({
		onSelect:function(rowIndex, rowData){
			
			/*
				Jika customer adalah usd maka tombol fiscal nyala
			*/
			if(rowData.CurrencyCode == 'USD'){
				if(rowData.ItemCategoryName == 'PPn Fiscal'){
					$('#chk_fiscal_payment').prop('checked',true);
					$('#chk_fiscal_payment').prop('disabled',true);
				}
			}
			/*===============End=====================================*/
			
			/*
				Tampung currency ke textbox 
			*/
			$('#hdn_cust_curr').val(rowData.CurrencyCode);
			$('#txa_address').val(rowData.VendorAddress);
			$('#hdn_lock_status').val(rowData.LockStatus);
			/*==============End=====================*/
			
			
			/*
				Merubah label di invoice amount,owinf dan receipt
			*/
			$('.spn_customer_curr_code').html('('+rowData.CurrencyCode+')');
			
			//$('#spn_cust_curr').text(rowData.CurrencyCode);
			/*================End================================*/
			
			/*
				Load pop up by customer
			*/
			$('#tt_choose').datagrid({
				url:'<?=base_url()?>list/purchase_expense/get_detail_invoice/'+rowData.VendorCode
			 });
			/*===============End==============================*/
		}		
	});	
	/*============================End================================*/
	
	/*=============Function editor grid=============================*/
	editoy_grid();
	tab_pop_up();
	/*====================End=========================================*/
	
	/*
		Save Row
	*/
	$('#frm_purchase_journal').click(function(){
		$('#dtg_item').edatagrid('saveRow');
	});
	/*=================End================*/
});

function editoy_grid(){
	$('#dtg_item').datagrid({
		onBeginEdit:function(rowIndex){
			editors = $('#dtg_item').datagrid('getEditors', rowIndex);
			InvoiceType		 	= $(editors[0].target);
			InvoiceNumber 	 	= $(editors[1].target);
			InvoiceDate 	 	= $(editors[2].target);
			InvoiceAmount 		= $(editors[3].target);
			InvoiceOwing 		= $(editors[4].target);
			PaymentAmount 		= $(editors[5].target);
			ItemCategoryName 	= $(editors[6].target);
			ItemCategoryID		= $(editors[7].target);
			ItemCategoryCode 	= $(editors[8].target);
			InvoiceCurrCode 	= $(editors[9].target);
			InvoiceCurrRate 	= $(editors[10].target);
			InvoiceFiscalRate 	= $(editors[11].target);
			PPnFiscalStatus 	= $(editors[12].target);
			InvoiceAmountCopy 	= $(editors[15].target);
			
			InvoiceNumber.prop('readonly',true);
			InvoiceType.prop('readonly',true);
			InvoiceDate.prop('readonly',true);
			ItemCategoryName.prop('readonly',true);
			
			InvoiceAmount.numberbox('disable');
			InvoiceOwing.numberbox('disable');
			
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('disable');
			/*=================================end============================================================*/
			
			PaymentAmount.numberbox({
				onChange:function(){
					
					InvoiceAmount   = parseFloat(InvoiceAmountCopy.numberbox('getValue'));
					PaymentAmount   = parseFloat(PaymentAmount.numberbox('getValue'));
					
					if(isNaN(InvoiceAmount)){InvoiceAmount = 0;}
					
					if(PaymentAmount > InvoiceAmount){
						$.messager.alert('Maaf','Payment Amount tidak boleh lebih besar dari Invoice Amount','error',function(){
							//PaymentAmount.numberbox('setValue', 0);
							$('#dtg_item').datagrid('reload'); 
						});
					}
					else{
						owing   = InvoiceAmount - PaymentAmount;
						InvoiceOwing.numberbox('setValue', owing);
					}
				}
			});
		},
		onEndEdit:function(rowIndex, rowData, changes){
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('enable');
			/*=================================end============================================================*/
			dtg_item 		= $('#dtg_item').datagrid('getRows');
			payment_amount 	= 0;
			total_payment 	= 0;
			owing 			= 0;
			total_owing = 0;
			$.each(dtg_item, function(index, value) {
					payment_amount 			= parseFloat(value.Payment_Amount);
					total_owing 					= parseFloat(value.InvoiceAmountCopy) - parseFloat(value.Payment_Amount);
					
					if(isNaN(payment_amount)){payment_amount = 0;}
					total_payment   += payment_amount;
			});
			
			$('#hdn_rec_amount').numberbox('setValue', total_payment);
			$('#hdn_inv_owing').numberbox('setValue', total_owing);
		}
	});
}

function tab_pop_up(){
	$('#tab_purchase_req').tabs({
		border:true,
		onSelect:function(title){
			if(title == 'Select Purchase Invoice'){
				var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;
				$('#tt_choose').datagrid({
					url:'<?=base_url()?>list/purchase_expense/get_detail_invoice/'+vendor_code
				 });
			}
			else{
				 var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;
				 $('#tt_choose_dp').datagrid({
					url:'<?=base_url()?>list/purchase_expense/get_data_dp/'+vendor_code
				 });
			}
		}
	});
}

function select_purchase() {
	var Vendor			= $('#cbg_vendor').combogrid('grid');	// get datagrid object
	var SelectedCustomer	= Vendor.datagrid('getSelected'); // get the selected row

	if(SelectedCustomer == null ){
		$.messager.alert('Maaf','Silahkan pilih customer terlebih dahulu','warning',function(){});
	}
	else{
		modal_open('Purchase Journal','dialog-sq');
	}		
}

function choose_item_dp(){
	var si				 	= $('#tt_choose_dp').datagrid('getSelected').Invoice_Number;
	var Currencycode	 	= $('#tt_choose_dp').datagrid('getSelected').InvoiceCurrCode;
	var ItemCategoryName 	= $('#tt_choose_dp').datagrid('getSelected').ItemCategoryName;
	var PPnFiscalStatus  	= $('#tt_choose_dp').datagrid('getSelected').PPnFiscalStatus;
	var InvoiceType  		= $('#tt_choose_dp').datagrid('getSelected').Invoice_Type;
	var InvoiceFiscalRate  	= $('#tt_choose_dp').datagrid('getSelected').InvoiceFiscalRate;
	var InvoiceCurrRate  	= $('#tt_choose_dp').datagrid('getSelected').InvoiceCurrRate;
	var InvoiceAmount  		= $('#tt_choose_dp').datagrid('getSelected').Invoice_Amount;
	var InvoiceOwing  		= $('#tt_choose_dp').datagrid('getSelected').Invoice_Owing;
	
	$('#hdn_inv_amount').numberbox('setValue',InvoiceAmount);
	$('#hdn_inv_owing').numberbox('setValue',InvoiceOwing);
	$('#hdn_fiscal_status').val(PPnFiscalStatus);
	$('#hdn_inv_type').val(InvoiceType);
	$('#hdn_cust_curr_rate').numberbox('setValue', InvoiceCurrRate);
	$('#hdn_fiscal_rate').numberbox('setValue', InvoiceFiscalRate);
	
	/*
		Load datagrid detail
	*/
	$('#dtg_item').datagrid('load',{
		invoice_type : 0,
		invoice_name : InvoiceType,
		si_number: si
	});
	
	/*
	cek by type invoice, fiscal or dp
	*/
	if(ItemCategoryName == 'PPn Fiscal'){
		$('.spn_customer_curr_code').html('(IDR)');
	}
	else{
		$('.spn_customer_curr_code').html('('+Currencycode+')');
	}
	/*==================End============================================*/
			
	modal_close('dialog-sq');
}

function choose_item_sq(){
	var si				 	= $('#tt_choose').datagrid('getSelected').Invoice_Number;
	var Currencycode	 	= $('#tt_choose').datagrid('getSelected').InvoiceCurrCode;
	var ItemCategoryName 	= $('#tt_choose').datagrid('getSelected').ItemCategoryName;
	var PPnFiscalStatus  	= $('#tt_choose').datagrid('getSelected').PPnFiscalStatus;
	var InvoiceType  		= $('#tt_choose').datagrid('getSelected').Invoice_Type;
	var InvoiceFiscalRate  	= $('#tt_choose').datagrid('getSelected').InvoiceFiscalRate;
	var InvoiceCurrRate  	= $('#tt_choose').datagrid('getSelected').InvoiceCurrRate;
	var InvoiceAmount  		= $('#tt_choose').datagrid('getSelected').Invoice_Amount;
	var InvoiceOwing  		= $('#tt_choose').datagrid('getSelected').Invoice_Owing;
	
	$('#hdn_inv_amount').numberbox('setValue',InvoiceAmount);
	$('#hdn_inv_owing').numberbox('setValue',InvoiceOwing);
	$('#hdn_fiscal_status').val(PPnFiscalStatus);
	$('#hdn_inv_type').val(InvoiceType);
	$('#hdn_cust_curr_rate').numberbox('setValue', InvoiceCurrRate);
	$('#hdn_fiscal_rate').numberbox('setValue', InvoiceFiscalRate);
	
	/*
		Load datagrid detail
	*/
	
	if(ItemCategoryName == 'PPn Fiscal'){
		$('#dtg_item').datagrid('load',{
			invoice_type : 1,
			invoice_name : InvoiceType,
			si_number: si
		});
	}
	else{
		$('#dtg_item').datagrid('load',{
			invoice_type : 0,
			invoice_name : InvoiceType,
			si_number: si
		});
	}
	
	/*
	cek by type invoice, fiscal or dp
	*/
	if(ItemCategoryName == 'PPn Fiscal'){
		$('.spn_customer_curr_code').html('(IDR)');
	}
	else{
		$('.spn_customer_curr_code').html('('+Currencycode+')');
	}
	/*==================End============================================*/
			
	modal_close('dialog-sq');
}

function insert_data(){
	dtg_item = $('#dtg_item').datagrid('getRows');
	si_total_amount 	= 0;
	invoice_amount 		= 0;
	
	$.each(dtg_item,function(index,value){
			si_total_amount 	= parseFloat(value.Payment_Amount);
			invoice_amount 		= value.Invoice_Amount;
	});
	
	if($('#hdn_fiscal_status').val() == 1){
		if(si_total_amount != invoice_amount){
			$.messager.alert('Informasi','Maaf transaksi anda gagal, silahkan isi nilai sesuai Invoice Amount untuk Fiscal Payment','info');
			return false;
		}
		else{
			if(si_total_amount == 0){
				$.messager.alert('Informasi','Maaf transaksi anda gagal, nilai payment tidak boleh kosong','info');
				return false;
			}
			else if($('#hdn_lock_status').val() == 1){
				$.messager.alert('Warning','Maaf masih ada transaksi yang belum di approve di Purchases Journal, untuk vendor ini','warning');
			}
			else{
				var i       = $('#dtg_item').datagrid('getRows');
				var detail  = JSON.stringify(i);
			
				$("#detail").val(detail);	
				
				save_transaction('list/purchase_expense','#frm_purchase_journal');
			}
		}
	}
	else{
		if(si_total_amount == 0){
			$.messager.alert('Informasi','Maaf transaksi anda gagal, nilai payment tidak boleh kosong','info');
			return false;
		}
		/*else if($('#hdn_lock_status').val() == 1){
				$.messager.alert('Warning','Maaf masih ada transaksi yang belum di approve di Purchases Journal, untuk vendor ini','warning');
		}*/
		else{
			var i       = $('#dtg_item').datagrid('getRows');
			var detail  = JSON.stringify(i);
		
			$("#detail").val(detail);	
			
			save_transaction('list/purchase_expense','#frm_purchase_journal');
		}
	}
}

function tab_pop_up(){
	$('#tab_purchase_req').tabs({
		border:true,
		onSelect:function(title){
			if(title == 'Select Sales Invoice'){
				var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;
				
				$('#tt_choose').datagrid({
					url:'<?=base_url()?>list/purchase_expense/get_detail_invoice/'+vendor_code
				 });
			}
			else{
				var vendor_code = $('#cbg_vendor').combogrid('grid').datagrid('getSelected').VendorCode;

				 $('#tt_choose_dp').datagrid({
					url:'<?=base_url()?>list/purchase_expense/get_data_dp/'+vendor_code
				 });
			}
		}
	});
}

</script>