Update from usntable branch.
[fa-stable.git] / inventory / includes / db / items_locations_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function add_item_location($loc_code, $location_name, $delivery_address, $phone, $phone2, $fax, $email, $contact)
13 {
14         $sql = "INSERT INTO ".TB_PREF."locations (loc_code, location_name, delivery_address, phone, phone2, fax, email, contact)
15                 VALUES (".db_escape($loc_code).", ".db_escape($location_name).", ".db_escape($delivery_address).", "
16                         .db_escape($phone).", ".db_escape($phone2).", ".db_escape($fax).", ".db_escape($email).", "
17                         .db_escape($contact).")";
18
19         db_query($sql,"a location could not be added");
20
21         /* Also need to add loc_stock records for all existing items */
22         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id, reorder_level)
23                 SELECT ".db_escape($loc_code).", ".TB_PREF."stock_master.stock_id, 0 FROM ".TB_PREF."stock_master";
24
25         db_query($sql,"a location could not be added");
26 }
27
28 //------------------------------------------------------------------------------------
29
30 function update_item_location($loc_code, $location_name, $delivery_address, $phone, $phone2, $fax, $email, $contact)
31
32 {
33     $sql = "UPDATE ".TB_PREF."locations SET location_name=".db_escape($location_name).",
34         delivery_address=".db_escape($delivery_address).",
35         phone=".db_escape($phone).", phone2=".db_escape($phone2).", fax=".db_escape($fax).",
36         email=".db_escape($email).", contact=".db_escape($contact)."
37         WHERE loc_code = ".db_escape($loc_code);
38
39         db_query($sql,"a location could not be updated");
40 }
41
42 //------------------------------------------------------------------------------------
43
44 function delete_item_location($item_location)
45 {
46         $sql="DELETE FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);
47         db_query($sql,"a location could not be deleted");
48
49         $sql = "DELETE FROM ".TB_PREF."loc_stock WHERE loc_code =".db_escape($item_location);
50         db_query($sql,"a location could not be deleted");
51 }
52
53 //------------------------------------------------------------------------------------
54
55 function get_item_location($item_location)
56 {
57         $sql="SELECT * FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);
58
59         $result = db_query($sql,"a location could not be retrieved");
60
61         return db_fetch($result);
62 }
63
64 //------------------------------------------------------------------------------------
65
66 function set_reorder_level($stock_id, $loc_code, $reorder_level)
67 {
68         $sql = "UPDATE ".TB_PREF."loc_stock SET reorder_level = $reorder_level
69                 WHERE stock_id = ".db_escape($stock_id)." AND loc_code = ".db_escape($loc_code);
70
71         db_query($sql,"an item reorder could not be set");
72 }
73
74 //------------------------------------------------------------------------------------
75
76 function get_loc_details($stock_id)
77 {
78         $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name
79                 FROM ".TB_PREF."loc_stock, ".TB_PREF."locations
80                 WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
81                 AND ".TB_PREF."loc_stock.stock_id = ".db_escape($stock_id) 
82                 ." ORDER BY ".TB_PREF."loc_stock.loc_code";
83         return db_query($sql,"an item reorder could not be retreived");
84 }
85
86 //------------------------------------------------------------------------------------
87
88 ?>