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

function add_letter_of_credit($name, $description, $supplier_id,
                            $credit_facility_id, $bank_account_id, 
                            $lc_amount, $bank_charge,
                            $start_date, $expiry_date,  
                            $payable_type_id, $payable_no_of_days, 
                            $delivery_type_id, $status, 
                            $inactive, $expired)
        
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');        
        
	$sql = "INSERT INTO ".TB_PREF."letters_of_credit
                        (name, description, supplier_id,
                        credit_facility_id, bank_account_id, 
                        lc_amount, bank_charge,
                        start_date, expiry_date,  
                        payable_type_id, payable_no_of_days, 
                        delivery_type_id, status, 
                        inactive, expired,
                        created_by, created_on) 
                VALUES (".db_escape($name).", ".db_escape($description).",".db_escape($supplier_id).", "
                        .db_escape($credit_facility_id).", ".db_escape($bank_account_id).","
                        .db_escape($lc_amount). ", ".db_escape($bank_charge).", "
                        .db_escape($start_date). ", ".db_escape($expiry_date).", "
                        .db_escape($payable_type_id). ", ".db_escape($payable_no_of_days).", "
                        .db_escape($delivery_type_id). ", ".db_escape($status).", "
                        .db_escape($inactive).", ".db_escape($expired).", "                        
                        .db_escape($created_by).", ".db_escape($created_on).")";

	db_query($sql,"New Company Loan could not be added");
}

function update_letter_of_credit($selected_id, $name, $description, $supplier_id,
                                $lc_amount, $bank_charge,
                                $start_date, $expiry_date, 
                                $payable_type_id, $payable_no_of_days, 
                                $delivery_type_id, $status,  
                                $inactive, $expired)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."letters_of_credit 
            
                SET name=".db_escape($name).",
                    description=".db_escape($description).",
                    supplier_id=".db_escape($supplier_id).",  
                    lc_amount=".db_escape($lc_amount).",  
                    bank_charge=".db_escape($bank_charge).",
                    start_date =".db_escape($start_date).",
                    expiry_date =".db_escape($expiry_date).",
                    payable_type_id=".db_escape($payable_type_id).",
                    payable_no_of_days =".db_escape($payable_no_of_days).",
                    delivery_type_id =".db_escape($delivery_type_id).",
                    status =".db_escape($status).",
                    inactive=".db_escape($inactive).",
                    expired=".db_escape($expired).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
                        
		WHERE id=".db_escape($selected_id);                     
        
	db_query($sql,"New Company Loan could not be updated");
}


function update_lc_expired($loan_id)
{
        $sql = "UPDATE ".TB_PREF."letters_of_credit             
                SET expired= 1   
                WHERE id=".db_escape($loan_id);                     
        
	db_query($sql,"New Could not update_company_loan_completed");
}


function delete_lc($selected_id)
{
	$sql = "DELETE FROM ".TB_PREF."letters_of_credit WHERE id=".db_escape($selected_id);
        
	db_query($sql,"could not delete selected company loan");
}

function get_company_lcs()
{
    $sql = "SELECT cl.* , 
                    cf.name  credit_facility_name,
                    lv.value_name bank_name,
                    bb.branch_name 
        
            FROM ".TB_PREF."letters_of_credit cl 
                    LEFT JOIN ".TB_PREF."credit_facilities cf ON 
                            cl.credit_facility_id = cf.id 
                    LEFT JOIN ".TB_PREF."list_values lv ON
                            lv.id = cl.bank_id
                    LEFT JOIN ".TB_PREF."bank_branches bb ON
                            bb.id = cl.branch_id ";
    
    return db_query($sql, "Company Loans could not be retrieved");
}


function get_lc($selected_id)
{
    $sql = "SELECT lc.*,
                    bnk.value_name bank_name,
                    ba.bank_account_number  
            FROM ".TB_PREF."letters_of_credit lc 
                    LEFT JOIN ".TB_PREF."list_values AS bnk ON 
                            lc.bank_id = bnk.id
                    LEFT JOIN ".TB_PREF."bank_accounts ba ON
                            lc.bank_account_id = ba.id 
           WHERE lc.id = ".db_escape($selected_id); 

    $result = db_query($sql,"Could not get_lc");
    return db_fetch($result);     
}

function get_lc_header($company_loan_id)
{
    $sql = "SELECT cl.*,
                    cf.name credit_facility_name
        
            FROM ".TB_PREF."company_loans cl 
                    ".TB_PREF."credit_facilities cf


            WHERE   cf.id = cl.credit_facility_id AND 
                    cl.id=".db_escape($company_loan_id);

        $result = db_query($sql);
        return db_fetch($result);    
}




?>