Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[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, $fixed_asset = 0)
13 {
14         begin_transaction(__FUNCTION__, func_get_args());
15         $sql = "INSERT INTO ".TB_PREF."locations (loc_code, location_name, delivery_address, phone, phone2, fax, email, contact, fixed_asset)
16                 VALUES (".db_escape($loc_code).", ".db_escape($location_name).", ".db_escape($delivery_address).", "
17                         .db_escape($phone).", ".db_escape($phone2).", ".db_escape($fax).", ".db_escape($email).", "
18                         .db_escape($contact).", ".db_escape($fixed_asset).")";
19
20         db_query($sql,"a location could not be added");
21
22         /* Also need to add loc_stock records for all existing items */
23         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id, reorder_level)
24                 SELECT ".db_escape($loc_code).", ".TB_PREF."stock_master.stock_id, 0 FROM ".TB_PREF."stock_master";
25
26         db_query($sql,"a location could not be added");
27         commit_transaction();
28 }
29
30 //------------------------------------------------------------------------------------
31
32 function update_item_location($loc_code, $location_name, $delivery_address, $phone, $phone2, $fax, $email, $contact, $fixed_asset = 0)
33
34 {
35         begin_transaction(__FUNCTION__, func_get_args());
36     $sql = "UPDATE ".TB_PREF."locations SET location_name=".db_escape($location_name).",
37         delivery_address=".db_escape($delivery_address).",
38         phone=".db_escape($phone).", phone2=".db_escape($phone2).", fax=".db_escape($fax).",
39         email=".db_escape($email).", contact=".db_escape($contact).",
40       fixed_asset=".db_escape($fixed_asset)."
41         WHERE loc_code = ".db_escape($loc_code);
42
43         db_query($sql,"a location could not be updated");
44         commit_transaction();
45 }
46
47 //------------------------------------------------------------------------------------
48
49 function delete_item_location($item_location)
50 {
51         begin_transaction(__FUNCTION__, func_get_args());
52         $sql="DELETE FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);
53         db_query($sql,"a location could not be deleted");
54
55         $sql = "DELETE FROM ".TB_PREF."loc_stock WHERE loc_code =".db_escape($item_location);
56         db_query($sql,"a location could not be deleted");
57         commit_transaction();
58 }
59
60 //------------------------------------------------------------------------------------
61
62 function get_item_location($item_location)
63 {
64         $sql="SELECT * FROM ".TB_PREF."locations WHERE loc_code=".db_escape($item_location);
65
66         $result = db_query($sql,"a location could not be retrieved");
67
68         return db_fetch($result);
69 }
70
71 //------------------------------------------------------------------------------------
72
73 function get_item_locations($show_inactive, $fixed_asset = 0)
74 {
75         $sql = "SELECT * FROM ".TB_PREF."locations WHERE fixed_asset = ".db_escape($fixed_asset);
76         if (!$show_inactive) $sql .= " AND !inactive";
77         return db_query($sql, "could not query locations");
78 }
79
80 //------------------------------------------------------------------------------------
81
82 function set_reorder_level($stock_id, $loc_code, $reorder_level)
83 {
84         begin_transaction(__FUNCTION__, func_get_args());
85         $sql = "UPDATE ".TB_PREF."loc_stock SET reorder_level = $reorder_level
86                 WHERE stock_id = ".db_escape($stock_id)." AND loc_code = ".db_escape($loc_code);
87
88         db_query($sql,"an item reorder could not be set");
89         commit_transaction();
90 }
91
92 //------------------------------------------------------------------------------------
93
94 function get_loc_details($stock_id, $fixed_asset = 0)
95 {
96         $sql = "SELECT stock.loc_code, stock.location_name, "
97         .db_escape($stock_id)." as stock_id, reorders.reorder_level
98                 FROM ".TB_PREF."locations stock LEFT JOIN ".TB_PREF."loc_stock reorders ON
99                 reorders.loc_code=stock.loc_code
100                 AND reorders.stock_id = ".db_escape($stock_id)
101             ." WHERE stock.fixed_asset = ".db_escape($fixed_asset)
102                 ." ORDER BY reorders.loc_code";
103         return db_query($sql,"an item reorder could not be retreived");
104 }
105