<?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");
include_once($path_to_root . "/finance/includes/db/company_loans_inquiry_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);

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


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

display_heading("Company Loan: " . $loan_id);

start_table(TABLESTYLE2);

$myrow = view_company_loan($loan_id);

start_outer_table(TABLESTYLE2, "width=80%");
    table_section(1, "width=50%");

        label_cells(_("Reference"), $myrow['name'], "class='tableheader2'");
        end_row();
        label_cells(_("Description"), $myrow['description'], "class='tableheader2'");
        end_row();

        label_cells(_("Type"), $myrow['loan_type_name'], "class='tableheader2'");
        end_row();

        label_cells(_("Loan Capital"), number_format($myrow['loan_amount'],2), "class='tableheader2'");
        end_row();
        
        label_cells(_("Loan Interst"), number_format($myrow['interest'],2), "class='tableheader2'");
        end_row();
        
        label_cells(_("Total Loan"), number_format($myrow['loan_amount'] + $myrow['interest'],2), "class='tableheader2'");
        end_row();

        label_cells(_("Bank Charge"), number_format($myrow['bank_charge'],2), "class='tableheader2'");
        end_row();
        
        label_cells(_("Credit Facility"), $myrow['credit_facility_name'], "class='tableheader2'");
        end_row();

        label_cells(_("Bank Account"), $myrow['bank_account_name'], "class='tableheader2'");
        end_row();

        label_cells(_("Loan Account"), $myrow['loan_account_name'], "class='tableheader2'");
        end_row();

        label_cells(_("Interest Account"), $myrow['interest_account_name'], "class='tableheader2'");
        end_row();

        label_cells(_("Late Interest Account"), $myrow['late_interest_account_name'], "class='tableheader2'");
        end_row();

    table_section(2, "width=50%");

        label_cells(_("Installment Amount"), number_format($myrow['installment_amount'],2), "class='tableheader2'");
        end_row();

        //label_cells(_("Total Paid"), number_format($myrow['total_captal_paid'],2), "class='tableheader2'");
        //end_row();
        label_cells(_("Installment Interest"), number_format($myrow['installment_interest'],2), "class='tableheader2'");
        end_row();
        //label_cells(_("Total Interest"), number_format($myrow['total_interest_paid'],2), "class='tableheader2'");
        //end_row();
        
        if(substr($myrow['last_payment_date'],0,4) == "0000"){
            label_cells(_("Last Payment Date"), "", "class='tableheader2'");
        }
        else{
            label_cells(_("Last Payment Date"), sql2date($myrow['last_payment_date']), "class='tableheader2'");
        }
        end_row();



        label_cells(_("Total Capital Paid"), number_format($myrow['total_captal_paid'], 2), "class='tableheader2'");
        end_row();

        label_cells(_("Total Interest Paid"), number_format($myrow['total_interest_paid'], 2), "class='tableheader2'");
        end_row();

        label_cells(_("Total Late Interest Paid"), number_format($myrow['total_late_interest_paid'], 2), "class='tableheader2'");
        end_row();

   
        
//        table_section(2);

        $capital_outstanding = $myrow['loan_amount'] - $myrow['total_captal_paid'];
        label_cells(_("Capital Outstanding"), number_format($capital_outstanding,2), "class='tableheader2'");
        end_row();

        $interest_outstanding = $myrow['interest'] - $myrow['total_interest_paid'];
        label_cells(_("Interest Outstanding"), number_format($interest_outstanding,2), "class='tableheader2'");
        end_row();
        
        $total_outstanding =0;
        if($capital_outstanding > 0){
            $total_outstanding += $capital_outstanding;
        }
        if($interest_outstanding > 0){
            $total_outstanding += $interest_outstanding;
        }
        
        label_cells(_("Total Outstanding"), number_format($total_outstanding,2), "class='tableheader2'");
        end_row();
        
        label_cells(_("On Hold"), get_yes_no_name($myrow['inactive']), "class='tableheader2'");
        end_row();

        label_cells(_("Completed"), get_yes_no_name($myrow['completed']), "class='tableheader2'");
        end_row();
        
    end_table();
end_outer_table(1);

br();


//----------------------------------------------------------------------------------------------------------
//display_heading("Company");

function display_loan_installments($loan_id)
{ 
    $result = view_company_loan_installments($loan_id);
        
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
        display_note(_("There are no loan installments."));
    }
    else
    {
        start_table(TABLESTYLE, "width=95%");

        br();
        display_heading("Company Loan Installments");
        
        $th = array(_("ID"), _("Date"),_("Capital Amount"), _("Interest"),                          
                     _("Capital Paid"), _("Interest Piad"),
                    _("Late Interest Piad"),_("Comments"), "Days Outstanding", "Paid");

        
        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            label_cell($myrow["id"]);
            label_cell(sql2date($myrow["tran_date"]), "align=center");
            label_cell($myrow["installment_amount"], "align=right");
            label_cell($myrow["interest_amount"], "align=right");            
            
           // label_cell(get_yes_no_name($myrow["is_return"]), "align=center");
            
            label_cell($myrow['paid_amount'], "align=right");
            label_cell($myrow['total_payment_interest'], "align=right");
            label_cell($myrow['total_late_interest'], "align=right");
            label_cell($myrow["comments"]); 
                   
            if($myrow["paid"] == 0){
                $tran_date = sql2date($myrow["tran_date"]); 
                $today = Today();             

                $days = date_diff2($today, $tran_date, 'd');

                if($days > 0){
                    label_cell($days, 'align=center');
                }else{
                    label_cell("");
                }
            }
            else{
                label_cell("");
            }
            
            label_cell(get_yes_no_name($myrow["paid"]), "align=center");
            end_row();
            
            $j++;

       //end of page full new headings
        } //end of while loop

        end_table();
    }
    
    div_end();
}

display_loan_installments($loan_id);
 
br();

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

function display_company_loan_payments($loan_id)
{
    $result = view_company_loan_payments($loan_id);
        
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
        display_note(_("There are no loan payments."));
    }
    else
    {
        start_table(TABLESTYLE, "width=98%");

        br();
        display_heading("Company Loan Payments");
        
        $th = array(_("ID"), _("Reference"),                       
                    _("Date"),
                    _("Bank Account"),
                    _("Amount"),
                    _("Return"), 
                     _("Paid As"), "Comments");

        
        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            label_cell($myrow["id"], 'align=center');
//            label_cell($myrow["installment_id"], "align=center");
            label_cell($myrow["reference"]);
            label_cell(sql2date($myrow["tran_date"]),  "align=center"); 
            label_cell($myrow["bank_account_name"]);
            
            if($myrow['payment_amount'] != 0){
                $amount = $myrow['payment_amount'];
                $payment_type = "Capital";
            }
            elseif($myrow['payment_interest_amount'] != 0){
                $amount = $myrow['payment_interest_amount'];
                $payment_type = "Interest";
            }
            elseif($myrow['late_interest_amount'] != 0){
                $amount = $myrow['late_interest_amount'];
                $payment_type = "Late Interest";
            }
            label_cell(number_format($amount, 2), "align=right");          
            
            if($myrow['is_return'] > 0){
                $return = "Yes";
            }else{
                $return = "No";
            }
            
            label_cell($return,  "align=center");

            label_cell($payment_type);
            
            label_cell($myrow["comments"]);
                                      
            end_row();

            
            $j++;

       //end of page full new headings
        } //end of while loop

        end_table();
    }
    
    div_end();
}

display_company_loan_payments($loan_id);
 
end_page(true, false, false, ST_COMPANY_LOANS, $loan_id);

 


?>
