<!---- 
    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">
                <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Expense Payment','<?="activities/purchase_expense_real/"?>','icon-add','index')">New</a>
            	<a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload()">Refresh</a>
            </td>
            <td height="25" align="right" valign="middle">
                <span class="form-title"><?=$menu_label?></span>
            </td>
        </tr>
        </table>
    </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="fm_purchase_expense_payment" name="fm_purchase_expense_payment" method="post" action="<?php echo base_url(); ?><?php echo"activities/purchase_expense_real/insert"; ?>">
                <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" />	
                <input type="hidden" id="curren_code_casbon" name="curren_code_casbon" />	
                <input type="hidden" id="txt_exceed_hidden" name="txt_exceed_hidden" />	
                <input type="hidden" id="total_exceed" name="total_exceed" />
                <input type="hidden" id="total_exceed_convert" name="total_exceed_convert" />
                <input type="hidden" id="rate_casbon" name="rate_casbon" />
                <input type="hidden" id="txt_exceed_hidden_idr" name="txt_exceed_hidden_idr" />
                <input type="hidden" id="BankPayment" name="BankPayment" />
                <input type="hidden" id="txt_status_cpv" name="txt_status_cpv" />
                <input type="hidden" id="status_group" name="status_group" />
                
                <!-- Form Header -->
                <table width="100%" height="140" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="51%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr>
                        	<td width="130"><b>Department</b></td>
                            <td width="12"><b>:</b></td>
                            <td width="431">
                            	<select name="cmb_departement" class="easyui-combogrid" 
                                    id="cmb_departement" required editable="false" readonly
                                    style="width:302px;height:22px;" 
                                    data-options="
                                    	value:'<?=$department_code?>', 
                                        panelWidth: 302,
                                        idField: 'DepartmentCode',
                                        textField: 'DepartmentName',
                                        url: '<?=base_url()?>custom/get_data/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 name="cmb_employee" class="easyui-combogrid" 
                                    id="cmb_employee" required editable="false" 
                                    style="width:302px; height:22px;"
                                >
								</select>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="49%" 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=""  style="width:300px;" readonly />
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Exp. Payment Date</b></td>
                            <td><b>:</b></td>
                            <td>
								<input name="txt_payment_date" id="txt_payment_date" type="text" class="easyui-datebox validBackward" required editable="false" data-options="panelWidth:302" style="width:302px;height:20px" value="<?php echo $date_now;?>"/>
                            </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;" readonly="readonly"/>
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan="2"><hr/></td></tr>
                <tr>
                    <td width="51%" 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="129"><b>Rate</b></td>
                            <td width="12"><b>:</b></td>
                            <td width="436">
                            	<input type="text" style="width:100px;" name="txt_rate" id="txt_rate" class="easyui-validatebox" required="true" /> 
                                <b>Currency : </b><span id="spn_currencyName"></span> TO <span id="spn_currencyNameId"></span>
                            </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"></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" formatter="formatRp" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" value="0" 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()" disabled></a>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="49%" 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" 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" style="width:120px"/>
                                <input type="hidden" name="txt_ebon_date" id="txt_ebon_date" class="easyui-validatebox" style="width:120px"/>
                                <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" 
                            singleSelect="true" style="height:255px;"
                            url="<?php echo base_url().$menu_link."/ajax_item"; ?>"
                        >
							<thead>
                            <tr>
                                <th field="ExpDetailID" hidden="true" width="0" editor="{type:'text'}" data-options="align:'left',halign:'center'">ID</th>
                                <th field="ExpDetailName" width="30" editor="{type:'ItemName'}" 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:'.',min:0}}" 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-new" iconCls="icon-add" plain="true" onclick="open_dialog_expense_group()">New</a>
							<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="" data-options="tabWidth:65">
                        <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:'.'}}" data-options="align:'right',halign:'center'">Payment Amount <span id="span_idr_usd_2"></span></th>
                                <th field="PaymentAmount" formatter="formatRp" width="110" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',groupSeparator:'.'}}" data-options="align:'right',halign:'center'">Cash-Bon Amount <span id="span_idr_usd"></span></th>
								<th field="AmountDiff" width="110" formatter="formatRp" editor="{type:'numberbox',options:{min:0,precision:2,decimalSeparator:',',groupSeparator:'.'}}" data-options="align:'right',halign:'center'">Exceed/Under Amount <span id="span_idr_usd_3"></span></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 width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td>&nbsp;</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-save" id = "save_data" onclick="save_payment()">Save</a> 								
                <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="" disabled>Preview & Print</a>   
                <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="parent.tab_close_this('tt')">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',
                        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:16px;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>
                <th data-options="field:'BankPayment',width:110,halign:'center',align:'right'" sortable="true" hidden="true">Bank Payment</th>
                <th data-options="field:'EBonLockStatus',width:110,halign:'center',align:'right'" sortable="true" >Status Lock</th>
                <th data-options="field:'EBonAccountNumber',width:110,halign:'center',align:'right'" sortable="true" hidden="true">EBon Account Number</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:100,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:'BranchCode',width:80,halign:'center',align:'center'" sortable="true">Branch Code</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 type="text/javascript">
$(document).ready(function(){
	$('#tab_exp').tabs('disableTab',1);
	
	/*$('#fm_purchase_expense_payment').click(function(){
		$('#dg_expense').edatagrid('saveRow');
	});*/
	
	$('#txt_payment_no').val('<?php echo $exp_payment_number; ?>');
	//$('#cmb_bank')
	
	//$('#txt_payment_date').datebox({
		/*onChange:function(){
			epay_date = $(this).datebox('getValue').split('-');
			epay_month = epay_date[1];
			
			if(epay_month != <?=date('m')?>){
				create_epay_number($(this).datebox('getValue'),$('#txt_bankout').val());
			}
		}*/
	//});
	
	$('#txt_payment_date').datebox({
		onChange:function(){
			url             = base_url + 'activities/purchase_expense_real/get_epay_number';                
			var EPay_Date   = $('#txt_payment_date').datebox('getValue');
			
			$.post(url, {EPay_Date:EPay_Date}, function(result){
				if (result.status === true){
					$('#txt_payment_no').val(result.number);
				} 
			},'json');
		}
	});
	
	$('#cmb_employee').combogrid({
		delay: 300,
		url: '<?php echo base_url(); ?><?php echo"activities/purchase_expense_cashbon"; ?>/get_employee/<?=$department_code?>',
		idField: 'EmployeeCode',
		textField: 'EmployeeName',
		columns: [[
			{field:'EmployeeCode',title:'EmployeeCode',width:88,sortable:true},
			{field:'EmployeeName',title:'EmployeeName',width:165,sortable:true}
		]]
	});
	
	$('#cmb_employee').combogrid({
		onSelect: function(rowIndex, rowData){	
			var EmpCode = rowData.EmployeeCode;
			$('#dtg_expense_cashbon_list').datagrid({
				url:'<?php echo base_url(); ?><?php echo'activities/purchase_expense_real'; ?>/ajax_list_cashbon/'+EmpCode,
			});
			
			if($('#status_group').val() == 1){	
				location.reload();
			}
		}
    });
	
	$('#cmb_expense_group').combogrid({
		onSelect: function(rowIndex, rowData){	
			var Groupid = rowData.ExpCategoryID;
			
			$('#detail_item_expense_gorup').datagrid({
				url:'<?php echo base_url(); ?><?php echo'activities/purchase_expense_real'; ?>/ajax_list_item_group_expense/'+Groupid,
			});
			
			$("#ExpGroupId").val(Groupid);
		}
    });
});

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

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

function group_exp(ebon_akun){
	$('#cmb_expense_group').combogrid({
	 	url: '<?php echo base_url(); ?><?php echo"activities/purchase_expense_real/"; ?>get_expense_group/'+ebon_akun,
	});
}

function save_payment(){
	var i=$('#dg_expense').datagrid('getRows');
	var items = JSON.stringify(i);
	$("#detail_payment").val(items);
	
	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 total_cashbon = $("#txt_cashbon_amount").numberbox('getValue');
	
	var cek_selisih =  total_cashbon - total_amount;
	
	if(cek_selisih != 0){
		$('#dg_cpv').edatagrid('saveRow');	
		var j=$('#dg_cpv').datagrid('getRows');
		var crv_cpv = JSON.stringify(j);
		$("#detail_crcv").val(crv_cpv);
	}
	
	var cek_total = $('#total_payment').numberbox('getValue');
	var selisih_all =  total_cashbon - cek_total;
	
	
	if(cek_total < 1){
		$.messager.alert('Unseccessfull','Transaksi Belum Lengkap','error');
		return false;
	} 
	else if(total_amount != parseFloat(cek_total)){		
		 $.messager.alert('Unseccessfull','Total Amount Tidak Sama Dengan Amount, Silahkan Masukan Data Yang Benar','error',function(){					
				
		});	
	}
	else {
		//$("#fm_purchase_expense_payment").submit();
		if(selisih_all > 0){
			$('#tab_expense').show();
			$('.titel_crv').show();
			$('.titel_cpv').hide();
			$('#tab_exp').tabs('select', 1);				
		} else if(selisih_all < 0){
			$('#tab_expense').show();
			$('.titel_cpv').show();
			$('.titel_crv').hide();
			$('#tab_exp').tabs('select', 1);				
		} else {
			$('#tab_exp').tabs('close', 1);
			$('#tab_exp').tabs('select', 0);
		}
		save_transaction('<?php echo"$menu_link"; ?>', '#fm_purchase_expense_payment','1');
	}

}

function select_expense(){
	var Employee			= $('#cmb_employee').combogrid('grid');	// get datagrid object
	var SelectedEmployee	= Employee.datagrid('getSelected');    // get the selected row

	if(SelectedEmployee === null ){
		$.messager.alert('Maaf','Silahkan pilih Employee dulu','warning');
	}
	else{
		 modal_open('Select Expense Casbon','dialog-expense-cashbon-list');
	}	
	
}

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


function choose_cashbon(){
	var selected_data = $('#dtg_expense_cashbon_list').datagrid('getSelected');
	
	if(selected_data.EBonLockStatus == 'Lock'){
		$.messager.alert('Unseccessfull','Transaksi Casbon Terkunci Silahkan Approve Terlebih Dahulu','error');
		return false;
	}
	else{
		$('#txt_cashbon_amount').numberbox('setValue',selected_data.EBonAmount);
		$('#txt_ebon_number').val(selected_data.EBonNumber);
		$('#txt_exp_paymentno_other').val(selected_data.EBonNumber);
		
		$('#txt_ebon_date').val(selected_data.EBonDate);
		$('#curren_code_casbon').val(selected_data.CurrencyCode);
		$('#rate_casbon').val(selected_data.CurrencyRate);
		$('#spn_currencyName').text(selected_data.CurrencyCode);
		$('#BankPayment').val(selected_data.BankPayment);
		$('#spn_currencyNameId').text("IDR");
		
		
		$('#span_amont').text(selected_data.CurrencyCode);
		$('#span_total_amont').text(selected_data.CurrencyCode);
		
		group_exp(selected_data.EBonAccountNumber);
		
		if(selected_data.CurrencyCode == "IDR"){
			$('#txt_rate').prop('readonly',true);
			$('#txt_rate').val(1);
		}
		else{
			$('#txt_rate').prop('readonly',false);
			$('#txt_rate').val(1);
		}
		var txt_amont_payment		= $('#total_payment').numberbox('getValue');
		if(txt_amont_payment != 0){	
			location.reload();
		}
		
		if($('#status_group').val() == 1){	
			location.reload();
		}
		
		modal_close('dialog-expense-cashbon-list');
	}
}
	
function open_dialog_expense_group(){ 
	if( $('#txt_ebon_number').val()!='' ){
   		modal_open('Expense Group','dialog-expense');
	}
	else{
		$.messager.alert('Peringatan','Silahkan pilih transaksi Expense terlebih dahulu.');
	}
}

function choose_bank(){
	var selected_data = $('#dtg_bank').datagrid('getSelected');
	$('.BankName').val(selected_data.ExpBankName);
	$('.BankCode').val(selected_data.ExpBankNumber);
	$('#spn_currencyNameId').html('IDR');
	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);
}
	
function choose_item_expense_group(){
	//$('#dg_expense').datagrid();
	var ExpDetailID       = new Array();
	
		
		
	var rows = $('#detail_item_expense_gorup').datagrid('getSelections');
	$.each(rows, function(index, value) {
		if(value.ExpDetailID != undefined){
			ExpDetailID.push(value.ExpDetailID);
		}
	});
	
	$('#dg_expense').datagrid('load',{
		ExpDetailID: ExpDetailID
	});
	
	$('#icon-calculator').linkbutton('enable');
	$('#status_group').val(1);
	modal_close('dialog-expense');
}
	
$.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);
		}
	},
	BankPayment: {
		init: function(container, options){
			var input = $('<input type="text" class="BankPayment" 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);
		}
	},
	BankName: {
		init: function(container, options){
			var input = $('<input type="text" class="BankName" onClick="open_expense_bank()" required 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);
		}
	},
	EBonAccountNumber: {
		init: function(container, options){
			var input = $('<input type="text" class="EBonAccountNumber" 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);
		}
	}
});
	
function total_amount(){
	flag = true;
	
	if( $('#txt_payment_date').datebox('getValue')=='' ){
		$.messager.alert('Peringatan','Silahkan pilih tanggal terlebih dahulu.');
		flag = false;
	}
	
	
	if(flag==true){
		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 cek_selisih 	= total_cashbon - total_amount;
		var selisih			= formatRp(cek_selisih);
		
		EPayDate = $('#txt_payment_date').datebox('getValue');
		if(cek_selisih > 0){
			//create_epay_number(EPayDate/*,BankInCode*/);
			
			if($('#txt_status_cpv').val() == 1){
				$('#dg_cpv').datagrid('reload');
				$('#dg_cpv').edatagrid('addRow',{
                index:0, 
                row:{PaymentAmount      : total_cashbon,
					 AmountCRV			: payment_all,
					 AmountDiff 		: cek_selisih}
            	});
			}
			else{
				$('#dg_cpv').datagrid('reload');
				$('#dg_cpv').edatagrid('addRow',{
                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);
			$('#txt_exceed_hidden').val(cek_selisih);
			$('#txt_status_cpv').val(1);
			$('#txt_exceed_hidden_idr').val(cek_selisih);
			
			curren_code = $('#spn_currencyName').text();
			BankPayment = $('#BankPayment').val();
			
			if(curren_code == 'IDR'){
				$('#span_idr_usd').text(curren_code);
				$('#span_idr_usd_2').text(curren_code);
				$('#span_idr_usd_3').text(curren_code);
			}
			else{
				$('#span_idr_usd').text(curren_code);
				$('#span_idr_usd_2').text(curren_code);
				$('#span_idr_usd_3').text(curren_code);
			}
			search_items_crv(curren_code, BankPayment);

		} else if(cek_selisih < 0){
			//create_epay_number(EPayDate/*,BankOutCode*/);
			
			var tol_kurang = total_amount - total_cashbon;
			var kurang = formatRp(tol_kurang);
			
			if($('#txt_status_cpv').val() == 2){
				$('#dg_cpv').datagrid('reload');
				$('#dg_cpv').edatagrid('addRow',{
                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);
			$('#txt_exceed_hidden').val(tol_kurang);
			$('#txt_status_cpv').val(2);
			$('#txt_exceed_hidden_idr').val(tol_kurang);
			
			curren_code = $('#spn_currencyName').text();
			BankPayment = $('#BankPayment').val();
			
			if(curren_code == 'IDR'){
				$('#span_idr_usd').text(curren_code);
				$('#span_idr_usd_2').text(curren_code);
				$('#span_idr_usd_3').text(curren_code);
			}
			else{
				$('#span_idr_usd').text(curren_code);
				$('#span_idr_usd_2').text(curren_code);
				$('#span_idr_usd_3').text(curren_code);
			}
			
			search_items(curren_code, BankPayment);

	//		if(payment_all < 0){
	//			$('#tab_exp').tabs('select', 1);
	//		} 
		} else{
			//create_epay_number(EPayDate/*,BankOutCode*/);
			$('#txt_status_cpv').val(0);
		   $('#tab_exp').tabs('disableTab',1);
			$('#tab_exp').tabs('update',{tab:$('#tab_exp').tabs('getTab',1)});
			$('#tab_exp').tabs('select', 1);
		}
	}
}

//function create_epay_number(epay_date/*,bank_code*/){
	/*var url = base_url +'activities/purchase_expense_real/get_epay_number';

	$.post(url, {EPay_Date:epay_date}, function(result){
		if (result.status === true){
			$('#txt_payment_no').val(result.number);
		}
	},'json');*/
//}
</script>