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 ***********************************************************************/
13 item_codes table is used to store both multiply foreign codes and
16 function update_item_code($id, $item_code, $stock_id, $description, $category, $qty, $foreign=0)
18 $sql = "UPDATE ".TB_PREF."item_codes SET
19 item_code = ".db_escape($item_code).",
20 stock_id = ".db_escape($stock_id).",
21 description = ".db_escape($description).",
22 category_id = $category,
23 quantity = ".db_escape($qty).",
24 is_foreign = ".db_escape($foreign)."
27 if ($id == -1) // update with unknown $id i.e. from items table editor
28 $sql .= "item_code = ".db_escape($item_code)
29 ." AND stock_id = ".db_escape($stock_id);
33 db_query($sql,"an item code could not be updated");
36 function add_item_code($item_code, $stock_id, $description, $category, $qty, $foreign=0)
38 $sql = "INSERT INTO ".TB_PREF."item_codes
39 (item_code, stock_id, description, category_id, quantity, is_foreign)
40 VALUES( ".db_escape($item_code).",".db_escape($stock_id).",
41 ".db_escape($description).",$category,".db_escape($qty).",".$foreign.")";
43 db_query($sql,"an item code could not be added");
46 function delete_item_code($id)
48 $sql="DELETE FROM ".TB_PREF."item_codes WHERE id='$id'";
49 db_query($sql,"an item code could not be deleted");
52 function get_item_code($id)
54 $sql="SELECT * FROM ".TB_PREF."item_codes WHERE id='$id'";
56 $result = db_query($sql,"item code could not be retrieved");
58 return db_fetch($result);
61 function get_all_item_codes($stock_id, $foreign=1)
63 $sql="SELECT i.*, c.description as cat_name FROM "
64 .TB_PREF."item_codes as i,"
65 .TB_PREF."stock_category as c
66 WHERE stock_id='$stock_id'
67 AND i.category_id=c.category_id
68 AND i.is_foreign=$foreign";
70 $result = db_query($sql,"all item codes could not be retrieved");
75 function delete_item_kit($item_code)
77 $sql="DELETE FROM ".TB_PREF."item_codes WHERE item_code='$item_code'";
78 db_query($sql,"an item kit could not be deleted");
81 function get_item_kit($item_code)
83 $sql="SELECT DISTINCT kit.*, item.units, comp.description as comp_name
85 .TB_PREF."item_codes kit,"
86 .TB_PREF."item_codes comp
88 .TB_PREF."stock_master item
90 item.stock_id=comp.item_code
92 kit.stock_id=comp.item_code
93 AND kit.item_code='$item_code'";
95 $result = db_query($sql,"item kit could not be retrieved");
100 function get_item_code_dflts($stock_id)
102 $sql = "SELECT units, decimals, description, category_id
103 FROM ".TB_PREF."stock_master,".TB_PREF."item_units
104 WHERE stock_id='$stock_id'";
106 $result = db_query($sql,"item code defaults could not be retrieved");
107 return db_fetch($result);
110 // Check if kit contains given item, optionally recursive.
112 function check_item_in_kit($old_id, $kit_code, $item_code, $recurse=false)
114 $result = get_item_kit($kit_code);
117 while ($myrow = db_fetch($result))
119 if ($myrow['id'] == $old_id)
122 if ($myrow['stock_id'] == $item_code)
127 if ($recurse && $myrow['item_code'] != $myrow['stock_id']
128 && check_item_in_kit($old_id, $item_code, $myrow['stock_id'], true))
137 function get_kit_props($kit_code)
139 $sql = "SELECT description, category_id FROM ".TB_PREF."item_codes "
140 . " WHERE item_code='$kit_code'";
141 $res = db_query($sql, "kit name query failed");
142 return db_fetch($res);
145 function update_kit_props($kit_code, $name, $category)
147 $sql = "UPDATE ".TB_PREF."item_codes SET description="
148 . db_escape($name).",category_id=".db_escape($category)
149 . " WHERE item_code='$kit_code'";
150 db_query($sql, "kit name update failed");
153 function get_where_used($item_code)
155 $sql = "SELECT item_code, description FROM "
156 .TB_PREF."item_codes "
157 . " WHERE stock_id='$item_code'
158 AND item_code!='$item_code'";
159 return db_query($sql, "where used query failed");