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

function add_item_barcode($stock_id, $barcode,$inactive)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        begin_transaction();
        
	$sql = "INSERT INTO ".TB_PREF."barcodes (
                    stock_id, barcode, inactive,
                    created_by, created_on) 
                VALUES (
                    ".db_escape($stock_id).", ".db_escape($barcode). ", ".$inactive.", 
                    ".db_escape($created_by ).", ".db_escape($created_on).")";

	db_query($sql,"Item barcode could not be added");
        
        update_barcode_change($stock_id);
        
        commit_transaction();
}

function update_item_barcode($selected_id, $stock_id, $barcode, $inactive)
{
	$updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        begin_transaction();
        
        $sql = "UPDATE ".TB_PREF."barcodes 
                SET barcode =".db_escape($barcode).",
                    inactive =".$inactive.",
                    updated_by =".db_escape($updated_by).",
                    updated_on =".db_escape($updated_on)." 
		WHERE id=".db_escape($selected_id);
        
	db_query($sql,"Item barcode could not be updated");
        
        update_barcode_change($stock_id);
        
        commit_transaction();
}

function delete_item_barcode($selected_id, $stock_id)
{
    return;
	$sql = "DELETE FROM ".TB_PREF."barcodes WHERE id=".db_escape($selected_id)."
		AND stock_id=".db_escape($stock_id);
	db_query($sql,"could not delete barcode");
}

function get_items_barcodes($stock_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."barcodes 
            WHERE stock_id = ".db_escape($stock_id)." 
            ORDER BY created_on ";            

    return db_query($sql, "Item barcords could not be retrieved");
}

function get_item_barcode_details($selected_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."barcodes 
            WHERE id = ".db_escape($selected_id)." 
            ORDER BY created_on ";  

	$result = db_query($sql, "Item barcord details could not be retrieved");

	return db_fetch($result);
}

function get_barcode($barcode, $selected_id = -1)
{
    $sql = "SELECT barcode 
            FROM ".TB_PREF."barcodes 
            WHERE barcode = ".db_escape($barcode);  
    
    if($selected_id > 0)
    $sql .= " AND id <>".db_escape($selected_id);

	$result = db_query($sql, "Barcord could not be retrieved");

	return db_fetch($result);
}
?>