<!---- 
    Author 	: Wahyu
    Tanggal : 12-08-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" valign="middle">
							<a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="location.reload()" disabled="disabled">New</a>
							<a href="#" class="easyui-linkbutton" iconCls="icon-journal" plain="true" onclick="parent.tab_edit('Journal Payment Karyawan', '<?php echo $menu_link; ?>', 'icon-bank', 'to_journal',$('#txt_payment_no').val())">Journal</a>
                            <input type="checkbox" name="chk_suspended" id="chk_suspended" onclick="suspend(1)"  
                            <?php
								if($detail->TransStatus == 1 || $detail->AppLevel1Status == 0 || $detail->TransStatus == 2){echo "disabled";}
								if($detail->TransStatus == 3){echo "checked disabled";}
							?>/>Pembatalan Transaksi
						</td>							
						<td height="25" align="right" valign="middle">								
							<span class="form-title">
								<?php echo $menu_label; ?>
							</span>
						</td>
					</tr>
				</table>		     	
		 </div>			
	</div>
	<!-- end top view -->	
	<!-- center view -->
    
	<div id="form-body" data-options="region:'center',border:false">
    <?php if($detail->transstatus_debt==2){ ?>
        <div class="blanket"></div>
        <div class="paid-box"><b>Paid</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="width:1180%;height:548px;padding:5px;" fit='true'>
			<form id="fm_payment_karyawan" name="fm_payment_karyawan" method="post">
                <table cellpadding="0" cellspacing="3 width="100%" height="100%" border="0">
					<tr><input name="txt_payment_no_before" id="txt_payment_no_before" value="<?php echo  $detail->EInsNumber; ?>" type="hidden" style="width:100px" />
					    <input name="txt_piutang_no" id="txt_piutang_no" value="<?php echo"$piutang_no"; ?>" type="hidden" style="width:100px" />
					    <input name="txt_total_piutang" id="txt_total_piutang" value="<?php echo"$total_piutang_terakhir"; ?>" type="hidden" style="width:100px" />
						
                         <input type="hidden" id="hide_new_index" name="hide_new_index" />
                        <input type="hidden" id="payment_balance" name="payment_balance" />
                        
                        <input type="hidden" id="detail" name="detail" />
                        <input type="hidden" id="total_balance" name="total_balance" />
                        
                        <input type="hidden" name="pay_period" id="pay_period" value="<?php echo $detail->PaymentPerPeriod;?>"/>
                		<input type="hidden" name="pay_balance" id="pay_balance" value="<?php echo $detail->PaymentBalance;?>"/>
                        <input type="hidden" name="trans_status" id="trans_status" value="<?php echo $detail->TransStatus;?>"/>
                
                        <td valign="top" height="22" width="70" align="left">
							 <label>Payment for</label></td>
						<td valign="top" align="left"  width="800">
							 <input name="txt_payment_for" id="txt_payment_for" value="<?php echo  $detail->EDebtNumber; ?>" class="disabled" type="text" style="width:200px" readonly/>	
						</td>
						<td valign="top" align="left">&nbsp;</td>
					</tr>
					<tr>
						<td valign="top" height="22" width="70" align="left">
							 <label>Transfer To</label></td>
						<td valign="top" align="left"  width="800">
                        
							<select name="cmb_to_company" class="easyui-combogrid" id="cmb_to_company" required="required" readonly style="width:200px;height:20px" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?> data-options="
								value:'<?php echo  $detail->BranchCode; ?>', 
								panelWidth: 210,
								idField: 'BranchCode',
								textField: 'BranchName',
								url: '<?php echo base_url(); ?><?php echo"activities/payment_karyawan"; ?>/get_company_edit/<?php echo  $detail->BranchCode; ?>',            
								columns: [[
								{field:'BranchName',title:'Company Name',width:140}
								]],
								fitColumns: true
								">
							</select>
							
                            <select name="cmb_to_bank" class="easyui-combogrid" editable="false" id="cmb_to_bank" required="required" style="width:200px;height:20px" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?> data-options="
								value:'<?php echo  $detail->BankReceipt; ?>',
								panelWidth: 210,
								idField: 'AccountNumber',
								textField: 'AccountName',
								url: '<?php echo base_url(); ?><?php echo"activities/payment_karyawan"; ?>/get_akun_bank_edit/<?php echo  $detail->BranchCode; ?>',            
								columns: [[
								{field:'AccountName',title:'Account Name',width:140}
								]],
								fitColumns: true
								">
							</select>							
					  </td>
						<td valign="top" align="left" valign="middle" >Payment No.</td>
						<td valign="top" align="left"><input name="txt_payment_no" type="text" class="easyui-validatebox" id="txt_payment_no" style="width:120px;" value="<?php echo  $detail->EInsNumber; ?>" size="30" readonly  required="required" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?>/></td>
					</tr>
					<tr>
						<td valign="top" height="22" align="left">
							 From</td>
						<td valign="top" align="left">
							<input name="txt_karyawan" id="txt_karyawan" type="hidden" value="<?php echo  $detail->EmplTransCode; ?>" style="width:200px" />
							<input name="txt_karyawan_nama" id="txt_karyawan_nama" readonly type="text" value="<?php echo  $detail->EmployeeName; ?>" style="width:200px" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?>/>
						</td>
						<td valign="top" align="left">Payment Date</td>
						<td valign="top" align="left">
						   <input name="txt_payment_date" id="txt_payment_date" type="text" class="easyui-datebox validBackward" style="width:100px;height:20px;" required editable="false" value="<?php echo convert_to_dmy($detail->EInsDate);?>" readonly/>
						</td>
					</tr>
						<input name="txt_cheque_no" id="txt_cheque_no" type="hidden" style="width:100px" />							
						<input name="txt_cheque_date" id="txt_cheque_date" type="hidden"  style="width:100px;height:20px;" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?>/>
					<tr>
						<td valign="top" height="51" align="left">
							  Memo</td>
						<td valign="top" align="left"><textarea class="textbox" style="resize:none;" name="txa_description" id="txa_description" cols="55" rows="3" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?>><?php echo  $detail->EInsDesc; ?></textarea></td>
						<td valign="top" align="left">&nbsp;</td>
					</tr>
					<tr>
					  <td height="22" valign="top" align="left">Amount</td>
					  <td valign="top" height="22" align="left">
						<input name="txt_amount_rp" id="txt_amount_rp" type="text" class="easyui-numberbox" value='<?php echo $detail->EInsAmount; ?>' style="width:100px;height:18px" readonly
                        data-options="
                            min:0,
                            precision:2,
                            decimalSeparator:',',
                            groupSeparator:'.'
                        "
                        />
						<input name="txt_amount" id="txt_amount" type="hidden" class="easyui-numberbox" value="<?php echo  $detail->EInsAmount; ?>" data-options="
                            min:0,
                            precision:2,
                            decimalSeparator:',',
                            groupSeparator:'.'
                        "/>	
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-calculator',size:'small'" id="calc_validate" onClick="choose_cicilan()">					

                      </td>
					  <td valign="top" align="left">&nbsp;</td>
				    </tr>					
				</table>
				<?php 
				if($detail->TransStatus != 3 ){?>
                <table id="dtg_payment_history" class="easyui-<?=(in_array($detail->TransStatus,array(0,1,2,5)))?'datagrid':'edatagrid'?>"
				title="Payment History" idField="id" url="<?php echo base_url(); ?><?php echo'activities/payment_karyawan'; ?>/get_payment_periode/<?php echo $detail->EDebtNumber; ?>/<?php echo $detail->PaymentPeriod; ?>"
				rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;">
					<thead>
						<tr>
							<th formatter="formatRp" data-options="field:'LoanPayment',width:75,halign:'center',align:'left'">Loan Payment</th>
							<th data-options="field:'PaymentPeriod',width:15,halign:'center',align:'center'" hidden="true">Period</th>
							<th formatter="formatRp" data-options="field:'PaymentPerPeriod',width:75,halign:'center',align:'left'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',required:true,min:0}}">Payment Per Period</th>
							<th formatter="formatRp" data-options="field:'PaymentBalance',width:75,halign:'center',align:'left'">Payment Balance</th>
                            <th data-options="field:'EInsNumber',width:75,halign:'center',align:'left'" hidden='true'>Eins Number</th>
                            <th data-options="field:'EInsDetailID',width:75,halign:'center',align:'left'" hidden='true'>EInsDetailID</th>
                            <!--<th data-options="field:'Status',width:75,halign:'center',align:'left'" >Status</th>-->
						</tr>
					</thead>
				</table>
                <?php }else{?>
                <table id="dtg_payment_history" class="easyui-edatagrid"
				title="Payment History" idField="id" url="<?php echo base_url(); ?><?php echo'activities/payment_karyawan'; ?>/get_payment_periode_cancel/<?php echo $detail->EInsNumber; ?>"
				rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;">
					<thead>
						<tr>
							<th formatter="formatRp" data-options="field:'LoanPayment',width:75,halign:'center',align:'left'">Loan Payment</th>
							<th data-options="field:'PaymentPeriod',width:15,halign:'center',align:'center'" hidden="true">Period</th>
							<th formatter="formatRp" data-options="field:'PaymentPerPeriod',width:75,halign:'center',align:'left'">Payment Per Period</th>
							<th formatter="formatRp" data-options="field:'PaymentBalance',width:75,halign:'center',align:'left'">Payment Balance</th>
                            <th data-options="field:'EInsNumber',width:75,halign:'center',align:'left'" hidden='true'>Eins Number</th>
                            <th data-options="field:'EInsDetailID',width:75,halign:'center',align:'left'" hidden='true'>EInsDetailID</th>
                            <!--<th data-options="field:'Status',width:75,halign:'center',align:'left'" >Status</th>-->
						</tr>
					</thead>
				</table>
				<?php }?>
			</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 app-button" iconCls="icon-ok" 
					 <?php 
					 if(date('Y-m-d') < date('Y-m-d', strtotime($detail->EInsDate)) || $detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1){ 
					 	echo " disabled "; 
					} 
					else if($this->session->userdata("jobtitle_status") == 1 ||  $detail->AppLevel1Status !=1){ 
						echo " enabled "; 
					} 
					else { 
						echo"disabled"; 
					} 
					?> onclick="custom_data('<?php echo"activities/payment_karyawan"; ?>/approval1','#fm_payment_karyawan');"> App. Mgr</a>
			 		 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" 
					 <?php 
					 if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1){ 
					 	echo " disabled "; 
					} ?>  onclick="update_payment_afiliasi()" >Save</a> 								
			 	 	 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="">Preview & Print</a>   
			 		 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="<?php if($this->session->userdata('jobtitle_status')==0) echo 'parent.tab_close(\'tt\')';?>" <?=(in_array($detail->TransStatus,array(3,9)) || $detail->AppLevel1Status==1)?'disabled':''?>>Cancel</a>
                </td>
             </tr>
        </table>				
    </div>
</div>
<!-- Dialog Payment List -->
<div id="dialog-payment-list" class="easyui-dialog" style="width:595px; left:150px; top:50px; background-color:#F8F8F8; height:300px; padding: 0 0px 0 0px" closed="true" data-options="modal:true" buttons="#dialog-buttons">
	<table id="dtg_payment_list" class="easyui-datagrid"  style="width:580px;" 
        url="<?php echo base_url(); ?><?php echo"activities/payment_karyawan"; ?>/get_piutang"
        singleSelect="true"
        idField="" >
		<thead>
			<tr>
				<th colspan="2">Dari</th>
				<th rowspan="2" field="EDebtAmount" formatter="formatRp" width="135" align="center">Amount</th>				
			</tr>
			<tr>
				<th field="CompTransCode" width="120" align="center">Perusahaan</th>
				<th field="AccountName" width="120" align="center">Bank</th>
				<th field="EDebtNumber" hidden="true" width="50" align="center">No Piutang</th>
				<th field="EmployeeName" width="120" align="center">Kepada</th>	
			</tr>
		</thead>
	</table>
	<div id="dialog-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_piutang_karyawan()">OK</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-payment-list')">Cancel</a>
	</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
        $('#txt_payment_date').datebox('setValue', '<?php echo date("d-m-Y", strtotime($detail->EInsDate));?>');
		$('#dtg_payment_history').datagrid({
		onClickRow:function(){
			 var selected_data=$('#dtg_payment_history').datagrid('getSelected');
			 var no_paymen = $('#txt_payment_no').val();
			 if(selected_data.EInsNumber != no_paymen){
				 $.messager.alert('Unseccessfull','Data tidak sama dengan Nomor payment afiliasi','error',function(){	
				$('#dtg_payment_history').datagrid('reload'); 
				});
			 }
		},
        onBeginEdit:function(rowIndex){
			
            editors = $('#dtg_payment_history').datagrid('getEditors',rowIndex);
			var e1	= $(editors[0].target);
           
			e1.numberbox({
				onChange:function(){
					value1 = 0;
					value2 = e1.numberbox('getValue');
                    
					dg_item = $('#dtg_payment_history').datagrid('getRows');
					$.each(dg_item,function(index,value){
						if(rowIndex == 0){
							value1 = value.LoanPayment;
							
						}
						else if(index==(rowIndex-1)){
							value1 = value.PaymentBalance;
						}
						
						if(index==rowIndex){
							value3 = value1-value2;
							value.PaymentBalance = value3;
							$('#pay_balance').val(value3);
							$('#pay_period').val(value2);
						}
					});
				}
			});
		}
	});
       /* $('#fm_payment_karyawan').click(function(){
			$('#dtg_payment_history').edatagrid('saveRow');
		});*/
});	
function choose_piutang_karyawan(){
	var selected_data=$('#dtg_payment_list').datagrid('getSelected');
	$('#cmb_to_company').combogrid('setValue', selected_data.CompTransCode);
	
	var ComCode = selected_data.CompTransCode; 
	$('#cmb_to_bank').combogrid({
			delay: 300,
			url: '<?php echo base_url(); ?><?php echo"activities/piutang_karyawan"; ?>/get_akun_bank/'+ComCode,
			idField: 'AccountNumber',
			textField: 'AccountName',
			columns: [[
				{field:'AccountName',title:'AccountName',width:400,sortable:true}
			]]
	});
		
	$('#cmb_to_bank').combogrid('setValue', selected_data.BankTransfer); 
	$('#txt_karyawan').val(selected_data.EmplRecpCode);
	$('#txt_piutang_no').val(selected_data.EDebtNumber);
	$('#txt_total_piutang').val(selected_data.EDebtAmount);
			
	modal_close('dialog-payment-list');
}

/*function cek_amount(value){
	var total = toAngka(value);
	$("#txt_amount").val(total);
}*/

function choose_cicilan(){
		$('#dtg_payment_history').edatagrid('saveRow');
		txt_payment_no	= $('#txt_payment_no').val();
		var cek = $('#dtg_payment_history').datagrid('getRows');
		var loan_payment 		= 0;
		var payment_perperiod 	= 0;
		
		
		for(var i=0; i<cek.length; i++){
			if(isNaN(cek[i].LoanPayment)){ cek[i].LoanPayment = 0; }
			loan_payment 	= parseFloat(cek[i].LoanPayment);
			if(isNaN(cek[i].PaymentPerPeriod)){ cek[i].PaymentPerPeriod = 0; }
			payment_perperiod += parseFloat(cek[i].PaymentPerPeriod);
			if(isNaN(cek[i].PaymentBalance)){ cek[i].PaymentBalance = 0; }
		}
		
		
		if(payment_perperiod > loan_payment){
			$.messager.alert('Unseccessfull','Pembayaran melebihi sisa nilai pinjaman','error',function(){
				$('#dtg_payment_history').datagrid('reload');
				return false;
			});
		}
        
		$.each(cek, function(index,value){
			if(value.EInsNumber == txt_payment_no){
				$('#txt_amount_rp').numberbox('setValue',value.PaymentPerPeriod);
                $('#txt_amount').numberbox('setValue',value.PaymentPerPeriod);
			}
		});
}

function suspend(type){
	suspend_data_new('F',type,'<?php echo "activities/payment_karyawan/"?>',$('#txt_payment_no').val(),'Payments Karyawan');
}

function update_payment_afiliasi(){
	$('#dtg_payment_history').edatagrid('saveRow');
	var cek = $('#dtg_payment_history').datagrid('getRows');
    var loan_payment 		= 0;
    var payment_perperiod 	= 0;
    var amount = parseFloat($("#txt_amount_rp").numberbox('getValue'));
    
    
    for(var i=0; i<cek.length; i++){
        if(isNaN(cek[i].LoanPayment)){ cek[i].LoanPayment = 0; }
        loan_payment 	= parseFloat(cek[i].LoanPayment);
        if(isNaN(cek[i].PaymentPerPeriod)){ cek[i].PaymentPerPeriod = 0; }
        if(i==(cek.length-1)) payment_perperiod += parseFloat(cek[i].PaymentPerPeriod);
    }
   	
    if(payment_perperiod > loan_payment){
        $.messager.alert('Unseccessfull','Pembayaran melebihi sisa nilai pinjaman','error',function(){
            $('#dtg_payment_history').datagrid('reload');
            return false;
        });
    }
    else if(payment_perperiod != amount){
        $.messager.alert('Unseccessfull','Payment Per Period Tidak Sama Dengan Amount, Silahkan Klik Tombol Kalkulator','error',function(){
            $('#dtg_payment_history').datagrid('reload');
            return false;
        });
    }
    else{
        $('#dtg_payment_history').edatagrid('saveRow');
        var i=$('#dtg_payment_history').datagrid('getRows');
        var detail = JSON.stringify(i);
        $("#detail").val(detail);
        edit_data('<?php echo $menu_link;?>','#fm_payment_karyawan');
    }
}
function suspend(type){
	suspend_data_new('F',type,'<?php echo "activities/payment_karyawan/"?>',$('#txt_payment_no').val(),'Payments Karyawan');
} 
</script>