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

//$x=5;

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

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

function update_item_bin_location($selected_id, $stock_id, $bin_location)
{
	$updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        begin_transaction();
        
        $sql = "UPDATE ".TB_PREF."item_bin_locations 
                SET bin_location =".db_escape($bin_location).",                   
                    updated_by =".db_escape($updated_by).",
                    updated_on =".db_escape($updated_on)." 
		WHERE id=".db_escape($selected_id);
        
	db_query($sql,"Item bin location could not be updated");
        
        update_barcode_change($stock_id);
        
        commit_transaction();
}
//
function delete_item_bin_location($selected_id, $stock_id)
{
//    return;
	$sql = "DELETE FROM ".TB_PREF."item_bin_locations WHERE id=".db_escape($selected_id)."
		AND stock_id=".db_escape($stock_id);
	db_query($sql,"could not delete item bin location");
}
//
function get_item_bin_locations($stock_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."item_bin_locations
            WHERE stock_id = ".db_escape($stock_id)." 
            ORDER BY created_on ";            

    return db_query($sql, "Item bin locations could not be retrieved");
}
//
function get_item_bin_location($selected_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."item_bin_locations 
            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_item_bin_location_by_name($stock_id, $bin_location,$selected_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."item_bin_locations 
            WHERE id <> ".db_escape($selected_id)." AND 
                    stock_id = ".db_escape($stock_id)." AND 
                    bin_location = ".db_escape($bin_location)." 
            LIMIT 1 ";  

    $result = db_query($sql, "Could not get_item_bin_location_by_name");

    return db_fetch($result);
}


?>