<!---- 
    Author 	: Wahyu
    Tanggal : 16-10-2014	
    Revisi 	: Fachri
-->

<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false" >
        <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
            <tr>
                <td align="left" valign="middle">
                   
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-journal" plain="true"  id="btn_journal" 
                    	onclick="parent.tab_add('Preview Journal <?=$menu_label?>','<?php echo $menu_link; ?>/to_preview/<?php echo $EPayNumber = str_replace("/",".", $detail->EPayNumber); ?>')">Journal
               		 </a>                    
                     <input type="checkbox" name="chk_suspended" id="chk_suspended" onClick="suspend(1)" <?php if($detail->TransStatus == 3){ echo"checked disabled"; } else if($detail->TransStatus == 2){ echo"disabled"; } else { echo"enabled"; } ?> />
                    Pembatalan Transaksi							
                </td>							
                <td height="25" align="right" valign="middle">								
                    <span class="form-title">
                        <?php echo $menu_label; ?>
                    </span>
                </td>
            </tr>
        </table>    
    </div>
    <!-- End of top view -->
    
    <div id="form-body" data-options="region:'center',border:false">
     <?php if($detail->TransStatus == 2){ ?>
        <div class="blanket"></div>
        <div class="finished-box"><b>Finished</b></div>
    <?php }else if($detail->TransStatus == 3){ ?>
    	 <div class="blanket"></div>
         <div class="suspend-box"><b>Suspended</b></div>
	<?php
	}
	?>
        <div id="form-wrapper" class="easyui-panel" style="padding:5px;" fit="true">
        	<form id="fm_purchase_expense_payment" name="fm_purchase_expense_payment" method="post">
                <input type="hidden" id="detail_payment" name="detail_payment" />
                <input type="hidden" id="detail_crcv" name="detail_crcv" />
                <input type="hidden" id="curren_code" name="curren_code" value="<?php echo  $detail->CurrencyCode; ?>"/>	
                <input type="hidden" id="curren_code_casbon" name="curren_code_casbon" />	
                <input type="hidden" id="txt_total_convert" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"  name="txt_total_convert" />	
                <input type="hidden" id="txt_status_cpv" name="txt_status_cpv" />
                <input type="hidden" id="txt_status_cal" name="txt_status_cal" />
                <input type="hidden" id="bank_pay" name="bank_pay" value="<?php echo $detail->BankReceipt; ?>"/>
                <!-- Form Header -->
                <table width="100%" height="140" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr>
                        	<td width="135"><b>Department</b></td>
                            <td width="12"><b>:</b></td>
                            <td width="414">
                            <select editable="false" name="cmb_departement" class="easyui-combogrid" id="cmb_departement" required="required" style="width:302px; height:22;" readonly data-options="
                                value:'<?php echo  $detail->DepartmentCode; ?>', 
                                panelWidth: 302,
                                idField: 'DepartmentCode',
                                textField: 'DepartmentName',
                                url: '<?php echo base_url(); ?><?php echo"activities/purchase_expense_cashbon"; ?>/get_department',                
                                columns: [[
                                {field:'DepartmentCode',title:'Departement Code',width:110},
                                {field:'DepartmentName',title:'Departement Name',width:190}
                                ]],
                                fitColumns: true
                                ">
                        </select>
                            	
<!--                                <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-sales'" onClick="select_expense()">Select Expense</a>
-->                            </td>
                        </tr>
                        <tr>
                        	<td><b>Employee</b></td>
                            <td><b>:</b></td>
                            <td>
                                <select editable="false" name="cmb_employee" class="easyui-combogrid" id="cmb_employee" required="required" style="width:302px; height:22px;" readonly data-options="
                                    value:'<?php echo  $detail->EmployeeCode; ?>', 
                                    panelWidth: 300,
                                    idField: 'EmployeeCode',
                                    textField: 'EmployeeName',
                                    url: '<?php echo base_url(); ?><?php echo"activities/purchase_expense_cashbon"; ?>/get_employee/<?php $depCode = str_replace(" ", ".", $detail->DepartmentCode); echo"$depCode"; ?>',              
                                    columns: [[
                                    {field:'EmployeeCode',title:'Employee Code',width:110},
                                    {field:'EmployeeName',title:'Employee Name',width:190}
                                    ]],
                                    fitColumns: true
                                ">
                                </select>	
                                
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr>
                        	<td width="120" rowspan="3">&nbsp;</td>
                        	<td width="160"><b>Exp. Payment No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
								<input type="text" name="txt_payment_no" id="txt_payment_no" class="easyui-validatebox disabled" required="true" value="<?php echo $detail->EPayNumber; ?>"  style="width:300px;" readonly />
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Exp. Payment Date</b></td>
                            <td><b>:</b></td>
                            <td>
								<input name="txt_payment_date" value="<?php echo $detail->EPayDate; ?>" id="txt_payment_date" type="text" class="easyui-datebox validBackward" required editable="false" data-options="panelWidth:302" style="width:302px;height:20px" readonly="readonly"/>
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Exp Cashbon No.</b></td>
                            <td><b>:</b></td>
                            <td>
								<input type="text" name="txt_exp_paymentno_other" id="txt_exp_paymentno_other" class="easyui-validatebox" style="width:300px;" value="<?php echo $detail->EBonNumber; ?>" readonly="readonly"/>
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan="2"><hr/></td></tr>
                <tr>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <!--<tr>
                        	<td width="120"><b>Bank</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<select name="cmb_bank" class="easyui-combogrid" 
                                    id="cmb_bank" required editable="false" 
                                    style="width:302px;height:22px;" 
                                    data-options="
                                        panelWidth: 302,
                                        idField: 'BankCode',
                                        textField: 'BankName',
                                        url: '<?=base_url()?>custom/get_data/bank_by_branch_new',               
                                        columns: [[                  
                                        	{field:'BankCode',title:'Bank Code',width:190,halign:'center'},
                                            {field:'BankName',title:'Bank Name',width:190,halign:'center'}
                                        ]],
                                        fitColumns: true
                                    "
                                >
                                </select>
                            </td>
                        </tr>-->
                        <tr>
                        	<td width="138"><b>Rate</b></td>
                            <td width="12"><b>:</b></td>
                            <td width="410">
                            	<input type="text" style="width:100px;" name="txt_rate" id="txt_rate" class="easyui-numberbox" required="true" autocomplete="off" value="<?php echo $detail->EPayRate; ?>"/> 
                                <b>Currency : </b><?php echo  $detail->CurrencyCode; ?> TO IDR
                            </td>
                        </tr>
                        <tr>
                        	<td valign="top" style="padding-top:4px"><b>Description</b></td>
                            <td valign="top" style="padding-top:4px"><b>:</b></td>
                            <td>
								<textarea style="resize:none;width:296px;white-space:normal;" name="txa_memo" id="txa_memo" rows="4"><?php echo $detail->EPayDesc; ?></textarea>
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Total Amount (<span id="span_total_amont"></span>)</b></td>
                            <td><b>:</b></td>
                            <td>
								<input type="text" name="total_payment" id="total_payment" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo $amount_expensepay; ?>" style="width:270px;text-align:right;" readonly />
                                <a href="#" id="icon-calculator" class="easyui-linkbutton" data-options="iconCls:'icon-calculator',size:'small'" onClick="total_amount()" ></a>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr>
                        	<td width="120">&nbsp;</td>
                        	<td width="160"><b>Exp. Cash-Bon Amount</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input type="text" name="txt_cashbon_amount" id="txt_cashbon_amount"  class="easyui-numberbox" value="<?php echo $amount_cashbon; ?>" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.',prefix:''" disabled style="width:302px;text-align:right;"/>
                                <input type="hidden" name="txt_ebon_number" id="txt_ebon_number" class="easyui-validatebox" value="<?php echo $detail->EBonNumber; ?>" style="width:120px"/>
                                <input type="hidden" name="txt_ebon_date" id="txt_ebon_date" class="easyui-validatebox" style="width:120px" value="<?php echo $ebon_date; ?>"/>
                                <input type="hidden" name="ExpGroupId" id="ExpGroupId" />
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                </table>
                <!-- End of form header -->
                
                <!-- Datagrid -->
				<!--Item Grid -->
				<div class="easyui-tabs" id="tab_exp" style="margin-top:5px;" data-options="tabHeight:18">
					<div id="tab_item" title="Expense">
						<table toolbar="#tb_dg_expense" id="dg_expense" 
                        	class="easyui-edatagrid" title="" 
                            idField="id" 
						 	rownumbers="true" fitColumns="true" url="<?php echo base_url(); ?><?php echo $menu_link; ?>/ajax_detail_payment/<?php echo $id = str_replace("/",".", $detail->EPayNumber); ?>"
                            singleSelect="true" style="height:255px;"
                        >
							<thead>
                            <tr>
                                <th field="ExpDetailID" hidden="true" width="0" editor="{type:'text'}" data-options="align:'left',halign:'center'">ID</th>
                                <th field="EPayDetailID" width="0" hidden="true" data-options="align:'left',halign:'center'">Epay Detail Id</th>
                                <th field="ExpDetailName" width="30" data-options="align:'left',halign:'center'">Name</th>
                                <th field="ExpDetailDesc" width="30" editor="ItemDesc" data-options="align:'left',halign:'center'">Description</th>
                                <th field="ItemAmount"  width="30" formatter="formatRp" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.'}}" data-options="align:'right',halign:'center'">Amount <span id="span_amont"></span></th>
                                <!--<th field="TotalRate" width="30" editor="{type:'numberbox'}" data-options="align:'right',halign:'center'">Total Amount</th>	-->							 
                            </tr>
							</thead>
						</table>
						<div id="tb_dg_expense">
							<!--<a href="#" class="easyui-linkbutton" id="expense-delete" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg_expense').edatagrid('destroyRow')">Delete</a>-->
						</div>
					</div>
					<div id="tab_expense" title="<span class='titel_crv'>CRV</span> <span class='titel_cpv'>CPV</span>">
                        <table id="dg_cpv" class="easyui-edatagrid" 
                            title="" idField="id" 
                            rownumbers="true" fitColumns="true" 
                            singleSelect="true" style="height:255px;" 
                        >
                            <thead>
                            <tr>
                            	<th field="BankName" align="center" valign="middle" data-options="width:250,align:'left'" editor="{type:'BankName'}">Account Name</th>
                                <th field="AmountCRV" width="110" formatter="formatRp"  editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true}}" data-options="align:'right',halign:'center'">Payment Amount <?php echo  $detail->CurrencyCode; ?></th></th>
                                <th field="PaymentAmount" formatter="formatRp" width="110" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true}}" data-options="align:'right',halign:'center'">Cash-Bon Amount <?php echo  $detail->CurrencyCode; ?></th>
								<th field="AmountDiff" width="110" formatter="formatRp" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true}}" data-options="align:'right',halign:'center'">Exceed/Under Amount <?php echo  $detail->CurrencyCode; ?></th>
                                <th field="BankCode" width="110" hidden="true" editor="{type:'BankCode'}" data-options="align:'right',halign:'center'">Bank Code</th>

                            </tr>
                            </thead>
                        </table>		
					</div>					
				</div>
                <!-- End of Datagrid -->
			</form>
    	</div>
    </div>
    
   <div id="form-footer" data-options="region:'south',border:false">
			<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
				 <tr>
				   <td align="left">
						
				   </td>
					<td align="right">                              
						 <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
					     <!--<a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-ok" <?php if($this->session->userdata("jobtitle_status") == 0 && $detail->AppLevel2Status !=1 && $detail->TransStatus !=2 ){ echo"enabled"; } else{ echo"disabled"; } ?> onclick="custom_data('<?php echo"activities/purchase_expense_real"; ?>/approval1','#fm_purchase_expense_payment');"> App. Dir</a> -->                       
						 <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" 
						 <?php 
					 		if(date('Y-m-d') < date('Y-m-d', strtotime($detail->EPayDate)) || $this->session->userdata('jobtitle_status') != 1){
								echo "disabled";
							} 
							else if(($detail->TransStatus == 3) || ($detail->TransStatus == 2)){
								echo "disabled";
							}
							else if($this->session->userdata('jobtitle_status') == 1){
								echo "enabled";
							}
					 	?> 
                         onclick="custom_data('<?php echo"activities/purchase_expense_real"; ?>/approval2','#fm_purchase_expense_payment');" >App. Mgr</a>	
						 
						 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="update_data();" <?php if($detail->TransStatus ==3 || $detail->TransStatus ==2){ echo "disabled"; } else { echo"enabled"; } ?>>Save</a> 								
						 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_add('Print Payment Cashbon', '<?php echo"list/purchases_expense_real"; ?>', 'icon-purchase', 'to_preview')">Preview & Print</a>   
						 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="location.reload()" disabled>Cancel</a>
					</td>
				 </tr>
			</table>				
		</div>		
        
   
</div>

<!-- dialog select expense group-->

<div id="dialog-expense" class="easyui-dialog" style="width:550px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons">
	<form id="form_account" method="post" novalidate>
		<table width="100%" align="left">
        <tr>
            <td width="120"><b>Expense Group</b></td>
            <td width="10"><b>:</b></td>
            <td>
                <select name="cmb_expense_group" class="easyui-combogrid" 
                    id="cmb_expense_group" required editable="false" 
                    style="width:400px;height:22px;" 
                    data-options="
                        panelWidth: 400,
                        idField: 'ExpCategoryID',
                        textField: 'ExpCategoryName',
                        url: '<?php echo base_url(); ?><?php echo"activities/purchase_expense_real/"; ?>get_expense_group',                
                        columns: [[
                            {field:'ExpCategoryID',title:'Group ID',width:150},
                            {field:'ExpCategoryName',title:'Group Name',width:250}
                        ]],
                        fitColumns: true
                    "
                >
                </select>	
            </td>
        </tr>
      	</table>
        <table id="detail_item_expense_gorup" class="easyui-datagrid" 
            title="" idField="ExpDetailID"
            pageSize="20" pagination="true" 
            remoteSort="false" rowNumbers="true" 
            fitColumns="true" style="width:536px;height:422px;"
        >
            <thead>
            <tr>
                <th field="ck" checkbox="true" width="20">&nbsp;</th>
                <th field="ExpDetailID" width="20" hidden="true" sortable="true">ID</th>
                <th field="ExpDetailName" width="100" sortable="true">Item Name</th>
                <th field="ExpDetailDesc" width="338" sortable="true">Item Description</th>
            </tr>
            </thead>
        </table>
	</form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons" style="border:none;">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="choose_item_expense_group()">OK</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="modal_close('dialog-expense')">Close</a>
</div>

<!-- dialog select expense  cashbon list-->
<div id="dialog-expense-cashbon-list" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-expense-cashbon-button">
	<table id="dtg_expense_cashbon_list" class="easyui-datagrid"  
        singleSelect="true" fitColumns="true" 
        pageSize="20" pagination="true" 
        remoteSort="false" rowNumbers="true" 
        fit="true"
    >
		<thead>
			<tr>
				<th data-options="field:'EBonNumber',width:180,halign:'center',align:'left'" sortable="true">Exp. Cash-Bon No.</th>
				<th data-options="field:'EBonDate',width:80,halign:'center',align:'center'" sortable="true" formatter="formatDate">Date</th>
                <th data-options="field:'EBonAmount',width:110,halign:'center',align:'right'" sortable="true" formatter="formatRp">Amount</th>
                <th data-options="field:'CurrencyCode',width:110,halign:'center',align:'right'" sortable="true" hidden="true">Currency Code</th>
                <th data-options="field:'CurrencyRate',width:110,halign:'center',align:'right'" sortable="true" hidden="true">Currency Rate</th>
			</tr>
		</thead>
	</table>
</div>

<div id="dialog-expense-cashbon-button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_cashbon();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-expense-cashbon-list')">Close</a>
</div>

<div id="dialog-expense-bank" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-expense-bank-button">
	<table id="dtg_bank" class="easyui-datagrid"  
        singleSelect="true" fitColumns="true" 
        pageSize="20" pagination="true" 
        
        remoteSort="false" rowNumbers="true" 
        fit="true"
    >
		<thead>
			<tr>
				<th data-options="field:'ExpBankNumber',width:180,halign:'center',align:'left'" sortable="true">Account Number</th>
				<th data-options="field:'ExpBankName',width:80,halign:'center',align:'center'" sortable="true">Account Name</th>
                <th data-options="field:'ExpCurrency',width:80,halign:'center',align:'center'" sortable="true">Exp Currency</th>
			</tr>
		</thead>
	</table>
</div>
<div id="dialog-expense-bank-button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_bank();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-expense-bank')">Close</a>
</div>

<script>
    $(document).ready(function(){		
		var total_cash = $("#txt_cashbon_amount").numberbox('getValue');
        var total_pay  = $('#total_payment').numberbox('getValue');	
		var cek =  total_cash - total_pay;
		
		if($('#txt_status_cpv').val() == 1){
			$('#dg_cpv').datagrid({
   		 		url:'',
			});
		}
		else if($('#txt_status_cpv').val() == ''){
			$('#dg_cpv').datagrid({
   		 		url:'<?php echo base_url(); ?><?php echo $menu_link; ?>/ajax_detail_actual/<?php echo $id = str_replace("/",".", $detail->EPayNumber); ?>',
			});
		}
		
		if(cek > 0){
			$('.titel_crv').show();
		    $('.titel_cpv').hide();
			$('#dtg_bank').datagrid({
				url:'<?php echo base_url(); ?><?php echo $menu_link; ?>/get_akun/CRV/<?php echo $bank_paymanet; ?>'
			});
		} else if(cek < 0){
			$('.titel_cpv').show();
		    $('.titel_crv').hide();
			$('#dtg_bank').datagrid({
				url:'<?php echo base_url(); ?><?php echo $menu_link; ?>/get_akun/CPV/<?php echo $bank_paymanet; ?>'
			});
		}
		else{
			$('.titel_cpv').hide();
		    $('.titel_crv').hide();
			$('#tab_exp').tabs('disableTab',1);
		}
		
		//$('#fm_purchase_expense_payment').click(function(){
			//$('#dg_expense').edatagrid('saveRow');
		//});
		$('#span_total_amont').text('<?php echo  $detail->CurrencyCode; ?>');
		$('#span_amont').text('<?php echo  $detail->CurrencyCode; ?>');
	});

function search_items_crv(){
	$('#dtg_bank').datagrid({
		url:'<?php echo base_url(); ?><?php echo"activities/purchase_expense_real/get_akun/CRV/"; ?><?php echo $bank_paymanet; ?>'
	});
	
}

function search_items(){
	$('#dtg_bank').datagrid({
		url:'<?php echo base_url(); ?><?php echo"activities/purchase_expense_real/get_akun/CPV/"; ?><?php echo $bank_paymanet; ?>'
	});
	
}

function total_amount(){
	
	var total_amount = 0;
	$('#dg_expense').edatagrid('saveRow');
	var rows =$('#dg_expense').datagrid('getRows');
		
	for(var i=0; i<rows.length; i++){
		if(isNaN(rows[i].ItemAmount)){ rows[i].ItemAmount = 0; }
		total_amount += parseFloat(rows[i].ItemAmount);
	}
	
	$('#total_payment').numberbox('setValue', total_amount);
	
	var payment_all		= $('#total_payment').numberbox('getValue');
	var total_cashbon	= $("#txt_cashbon_amount").numberbox('getValue');
	var txt_rate		= $("#txt_rate").numberbox('getValue');
	
	var cek_selisih 	= total_cashbon - total_amount;
	var selisih			= formatRp(cek_selisih);
	var total_rate 		= payment_all * txt_rate;
	
	$('#txt_total_convert').numberbox('setValue',total_rate);
	$('#txt_status_cpv').val(1);
	
	if($('#txt_status_cpv').val() == 1){
		$('#dg_cpv').datagrid({
   		 	url:'',
		});
	}
	else{
		$('#dg_cpv').datagrid({
   		 	url:'<?php echo base_url(); ?><?php echo $menu_link; ?>/ajax_detail_actual/<?php echo $id = str_replace("/",".", $detail->EPayNumber); ?>',
		});
	}
	
	if(cek_selisih > 0){
		
		if($('#txt_status_cpv').val() == 1){
			$('#dg_cpv').datagrid('reload');
			$('#dg_cpv').edatagrid('updateRow',{
				index:0, 
				row:{PaymentAmount      : total_cashbon,
					 AmountCRV			: payment_all,
					 AmountDiff 		: cek_selisih}
			});
		}
		/*else{
			$('#dg_cpv').datagrid('reload');
			$('#dg_cpv').edatagrid('updateRow',{
				index:0, 
				row:{PaymentAmount      : total_cashbon,
					 AmountCRV			: payment_all,
					 AmountDiff 		: cek_selisih}
			});
		}*/
		
			
		 $('#tab_exp').tabs('enableTab',1);
		 $('#tab_exp').tabs('update',{tab:$('#tab_exp').tabs('getTab',1),options:{title:'CRV'}});
		 $('#tab_exp').tabs('select', 1);
		 $('#bank_pay').val("");
		 //$('.BankName').val("");
		 search_items_crv();
		
		
	} else if(cek_selisih < 0){
		var tol_kurang = total_amount - total_cashbon;
		var kurang = formatRp(tol_kurang);
		
		if($('#txt_status_cpv').val() == 1){
			$('#dg_cpv').datagrid('reload');
			$('#dg_cpv').edatagrid('updateRow',{
				index:0, 
				row:{PaymentAmount      : total_cashbon,
					 AmountCRV			: payment_all,
					 AmountDiff 		: tol_kurang}
			});
		}
		/*else{
			$('#dg_cpv').datagrid('reload');
			$('#dg_cpv').edatagrid('addRow',{
				index:0, 
				row:{PaymentAmount      : total_cashbon,
					 AmountCRV			: payment_all,
					 AmountDiff 		: tol_kurang}
			});
		}*/

		 
		 $('#tab_exp').tabs('enableTab',1);
		 $('#tab_exp').tabs('update',{tab:$('#tab_exp').tabs('getTab',1),options:{title:'CPV'}});
		 $('#tab_exp').tabs('select', 1);
		 $('#bank_pay').val("");
		 //$('.BankName').val("");
		 search_items();

	} else if(cek_selisih == 0){
			$('#txt_status_addrow').val(0);
		   $('.titel_crv').hide();
		   $('.titel_cpv').hide();
		   
		   $('#tab_exp').tabs('disableTab',1);
		   $('#bank_pay').val(1);
	}
}

function open_expense_bank(){
	modal_open('Select Bank','dialog-expense-bank');
}

function update_data(){
	var cek_total = $('#total_payment').numberbox('getValue');
	var cek = $('#dg_expense').datagrid('getRows');
	var rate = $('#txt_rate').numberbox('getValue');
	$('#dg_expense').edatagrid('saveRow');
	var hasil_amount = 0;
	
	$.each(cek, function(index, value) {
		if(isNaN(value.ItemAmount)){ value.ItemAmount = 0; }
         hasil_amount += parseFloat(value.ItemAmount);			
	});
	
	if(parseFloat(cek_total) != hasil_amount){
		$.messager.alert('Unseccessfull','Total Payment tidak sama dengan Amount.','error',function(){
			$('#dg_expense').datagrid('reload');
		});
	}
	else if($('#bank_pay').val() == ""){
		$.messager.alert('Unseccessfull','Account Name Tidak Boleh Kosong','error',function(){
			$('#dg_expense').datagrid('reload');
		});
	}
	else if(rate < 1){
		$.messager.alert('Unseccessfull','Rat Tidak Boleh Kurang Dari 1','error',function(){
			$('#dg_expense').datagrid('reload');
		});
	}
	else{
		var i=$('#dg_expense').datagrid('getRows');
		var items = JSON.stringify(i);
		$("#detail_payment").val(items);
		
		$('#dg_cpv').edatagrid('saveRow');	
		var j=$('#dg_cpv').datagrid('getRows');
		var crv_cpv = JSON.stringify(j);
		$("#detail_crcv").val(crv_cpv);
		
		edit_data('<?php echo"$menu_link"; ?>', '#fm_purchase_expense_payment');
	}
}

function choose_bank(){
	var selected_data = $('#dtg_bank').datagrid('getSelected');
	$('.BankName').val(selected_data.ExpBankName);
	$('.BankCode').val(selected_data.ExpBankNumber);
	$('#spn_currencyNameId').html(selected_data.ExpCurrency);
	modal_close('dialog-expense-bank');
	
	nilai_exceed 	= $('#txt_exceed_hidden').val();
	nilai_rate 		= $('#txt_rate').val();
	total 			= parseFloat(nilai_exceed) * parseFloat(nilai_rate);
	format_rp 		= formatRp(total);
	
	$('#curren_code').val(selected_data.ExpCurrency);
	$('#total_exceed_convert').val(total);
	$('#total_exceed').val(total);
	$('#bank_pay').val(1);
}
		
$.extend($.fn.datagrid.defaults.editors, {
ItemDesc: {
	init: function(container, options){
		var input = $('<input type="text" class="ItemDesc" readonly />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
CurrencyCode: {
	init: function(container, options){
		var input = $('<input type="text" class="CurrencyCode" readonly />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
CurrencyRate: {
	init: function(container, options){
		var input = $('<input type="text" class="CurrencyRate" readonly />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
ItemAmount: {
	init: function(container, options){
		var input = $('<input type="text" class="ItemAmount easyui-numberbox" required style="text-align:right;" data-options="precision:2,decimalSeparator:\',\',groupSeparator:\'.\'" />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
PaymentAmount: {
	init: function(container, options){
		var input = $('<input type="text" class="PaymentAmount" style="text-align:right;" />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
ItemName: {
	init: function(container, options){
		var input = $('<input type="text" class="ItemName" onClick="open_expense_group()" required readonly style="cursor:pointer;" />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
AmountCRV: {
	init: function(container, options){
		var input = $('<input type="text" class="AmountCRV" style="text-align:right;" readonly/>').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
AmountCashbon: {
	init: function(container, options){
		var input = $('<input type="text" class="AmountCashbon" style="text-align:right;" readonly />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
AmountDiff: {
	init: function(container, options){
		var input = $('<input type="text" class="AmountDiff" style="text-align:right;" readonly />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
},
BankName: {
		init: function(container, options){
			var input = $('<input type="text" class="BankName" onClick="open_expense_bank()" readonly style="cursor:pointer;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
BankCode: {
	init: function(container, options){
		var input = $('<input type="text" class="BankCode" readonly style="cursor:pointer;" />').appendTo(container);
		return input.validatebox(options);
	},
	destroy: function(target){
		$(target).remove();
	},
	getValue: function(target){
		 return $(target).val();
	},
	setValue: function(target, value){
		  $(target).val(value);
	},
	resize: function(target, width){
		   $(target)._outerWidth(width);
	}
}
});

function suspend(type){
	suspend_data_new('F', type, '<?=$menu_link?>', $('#txt_payment_no').val(), 'Expense Payment');
}
</script>