<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    @import  url('https://fonts.googleapis.com/css?family=Battambang');

    .header,
    .header-space,
    .footer,
    .footer-space {
        height: 20px;
    }

    .header {
        position: fixed;
        top: 0;
    }

    .footer {
        position: fixed;
        bottom: 0;
    }

    @media  print {
        body {
            width: 21cm;
            height: 29.7cm;
        }
    }

    body {
        width: 21cm;
        margin: 0 auto;
        font-size: 13px;
        font-family: 'Battambang', "Helvetica Neue", Helvetica, Arial, sans-serif !important;
    }

    body table {
        width: 100%;
        float: left
    }

    .content .content-header .header-title {
        text-align: center;
    }

    .content-body table {
        border-collapse: collapse;
    }

    .content-body table tbody tr td {
        border: 1px solid black;
        padding: 3px;
    }

    .text-right {
        text-align: right
    }
    tbody .txt-right td{
        text-align: center
    }
</style>
<body>
    <table>
        <thead>
            <tr>
                <td>
                    <div class="header-space">&nbsp;</div>
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <div class="content">
                        <div class="content-header">
                            <div class="header-title">
                                <h3><?php echo e($branch->branch_en); ?></h3>
                                <h3>Schedule of Payment</h3>
                            </div>
                        </div>
                        <div class="content-body">
                        <table class="table table-bordered schedul" border="1" cellpadding="0" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th>លរ</th>
                                <th>ថ្ងៃបង់ប្រាក់</th>
                                <th>ប្រាក់ដើមត្រូវបង់</th>
                                <th>ការប្រាក់ត្រូវបង់</th>
                                <th>សរុបប្រាក់ត្រូវបង់</th>
                                <th>សមតុល្យប្រាក់ចុងគ្រា</th>
                            </tr>
                        </thead>

                        <tbody>
                        <?php
                            $total_step_amount  = $installment->loan_amountos;
                            $loanInstallment    = $installment->loan_amountos;
                            $principal_remain   = $installment->loan_amountos;
                            $index              = 0;
                            $interest           = empty($installment->loan_interest)?0:$installment->loan_interest;
                            $datepayment        = $installment->loan_firstpaydate;
                        ?>

                        <?php if(count($installment_step)>0): ?>
                            <?php $__currentLoopData = $installment_step; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $step): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $total_step_amount  -= $step->itempro_payamount;
                                    $loanInstallment    -= $step->itempro_payamount;
                                    $principal_remain   -= $step->itempro_payamount;
                                    $index++;
                                ?>
                                <tr>
                                    <td width="8%">
                                       <?php echo e($key+1); ?>

                                    </td>
                                    <td>
                                        <?php echo e(date('d-m-Y',strtotime($step->itempro_date))); ?>

                                    </td>
                                    <td>
                                        $<?php echo e(number_format($step->itempro_payamount,2)); ?>

                                    </td>
                                    <td>
                                        $0.00
                                    </td>
                                    <td>
                                        $<?php echo e(number_format($step->itempro_payamount,2)); ?>

                                    </td>
                                    <td>
                                        $<?php echo e(number_format($principal_remain,2)); ?>

                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endif; ?>

                        <?php if($installment->loan_duration && $installment->loan_duration>0): ?>
                            <?php  $loanInstallment=$installment->loan_amount_payment;  ?>
                            <?php if($installment->loan_type==2 || ($installment->loan_type==1 && $installment->loan_interest<=0)): ?>
                                <?php
                                    $principal   = $loanInstallment / $installment->loan_duration;
                                    $remainingBalance = $loanInstallment;
                                ?>
                                <?php for($i=1;$i<=$installment->loan_duration;$i++): ?>
                                    <?php
                                        $AnnualInterest = $remainingBalance*$interest/100;
                                        $payment = $principal+$AnnualInterest;
                                        $remainingBalance -= $principal;
                                        $principal_remain -= $principal;
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                           $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php else: ?>
                                <?php 
                                    $rate               = $interest/100;
                                    $loan_duration      = $installment->loan_duration;
                                    $payment            = ($loanInstallment * $rate) / (1 - (pow((1 + $rate), -$loan_duration)));
                                    $remainingBalance   = $loanInstallment;
                                ?>
                                <?php for($i=1;$i<=$installment->loan_duration;$i++): ?>
                                    <?php 
                                        $start              = $i + 1;
                                        $AnnualInterest     = $remainingBalance * $rate;
                                        $principal          = $payment-$AnnualInterest;
                                        $remainingBalance  -= $principal;
                                        $principal_remain  -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;

                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if(!empty($installment_change_rate[0])): ?>
                            <?php  $loanInstallment=$installment_change_rate[0]->amount_generate_loan; ?>
                            <?php if($installment_change_rate[0]->loan_type==2 || ($installment_change_rate[0]->loan_type==1 && $installment_change_rate[0]->first_interest<=0 && count(installment_change_rate)<=0)): ?>
                                <?php
                                    $principal   = $loanInstallment / $installment_change_rate[0]->duration;
                                    $remainingBalance = $loanInstallment;
                                    $datepayment = $installment_change_rate[0]->first_payment_date;
                                ?>
                                <?php for($i=1;$i<=$installment_change_rate[0]->duration;$i++): ?>
                                    <?php

                                        $AnnualInterest = $remainingBalance*$installment_change_rate[0]->first_interest/100;
                                        foreach ($installment_change_rate as $key => $value) {
                                           if($value->month_from<=$i&&$value->month_to>=$i){
                                                $AnnualInterest = $remainingBalance*$value->interest_for_change/100;
                                           }
                                        }

                                        $payment = $principal+$AnnualInterest;
                                        $remainingBalance -= $principal;
                                        $principal_remain -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;
                                        
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                           $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php else: ?>
                                <?php 
                                    $rate               = $installment_change_rate[0]->first_interest/100;
                                    $loan_duration      = $installment_change_rate[0]->duration;
                                    $payment            = ($loanInstallment * $rate) / (1 - (pow((1 + $rate), -$loan_duration)));
                                    $remainingBalance   = $loanInstallment;
                                    $index_count_month = 0;
                                    $check_change_rate = $rate;
                                    $rate_cp = $rate;
                                    $datepayment = $installment_change_rate[0]->first_payment_date;
                                ?>
                                <?php for($i=1;$i<=$installment_change_rate[0]->duration;$i++): ?>
                                    <?php 
                                        $AnnualInterest    = $remainingBalance * $rate;
                                        $check_change_rate = $rate;
                                        foreach ($installment_change_rate as $key => $value) {
                                           if($value->month_from<=$i&&$value->month_to>=$i){
                                                $check_change_rate =  $value->interest_for_change/100;
                                           }
                                        }
                                        if($rate_cp!=$check_change_rate){
                                            $rate_cp = $check_change_rate;
                                            $payment = ($remainingBalance * $check_change_rate) / (1 - (pow((1 + $check_change_rate), -($loan_duration-$index_count_month))));
                                            
                                        }
                                        $AnnualInterest     = $remainingBalance * $check_change_rate;
                                        $start              = $i + 1;
                                        $principal          = $payment-$AnnualInterest;
                                        $remainingBalance   -= $principal;
                                        $principal_remain   -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                        $index_count_month++;
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                              <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if(isset($installment_loan_bank[0])&&$installment_loan_bank[0]->amount>0): ?>
                             <tr>
                                <td width="8%">
                                    <?php echo e(++$index); ?>

                                </td>
                                <td>
                                    <?php echo e(date('d-m-Y',strtotime($installment_loan_bank[0]->date_payment))); ?>

                                </td>
                                <td>
                                    $<?php echo e(number_format($installment_loan_bank[0]->amount,2)); ?> 
                                </td>
                                <td>
                                    $0.00
                                </td>
                                <td>
                                     $<?php echo e(number_format($installment_loan_bank[0]->amount,2)); ?>

                                </td>
                                <td>
                                    $0.00
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                        </div>
                        <div class="content-footer">
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td>
                    <div class="footer-space">&nbsp;</div>
                </td>
            </tr>
        </tfoot>
    </table>
    <!-- <div class="header"><small>KH-Land Company</small></div>
    <div class="footer"><small>KH-Land Company</small></div> -->
    <script src="https//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
    <script>
        window.print();
    </script>
</body>

</html>