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

$page_security = 'SA_PROJECT_BILLING_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/finance/includes/db/credit_facilities_inquiry_db.inc");
include_once($path_to_root . "/finance/includes/db/credit_facilities_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Credit Facilities Inquiry"));
//------------------------------------------------------------------------------------------------

simple_page_mode(true);

if (isset($_GET['id']))
{
    $_POST['id'] = $_GET['id'];
}

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

if ($Mode == 'RESET')
{
    $selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
    $selected_id = $_POST['selected_id'];
    //$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();
start_table();
  start_row();

    credit_facility_list_row("Credit Facility :", 'credit_facility_id', null, 'Select a Credit Facility', true);
    
    $result = get_credit_facilities_for_inquiry($_POST['credit_facility_id']);
    
start_form();
start_table(TABLESTYLE, "width=60%");
br();
display_heading("Credit Facility");

$th = array(_("ID"), _("Description"), _("Bank"),
            _("Bank Branch"), _("Bank Account"),_("Start Date"), 
            _("End Date"), _("Amount"));
        
 table_header($th);
  
    $k = $j = 0; //row colour counter

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

        label_cell($myrow["id"], "align=right");
        label_cell($myrow["description"]);                     
        label_cell($myrow["bank_name"]);
        label_cell($myrow["branch_name"]);
        label_cell($myrow["bank_account_name"]);
        label_cell(sql2date($myrow["start_date"]));
        label_cell(sql2date($myrow["end_date"]));
        label_cell(number_format($myrow["amount"], 2), "align=right");  

        end_row();

    }
    end_table(1);

  
  end_row();
end_table(1);

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


$result = get_company_loans_for_credit_facility_inquiry($_POST['credit_facility_id']);
  
start_form();
start_table(TABLESTYLE, "width=60%");
display_heading("Company Loans");

$th = array(_("ID"), _("Description"), _("Total Loan"),
            _("Installment"), _("Interest"),_("Start Date"), 
            _("Total paid"), _("Total Interest"),
            _("On Hold"), _("Completed"));

    table_header($th);
  
    $k = $j = 0; //row colour counter
    $loan_total = 0;   
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell($myrow["id"], "align=right");
        label_cell($myrow["loan_description"]);                     
        label_cell(number_format($myrow["loan_amount"], 2), "align=right");
        label_cell(number_format($myrow["installment_amount"], 2), "align=right");
        label_cell(number_format($myrow["interest"], 2), "align=right");
        label_cell(sql2date($myrow["start_date"]), "align=center");

        $total_installment = get_total_paid_from_company_loan_installments($myrow["id"]);
        $total_paid = $total_installment['total_installment_amount'];

        label_cell(number_format($total_paid, 2), "align=right");

        $interest = get_total_interest_from_company_loan_installments($myrow["id"]);
        $total_interest = $interest['total_interest_amount'];

        label_cell(number_format($total_interest, 2), "align=right");

        if($myrow["inactive"] == 1){
            label_cell("Yes");
        } else {
            label_cell("No");
        }

        if($myrow["completed"] == 1){
            label_cell("Yes");
        }else {
            label_cell("No");
        }

        end_row();

        $loan_total += $myrow["loan_amount"];     

    }

    start_row();
        label_cell("Total :", "colspan=2 align=right");
        amount_decimal_cell($loan_total);
      
    end_row();    

    end_table(1);

//---------------------------------------------------------------------------------------------------
        
        
$result = get_bonds_for_credit_facility_inquiry($_POST['credit_facility_id']);
  
start_form();
start_table(TABLESTYLE, "width=60%");
display_heading("Bonds");

$th = array(_("ID"),_("Description"),_("Type"),_("Payable"),
            _("Guarantor"),_("Bond Value"),_("Bank Charge"),
            _("From Date"),_("Expiry Date"), _("Project"), _("Comment"));

 table_header($th);
    $k = 0;
    $value_total = 0;
    $bank_charge_total = 0;
    
    while ($myrow = db_fetch($result)) 
{
	alt_table_row_color($k);	

        label_cell($myrow["id"], "align=right"); 
	label_cell($myrow["bonds_description"]);       
	label_cell($myrow["value_name"]);
        if(($myrow["payable"]) == 0 ){
            label_cell("No");
        }else{
            label_cell("Yes");
        }
        
        label_cell($myrow["name"]);      
        label_cell(number_format($myrow["value"], 2), "align=right");
        label_cell(number_format($myrow["bank_charge"], 2), "align=right");               
	label_cell(sql2date($myrow["from_date"], "align=center"));
        label_cell(sql2date($myrow["expiry_date"], "align=center"));
        label_cell($myrow["project_description"]);         
        label_cell($myrow["comments"]);		        
 	
	end_row();
        
        $value_total += $myrow["value"];
        $bank_charge_total += $myrow["bank_charge"];
}
  
start_row();
    label_cell("Total :", "colspan=5 align=right");
    amount_decimal_cell($value_total);
    amount_decimal_cell($bank_charge_total);
end_row();

end_table(1);

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


$result = get_letter_of_credit_for_credit_facility_inquiry($_POST['credit_facility_id']);
  
start_form();
start_table(TABLESTYLE, "width=60%");
display_heading("Letters Of Credit");


$th = array(_("ID"),_("Description"),_("Type"),_("Payable"),
            _("Guarantor"),_("LC Value"),_("Bank Charge"),
            _("From Date"),_("Expiry Date"), _("Project"), _("Comment"));

 table_header($th);
   
 $k = 0;
 $total_value = 0;
 $bnk_charge_total = 0;
    
while ($myrow = db_fetch($result)) 
{
    alt_table_row_color($k);	

    label_cell($myrow["id"], "align=right"); 
    label_cell($myrow["bonds_description"]);       
    label_cell($myrow["value_name"]);
        if(($myrow["payable"]) == 0 ){
            label_cell("No");
        }else{
            label_cell("Yes");
        }

    label_cell($myrow["name"]);  
    label_cell(number_format($myrow["value"], 2), "align=right");
    label_cell(number_format($myrow["bank_charge"], 2), "align=right");               
    label_cell(sql2date($myrow["from_date"], "align=center"));
    label_cell(sql2date($myrow["expiry_date"], "align=center"));
    label_cell($myrow["project_description"]);         
    label_cell($myrow["comments"]);	
   
    end_row();
    
    $total_value += $myrow["value"];
    $bnk_charge_total += $myrow["bank_charge"];
}

  
start_row();
    label_cell("Total :", "colspan=5 align=right");
    amount_decimal_cell($total_value);
    amount_decimal_cell($bnk_charge_total);
end_row();

end_table(1);
   

$total_balance = get_total_values_of_bonds_lc_and_company_loans($_POST['credit_facility_id']);


    $credit_facility = get_credit_facility_amount_total($_POST['credit_facility_id']);
    $cf_total = $credit_facility['amount_total'];
    
    label_cell("Credit Facility Total :", "colspan=3 align=right");  
    label_cell($cf_total);
    
    br();
    
start_row();
    label_cell("Balance :", "colspan=5 align=right");  
    amount_decimal_cell($total_balance); 
end_row();   


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


br();

if (!@$_GET['popup'])
{
    end_form();
    end_page();
}	




?>
