<!---- 
    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 Cash Bon','<?="activities/purchase_expense_cashbon/"?>','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_cashbon" name="fm_purchase_expense_cashbon" method="post" action="<?php echo base_url(); ?><?php echo"activities/purchase_expense_cashbon/insert"; ?>">
                <input type="hidden" id="detail_amount_val" name="detail_amount_val" />	
				<input type="hidden" id="detail_account_val" name="detail_account_val" />
				<input type="hidden" id="detail_memo_val" name="detail_memo_val" />
                <input type="hidden" id="detail" name="detail" />
                <input type="hidden" id="currency_code" name="currency_code" />
                <input type="hidden" id="kategoriid" name="kategoriid" />
                <input type="hidden" id="cal_culate_status" name="cal_culate_status" />
                
                <!-- 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="120"><b>Department</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<select name="cmb_departement" class="easyui-combogrid" 
                                    id="cmb_departement" required editable="fasle" 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:'DepartmentName',title:'Departement Name',width:190}
                                        ]],
                                        fitColumns: true
                                    "
                                >
                                </select>
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Employee</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<select name="cmb_employee" class="easyui-combogrid" 
                                	id="cmb_employee" 
                                	style="width:302px;height:22px;"
                                	required="required" editable="false" 
                                    data-options="
                                        panelWidth: 302,
                                        idField: 'EmployeeCode',
                                        textField: 'EmployeeName',
                                        url: '<?=base_url() . $menu_link?>/get_employee/<?=$department_code?>',
                                        columns: [[
                                            {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="2">&nbsp;</td>
                        	<td width="160"><b>Exp. Cash-Bon No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input type="text" class="easyui-validatebox" name="txt_exp_cashbon_no" id="txt_exp_cashbon_no" required="true" style="width:300px;text-align:center;" readonly value="<?php echo $cachbon_number; ?>"/>
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Exp. Cash-Bon Date</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input type="text" class="easyui-datebox validBackward" name="txt_cashbon_date" id="txt_cashbon_date" data-options="panelWidth:302" style="width:302px;height:22px" value='<?= $pr_date ?>' required editable="false" />
                            </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: 'AccountNumber',
                                        textField: 'AccountName',
                                        url: '<?php echo base_url(); ?><?php echo"$menu_link"; ?>/get_akun',               
                                        columns: [[                  
                                        {field:'AccountName',title:'Account Name',width:190}
                                        ]],
                                        fitColumns: true
                                    "
                                >
                                </select>
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Rate</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input type="text" style="width:100px;" name="txt_rate" id="txt_rate" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" class="easyui-numberbox" required="true" /> 
                                <b>Currency : </b><span id="spn_currencyName"></span> 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 class="textbox" style="resize:none;width:300px;white-space:normal;" name="txa_description" id="txa_description" rows="4"></textarea>
                            </td>
                        </tr>
                        <tr>
                        	<!--<td><b>Amount (<span id="spn_currency_amount"></span>)</b></td>
                            <td><b>:</b></td>
                            <td>-->
                            	<input name="txt_amount" id="txt_amount" type="hidden" class="easyui-numberbox" style="width:100px;height:18px" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" required />
								<input name="txt_amountRp" id="txt_amountRp" type="hidden" class="easyui-numberbox" 
                                data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" style="width:270px;height:18px;text-align:right;" required readonly />
								<!--<a href="#" id="icon-calculator" class="easyui-linkbutton" data-options="iconCls:'icon-calculator',size:'small'" onClick="setAmount()" ></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" rowspan="3">&nbsp;</td>
                        	<td width="160"><b>Cheque No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input type="text" name="txtcheque_no" id="txtcheque_no" class="easyui-validatebox" style="width:300px;" autocomplete="off" />
                            </td>
                        </tr>
                        
                        <tr>
                        	<td><b>Cheque Amount</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input type="text" name="txt_cheque_amount" id="txt_cheque_amount" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" class="easyui-numberbox" style="width:300px;text-align:right;" readonly />
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                </table>
                <!-- End of form header -->
                
                <!-- Datagrid -->
				<div class="easyui-tabs" style="height:auto;margin-top:5px" data-options="tabHeight:18">
					<div id="tab_general" title="Items">
                        <table id="dtg_expensecashbon" class="easyui-edatagrid" 
                            toolbar="" idField="id"
                            rownumbers="true" fitColumns="true" 
                            singleSelect="true" style="height:279px;"
                        >
                            <thead>
                            <tr>
                                <th align="center" valign="middle" data-options="
                                    field:'ExpCategoryID',
                                    width:250,									
                                    align:'left',
                                    editor:{
                                    type:'combobox',
                                    options:{
                                        valueField:'ExpCategoryName',
                                        textField:'ExpCategoryName',
                                        url:'<?=base_url() . $menu_link?>/cashbon_account',
                                        required:true,
                                        onSelect: function(rec){
                                            $('#kategoriid').val(rec.ExpCategoryID);
                                        },
                                        editable:false
                                    }
                                }">Account Name</th>							
                                <th align="center" valign="middle" formatter="formatRp" data-options="field:'Amount', width:120,align:'center', editor:{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true}}">Amount (<span id="spn_currency"></span>)</th>
                                <th align="center" valign="middle" data-options="field:'Memo',width:400,align:'left', editor:{type:'text'}">Memo</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)" id="save_button" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_data()">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 Item -->
<div id="dialog-item" class="easyui-dialog" style="width:660px; left:150px; top:50px; background-color:#F8F8F8; height:500px; padding: 0 0px 0 0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons">
	<form id="form_account" method="post" novalidate>
		<table width="90%" align="left">
        <tr>
            <td width="183" align="left" valign="middle">Expense Group :</td>
            <td width="154">&nbsp;</td>
            <td width="295">
                <select name="cmb_list_type_item" class="easyui-combobox" id="cmb_list_type_item" style="width:290px;">
                    <option value=""></option>
                </select>
            </td>
        <td width="380" align="left" valign="middle">						
        </td>
        </tr>
        <tr>
            <td colspan="4">
                <table id="tt" class="easyui-datagrid" url="" title="" iconCls="icon-save" rownumbers="true" pagination="true" fitColumns="true" singleSelect="true" style="width:635px;height:260px">
                    <thead>
                        <tr>
                            <th field="glaccount" width="150" sortable="true">Item Name</th>
                            <th field="accountname" width="200" sortable="true">Item description</th>
                        </tr>
                    </thead>
                </table>
            </td>
        </tr>
		</table>
	</form>
</div>
<!-- Dialog Button -->
<div id="dialog-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="choose_item()">OK</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="modal_close('dialog-item')">Cancel</a>
</div>

<script type="text/javascript">
$(document).ready(function(){
	
	$('#dtg_expensecashbon').datagrid({
		onBeginEdit:function(rowIndex){
			var editors = $('#dtg_expensecashbon').datagrid('getEditors', rowIndex);
			var n1 = $(editors[0].target);
			var n2 = $(editors[1].target);
			n2.numberbox({
				onChange:function(){
					var amount =  n2.numberbox('getValue');
					$('#txt_amountRp').numberbox('setValue',amount);
					$('#txt_amount').numberbox('setValue',amount);
				}
			});
		}
	});
	
	$('#txt_exp_cashbon_no').val('<?php echo $cachbon_number; ?>');
	
	$('#txt_cashbon_date').datebox({
		onChange:function(){
			url  = base_url + 'activities/purchase_expense_cashbon/get_ebon_number';                
			var ebon_af_date   = $('#txt_cashbon_date').datebox('getValue');
			
			$.post(url, {ebon_af_date:ebon_af_date}, function(result){
				if (result.status === true){
					$('#txt_exp_cashbon_no').val(result.number);
				} 
			},'json');
		}
	});
	
	/*$('#dtg_expensecashbon').datagrid({
		onEndEdit:function(rowIndex, rowData, changes){
			item = $('#dtg_expensecashbon').datagrid('getRows');
			
			if(item.length > 0){
				$('#icon-calculator').linkbutton('enable');
				$('#txt_cheque_amount').attr('readonly',false);
			}
		}
	});*/
	
	$('#dtg_expensecashbon').edatagrid('addRow');
	
	
	/*$('#fm_purchase_expense_cashbon').click(function(){
		$('#dtg_expensecashbon').edatagrid('saveRow');
	});*/
	
	$('#cmb_bank').combogrid({
		onSelect: function(rowIndex, rowData){	
			var AccountNumber 	= rowData.AccountNumber;
			
			$.ajax({
				type: "POST",
				dataType: "html",
				url: "<?php echo base_url(); ?><?php echo"$menu_link"; ?>/get_rate_currency",
				data: $.param({ AccountNumber:AccountNumber}),
				 success: function(data){
					$("#txt_rate").numberbox('setValue',1);
				}
			}); 
			amount_rupiah = parseFloat($("#txt_amountRp").numberbox('getValue'));
			
			//$('#dtg_expensecashbon').datagrid('reload');
			
			call = $('#cal_culate_status').val();
			if(call == ""){
				$('#dtg_expensecashbon').datagrid('reload');
				$('#dtg_expensecashbon').edatagrid('addRow');
			}
			else{
				location.reload();
			}
			//$('#dtg_expensecashbon').edatagrid('loadData', {"total":0,"rows":[1]});
			
			$('#spn_currencyName').text(rowData.CurrencyCode);
			$('#spn_currency').text(rowData.CurrencyCode);
			$('#spn_currency_amount').text(rowData.CurrencyCode);
			$('#currency_code').val(rowData.CurrencyCode);
			
		}
    });
});

function save_data(){
	 var selected	= $('#dtg_expensecashbon').datagrid('getSelected');
		 var index 		= $('#dtg_expensecashbon').datagrid('getRowIndex', selected);

		 var ed_amount = $('#dtg_expensecashbon').datagrid('getEditor', {
			index: index,
			field: 'Amount'
		 });
		 
		 if (ed_amount){
			var amount_val = $(ed_amount.target).numberbox('getValue');	// get combobox text
			$("#detail_amount_val").val(amount_val);
		 }
		 
		 var ed_account = $('#dtg_expensecashbon').datagrid('getEditor', {
			index: index,
			field: 'AccountNumber'
		 });
		 
		 if (ed_account){
			var account_val = $(ed_account.target).combobox('getValue');	// get combobox text
			$("#detail_account_val").val(account_val);
		 }
		 
		 var ed_memo = $('#dtg_expensecashbon').datagrid('getEditor', {
			index: index,
			field: 'Memo'
		 });
		 
		 if (ed_memo){
			var memo_val = $(ed_memo.target).val();	// get text
			$("#detail_memo_val").val(memo_val);
		 }
		 
		 var total_cek 	= parseFloat($('#txt_amount').numberbox('getValue'));
		 var total_rate 	= parseFloat($('#txt_rate').numberbox('getValue'));
		 var item = $('#dtg_expensecashbon').datagrid('getRows');
		 
		 $.each(item,function(index,value){
			cashbon_amount = parseFloat(value.Amount);
		});
		
		if(total_cek < 1){
			$.messager.alert('Unseccessfull','Transaksi Belum Lengkap','error');
			return false;
		} 
		
		else if(total_rate < 1){
			$.messager.alert('Unseccessfull','Rate Tidak Boleh Kurang Dari (1)','error');
			return false;
		}
		else {
			if(item.length == 0){
				$.messager.alert('Unseccessfull','Table items masih kosong. Silahkan buat detail transaksi terlebih dahulu.','error');
				return false;
			}
			else{
				$('#dtg_expensecashbon').edatagrid('saveRow');
				var i=$('#dtg_expensecashbon').datagrid('getRows');
				var items = JSON.stringify(i);
				$("#detail").val(items);
		    	save_transaction('<?php echo"$menu_link"; ?>', '#fm_purchase_expense_cashbon','1');
			}
		}
}

function setAmount(){
	var total_amount = 0;
	var selected	= $('#dtg_expensecashbon').datagrid('getSelected');
	var index		= $('#dtg_expensecashbon').datagrid('getRowIndex', selected);
	var editor 		= $('#dtg_expensecashbon').datagrid('getEditors', index);
	
	var Amount	= $(editor[1].target);
	
	$('#cal_culate_status').val(1);
	$('#dtg_expensecashbon').edatagrid('saveRow');		
	var rows 	= $('#dtg_expensecashbon').datagrid('getRows');
	for(var i=0; i<rows.length; i++){
		total_amount += parseFloat(rows[i].Amount);
	}

	$('#txt_amount').numberbox('setValue',total_amount);
	$('#txt_amountRp').numberbox('setValue',total_amount);
	$('#txt_cheque_amount').attr('readonly',false);
	$('#txt_cheque_amount').numberbox('setValue',0);
	
}

</script>