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


function get_company_loans_for_credit_facility_inquiry($credit_facility_id, $completed)
{
    $sql = "SELECT *
            FROM ".TB_PREF."company_loans 
                   
            WHERE credit_facility_id=".db_escape($credit_facility_id)."AND
                  completed=".db_escape($completed);
                    
    return db_query($sql, "Company Loans could not be retrieved");
}
 
//-------------------------------------------------------------------------------------------------------

function get_total_paid_from_company_loan_installments($loan_id)
{
    $sql = "SELECT SUM(installment_amount) total_installment_amount
            FROM ".TB_PREF."company_loan_installments
            WHERE loan_id=".db_escape($loan_id);
    
    $result = db_query($sql);
    return db_fetch($result);
}

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

function get_total_interest_from_company_loan_installments($loan_id)
{
    $sql = "SELECT SUM(interest_amount) total_interest_amount
            FROM ".TB_PREF."company_loan_installments
            WHERE loan_id=".db_escape($loan_id);
    
    $result = db_query($sql);
    return db_fetch($result);
}

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

function get_bonds_for_credit_facility_inquiry($credit_facility_id, $payable)
{
    $sql = "SELECT bo.*,
                    lv.value_name bond_type,
                    pm.project_name,
                    li.value_name bank_name,
                    bb.branch_name

            FROM ".TB_PREF."bonds bo, 
                    ".TB_PREF."list_values lv,
                    ".TB_PREF."projects_master pm,
                    ".TB_PREF."list_values li,
                    ".TB_PREF."bank_branches bb
           
            WHERE lv.id = bo.type_id AND
                    pm.project_id = bo.project_id AND
                    li.id = bo.bank_id AND
                    bb.id = bo.branch_id AND
                    bo.credit_facility_id=".db_escape($credit_facility_id)."AND
                    bo.payable=".db_escape($payable);
                 
                
    return db_query($sql, "Could not get bonds");    
    
}


function get_letter_of_credit_for_credit_facility_inquiry($credit_facility_id, $lc)
{
     $sql = "SELECT lc.*,
                    s.supp_name beneficiary_name,                   
                    ba.bank_account_name,
                    li.value_name payable_type_name,
                    lv.value_name delivery_type_name 
        
            FROM ".TB_PREF."letters_of_credit lc,
                    ".TB_PREF."suppliers s,                   
                    ".TB_PREF."bank_accounts ba,
                    ".TB_PREF."list_values li,
                    ".TB_PREF."list_values lv
                      
            WHERE lc.supplier_id = s.supplier_id AND                   
                    lc.bank_account_id = ba.id AND
                    li.id = lc.payable_type_id AND
                    lv.id = lc.delivery_type_id AND                    
                    lc.expired=".db_escape($lc)."AND 
                    lc.credit_facility_id=".db_escape($credit_facility_id);
    
    return db_query($sql, "Could not get letters_of_credit");    
    
}


function get_credit_facilities_for_inquiry($selected_id)
{
    $sql = "SELECT cf.*,
                    lv.value_name bank_name,
                    bb.branch_name,
                    ba.bank_account_name

            FROM ".TB_PREF."credit_facilities cf
                    LEFT JOIN ".TB_PREF."list_values lv ON
                        lv.id = cf.bank_id

                    LEFT JOIN ".TB_PREF."bank_branches bb ON
                        bb.id = cf.branch_id

                    LEFT JOIN ".TB_PREF."bank_accounts ba ON
                        ba.id = cf.bank_account_id 

            WHERE  cf.id=".db_escape($selected_id); 
    
    return db_query($sql);
          
}



        
?>