<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_ASSETTRANSVIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/finance/includes/db/company_loans_db.inc");
include_once($path_to_root . "/finance/includes/db/company_loan_installments_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Company Loan Installments"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------

$payment_id = 0;

if ($_GET['trans_no'] != "")
{
    $payment_id = $_GET['trans_no'];
}

function display_company_loan_repayment($batch_ref)
{
    $result = get_company_loan_payments_by_batch_ref($batch_ref);
    
    display_heading("Company Loan Repayment " . $batch_ref);

    br(1);

    $reference = '';
    $payment_date = '';
    $installment_id = 0;
    $installment_name = "Not found";
    
    $amount_capital = 0;
    $account_capital = '';
    
    $amount_interest = 0;
    $account_interest = '';
    
    $amount_late_interest = 0;
    $account_late_interest = '';

    while($row = db_fetch($result)){
        if($row['payment_amount'] != 0){
            $reference = $row['reference'];
            $payment_date = $row['tran_date'];    
            $installment_id = $row['installment_id'];

            $amount_capital = $row['payment_amount'];
            $bank_account = get_bank_account($row['bank_account_id']);
            $account_capital = $bank_account['bank_account_number']." - ".$bank_account['bank_account_name'];
            
            $return = $row['is_return'];
            $comments = $row['comments'];
        }
        elseif($row['payment_interest_amount'] != 0){
            $reference = $row['reference'];
            $payment_date = $row['tran_date'];    
            $installment_id = $row['installment_id'];
            
            $amount_interest = $row['payment_interest_amount'];
            $bank_account = get_bank_account($row['bank_account_id']);
            $account_interest = $bank_account['bank_account_number']." - ".$bank_account['bank_account_name'];       
            
            $return = $row['is_return'];
            $comments = $row['comments'];
        }
        elseif($row['late_interest_amount'] != 0){
            $reference = $row['reference'];
            $payment_date = $row['tran_date'];    
            $installment_id = $row['installment_id'];
            
            $amount_late_interest = $row['late_interest_amount'];
            $bank_account = get_bank_account($row['bank_account_id']);
            $account_late_interest = $bank_account['bank_account_number']." - ".$bank_account['bank_account_name'];     
            
            $return = $row['is_return'];
            $comments = $row['comments'];
        }
    }
    
    $installment = get_company_loan_installment($installment_id);
    if(is_array($installment)){
        $installment_name = sql2date($installment['tran_date'])." - "
                        . number_format($installment['installment_amount'] + $installment['interest_amount'],2)." ("
                        .$installment['installment_amount']." + ".$installment['interest_amount'].")";
    }
    

    start_table(TABLESTYLE2, "width=80%");
    
    label_cells(_("Installment"), $installment_name, "class='tableheader2'");
    end_row();
    label_cells(_("Reference"), $batch_ref, "class='tableheader2'");
    end_row();
    label_cells(_("Date"), $payment_date, "class='tableheader2'");
    end_row();
    
    label_cells(_("Capital Payment Amount"), number_format($amount_capital,2), "class='tableheader2'");
    end_row();
    label_cells(_("Capital Payment Account"), $account_capital, "class='tableheader2'");
    end_row();

    label_cells(_("Interest Payment Amount"), number_format($amount_interest,2), "class='tableheader2'");
    end_row();
    label_cells(_("Interest Payment Account"), $account_interest, "class='tableheader2'");
    end_row();

    label_cells(_("Late Interest Payment Amount"), number_format($amount_late_interest,2), "class='tableheader2'");
    end_row();
    label_cells(_("Late Interest Payment Account"), $account_late_interest, "class='tableheader2'");
    end_row();

    label_cells(_("Return"), get_yes_no_name($return), "class='tableheader2'");
    end_row();

    label_cells(_("Comments"), $comments, "class='tableheader2'");
    end_row();



    end_table();

br();
}


$payment = get_company_loan_payment_record($payment_id);

display_company_loan_repayment($payment['batch_ref']);
 
end_page(true, false, false, ST_COMP_LOAN_REPAYMENT, $payment_id);

 


?>
