<style>
.paymentmodal {
    width: 75%;
    margin: 30px auto;
}
</style>
<script src="assets/js/king-table.js"></script>
<script src="assets/js/popper.min.js"></script>
<?php
$LoanTotalPaid=0;
?>
<div class="printreport">
<!-- MAIN CONTENT WRAPPER -->
<div class="content">	
	<div class="main-content">
	<div id="printableArea" >
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-content">
			<form id="frmLoanApplication" name="frmLoanApplication" data-parsley-validate novalidate method="POST">
					<?php  $i=1;?>
					<?php $__currentLoopData = $data['LoanApplicantRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanApplicantRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
				<table class="table table-striped table-bordered">
					<tr>
						<th>
							<centeR>Welcome Kit for Loan Account Number - <?php echo $loanacc = $LoanApplicantRec->lacc_no; ?></center>
						</th>
					</tr>
				</table>
				
				<table  class="table table-striped table-bordered">
					<?php $lacc_id = $LoanApplicantRec->lacc_id; ?>
				<input type="hidden" name="lacc_id" id="lacc_id" value="<?php echo e($LoanApplicantRec->lacc_id); ?>">
				<input type="hidden" name="lacc_no" id="lacc_no" value="<?php echo e($LoanApplicantRec->lacc_no); ?>">
				
				<tr>
					<th>
					<p><img src="img/navachethanalogo.png" align="left" style="padding-right: 10px;">
					Navachethana Finance Co. Pvt. Ltd., Ground Floor,<br>Beauty Plaza Building 
Balmatta Road, Mangaluru-575 001 <br>
Phone : 0824 - 4255640  
Mobile : +91 77608 33500
					</p>
					</th>
				</tr>
				</table>
				
				<table  class="table table-striped table-bordered">
						<tr>
							<td><b>To Address</b></td>
						</tr>							
						<tr>
							<td>
							<b><?php echo e($LoanApplicantRec->lacc_custname); ?></b><br>
							<?php
							$resaddr = unserialize($LoanApplicantRec->lacc_resaddr);
							echo $resaddr[0] ."<Br>";
							echo $resaddr[1] ." - " . $resaddr[2] .  "<Br>";
							echo "<b>Ph. No.- </b>". $resaddr[5] ;
							?>
							</td>
						</tr>	
				</table>
				
				<table  class="table table-striped table-bordered">
						<tr>
							<th>Loan Account No.</th><td><b><?php echo $loanacc = $LoanApplicantRec->lacc_no; ?></b></td>	
							<th>Loan amount</th><td><b><?php echo e($LoanApplicantRec->lacc_loanamt); ?></b></td>								
						</tr>						
						<tr>
							<th>Name</th><td><b><?php echo e($LoanApplicantRec->lacc_custname); ?></b></td>	
							<th>Tenure</th><td><b><?php echo e($LoanApplicantRec->lacc_tenor); ?> months</b></td>								
						</tr>
						<tr>
							<th>Location</th><td><b><?php echo e($LoanApplicantRec->br_name); ?></b></td>	
							<th>Rate of Interest</th><td><b><?php echo e($LoanApplicantRec->lacc_intrate); ?>% per annum</b></td>
						</tr>
						<tr><?php /* $LoanApplicantRec->br_name */ ?>
							<th>Loan Account date</th><td><b><?php echo  date("d-m-Y",strtotime($LoanApplicantRec->lacc_opendt)); ?></b></td>	
							<th>Interest Amount</th>
							<td><b><?php echo $data['totemiamt']; ?></b></td>
						</tr>						
					<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>	
				</table>
			</div>
		</div>
		<!-- END DATA EXPORT -->
		
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> Loan Installment detail </h3>
			</div>
			<div class="widget-content">
				<table  class="table table-striped table-bordered" id="tabloan">
				<thead>
					<tr>
						<th>Installment No.</th>
						<th>Installment Date</th>					
						<th  style="text-align: right;">Principal ()</th>	
						<th style="text-align: right;">Interest ()</th>				
						<th style="text-align: right;">EMI ()</th>
						<th>Chq No.</th>
					</tr>
				</thead>
				<tbody>	
				<?php					
					$totinterestamt =0;
					$tototheramt = 0;
					$totemiamt = 0;
					$totpaidamt = 0;
					$totbalamt = 0;
					$btn=0;
					$paid_amt = 0;
					$bal_amt=0;
					$totdelaypayment=0;
					$totpaiddecreaseonloop = $data['LoanTotalPaid'];
				?>
						<?php $__currentLoopData = $data['LoanInstallmentRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanInstallmentRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<?php
						$lins_amt=$LoanInstallmentRec->lins_amt;						
						$lins_iamt=$LoanInstallmentRec->lins_iamt;						
						$othr_amt=$LoanInstallmentRec->penaltysum;
						$delayamt=$LoanInstallmentRec->delayamt;
						$totamt = $lins_amt+$lins_iamt+$othr_amt+$delayamt;
						
						//Sum Total
						$totinterestamt = $totinterestamt +  $lins_iamt;
						$tototheramt = $tototheramt + $othr_amt;
						$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt+$delayamt);
						$totdelaypayment = $totdelaypayment + $delayamt;
						
						if($totpaiddecreaseonloop > $totamt)
						{
							$paid_amt=$totamt;
							$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
						}
						else
						{
							$paid_amt = $totpaiddecreaseonloop;
							$totpaiddecreaseonloop = 0;
						}
												
						$bal_amt=$totamt-$paid_amt;
						
						$totpaidamt = $totpaidamt + $paid_amt;
						$totbalamt = $totbalamt + $bal_amt;	
						
				//Coding to Calculate days difference between two days start here	
				$currentdt = strtotime(date("Y-m-d")); //Current day
				$installmentdt = strtotime($LoanInstallmentRec->lins_date); //Installment day
				$datediff = $currentdt - $installmentdt;				
				$daysdifference =  round($datediff / (60 * 60 * 24)) ;
				if($daysdifference >=0)
				{
					if($bal_amt > 0)
					{
						$daysdifference=$daysdifference;
					}
					else
					{
						$daysdifference=0;
					}
				}
				else
				{
					$daysdifference =0;
				}
				//Coding to Calculate days difference between two days ends here
						?>
						<tr>
							<td><?php echo e($LoanInstallmentRec->lins_no); ?></td><!-- SL No. -->
							<td><?php echo date("d-m-Y",strtotime($LoanInstallmentRec->lins_date)); ?></td><!-- Installment dt -->
							<td style="text-align: right;"><?php echo e($LoanInstallmentRec->lins_amt); ?></td><!-- Principle Amt -->
							
							<td style="text-align: right;"><?php echo e($LoanInstallmentRec->lins_iamt); ?></td>  <!-- Interest Amt -->
							
							<td style="text-align: right;"><?php echo round($totamt); ?></td><!-- EMI Amt -->
							<td><?php echo e($LoanInstallmentRec->lins_chqno); ?></td><!-- EMI Amt -->
						</tr>
						<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
				</tbody>
				<tfoot>
						<th></th>
						<th>Total</th>					
						<th style="text-align: right;"><?php echo e($LoanApplicantRec->lacc_loanamt); ?></th>
						<th style="text-align: right;"><?php echo $totinterestamt; ?></th>							
						<th style="text-align: right;"><?php echo round($totemiamt); ?></th>							
						<th></th>		
				</tfoot>
				</table>
			</div>
		</div>
		<!-- END DATA EXPORT -->
	</div>
				<div class="widget">
					<div class="widget-header"><center> 
						<button class="btn btn-info btn-default"    id="btnPrint1" onclick="printDiv('printableArea')" ><span class="fa fa-asterisk"></span> Print</button>
						</center>
					</div>
				</div>
		</form>					
	</div>
</div>
<!-- /main -->
</div>
<script>
	$('.createMod').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.edit').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href')); 
	});
	
	$('#mypayment').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id1').val($("input[name='lacc_id']").val());
				$('#lins_id1').val(lins_id);
				var CurrentPayableAmt =Obj.CurrentPayableAmt;
				var PreviousPayableAmt =Obj.PreviousPayableAmt;
				var TotalPayableAmt =Obj.TotalPayableAmt;
				$('#currentpayable').val(CurrentPayableAmt);
				$('#previousbalance').val(PreviousPayableAmt);
				$('#totalpayable').val(TotalPayableAmt);
            }
        });
    })
	
	$('#myotheramt').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id2').val($("input[name='lacc_id']").val());
				$('#lins_id2').val(lins_id);
            }
        });
    })
	
	function calculatebal() 
	{
		balanceamt = parseFloat($('#totalpayable').val()) - parseFloat($('#ltr_amt').val());
		$('#balanceamt').val(balanceamt);
		if($('#balanceamt').val() == "NaN")
		{
			$('#balanceamt').val(0);
		}
	}
	
	//### Payment submission entry code starts here
	flag=0;
	$('.btnloanpayment').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();		           
		var lacc_id1 = $("input[name='lacc_id1']").val();
		var lins_id1 = $("input[name='lins_id1']").val();
		var ltr_transdt = $("input[name='ltr_transdt']").val();
		var currentpayable = $("input[name='currentpayable']").val();
		var previousbalance = $("input[name='previousbalance']").val();
		var totalpayable = $("input[name='totalpayable']").val();
		var ltr_amt = $("input[name='ltr_amt']").val();
		var balanceamt = $("input[name='balanceamt']").val();
		var ltr_billno = $("input[name='ltr_billno']").val();
		var ltr_chqno = $("input[name='ltr_chqno']").val();
		var ltr_paymenttype = $('#ltr_paymenttype').val();
		var ltr_bank = $("input[name='ltr_bank']").val();
		var ltr_note = $("input[name='ltr_note']").val();
		if(ltr_billno == "" )
		{
			alert("Transaction No. Should not be empty...");
		}
		else if(ltr_transdt == "" )
		{
			alert("Transaction Date Should not be empty...");
		}
		else if(ltr_amt == "" )
		{
			alert("Paid Amount. Should not be empty...");
		}
		else if(parseFloat(ltr_amt) < parseFloat(previousbalance) )
		{
			alert("Paid amount should be greater than Previous balance...");
		}
		else if(ltr_paymenttype == "" )
		{
			alert("Payment type should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentpayment",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id1, lins_id:lins_id1, ltr_transdt:ltr_transdt, ltr_transtype:"Payment", ltr_billno:ltr_billno, ltr_amt:ltr_amt, ltr_paymenttype:ltr_paymenttype, ltr_chqno:ltr_chqno, ltr_bank:ltr_bank, ltr_note:ltr_note, ltr_status:"Active"},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan payment done successfully..");
						//$('#sideMenu').load('viewloanaccount');
						LoadLoanApplication(lacc_id1,lins_id1);
						$('#mypayment').modal('toggle');
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	//### Payment submission entry code ends here
	
	function LoadLoanApplication(b,acno)
	{
		$.ajax({
			url: 'LoanAcPanel',
			type: 'GET',
			data:'&lacc_id='+b+'&acno='+acno,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	
	//### Penalty submission entry code starts here
	$('.btnloanpenalty').click(function(e)
	{
		//_token2 lacc_id2 lins_id2 penaltydate penaltytype  penaltyamt penaltynote
		e.preventDefault();
		_token = $("input[name='_token2']").val();		           
		var lacc_id1 = $("input[name='lacc_id2']").val();
		var lins_id1 = $("input[name='lins_id2']").val();
		var ltr_transdt = $("input[name='penaltydate']").val();
		var penaltytype = $('#penaltytype').val();
		var currentpayable = 0;
		var previousbalance = 0;
		var totalpayable = 0;
		var ltr_amt = $("input[name='penaltyamt']").val();
		var balanceamt =0;
		var ltr_billno = 0;
		var ltr_chqno = '';
		var ltr_paymenttype = '';
		var ltr_bank = '';
		var ltr_note = $("input[name='penaltynote']").val();
		if(penaltytype == "" )
		{
			alert("Type Should not be empty...");
		}
		else if(ltr_transdt == "" )
		{
			alert("Date Should not be empty...");
		}
		else if(ltr_amt == "" )
		{
			alert("Amount should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentpayment",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id1, lins_id:lins_id1, ltr_transdt:ltr_transdt, ltr_transtype:penaltytype, ltr_billno:ltr_billno, ltr_amt:ltr_amt, ltr_paymenttype:ltr_paymenttype, ltr_chqno:ltr_chqno, ltr_bank:ltr_bank, ltr_note:ltr_note, ltr_status:"Active"},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Other charges added successfully..");
						LoadLoanApplication(lacc_id1,lins_id1);
						$('#myotheramt').modal('toggle');
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	//### Penalty submission entry code ends here
	
	//Preclosure starts here
	function funLoanPreClosure(lacc_id,lacc_no)
	{
		$.ajax({
			url: 'loanAccountPreclosure',
			type: 'GET',
			data:'&lacc_id='+lacc_id+'&lacc_no='+lacc_no,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	//Preclosure ends here
	
	//View Report starts here
	$('#viewreport').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanrptsingle',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				$('#rptsingle').html(data);
            }
        });
    })
	//View Report ends here
	
	//Load Loan Cancellation Starts here
	$('#modcancellation').on('show.bs.modal', function(e) {
        var $modal = $(this),
            ltr_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanCancellationLoad',
            data: 'ltr_id=' + ltr_id,
            success: function(data) {
				$('#rptCancellation').html(data);
            }
        });
    })
	//Load Loan Cancellation Ends here
	//loanCancellation insert
	flag=0;
	$('.btncancel').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();	           
		var ltr_id = $("input[name='ltr_id']").val();
		var lacc_id = $("input[name='lacc_id']").val();
		var lins_id = $("input[name='lins_id']").val();
		var ltr_cancellationtype = $("input[name='ltr_cancellationtype']").val();
		var ltr_cancellationreason = $('#ltr_cancellationreason').val();
		if(ltr_cancellationtype == "" )
		{
			alert("Cancellation type Should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentcancellationins",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,ltr_id:ltr_id, lacc_id:lacc_id, lins_id:lins_id, ltr_cancellationtype:ltr_cancellationtype, ltr_cancellationreason:ltr_cancellationreason},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan Cancellation done successfully..");
						$('#modcancellation').modal('toggle');
						//############
						reloadcancelmodal(lins_id,lacc_id);
						//############
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	
	function reloadcancelmodal(lins_id,lacc_id)
	{
		var $modal = $(this),
            lins_id = lins_id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanrptsingle',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				$('#rptsingle').html(data);
            }
        });		
	}
	
	$('#viewreport').on('hidden.bs.modal', function () {	           
		var lacc_id = $("input[name='lacc_id']").val();
		var lacc_no = $("input[name='lacc_no']").val();		 
			LoadLoanApplication(lacc_id,lacc_no);
	})
	
	function funSingleTransactionRPT(lacc_id,acno)
	{
		$.ajax({
			url: 'RPTSingleTrans',
			type: 'GET',
			data:'&lacc_id='+lacc_id+'&acno='+acno,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	
	function loadreport(lins_id)
	{
		$.ajax({
			url: 'LoanEMIreport',
			type: 'GET',
			data:'&lins_id='+lins_id,
			success: function(data)
			{
				$('.printreport').html(data);
			}
		});
	}
	
	
</script>
<script>

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

        $("#btnPrint").click(function(){
            var divContents = $("#printableArea").html();
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>NavChetana Transaction Report</title>');
            printWindow.document.write('<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"><link href="assets/css/main.css" rel="stylesheet" type="text/css"><link href="assets/css/my-custom-styles.css" rel="stylesheet" type="text/css"><link href="css/jquery.toast.min.css" rel="stylesheet" type="text/css"><link rel="stylesheet" href="css/jquery-confirm.min.css"></head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        });
</script>