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