<style>
table {
  border-collapse: collapse;
  border: 1px solid black;
  width: 100%;
} 

th,td {
  border: 1px solid black;
  padding-left: 5px;
  padding-right: 5px;
}

table.c {
  table-layout: auto;
  width: 100%;
}

</style>
<?php
$LoanTotalPaid=0;
foreach($data['LoanApplicantRecords'] as $LoanApplicantRec)
{
	$lacc_id = $LoanApplicantRec->lacc_id;
}
foreach($data['LoanInstallmentRecords'] as $LoanInstallmentRec)
{
	$lins_amt = $LoanInstallmentRec->lins_amt;
	$lins_iamt =$LoanInstallmentRec->lins_iamt;
}
//######AllInstRecords starts here ***************				
$totinterestamt =0;
$tototheramt = 0;
$totemiamt = 0;
$totpaidamt = 0;
$totbalamt = 0;
$btn=0;
$paid_amt = 0;
$bal_amt=0;
$totdelaypayment=0;
$chqbounce=0;
$totchqbounce=0;
$totinsamt=0;
$totalloanamnt = $LoanApplicantRec->lacc_loanamt;
$totpaiddecreaseonloop = $data['LoanTotalPaid'];
$paidamtbal = $data['LoanTotalPaid'];
$nopending = 0;
foreach($data['AllInstRecordsPrev'] as $LoanInstallmentRecPrev)
{
		$lins_amt=$LoanInstallmentRecPrev->lins_amt;						
		$lins_iamt=$LoanInstallmentRecPrev->lins_iamt;						
		$othr_amt=$LoanInstallmentRecPrev->penaltysum;
		$delayamt=$LoanInstallmentRecPrev->delayamt;
		$chqbounce=$LoanInstallmentRecPrev->chequebounce;
		$totamt = $lins_amt+$lins_iamt+$othr_amt+$delayamt+$chqbounce;
		//Sum Total
		$totinsamt = $totinsamt + $lins_amt;
		$totinterestamt = $totinterestamt +  $lins_iamt;
		$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt+$delayamt);
		$totdelaypayment = $totdelaypayment + $delayamt;
		$totchqbounce= $totchqbounce + $chqbounce;
		$tototheramt = $tototheramt + $othr_amt;
		if($totpaiddecreaseonloop > $totamt)
		{
			$paid_amt=$totamt;
			$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
		}
		else
		{
			$paid_amt = $totpaiddecreaseonloop;
			$totpaiddecreaseonloop = 0;
		}												
		$bal_amt=$totamt-$paid_amt;						
		$totpaidamt = $totpaidamt + $paid_amt;
		if(round($bal_amt) > 0)
		{
			$nopending = $nopending + 1;
		}
}
if($data['CurrentPayableAmt'] > 0 )
{
	$nopending = $nopending + 1;
}
//###-Other charges starts here
if($paidamtbal > $tototheramt)
{
	$paidamtbal = $paidamtbal - $tototheramt;
	$tototheramt = 0;
}
else
{
	$tototheramt = $tototheramt - $paidamtbal;
	$paidamtbal =0;
}
//###-Other charges ends here
//###-Penalty charges starts here
if($paidamtbal > $totdelaypayment)
{
	$paidamtbal = $paidamtbal - $totdelaypayment;
	$totdelaypayment =0;
}
else
{
	$totdelaypayment = $totdelaypayment - $paidamtbal;
	$paidamtbal =0;
}
//###-Penalty charges ends here
//###-Cheque Bounce charges starts here
if($paidamtbal > $totchqbounce)
{
	$paidamtbal = $paidamtbal - $totchqbounce;
	$totchqbounce = 0;
}
else
{
	$totchqbounce = $totchqbounce - $paidamtbal;
	$paidamtbal =0;
}
//###-Cheque Bounce charges ends here
//##Interest Amount starts here
if($paidamtbal > $totinterestamt)
{
	$paidamtbal = $paidamtbal - $totinterestamt;
	$totinterestamt = 0;
}
else
{
	$totinterestamt = $totinterestamt - $paidamtbal;
	$paidamtbal =0;
}
//##Interest amount ends here
//##Principal Amount starts here
if($paidamtbal > $totinsamt)
{
	$totinsamt = 0;
	$paidamtbal = $paidamtbal - $totinsamt;
}
else
{
	$totinsamt = $totinsamt - $paidamtbal;
	$paidamtbal =0;
}
//##Principal Amount ends here
//######AllInstRecords ends here ***************
?>
<!-- MAIN CONTENT WRAPPER -->
<div class="content">
				<img src="img/navachethana-logo.png" align="left" >
				<b style="font-size: 25px;">Navachethana Finance Company (R)</b><br>
				"Beauty Plaza" Building, Ground Floor, Balmatta Road, mangalore-575001<br>
				<b>Phone : 0824 - 4255640 Mobile No. 7760833500</b>
				<hr> 
				<table>
					<?php  $i=1;?>
					@foreach($data['LoanApplicantRecords'] as $LoanApplicantRec)
					<?php
					//print_r($LoanApplicantRec);
					?>
					<?php $lacc_id = $LoanApplicantRec->lacc_id; ?>
					<input type="hidden" name="lacc_id" id="lacc_id" value="{{$LoanApplicantRec->lacc_id}}">
					<input type="hidden" name="lacc_no" id="lacc_no" value="{{$LoanApplicantRec->lacc_no}}">
						<tr>
							<th style="text-align: left;">Loan Account No:</th><td ><?php echo $loanacc = $LoanApplicantRec->lacc_no; ?></td>	
							<th style="text-align: left;">Loan Account Date:</th><td ><?php echo  date("d-m-Y",strtotime($LoanApplicantRec->lacc_opendt)); ?></td>								
						</tr>			
						<tr>
							<th style=" text-align: left;">Customer Name:</th><td>{{$LoanApplicantRec->lacc_custname}}</td>	
							<th style=" text-align: left;">EMI Start Date:</th>
							<td><?php 
echo $effectiveDate = date('d-M-Y', strtotime("+1 month", strtotime($LoanApplicantRec->lacc_opendt)));
					?></td>						
						</tr>
						<tr style="width: 15%">
							<th rowspan="3"  style=" text-align: left;" valign="Top">Customer Address</th><td rowspan="3"><?php 
							$addr=unserialize($LoanApplicantRec->lacc_resaddr); 
							//print_r($addr);
							echo $addr[0]."<br>";
							echo $addr[1]."<br>";
							echo $addr[2]."<br>";
							echo $addr[3];
							if($addr[4] != "")
							{
							echo "," . $addr[4];
							}
							?></td>	
							<th style="text-align: left;">EMI Close Date</th><td><?php 
//echo  date("d-m-Y",strtotime($LoanApplicantRec->lacc_opendt));
echo $effectiveDate = date('d-M-Y', strtotime("+$LoanApplicantRec->lacc_tenor months", strtotime($LoanApplicantRec->lacc_opendt)));
					?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Interest rate.</th><td><?php echo $loanacc = $LoanApplicantRec->lacc_intrate; ?>%</td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Loan Tenure.</th><td>{{$LoanApplicantRec->lacc_tenor}} months</td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Mobile No.</th><td><?php echo $addr[5]; ?></td>
							<th style="text-align: left;">Monthly EMI.</th><td><?php echo round($lins_amt + $lins_iamt); ?></td>
						</tr>
						<tr>
							<th style="text-align: left;">Tel.</th><td><?php echo $addr[6]; ?></td>
							<th style="text-align: left;">Outstanding Principal.</th><td><?php 
							echo round(($LoanApplicantRec->lacc_loanamt) - ($data['LoanTotalPaid'] - $data['LoanAllCharges'] ));
							?></td>
						</tr>
						<tr>
							<th style="text-align: left;">Branch</th><td>{{$LoanApplicantRec->br_name}}</td>	
							<th style="text-align: left;">No. of EMI Arrears</th><td><?php echo $nopending; ?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Loan Amount</th><td>{{$LoanApplicantRec->lacc_loanamt}}</td>	
							<th style="text-align: left;">Arrear EMI</th>
							<td><?php echo $totinsamt + $totinterestamt; ?></td>
						</tr>
						
						<tr>
							<th rowspan="3" style="text-align: left;">Loan Paid details</th><td rowspan="3">{{$LoanApplicantRec->lacc_remarks}}</td>	
							<th style="text-align: left;">Bank charges</th><td><?php echo $totchqbounce; ?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Arrear Penalty.</th><td><?php echo $totdelaypayment; ?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Legal/Other charges</th><td><?php echo $tototheramt; ?></td>
						</tr>
							
						<tr>
							<th style="text-align: left;">Chit Reference No.</th><td>{{$LoanApplicantRec->lacc_chitrefno}}</td>	
							<th style="text-align: left;">Total Arrear's.</th>
							<td><?php echo round($data['PreviousPayableAmt']); ?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Branch name.</th><td>{{$LoanApplicantRec->chitbr}}</td>	
							<th style="text-align: left;">Current installment</th><td><?php echo round($data['CurrentPayableAmt']);
							?></td>
						</tr>
						
						<tr>
							<th style="text-align: left;">Paid up amount.</th><td><?php
						if($LoanApplicantRec->lacc_paidupamt != "")
						{
						echo "". $LoanApplicantRec->lacc_paidupamt;
						}
						?></td>	
							<th style="text-align: left;">Total Amt pending</th>
							<td><?php 
							echo $LoanApplicantRec->lacc_loanamt-$data['LoanTotalPaid']; 
							?></td>
						</tr>
					@endforeach	
				</table>		
<hr>
<b>Loan Installment detail<b>
<table  class="c">
	<thead>
		<tr>
			<th>Inst No.</th>
			<th>Receipt<br>No.</th>					
			<th>Month</th>							
			<th>Date of<br>Repayment</th>
			<th>Principal</th>							
			<th>Interest</th>					
			<th>Bank<br>charges</th>						
			<th>Late fee</th>								
			<th>Case/Other<br>charges</th>		
			<th>Mode of<br> Payment</th>
			<th>Cheque No. & <br> Bank</th>
		</tr>
	</thead>
	<tbody>	
	<?php
	$i=1;
	$linsamt= 0;
	$intamt = 0;
	$instno = 0;
	$principleamt = 0;	
	$Interestamt = 0;	
	$chqbounce = 0;
	$penaltyamt = 0;	
	$othercharges = 0;
	$totalinstallmentamount = 0;
	?>
		@foreach($data['LoanPaymentRecords'] as $LoanPaymentRec)
		<?php
		$paidamt = $LoanPaymentRec->ltr_amt;
		$paidamtbal = $LoanPaymentRec->ltr_amt;
		if($LoanPaymentRec->lins_no  == $instno)
		{
		}
		else
		{
			$principleamt = $principleamt + $LoanPaymentRec->principleamt;	
			$Interestamt = $Interestamt  + $LoanPaymentRec->Interestamt;	
			$chqbounce = $chqbounce + $LoanPaymentRec->chqbounce;
			$penaltyamt = $penaltyamt + $LoanPaymentRec->penaltyamt;	
			$othercharges = $othercharges + $LoanPaymentRec->othercharges;
		}
		//###-Other charges starts here
		if($paidamtbal > $othercharges)
		{
			$othercharges = $othercharges;
			$paidamtbal = $paidamtbal - $othercharges;
		}
		else
		{
			$othercharges = $paidamtbal;
			$paidamtbal =0;
		}
		//###-Other charges ends here
		//###-Penalty charges starts here
		if($paidamtbal > $penaltyamt)
		{
			$penaltyamt = $penaltyamt;
			$paidamtbal = $paidamtbal - $penaltyamt;
		}
		else
		{
			$penaltyamt = $paidamtbal;
			$paidamtbal =0;
		}
		//###-Penalty charges ends here
		//###-Cheque Bounce charges starts here
		if($paidamtbal > $chqbounce)
		{
			$chqbounce = $chqbounce;
			$paidamtbal = $paidamtbal - $chqbounce;
		}
		else
		{
			$chqbounce = $paidamtbal;
			$paidamtbal =0;
		}
		//###-Cheque Bounce charges ends here
		//##Interest Amount starts here
		if($paidamtbal > $Interestamt)
		{
			$Interestamt = $Interestamt;
			$paidamtbal = $paidamtbal - $Interestamt;
		}
		else
		{
			$Interestamt = $paidamtbal;
			$paidamtbal =0;
		}
		//##Interest amount ends here
		//##Principal Amount starts here
		if($paidamtbal > $principleamt)
		{
			$principleamt = $principleamt;
			$paidamtbal = $paidamtbal - $principleamt;
		}
		else
		{
			$principleamt = $paidamtbal;
			$paidamtbal =0;
		}
		//##Principal Amount ends here
		?>
		<tr>
			<td><?php echo $LoanPaymentRec->lins_no; ?></td>
			<td><?php echo $LoanPaymentRec->ltr_billno; ?></td>
			<td><?php echo date("M",strtotime($LoanPaymentRec->ltr_transdt)); ?></td>
			<td><?php echo date("d-m-Y",strtotime($LoanPaymentRec->ltr_transdt)); ?></td>
			<td style="text-align: right;"><?php echo round($principleamt); ?></td>
			<td style="text-align: right;"><?php echo round($Interestamt); ?></td>
			<td style="text-align: right;"><?php echo round($chqbounce); ?></td>
			<td style="text-align: right;"><?php echo round($penaltyamt); ?></td>
			<td style="text-align: right;"><?php echo round($othercharges); ?></td>
			<td><?php echo $LoanPaymentRec->ltr_paymenttype; ?></td>
			<td><?php echo $LoanPaymentRec->ltr_chqno . " " . $LoanPaymentRec->ltr_bank; ?></td>
		</tr>
		<?php
		$linsamt = $linsamt + $LoanInstallmentRec->lins_amt;
		$intamt  = $intamt + $LoanInstallmentRec->lins_iamt;
		$totalinstallmentamount = $totalinstallmentamount + $paidamt;
		?>
		@endforeach
	</tbody>
	<tfoot>
		<tr>		
			<th colspan="11">Total Amount : <?php echo $totalinstallmentamount; ?></th>
		</tr>
	</tfoot>
</table>
<hr>
<b>Cheque Return / Cancelled Receipt Transactions</b>

				<table  class="table table-striped table-bordered" >
					<thead>
						<tr>
							<th>Inst No.</th>
							<th>Cheque No. & Bank</th>
							<th>Receipt No.</th>								
							<th>Payment Date</th>				
							<th>Cheque Amount</th>	
							<th>Cheque return Date</th>				
							<th>Bank charges</th>
						</tr>
					</thead>
					<tbody>
					<?php
					$totcancellationcharge = 0;
					?>
						@foreach($data['LoanChqBounceRecords'] as $LoanChqBounceRecords)
						<tr>
							<td><?php echo $LoanChqBounceRecords->lins_no; ?></td>
							<td><?php echo $LoanChqBounceRecords->ltr_chqno; ?> <?php echo $LoanChqBounceRecords->ltr_bank; ?></td>
							<td><?php echo $LoanChqBounceRecords->ltr_billno; ?></td>
							<td><?php echo date("d-m-Y",strtotime($LoanChqBounceRecords->ltr_transdt)); ?></td>
							<td style="text-align: right;"><?php echo round($LoanChqBounceRecords->chequeamt); ?></td>
							<td><?php echo $LoanChqBounceRecords->cancellationdate; ?></td>
							<td style="text-align: right;"><?php echo round($LoanChqBounceRecords->cancellationcharge); ?></td>
						</tr>
						<?php
						$totcancellationcharge = $totcancellationcharge + $LoanChqBounceRecords->cancellationcharge;
						?>
						@endforeach
					</tbody>
	<tfoot>
		<tr>		
			<th colspan="6" style="text-align: right;">Total Amount</th>
			<th style="text-align: right;"><?php echo round($totcancellationcharge); ?></th>
		</tr>
	</tfoot>
				</table>
<hr>
<b>Balance amount Details </b>
			</div>
			<div class="widget-content">
				<table  class="table table-striped table-bordered" >
					<thead>
						<tr>
							<th>Inst No.</th>
							<th>Amount</th>					
							<th>Paid Date</th>					
							<th>Status</th>		
						</tr>
					</thead>
					<tbody>	
					<?php
					/*
						@foreach($data['LoanRpt'] as $LoanInstallmentRec)
						<tr>
							<td><?php echo $i; $i=$i+1; ?></td>
							<td><?php echo $LoanInstallmentRec->ltr_billno; ?></td>
							<td><?php echo date("M",strtotime($LoanInstallmentRec->ltr_transdt)); ?></td>
							<td><?php echo date("d-m-Y",strtotime($LoanInstallmentRec->lins_date)); ?></td>
							<td><?php echo $LoanInstallmentRec->lins_amt; ?></td>
							<td><?php echo $LoanInstallmentRec->lins_iamt; ?></td>
							<td><?php echo $LoanInstallmentRec->bankcharges; ?></td>
							<td><?php echo $LoanInstallmentRec->latefee; ?></td>
							<td><?php echo $LoanInstallmentRec->caseotherchrge; ?></td>
							<td><?php echo $LoanInstallmentRec->ltr_paymenttype; ?></td>
							<td><?php echo $LoanInstallmentRec->ltr_chqno; ?></td>
						</tr>
						@endforeach
					*/
					?>	
					</tbody>
	<tfoot>
		<tr>		
			<th colspan="3" style="text-align: right;">Total Amount</th>
			<th style="text-align: right;">0</th>
		</tr>
	</tfoot>
				</table>
				<hr>
				<b>Expense Details </b>
				<table  class="table table-striped table-bordered" >
					<thead>
						<tr>
							<th>Inst No.</th>
							<th>Reason</th>			
							<th>Expense Date</th>
							<th>Expense Amount</th>					
							<th>Note</th>		
						</tr>
					</thead>
					<tbody>
					<?php
					$totLoanExpenseRec = 0;
					?>
						@foreach($data['LoanExpenseRecords'] as $LoanExpenseRec)
						<tr>
							<td><?php echo $LoanExpenseRec->lins_no; ?></td>
							<td><?php echo $LoanExpenseRec->ltr_transtype; ?></td>
							<td><?php echo date("d-m-Y",strtotime($LoanExpenseRec->expensedate)); ?></td>
							<td style="text-align: right;"><?php echo round($LoanExpenseRec->expenseamt); ?></td>
							<td><?php echo $LoanExpenseRec->ltr_note ; ?></td>
						</tr>
						<?php
						$totLoanExpenseRec = $totLoanExpenseRec + $LoanExpenseRec->expenseamt;
						?>
						@endforeach
					</tbody>
					<tfoot>
						<tr>		
							<th colspan="3" style="text-align: right;">Total Amount</th>
							<th style="text-align: right;"><?php echo round($totLoanExpenseRec); ?></th>
							<th ></th>
						</tr>
					</tfoot>
				</table>
	<hr>

				<table  class="c" >
					<thead>
						<tr>
							<td style="width: 250px;"><b>Security Entry</b></td>		
							<td>{{$LoanApplicantRec->lacc_securityentry}}</td>		
						</tr>
					</thead>
				</table>

    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $( document ).ready(function() {
		window.print();
    });
    </script>
		<!-- END DATA EXPORT -->