<?php


function loan_repayments_summary_table_display($from_month_id, $to_month_id)
{
//    display_heading2("Project P&L");
    
            
    display_title("Loan Repayments Summary");
    
    start_table(TABLESTYLE, "width=90%");
    $th = array("Month",
                "Schedulled Capital Repayments", "Schedulled Interest Repayments", "Month Schedulled Repayments",
                "Capital Repayments", "Interest Repayments", "Late Interest Repayments", 
                "Total Repayments", 
                "Month Unpaid Balance", 
                "Loan Balances");

    table_header($th);

    $months_result = get_months_range($from_month_id, $to_month_id);
    
    $k = $total_capital_payments = $total_late_interest = $total_interest = $total_repayments = 
            $total_schedulled_capital_repaymets = $total_schedulled_interest_repaymets = $total_schedulled_repaymets =  
            $total_unpaid_balance = $total_loans_balance = 0;
    
    
    while($month = db_fetch($months_result)){
        alt_table_row_color($k);

        label_cell($month['description']);
        
        $schedulled_repaymets = get_schedulled_installments_total($month['month_begin_date'], $month['month_end_date']);
        $schedulled_capital_repaymets = $schedulled_repaymets['capital_total'];
        $schedulled_interest_repaymets = $schedulled_repaymets['interest_total'];
        amount_cell($schedulled_capital_repaymets);
        amount_cell($schedulled_interest_repaymets);
        amount_cell($schedulled_capital_repaymets + $schedulled_interest_repaymets);
        
        $total_schedulled_capital_repaymets += $schedulled_capital_repaymets;
        $total_schedulled_interest_repaymets += $schedulled_interest_repaymets;
        $total_schedulled_repaymets += $total_schedulled_capital_repaymets + $schedulled_interest_repaymets;
        
        $month_repayments_summary = get_month_loan_repayments_summary($month['month_begin_date'], $month['month_end_date']);
        $capital_payments = $month_repayments_summary['capital_paid'];
        $late_interest = $month_repayments_summary['late_interest_paid'];       
        $interest = $month_repayments_summary['interest_paid'];    
        
        $total_capital_payments += $capital_payments;
        $total_late_interest += $late_interest;
        $total_interest += $interest;
                
        amount_cell($capital_payments);
        amount_cell($interest); 
        amount_cell($late_interest);
        
        $repayments = $capital_payments + $interest + $late_interest;
        amount_cell($repayments);
        $total_repayments += $repayments;
        
        $unpaid_balance = ($schedulled_repaymets['capital_total'] + $schedulled_repaymets['interest_total']) - ($capital_payments + $interest);
        $total_unpaid_balance += $unpaid_balance;
        amount_cell($unpaid_balance);
        
        $loans_balance = get_month_loans_total($month['id']);
        $total_loans_balance += $loans_balance;
        amount_cell($loans_balance);
        

        end_row();
        
    }
    start_row();
        label_cell(text_bold("Totals"), "align=right");
        amount_cell($schedulled_capital_repaymets, true);
        amount_cell($schedulled_interest_repaymets, true);
        amount_cell($total_schedulled_repaymets, true);
        
        amount_cell($total_capital_payments, true);
        amount_cell($total_interest, true);
        amount_cell($total_late_interest, true);        
        amount_cell($total_repayments, true);
        
        amount_cell($total_unpaid_balance, true);
//        amount_cell($total_loans_balance, true);
        label_cell('');
        end_row();
    end_table();
}

