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