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

<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false" >
        <div id="form-header-wrapper">
            <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
            <tr>
                <td align="left">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-journal" plain="true" onClick="parent.tab_edit('Print Journal', '<?php echo $menu_link; ?>', 'icon-journal', 'to_preview','<?php echo $id = str_replace("/",".", $detail->EBonNumber); ?>')">Journal</a>
                    <input type="checkbox" name="chk_suspended" id="chk_suspended" value="3" onClick="suspend(1)" style="margin-top:0px;margin-right:1px;" <?php if($detail->TransStatus == 3){echo"checked disabled";} ?> <?php if($detail->AppLevel1Status==1 || $detail->AppLevel2Status==1){echo 'disabled';}?>/>
                    <span style="display:inline-block;margin-top:2px;<?=(($detail->AppLevel1Status!=4)?'color:gray;':'')?>"><?=(($detail->TransStatus!=3)?"Suspend":"Un-Suspend")?> Transaction</span>
                    <!--
                    <input type="checkbox" name="chk_status" id="chk_status" onClick="suspendTransaksi()" <?php if($detail->TransStatus == 3){ echo"checked"; } ?> />
                    Pembatalan Transaksi							
                	-->
                </td>
                <td height="25" align="right" valign="middle">
                    <span class="form-title" style="margin-left:7px;">
                        <?=$menu_label?>
                    </span>
                </td>
            </tr>
            </table>		     	
        </div>			
    </div>
    <!-- End of top view -->
    <?php 
		if($detail->TransStatus ==3 || $detail->TransStatus ==2 || $detail->TransStatus == 1){ 
			$readonly 		= "readonly";
			$disabled 		= "disabled";
		} 
		else { 
			$readonly 		= "";
			$disabled 		= "";
		} 
	?>
    <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_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_expense_cashbon" name="detail_expense_cashbon" />
                <input type="hidden" id="bank_number" name="bank_number"  value="<?php echo $detail->BankPayment ?>"/>
                 <input type="hidden" id="currency_code" name="currency_code" value="<?php echo $detail->CurrencyCode ?>"/>
                 <input type="hidden" id="kategoriid" name="kategoriid" />
                <!-- Form Header -->
                <table width="100%" height="141" 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="required" 
                                    style="width:302px;height:22px;" editable = "false" readonly
                                    data-options="
                                    	value:'<?php echo  $detail->DepartmentCode; ?>', 
                                        panelWidth: 302,
                                        idField: 'DepartmentCode',
                                        textField: 'DepartmentName',
                                        url: '<?php echo base_url(); ?><?php echo"$menu_link"; ?>/get_department_edit/<?php echo  $detail->DepartmentCode; ?>',                
                                        columns: [[
                                        {field:'DepartmentCode',title:'Departement Code',width:110},
                                        {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" required="required" readonly
                                    style="width:259px; height:18px;" editable = "false" <?php echo $readonly;?>
                                    data-options="
                                        value:'<?php echo  $detail->EmployeeCode; ?>', 
                                        panelWidth: 300,
                                        idField: 'EmployeeCode',
                                        textField: 'EmployeeName',
                                        url: '<?php echo base_url(); ?><?php echo"$menu_link"; ?>/get_employee_edit/<?php echo"$detail->DepartmentCode"; ?>/<?php echo"$detail->BranchCode"; ?>',              
                                        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="2">&nbsp;</td>
                        	<td width="140"><b>Exp. Cash-Bon No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input type="text" name="txt_exp_cashbon_no" id="txt_exp_cashbon_no" class="easyui-validatebox disabled" required="true" value="<?php echo $detail->EBonNumber ?>"  style="width:300px;" readonly />
                            </td>
                        </tr>
                        <tr>
                        	<td><b>Exp. Cash-Bon Date</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input name="txt_cashbon_date" id="txt_cashbon_date" <?php echo $readonly;?> type="text" class="easyui-datebox validBackward" required="required" style="width:302px;height:22px" editable="false" 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="required" 
                                    style="width:302px;height:22px;" editable = "false" <?php echo $readonly;?>
                                    data-options="
                                    	value:'<?php echo  $detail->BankPayment; ?>', 
                                        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:218px;"  <?php echo $readonly;?> name="txt_rate" id="txt_rate" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo $detail->CurrencyRate; ?>" required="true" /> Currency <span id="curren_code"></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" <?php echo $readonly;?> style="resize:none;width:300px;white-space:normal;" name="txa_description" id="txa_description" rows="4"><?php echo $detail->EBonDesc ?></textarea>
                            </td>
                        </tr>
                        <!--<tr>
                        	<td><b>Amount (<span id="curren_code_grid_amount"></span>)</b></td>
                            <td><b>:</b></td>
                            <td>-->
                            	<input name="txt_amount" id="txt_amount"  type="hidden"  class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo"$amount_cashbon"; ?>" style="width:100px;height:18px" required="required" />
								<input name="txt_amountRp" id="txt_amountRp" type="hidden" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo"$amount_cashbon"; ?>" style="width:270px;height:18px" required="required" readonly/>
								<!--<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-calculator',size:'small'" onClick="setAmount()" <?php echo $disabled;?>></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="140"><b>Cheque No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input type="text" name="txtcheque_no" id="txtcheque_no" class="easyui-validatebox" value="<?php echo $detail->ChequeNumber ?>" style="width:300px" <?php echo $readonly;?>/>
                            </td>
                        </tr>
                        <!--<tr>
                        	<td><b>Cheque Date</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input name="txt_cheque_date" id="txt_cheque_date" type="text" class="easyui-datebox validBackward" required="required" style="width:302px;height:22px" />
                            </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" value="<?php echo $detail->ChequeAmount ?>" style="width:300px" <?php echo $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-<?=(in_array($detail->TransStatus,array(0,1,2)))?'datagrid':'edatagrid'?>" url="<?php echo base_url(); ?><?php echo $menu_link; ?>/ajax_detail_item/<?php echo $id = str_replace("/",".", $detail->EBonNumber); ?>" toolbar="#tb_dtg_payment" 
							idField="id"
							rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;">
							<thead>
									<th align="center" valign="middle" data-options="
                                    field:'ExpCategoryName', width:120,align:'center',
                                    editor:{
                                        type:'combobox',
                                        options:{
                                        	value :'<?php echo $detail->EBonAccountNumber;?>',
                                            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:'EBonAmount', width:120,align:'center', editor:{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true}}">Amount (<span id="curren_code_grid"></span>)</th>
									<th align="center" valign="middle" data-options="field:'EBonMemo',width:400,align:'left',editor:'text'">Memo</th>
                                    <th align="center" valign="middle" hidden="true" data-options="field:'AccountNumber',width:400,align:'left',editor:'text'">Akun Number</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 app-button" iconCls="icon-ok" id="app_dir" <?=($detail->TransStatus==3 and $this->session->userdata('jobtitle_status')==1 and date('d-m-Y')>=date('d-m-Y', strtotime($detail->EBonDate)))?'':'disabled'?> onclick="custom_data('<?php echo"activities/purchase_expense_cashbon"; ?>/approval1','#fm_purchase_expense_cashbon');"> App. Dir</a> -->                    
                <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" id="app_mgr" 
				<?php 
					if(date('Y-m-d') < date('Y-m-d', strtotime($detail->EBonDate)) || $this->session->userdata('jobtitle_status') != 1){
						echo "disabled";
					} 
					else if(($detail->TransStatus == 3) || ($detail->TransStatus == 2 ) || ($detail->TransStatus == 1)){
						echo "disabled";
					}
					else if($this->session->userdata('jobtitle_status') == 1){
						echo "enabled";
					}
				?>
				onclick="custom_data('<?php echo"activities/purchase_expense_cashbon"; ?>/approval2','#fm_purchase_expense_cashbon');" >App. Mgr</a>					 
                <a href="javascript:void(0)" id="save_button" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_data()" <?php if($detail->TransStatus ==3 || $detail->TransStatus ==2 || $detail->TransStatus == 1){ echo "disabled"; } else { echo"enabled"; } ?>>Save</a> 								
                <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onClick="link_print('<?php echo $menu_link; ?>/to_print/<?php echo $EBonNumber = str_replace("/",".", $detail->EBonNumber); ?>')">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 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){
			editors = $('#dtg_expensecashbon').datagrid('getEditors',rowIndex);
			EBonAmount		= $(editors[1].target);
			//EBonMemo		= $(editors[1].target);
			
			EBonAmount.numberbox({
				onChange:function(){
					var amount =  EBonAmount.numberbox('getValue');
					$('#txt_amountRp').numberbox('setValue',amount);
					$('#txt_amount').numberbox('setValue',amount);
				}
			});
		}
	});
	
	/*$('#txt_rate').change(function(){
		$('#app_dir').linkbutton('disable', true);
		$('#app_mgr').linkbutton('disable', true);
	});
	
	$('#txa_description').change(function(){
		$('#app_dir').linkbutton('disable', true);
		$('#app_mgr').linkbutton('disable', true);
	});
	
	$('#txt_cheque_amount').change(function(){
		$('#app_dir').linkbutton('disable', true);
		$('#app_mgr').linkbutton('disable', true);
	});
	
	$('#txtcheque_no').change(function(){
		$('#app_dir').linkbutton('disable', true);
		$('#app_mgr').linkbutton('disable', true);
	});*/
	
	var currency_code = $('#currency_code').val();
	
	if(currency_code == "IDR"){
		$('#curren_code').text('<?php echo $detail->CurrencyCode;?>');
		$('#curren_code_grid').text('<?php echo $detail->CurrencyCode;?>');
		$('#curren_code_grid_amount').text('<?php echo $detail->CurrencyCode;?>');
	}
	else{
		$('#curren_code').text('<?php echo $detail->CurrencyCode;?>');
		$('#curren_code_grid').text('<?php echo $detail->CurrencyCode;?>');
		$('#curren_code_grid_amount').text('<?php echo $detail->CurrencyCode;?>');
	}
	
	$('#txt_cashbon_date').datebox('setValue', '<?php echo $detail->EBonDate ?>');
	$('#txt_cheque_date').datebox('setValue', '<?php echo $detail->ChequeDate ?>');
	
	$('#cmb_departement').combogrid({
		onSelect: function(rowIndex, rowData){	
			var DepCode = rowData.DepartmentCode;
			$('#cmb_employee').combogrid({
				delay: 300,
				url: '<?php echo base_url(); ?><?php echo"$menu_link"; ?>/get_employee/'+DepCode,
				idField: 'EmployeeCode',
				textField: 'EmployeeName',
				columns: [[
					{field:'EmployeeCode',title:'EmployeeCode',width:120,sortable:true},
					{field:'EmployeeName',title:'EmployeeName',width:400,sortable:true}
				]]
			});
		}
    });
	
	
	$('#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',0);
				}
			}); 
			
			$('#dtg_expensecashbon').edatagrid('updateRow',{
				index:0,
				row:{EBonAmount : 0,
					ExpCategoryName : ""}
			});
			
				
			$('#txt_amountRp').numberbox('setValue',0);
			$('#txt_amount').numberbox('setValue',0);
			
			$('#curren_code').text(rowData.CurrencyCode);
			$('#curren_code_grid').text(rowData.CurrencyCode);
			$('#curren_code_grid_amount').text(rowData.CurrencyCode);
			$('#currency_code').val(rowData.CurrencyCode);
		}
    });
	
});
	
function save_data(){
	var total_cek 	= parseFloat($('#txt_amount').numberbox('getValue'));
	var rate_nilai 	= parseFloat($('#txt_rate').numberbox('getValue'));
	var item = $('#dtg_expensecashbon').datagrid('getRows');
		 
	 $.each(item,function(index,value){
		cashbon_amount = parseFloat(value.EBonAmount);
	});
	
	if(total_cek < 1){
		$.messager.alert('Unseccessfull','Transaksi Belum Lengkap','error');
		return false;
	} 
	//else if(total_cek != cashbon_amount){
		//$.messager.alert('Unseccessfull','Amount Tidak Sama Dengan Total Amount.','error');
		//return false;
	//}
	else if(rate_nilai <= 0){
		$.messager.alert('Unseccessfull','Rate Tidak Boleh Kurang Dari Satu (1)','error');
		return false;
	}
	else {
		$('#dtg_expensecashbon').edatagrid('saveRow');		
	 	var i=$('#dtg_expensecashbon').datagrid('getRows');
	 	var items = JSON.stringify(i);
	 	$("#detail_expense_cashbon").val(items);		
		edit_data('<?php echo $menu_link ?>', '#fm_purchase_expense_cashbon');
	}			
}

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[0].target);
	rate_val 	= $(Amount).numberbox('getValue');
	
	$('#dtg_expensecashbon').edatagrid('saveRow');		
	/*var rows 	= $('#dtg_expensecashbon').datagrid('getRows');
	for(var i=0; i<rows.length; i++){
		total_amount += parseFloat(rows[i].rate_val);
	}*/
	
	$('#txt_amount').numberbox('setValue',rate_val);
	$('#txt_amountRp').numberbox('setValue',rate_val);
	
}

function suspend(type){
	suspend_data_new('F',type,'<?=$menu_link?>',$('#txt_exp_cashbon_no').val(),'Expense Cash-Bon');
}

function suspendTransaksi(){
	var cashbonNumber = $("#txt_exp_cashbon_no").val();
	var rep_1 = cashbonNumber.replace("/", ".");
	var rep_2 = rep_1.replace("/", ".");
	var rep_3 = rep_2.replace("/", ".");
	
	suspend_data_on_new_form('<?php echo $menu_link; ?>', rep_3);
}
</script>