<?php
/**********************************************************************
***********************************************************************/
function write_lot($data)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    if($data['Mode'] == 'UPDATE_ITEM')
    {
        $sql = "UPDATE ".TB_PREF."lots 
                SET
                    lot_reference = ".db_escape($data['lot_reference'])."
                     
                WHERE 
                    id = ".db_escape($data['lot_id']);
        
        db_query($sql, "Selected lot could not be updated ");
    }
    else
    {
        $cost = 0;
        $qty = 0;
        
        $sql = "INSERT INTO ".TB_PREF."lots
                        (lot_reference,stock_id,cost,qty,created_by,created_on) 
                VALUES
                        (".db_escape($data['lot_reference']).",".db_escape($data['stock_id']).","
                            .db_escape($cost).",".db_escape($qty).","
                            .db_escape($id_user).",".db_escape($datetime).")";
        
        db_query($sql, "New lot could not be added");
        return db_insert_id();
    }
    
}
////
function delete_lot($id)
{
	$sql="DELETE FROM ".TB_PREF."lots 
                    WHERE 
                        id=".db_escape($id);

	db_query($sql,"Category code could not be deleted");
}
////
function get_lot_details($id)
{
	$sql="SELECT * FROM ".TB_PREF."lots 
                WHERE 
                    id=".db_escape($id);

	$result = db_query($sql,"Selected lotdetails could not be retrieved");
	return db_fetch($result);
}

function get_lots_for_stock_id($stock_id) {
    $sql = "SELECT l.*, 
                sm.description
        FROM ".TB_PREF."lots l,
                ".TB_PREF."stock_master sm
        WHERE
            l.stock_id=".db_escape($stock_id)." AND
            l.stock_id = sm.stock_id
                ";
        
    return db_query($sql, "Could not get lots");

}

function get_existing_lot_reference($stock_id, $lot_reference) {
    $sql = "SELECT l.* 
            FROM ".TB_PREF."lots l 
            WHERE
                l.stock_id=".db_escape($stock_id)." AND
                l.lot_reference = ".db_escape($lot_reference);
    
    $result = db_query($sql, "Could not get lots");
    
    return db_fetch($result);
}    
?>