<?php
/**********************************************************************
***********************************************************************/
function add_item_location($loc_code, $location_name, $delivery_address, $project_id, $phone, $phone2, $fax, $email, $contact,
                                $reservation_loc_code)
{
	$sql = "INSERT INTO ".TB_PREF."locations (loc_code, location_name, delivery_address, 
                                            project_id, phone, phone2, 
                                            fax, email, contact,
                                            reservation_loc_code)
		VALUES (".db_escape($loc_code).", ".db_escape($location_name).", ".db_escape($delivery_address).", "
                    .db_escape($project_id).", ".db_escape($phone).", ".db_escape($phone2).", "
                    .db_escape($fax).", ".db_escape($email).", ".db_escape($contact).", "
                    .db_escape($reservation_loc_code).")";

	db_query($sql,"a location could not be added");

	/* Also need to add loc_stock records for all existing items */
	$sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id, reorder_level)
		SELECT ".db_escape($loc_code).", ".TB_PREF."stock_master.stock_id, 0 FROM ".TB_PREF."stock_master";

	db_query($sql,"a location could not be added");
}

//------------------------------------------------------------------------------------

function update_item_location($loc_code, $location_name, $delivery_address, $project_id, $phone, $phone2, $fax, $email, $contact,
                                $reservation_loc_code)

{
    $sql = "UPDATE ".TB_PREF."locations SET location_name=".db_escape($location_name).",
                            delivery_address=".db_escape($delivery_address).",
                            project_id=".db_escape($project_id).",
                            phone=".db_escape($phone).", phone2=".db_escape($phone2).", fax=".db_escape($fax).",
                            email=".db_escape($email).", contact=".db_escape($contact).", 
                            reservation_loc_code=".db_escape($reservation_loc_code)."
            WHERE loc_code = ".db_escape($loc_code);

	db_query($sql,"a location could not be updated");
}

//------------------------------------------------------------------------------------

function delete_item_location($item_location)
{
	$sql="DELETE FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);
	db_query($sql,"a location could not be deleted");

	$sql = "DELETE FROM ".TB_PREF."loc_stock WHERE loc_code =".db_escape($item_location);
	db_query($sql,"a location could not be deleted");
}

//------------------------------------------------------------------------------------

function get_item_location($item_location)
{
	$sql="SELECT * FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);

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

	return db_fetch($result);
}

//------------------------------------------------------------------------------------

function get_item_locations($show_inactive)
{
	$sql = "SELECT l.*, 
                    rl.location_name reservation_location_name, 
                    pm.description 
                FROM ".TB_PREF."locations l 
                    LEFT JOIN ".TB_PREF."locations rl ON 
                            l.reservation_loc_code = rl.loc_code 
                    LEFT JOIN ".TB_PREF."projects_master pm ON 
                            l.project_id = pm.project_id 
                    " ;
	if (!$show_inactive) $sql .= " WHERE !l.inactive";
	return db_query($sql, "could not query locations");;
        
//        $sql = "SELECT l.*,
//                        pm.description 
//		FROM ".TB_PREF."locations l, "
//                        .TB_PREF."projects_master pm 
//		WHERE l.project_id = $id 
//                AND l.project_id = pm.project_id ";
//        if (!$show_inactive) $sql .= " WHERE !inactive";
//	return db_query($sql, "could not query locations");;
}

//------------------------------------------------------------------------------------

function set_reorder_level($stock_id, $loc_code, $reorder_level)
{
	$sql = "UPDATE ".TB_PREF."loc_stock SET reorder_level = $reorder_level
		WHERE stock_id = ".db_escape($stock_id)." AND loc_code = ".db_escape($loc_code);

	db_query($sql,"an item reorder could not be set");
}

//------------------------------------------------------------------------------------

function get_loc_details($stock_id)
{
	$sql = "SELECT stock.loc_code, stock.location_name, "
	.db_escape($stock_id)." as stock_id, reorders.reorder_level
		FROM ".TB_PREF."locations stock LEFT JOIN ".TB_PREF."loc_stock reorders ON
		reorders.loc_code=stock.loc_code
		AND reorders.stock_id = ".db_escape($stock_id)
		." ORDER BY reorders.loc_code";
	return db_query($sql,"an item reorder could not be retreived");
}


function get_all_locations()
{
	$sql = "SELECT * 
            FROM ".TB_PREF."locations";
    
	return db_query($sql,"an item reorder could not be retreived");
}


function get_reservation_location($parent_location)
{
    $sql = "SELECT rl.* 
            FROM ".TB_PREF."locations l 
                LEFT JOIN ".TB_PREF."locations rl ON 
                        l.reservation_loc_code = rl.loc_code 
            WHERE l.loc_code = ".db_escape($parent_location);        
    
    $result = db_query($sql,"Could not get_reservation_location");

	return db_fetch($result);
}


	
function get_item_reorder_level($stock_id, $loc_code) { 
    $sql = "SELECT reorder_level 
            FROM ".TB_PREF."loc_stock 
            WHERE stock_id = ".db_escape($stock_id)." 
                AND loc_code = ".db_escape($loc_code);
	$result = db_query($sql, "get_item_reorder_level");
        return db_result_value($result, 'reorder_level');
}


?>