<?php
/**********************************************************************
***********************************************************************/
function add_item_price($stock_id, $sales_type_id, $curr_abrev, $price,$effective_date,$expiry_date, $unit)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
        $sql = "INSERT INTO ".TB_PREF."prices (stock_id, sales_type_id, 
                                curr_abrev, price, 
                                created_by, created_on,
                                effective_date, expiry_date, 
                                unit) 
                VALUES (".db_escape($stock_id).", ".db_escape($sales_type_id).", "
                                .db_escape($curr_abrev).", ".db_escape($price).", "
                                .db_escape($id_user).", ".db_escape($datetime).", "
                                .db_escape(date2sql($effective_date)).", ".db_escape(date2sql($expiry_date)).", "
                                .db_escape($unit).")";

        db_query($sql,"an item price could not be added");		
}

function update_item_price($price_id, $sales_type_id, $curr_abrev, $price,$effective_date,$expiry_date, $unit)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ".TB_PREF."prices SET 
                                sales_type_id=".db_escape($sales_type_id).", 
                                curr_abrev=".db_escape($curr_abrev).", 
                                updated_by=".db_escape($id_user).",
                                updated_on=".db_escape($datetime).",
                                effective_date=".db_escape(date2sql($effective_date)).",
                                expiry_date=".db_escape(date2sql($expiry_date)).",
                                unit=".db_escape($unit).", 
                                price=".db_escape($price)." 
                WHERE id=".db_escape($price_id);

        db_query($sql,"an item price could not be updated");		
}

function delete_item_price($price_id)
{
	$sql="DELETE FROM ".TB_PREF."prices WHERE id= ".db_escape($price_id);
	db_query($sql,"an item price could not be deleted");			
}

function get_prices($stock_id)
{
	$sql = "SELECT st.sales_type, p.*, u.name unit_name  
            FROM ".TB_PREF."prices p, "
                    .TB_PREF."sales_types st, "
                    .TB_PREF."item_units u  
            WHERE p.sales_type_id = st.id AND 
                    p.unit = u.abbr AND 
                    p.stock_id=".db_escape($stock_id)
		." ORDER BY curr_abrev, sales_type_id";	
	
	return db_query($sql,"item prices could not be retreived");
}

function get_stock_price($price_id)
{
	$sql = "SELECT * FROM ".TB_PREF."prices WHERE id=".db_escape($price_id);
	
	$result = db_query($sql,"price could not be retreived");
	
	return db_fetch($result);
}

function get_stock_price_type_currency($stock_id, $type, $currency)
{
	$sql = "SELECT * FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id)."
		AND sales_type_id=".db_escape($type)."
		AND curr_abrev=".db_escape($currency);
	
	$result = db_query($sql,"price could not be retreived");
	
	return db_fetch($result);
}
?>