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


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

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

            WHERE id=".db_escape($id);

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

function get_all_pi_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."project_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_pi_line_category_accounts");
}

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

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

	return db_fetch($result);
}


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

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


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

            FROM ".TB_PREF."project_invoice_line_categories pilc
                LEFT JOIN ".TB_PREF."list_values lv ON
                    pilc.category_id = lv.id

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


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



?>
