Further cleanups in sql statements.
[fa-stable.git] / inventory / includes / db / items_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 update_item($stock_id, $description, $long_description, $category_id, 
13         $tax_type_id, $units='', $mb_flag='', $sales_account, $inventory_account, 
14         $cogs_account,  $adjustment_account, $assembly_account, $dimension_id, 
15         $dimension2_id, $no_sale, $editable, $no_purchase)
16 {
17         $sql = "UPDATE ".TB_PREF."stock_master SET long_description=".db_escape($long_description).",
18                 description=".db_escape($description).",
19                 category_id=".db_escape($category_id).",
20                 sales_account=".db_escape($sales_account).",
21                 inventory_account=".db_escape($inventory_account).",
22                 cogs_account=".db_escape($cogs_account).",
23                 adjustment_account=".db_escape($adjustment_account).",
24                 assembly_account=".db_escape($assembly_account).",
25                 dimension_id=".db_escape($dimension_id).",
26                 dimension2_id=".db_escape($dimension2_id).",
27                 tax_type_id=".db_escape($tax_type_id).",
28                 no_sale=".db_escape($no_sale).",
29                 no_purchase=".db_escape($no_purchase).",
30                 editable=".db_escape($editable);
31
32         if ($units != '')
33                 $sql .= ", units=".db_escape($units);
34
35         if ($mb_flag != '')
36                 $sql .= ", mb_flag=".db_escape($mb_flag);
37
38         $sql .= " WHERE stock_id=".db_escape($stock_id);
39
40         db_query($sql, "The item could not be updated");
41
42         update_item_code(-1, $stock_id, $stock_id, $description, $category_id, 1, 0);
43 }
44
45 function add_item($stock_id, $description, $long_description, $category_id, 
46         $tax_type_id, $units, $mb_flag, $sales_account, $inventory_account, 
47         $cogs_account, $adjustment_account,     $assembly_account, $dimension_id, 
48         $dimension2_id, $no_sale, $editable, $no_purchase)
49 {
50         $sql = "INSERT INTO ".TB_PREF."stock_master (stock_id, description, long_description, category_id,
51                 tax_type_id, units, mb_flag, sales_account, inventory_account, cogs_account,
52                 adjustment_account, assembly_account, dimension_id, dimension2_id, no_sale, no_purchase, editable)
53                 VALUES (".db_escape($stock_id).", ".db_escape($description).", ".db_escape($long_description).",
54                 ".db_escape($category_id).", ".db_escape($tax_type_id).", "
55                 .db_escape($units).", ".db_escape($mb_flag).",
56                 ".db_escape($sales_account).", ".db_escape($inventory_account)
57                 .", ".db_escape($cogs_account).",".db_escape($adjustment_account)
58                 .", ".db_escape($assembly_account).", "
59                 .db_escape($dimension_id).", ".db_escape($dimension2_id).","
60                 .db_escape($no_sale).","
61                 .db_escape($no_purchase).","
62                 .db_escape($editable).")";
63
64         db_query($sql, "The item could not be added");
65
66         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id)
67                 SELECT loc_code, ".db_escape($stock_id)
68                 ." FROM ".TB_PREF."locations";
69
70         db_query($sql, "The item locstock could not be added");
71
72         add_item_code($stock_id, $stock_id, $description, $category_id, 1, 0);
73 }
74
75 function delete_item($stock_id)
76 {
77         $sql="DELETE FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
78         db_query($sql, "could not delete stock item");
79
80         /*and cascade deletes in loc_stock */
81         $sql ="DELETE FROM ".TB_PREF."loc_stock WHERE stock_id=".db_escape($stock_id);
82         db_query($sql, "could not delete stock item loc stock");
83
84         /*and cascade deletes in purch_data */
85         $sql ="DELETE FROM ".TB_PREF."purch_data WHERE stock_id=".db_escape($stock_id);
86         db_query($sql, "could not delete stock item purch data");
87
88         /*and cascade deletes in prices */
89         $sql ="DELETE FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id);
90         db_query($sql, "could not delete stock item prices");
91
92         /*and cascade delete the bill of material if any */
93         $sql = "DELETE FROM ".TB_PREF."bom WHERE parent=".db_escape($stock_id);
94         db_query($sql, "could not delete stock item bom");
95
96         delete_item_kit($stock_id);
97 }
98
99 function get_item($stock_id)
100 {
101         $sql = "SELECT item.*, taxtype.name AS tax_type_name
102                 FROM ".TB_PREF."stock_master item,"
103                         .TB_PREF."item_tax_types taxtype
104                 WHERE taxtype.id=item.tax_type_id
105                 AND stock_id=".db_escape($stock_id);
106         $result = db_query($sql,"an item could not be retreived");
107
108         return db_fetch($result);
109 }
110
111 function get_items()
112 {
113         $sql = "SELECT * FROM ".TB_PREF."stock_master";
114         return db_query($sql,"items could not be retreived");
115 }
116
117 function item_in_foreign_codes($stock_id)
118 {
119         $sqls=  array(
120         "SELECT COUNT(*) FROM "
121                 .TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) =>
122          _('Cannot delete this item because there are stock movements that refer to this item.'),
123         "SELECT COUNT(*) FROM "
124                 .TB_PREF."bom WHERE component=".db_escape($stock_id)=>
125          _('Cannot delete this item record because there are bills of material that require this part as a component.'),
126         "SELECT COUNT(*) FROM "
127                 .TB_PREF."sales_order_details WHERE stk_code=".db_escape($stock_id) =>
128          _('Cannot delete this item because there are existing purchase order items for it.'),
129         "SELECT COUNT(*) FROM "
130                 .TB_PREF."purch_order_details WHERE item_code=".db_escape($stock_id)=>
131          _('Cannot delete this item because there are existing purchase order items for it.')
132         );
133
134         $msg = '';
135
136         foreach($sqls as $sql=>$err) {
137                 $result = db_query($sql, "could not query stock usage");
138                 $myrow = db_fetch_row($result);
139                 if ($myrow[0] > 0) 
140                 {
141                         $msg = $err; break;
142                 }
143         }
144         if ($msg == '') {       
145
146                 $kits = get_where_used($stock_id);
147                 $num_kits = db_num_rows($kits);
148                 if ($num_kits) {
149                         $msg = _("This item cannot be deleted because some code aliases 
150                                 or foreign codes was entered for it, or there are kits defined 
151                                 using this item as component")
152                                 .':<br>';
153
154                         while($num_kits--) {
155                                 $kit = db_fetch($kits);
156                                 $msg .= "'".$kit[0]."'";
157                                 if ($num_kits) $msg .= ',';
158                         }
159
160                 }
161         }
162         return $msg;
163 }