<!---- 
    Author 	: Wahyu
    Tanggal : 12-08-2014	
    Revisi 	:
-->
<?php
//---------Untuk privellege jika dia ada tombol save,suspen,jurnal----------
	if($group_user->SavePrivillage == 1){
		$cek_autority_save  = "";
	}
	else{
		$cek_autority_save 	= "disabled";
	}
	
	if($group_user->JournalPrivillage == 1){
		$cek_autority_journal  = "";
	}
	else{
		$cek_autority_journal 	= "disabled";
	}
	
	if($group_user->DeletePrivillage == 1){
		$cek_autority_delete  = "";
	}
	else{
		$cek_autority_delete 	= "disabled";
	}
	if($group_user->ReversePrivillage == 1){
        $cek_autority_reverse  = "";
    }
    else{
        $cek_autority_reverse    = "disabled";
    }
//-------------end----------------------------
?>
<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">
                            <input type="checkbox" class="easyui-checkbox" name="chk_suspended" id="chk_suspended" style="margin-top:0px;margin-right:1px;" onclick="suspend_transaction(1,'<?= $menu_link;?>',$('#txt_payment_no').val(),'<?=$menu_label;?>')" <?=(!in_array($detail->TransStatus,array(4,6))) ? 'disabled' : ''?> <?=($detail->TransStatus) == 3 ? 'checked':''?> <?= $cek_autority_delete;?>/>
                            <span style="display:inline-block;margin-top:2px;<?=(($detail->TransStatus==3)?'color:gray;':'color:black')?>">Suspend</span>
							<a href="#" class="easyui-linkbutton" iconCls="icon-journal" plain="true" onclick="parent.tab_edit('Journal Payment Karyawan', '<?= $menu_link; ?>', 'icon-bank', 'to_journal',$('#txt_payment_no').val())" <?= $cek_autority_journal;?>>Journal</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="location.reload()">Refresh</a>
							
						</td>
						<td align="center" width="40%"><span id="approval_information">
		                <?php
		                if($detail->TransStatus == 4){
		                    echo get_status_approval_header($detail->TransStatus);
		                }
		                ?>
		                </span></td>							
						<td height="25" align="right" valign="middle">								
							<span class="form-title">
								<?= $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==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">
				<!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
	                <tr>
	                    <td width="50%" valign="top">   
			                <table cellpadding="0" cellspacing="3" width="100%" height="100%" border="0">
								<tr><input name="txt_payment_no_before" id="txt_payment_no_before" value="<?=  $detail->EInsNumber; ?>" type="hidden" style="width:100px" />
								    <input name="txt_piutang_no" id="txt_piutang_no" value="<?="$piutang_no"; ?>" type="hidden" style="width:100px" />
								    <input name="txt_total_piutang" id="txt_total_piutang" value="<?="$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="<?= $detail->PaymentPerPeriod;?>"/>
			                		<input type="hidden" name="pay_balance" id="pay_balance" value="<?= $detail->PaymentBalance;?>"/>
			                        <input type="hidden" name="trans_status" id="trans_status" value="<?= $detail->TransStatus;?>"/>
			                
			                        <td width="150"><b><label>Payment For</label></b></td>
								  	<td width="10"><b>:</b></td>
								  	<td>
										 <input name="txt_payment_for" id="txt_payment_for" value="<?=  $detail->EDebtNumber; ?>" class="disabled" type="text" style="width:200px" readonly/>	
									</td>
									<td valign="top" align="left">&nbsp;</td>
								</tr>
								<tr>
									<td width="150"><b><label>Transfer To</label></b></td>
								  	<td width="10"><b>:</b></td>
								  	<td>
			                        
										<select name="cmb_to_company" editable='false' class="combo-grid" id="cmb_to_company" readonly required="required" style="width:200px;height:20px" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?> data-options="
											value:'<?=  $detail->BranchCode; ?>', 
											panelWidth: 210,
											idField: 'BranchCode',
											textField: 'BranchName',
											url: '<?= base_url(); ?><?= $menu_link; ?>/get_company_edit/<?=  $detail->BranchCode; ?>',            
											columns: [[
											{field:'BranchName',title:'Company Name',width:140}
											]],
											fitColumns: true,
											onSelect:function(){
												$('.app-button').linkbutton('disable');
											}
											">
										</select>
										
			                            <select name="cmb_to_bank" editable='true' class="combo-grid" 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:'<?= $detail->BankReceipt; ?>',
											panelWidth: 210,
											idField: 'AccountNumber',
											textField: 'AccountName',
											url: '<?= base_url(); ?><?= $menu_link; ?>/get_akun_bank_edit/<?=  $detail->BranchCode; ?>',            
											columns: [[
											{field:'AccountName',title:'Account Name',width:140}
											]],
											fitColumns: true,
											onSelect:function(rowIndex,rowData){
												$('.app-button').linkbutton('disable');
												
                    							get_bank_balance(rowData.AccountNumber,rowData.CurrencyCode);
											}
											">
										</select>							
								  </td>
									
								</tr>
								<tr>
									<td width="150"><b><label>From</label></b></td>
								  	<td width="10"><b>:</b></td>
								  	<td>
										<input name="txt_karyawan" id="txt_karyawan" type="hidden" value="<?=  $detail->EmplTransCode; ?>" style="width:200px" />
										<input name="txt_karyawan_nama" id="txt_karyawan_nama" disabled="true" type="text" value="<?=  $detail->EmployeeName; ?>" style="width:200px" <?php if($detail->TransStatus == 3 || $detail->TransStatus == 2 || $detail->TransStatus == 1) {echo "disabled";} ?>/>
										<input name="txt_cheque_no" id="txt_cheque_no" class="easyui-validatebox" value="<?=$detail->ChequeNumber;?>" type="text" style="width:198px" placeholder="<?php echo label_grid('check_no');?>" autocomplete="off" <?=in_array($detail->TransStatus, array(1,2,3,9))?'disabled':'';?>/>
									</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 width="150" valign="top"><b><label>Memo</label></b></td>
								  	<td width="10" valign="top"><b>:</b></td>
									<td><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";} ?>><?=  $detail->EInsDesc; ?></textarea></td>
									
								</tr>
								<tr>
								  <td width="150"><b><label>Amount</label></b></td>
								  <td width="10"><b>:</b></td>
								  <td>
									<input name="txt_amount_rp" id="txt_amount_rp" type="text" class="easyui-numberbox" value='<?= $detail->EInsAmount; ?>' style="width:405px;height:20px" readonly
			                        data-options="
			                            min:0,
			                            precision:2,
			                            decimalSeparator:',',
			                            groupSeparator:'.'
			                        "
			                        />
									<input name="txt_amount" id="txt_amount" type="hidden" class="easyui-numberbox" value="<?=  $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()">	</a> -->				

			                      </td>
								  
							    </tr>
							   				
							</table>
						</td>
						<td width="50%" valign="top">
	                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
	                        <tr height="20">
	                            <td rowspan="7" valign="top">&nbsp;</td>
	                            <td width="122">&nbsp;</td>
	                            <td width="190" style="background:#333399;text-align:center;color:#FFF;">Payment No.</td>
	                            <td width="122" style="background:#333399;text-align:center;color:#FFF;">Payment Date</td>
	                        </tr>
	                        <tr height="20">
	                        	<td>&nbsp;</td>
	                            <td><input type="text" name="txt_payment_no" id="txt_payment_no" value="<?= $detail->EInsNumber; ?>" class="easyui-validatebox" style="width:190px;height:18px;text-align:center;" required readonly  /></td>
	                            <td><input type="text" name="txt_payment_date" id="txt_payment_date" class="easyui-datebox validBackward" style="width:122px;height:22px" required editable="false" value="<?= convert_to_dmy($detail->EInsDate);?>" readonly></td>
	                        </tr>
	                        </table>
	                        <table width="100%" cellpadding="0" cellspacing="0" style="margin-top:1px;">
	                        <tr height="20">
	                            <td rowspan="7" valign="top">&nbsp;</td>
	                            <td width="122">&nbsp;</td>
	                            <td width="190" style="background:#333399;text-align:center;color:#FFF;"><b>Availabel Balance (<span class='bank_balance_currency'></span>)</b></td>
	                        </tr>
	                        <tr height="20">
	                        	<td>&nbsp;</td>
	                            <td><input name="txt_bank_balance" id="txt_bank_balance" type="text" class="easyui-numberbox" data-options="precision:2,decimalSeparator:',',groupSeparator:'.',prefix:''" style="text-align:right;height:18px;width:316px" readonly /></td>
	                        </tr>
	                        </table>
	                    </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="<?= base_url(); ?><?='list/payment_karyawan'; ?>/get_payment_periode/<?= $detail->EDebtNumber; ?>/<?= $detail->PaymentPeriod; ?>"
				rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;"
				data-options = "
					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){

						save_button_begendedit('<?=$group_user->SavePrivillage;?>');

			            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);
									}
								});
							}
						});
					},onEndEdit:function(){
						save_button_endedit('<?=$group_user->SavePrivillage;?>');
					}

				"
				>
					<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:',',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="<?= base_url(); ?><?= $menu_link; ?>/get_payment_periode_cancel/<?= $detail->EInsNumber; ?>"
				rownumbers="true" fitColumns="true" singleSelect="true" style="height:290px;"
				data-options = "
					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'); 
							});
						 }
					},
					onBeforeLoad:function(){
						$('#dtg_payment_history').datagrid('loadData', {'total':0,'rows':[]});
					},
			        onBeginEdit:function(rowIndex){
						
						save_button_begendedit('<?=$group_user->SavePrivillage;?>');

			            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);
									}
								});
							}
						});
					},
					onEndEdit:function(rowIndex){
							
						save_button_endedit('<?=$group_user->SavePrivillage;?>');
					}


				"
				>
					<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" onclick="app_mgr()" <?= (($this->session->userdata("jobtitle_status") != 1 || !in_array($detail->TransStatus, array(4,6))) || (date('Y-m-d') < date('Y-m-d', strtotime($detail->EInsDate))))?'disabled':'' ?>>App. Mgr</a>
			 		 <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-reload" onclick="app_reverse()" <?=(in_array($detail->TransStatus,array(1,2,9)))?'':'disabled'?> <?= $cek_autority_reverse;?>>Reverse</a>
			 		 <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="update_payment_afiliasi()" <?=(in_array($detail->TransStatus,array(0,1,2,3)))?'disabled':''?> <?= $cek_autority_save;?>>Save</a> 								
			 	 	 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="parent.tab_edit('Print Payment Karyawan', '<?=$menu_link?>', 'icon-print', 'to_print_detail','<?= $detail->EInsNumber;?>');" <?=(in_array($detail->TransStatus,array(4,5,6,1,2,3,9)))?'':'disabled'?>>Preview & Print</a>  
			 		 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="<?=($this->session->userdata('jobtitle_status')==0)?'suspend(2)':'parent.tab_close(\'tt\')'?>" <?=(!in_array($detail->TransStatus,array(0,4,6)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></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="<?= base_url(); ?><?= $menu_link; ?>/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">

var SavePrivillage 	= '<?=$group_user->SavePrivillage;?>';

$(document).ready(function(){

    $('#txt_payment_date').datebox('setValue', '<?= date("d-m-Y", strtotime($detail->EInsDate));?>');
       
    $('#fm_payment_karyawan').click(function(){
		choose_cicilan();
    });

    var AccountNumber	= '<?= $detail->BankReceipt; ?>';
    
    get_bank_balance(AccountNumber,'IDR'); 

    $('#txt_payment_for').on('mouseover',function(){
    	$(this).css({'color':'blue','font-weight':'bold','cursor':'pointer'});
    }).on('mouseout',function(){
    	$(this).css({'color':'#545454','font-weight':'normal'});
    }).on('click',function(){
    	var TransNumber = $(this).val();
    	parent.tab_edit('Edit Piutang Karyawan','list/piutang_karyawan', 'icon-edit', 'edit',TransNumber) ;
    });
    
});	

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: '<?= base_url(); ?><?= $menu_link; ?>/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;
			});
		}else{
			
			$.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 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{

        var login_as        = "<?= $this->session->userdata('username');?>";
	    var created_by      = "<?= $detail->CreatedBy;?>";
	    var depart_by       = "<?= $detail->DepartmentCode;?>";
	    var depart_ses      = "<?= $this->session->userdata('department');?>";
	    var jobtitle_status = "<?= $this->session->userdata('jobtitle_status');?>";

	    var no_transaksi    = $('#txt_payment_no').val();
	    var status          = '<?= $detail->TransStatus;?>';
	    

	    checking_update(no_transaksi,status,function(check){
        
	        if(check.status == true){

	        	$('#dtg_payment_history').edatagrid('saveRow');
		        var i=$('#dtg_payment_history').datagrid('getRows');
		        var detail = JSON.stringify(i);
		        $("#detail").val(detail);
	            edit_data('<?=$menu_link?>','#fm_payment_karyawan',created_by,login_as,depart_by,depart_ses,jobtitle_status);

	        }else{
	            $.messager.alert('Unseccessfull',check.message,'error',function(){
	                location.reload();
	            });
	        }

	    });
    }
}

function app_mgr(){

	var no_transaksi    = $('#txt_payment_no').val();
    

    checking_approval(no_transaksi,function(check){
        
        if(check.status == false){
             approved_data('<?=$menu_link?>','#fm_payment_karyawan');
        }else{
            $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
		
}

function app_reverse(){
	var no_transaksi    = $('#txt_payment_no').val();
    

    checking_reverse(no_transaksi,function(check){
        if(check.status == true){
            reversed_data('F','<?=$menu_link?>',no_transaksi, 'Payment Karyawan');
        }else{
           $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function suspend(type){
    var created_by      = '<?php echo $detail->CreatedBy;?>';
    var login_as        = '<?php echo$this->session->userdata('username');?>';
    var job_title       = '<?php echo $this->session->userdata('jobtitle_status');?>';
    var url             = '<?= base_url();?>custom/custom/checking_suspend';
    
    suspend_check_trans('F', type, '<?=$menu_link?>', $('#txt_payment_no').val(), '<?=$menu_label;?>', url, created_by, login_as, job_title);
}

</script>