<?php
/**********************************************************************
***********************************************************************/
function write_serial_no($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."serial_nos 
                SET
                    serial_no = ".db_escape($data['serial_no'])."
                     
                WHERE 
                    id = ".db_escape($data['serial_no_id']);
        
        db_query($sql, "Selected serial no could not be updated ");
    }
    else
    {
        $cost = 0;
        $qty = 0;
        
        $sql = "INSERT INTO ".TB_PREF."serial_nos
                        (serial_no,stock_id,cost,qty,created_by,created_on) 
                VALUES
                        (".db_escape($data['serial_no']).",".db_escape($data['stock_id']).","
                            .db_escape($cost).",".db_escape($qty).","
                            .db_escape($id_user).",".db_escape($datetime).")";
        
        db_query($sql, "New serial no could not be added");
        return db_insert_id();
    }
    
}
////
function delete_serial_no($id)
{
	$sql="DELETE FROM ".TB_PREF."serial_nos
                    WHERE 
                        id=".db_escape($id);

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

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

function get_serial_no_header_details($id)
{
	$sql = "SELECT sn.*, 
                sm.description
        FROM ".TB_PREF."serial_nos sn,
                ".TB_PREF."stock_master sm
                WHERE 
                    sn.stock_id = sm.stock_id AND 
                    id=".db_escape($id);

	return db_query($sql,"Selected serial no details could not be retrieved");

}


function get_serial_nos_for_stock_id($stock_id) {
    $sql = "SELECT sn.*, 
                sm.description
        FROM ".TB_PREF."serial_nos sn,
                ".TB_PREF."stock_master sm
        WHERE
            sn.stock_id=".db_escape($stock_id)." AND
            sn.stock_id = sm.stock_id
                ";
        
    return db_query($sql, "Could not get serial no");
}
  
function get_existing_serial_no($stock_id, $serial_no){
    $sql = "SELECT sn.*
        FROM ".TB_PREF."serial_nos sn 
        WHERE
            sn.stock_id=".db_escape($stock_id)." AND
            sn.serial_no = ".db_escape($serial_no);
        
    $result = db_query($sql, "Could not get existing serial no");
    
    return db_fetch($result);
}
?>