d90b03b39e18944493aa4819da6826d756e49cb2
[fa-stable.git] / inventory / includes / db / items_units_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 write_item_unit($selected, $abbr, $description, $decimals)
13 {
14     if($selected!='')
15                 $sql = "UPDATE ".TB_PREF."item_units SET
16                 abbr = ".db_escape($abbr).",
17                 name = ".db_escape($description).",
18                 decimals = ".db_escape($decimals)."
19                 WHERE abbr = ".db_escape($selected);
20     else
21                 $sql = "INSERT INTO ".TB_PREF."item_units
22                         (abbr, name, decimals) VALUES( ".db_escape($abbr).",
23                         ".db_escape($description).", ".db_escape($decimals).")";
24
25         db_query($sql,"an item unit could not be updated");
26 }
27
28 function delete_item_unit($unit)
29 {
30         $sql="DELETE FROM ".TB_PREF."item_units WHERE abbr=".db_escape($unit);
31
32         db_query($sql,"an unit of measure could not be deleted");
33 }
34
35 function get_item_unit($unit)
36 {
37         $sql="SELECT * FROM ".TB_PREF."item_units WHERE abbr=".db_escape($unit);
38
39         $result = db_query($sql,"an unit of measure could not be retrieved");
40
41         return db_fetch($result);
42 }
43
44 function get_unit_descr($unit)
45 {
46         $sql = "SELECT name FROM ".TB_PREF."item_units WHERE abbr=".db_escape($unit);
47
48         $result = db_query($sql, "could not retrieve unit description");
49
50         $row = db_fetch_row($result);
51         return $row[0];
52 }
53
54 function item_unit_used($unit) {
55         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE units=".db_escape($unit);
56         $result = db_query($sql, "could not query stock master");
57         $myrow = db_fetch_row($result);
58         return ($myrow[0] > 0);
59 }
60
61 function get_all_item_units($all=false) {
62     $sql = "SELECT * FROM ".TB_PREF."item_units";
63         if (!$all) $sql .= " WHERE !inactive";
64         $sql .= " ORDER BY name";
65     return  db_query($sql, "could not get stock categories");
66 }
67 // 2008-06-15. Added to get a measure of unit by given stock_id
68 function get_unit_dec($stock_id)
69 {
70         $sql = "SELECT decimals FROM ".TB_PREF."item_units,     ".TB_PREF."stock_master
71                 WHERE abbr=units AND stock_id=".db_escape($stock_id)." LIMIT 1";
72         $result = db_query($sql, "could not get unit decimals");
73
74         $row = db_fetch_row($result);
75         return $row[0];
76 }
77