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


function add_subcontractor_invoice_line_category($category_id, $debit_account, $credit_account, $budget_required)                                 
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
	$sql = "INSERT INTO ".TB_PREF."subcontractor_invoice_line_categories
                        (category_id, debit_account, credit_account,
                        budget_required,
                            created_by, created_on)
                                                    
		VALUES (". db_escape($category_id).",
                        ".db_escape($debit_account).", ".db_escape($credit_account).","
                        .db_escape($budget_required).", "
                        . "".db_escape($created_by).", ".db_escape($created_on).")";
                    
    db_query($sql, "could not add_subcontractor_invoice_line_category");
    return db_insert_id(); 
}

function update_subcontractor_invoice_line_category($id, $debit_account, $credit_account, $budget_required)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."subcontractor_invoice_line_categories  SET 
                  
                    debit_account=".db_escape($debit_account).",
                    credit_account=".db_escape($credit_account).",
                    budget_required=".db_escape($budget_required).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."                        

            WHERE id=".db_escape($id);

    db_query($sql, "could not update_subcontractor_invoice_line_category");
}


function get_all_subcontractor_invoice_line_categories()
{
    $sql = "SELECT pic.*,
                    da.account_name debit_account_name,
                    ca.account_name credit_account_name,
                    lv.value_name category_name
                    
            FROM ".TB_PREF."subcontractor_invoice_line_categories pic 
                LEFT JOIN ".TB_PREF."list_values lv ON
                    pic.category_id = lv.id

                LEFT JOIN ".TB_PREF."chart_master da ON 
                    pic.debit_account = da.account_code 
                    
                LEFT JOIN ".TB_PREF."chart_master ca ON 
                    pic.credit_account = ca.account_code  
                    
            WHERE 1=1 
            ORDER BY lv.value_name";	
    
    return db_query($sql, "could not get_all_subcontractor_invoice_line_category_accounts");
}


function get_subcontractor_invoice_line_category($selected_id)
{
    $sql = "SELECT * FROM ".TB_PREF."subcontractor_invoice_line_categories  WHERE id=".db_escape($selected_id);

    $result = db_query($sql, "could not get_subcontractor_invoice_line_category_accounts");

    return db_fetch($result);
}

function delete_subcontractor_invoice_line_category($id)
{
    $sql="DELETE FROM ".TB_PREF."subcontractor_invoice_line_categories WHERE id=".db_escape($id);

    db_query($sql, "could not delete_subcontractor_invoice_line_category_accounts");
}


function get_subcontractor_invoice_line_category_record($selected_id)
{
    $sql = "SELECT spil.*,
                    lv.value_name category_name

            FROM ".TB_PREF."subcontractor_invoice_line_categories spil
                LEFT JOIN ".TB_PREF."list_values lv ON
                    spil.category_id = lv.id

            WHERE spil.id=".db_escape($selected_id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}


function get_subcontractor_invoice_line_category_detail($category_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."subcontractor_invoice_line_categories
            WHERE category_id=".db_escape($category_id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}

function get_subcontractor_invoice_line_category_by_category_id($selected_id)
{
    $sql = "SELECT * FROM ".TB_PREF."subcontractor_invoice_line_categories  WHERE category_id =".db_escape($selected_id);

    $result = db_query($sql, "could not get_subcontractor_invoice_line_category_accounts");

    return db_fetch($result);
}
?>
