cfbbbdbe579bd0f68097906ecead76ce54957c08
[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, $wip_account, $dimension_id, 
15         $dimension2_id, $no_sale, $editable, $no_purchase,
16         $depreciation_method = 'D', $depreciation_rate=100, $depreciation_factor=1,
17         $depreciation_start=null, $fa_class_id=null, $vat_category='')
18 {
19         $sql = "UPDATE ".TB_PREF."stock_master SET long_description=".db_escape($long_description).",
20                 description=".db_escape($description).",
21                 category_id=".db_escape($category_id).",
22                 sales_account=".db_escape($sales_account).",
23                 inventory_account=".db_escape($inventory_account).",
24                 cogs_account=".db_escape($cogs_account).",
25                 adjustment_account=".db_escape($adjustment_account).",
26                 wip_account=".db_escape($wip_account).",
27                 dimension_id=".db_escape($dimension_id).",
28                 dimension2_id=".db_escape($dimension2_id).",
29                 tax_type_id=".db_escape($tax_type_id).",
30                 no_sale=".db_escape($no_sale).",
31                 no_purchase=".db_escape($no_purchase).",
32                 editable=".db_escape($editable).",
33                 depreciation_method=".db_escape($depreciation_method).",
34                 depreciation_rate=".db_escape($depreciation_rate).",
35                 depreciation_factor=".db_escape($depreciation_factor).",
36                 fa_class_id=".db_escape($fa_class_id);
37
38         if ($units != '')
39                 $sql .= ", units=".db_escape($units);
40
41         if ($mb_flag != '')
42                 $sql .= ", mb_flag=".db_escape($mb_flag);
43
44         if ($vat_category != '')
45                 $sql .= ", vat_category=".db_escape($vat_category);
46
47         if (isset($depreciation_start)) {
48                 $sql .= ", depreciation_start='".date2sql($depreciation_start)."'"
49                         .", depreciation_date='".date2sql($depreciation_start)."'";
50         }
51
52         $sql .= " WHERE stock_id=".db_escape($stock_id);
53
54         db_query($sql, "The item could not be updated");
55
56         update_item_code(-1, $stock_id, $stock_id, $description, $category_id, 1, 0);
57 }
58
59 function add_item($stock_id, $description, $long_description, $category_id, 
60         $tax_type_id, $units, $mb_flag, $sales_account, $inventory_account, 
61         $cogs_account, $adjustment_account,     $wip_account, $dimension_id, 
62         $dimension2_id, $no_sale, $editable, $no_purchase,
63         $depreciation_method='D', $depreciation_rate=100,  $depreciation_factor=1, $depreciation_start=null,
64         $fa_class_id=null, $vat_category=0)
65 {
66         $sql = "INSERT INTO ".TB_PREF."stock_master (stock_id, description, long_description, category_id,
67                 tax_type_id, units, mb_flag, sales_account, inventory_account, cogs_account,
68                 adjustment_account, wip_account, dimension_id, dimension2_id, no_sale, no_purchase, editable, vat_category,
69                 depreciation_method, depreciation_rate, depreciation_factor"
70                 .(isset($depreciation_start) ? ", depreciation_start, depreciation_date, fa_class_id" : "")
71                 .") VALUES (".db_escape($stock_id).", ".db_escape($description).", ".db_escape($long_description).",
72                 ".db_escape($category_id).", ".db_escape($tax_type_id).", "
73                 .db_escape($units).", ".db_escape($mb_flag).",
74                 ".db_escape($sales_account).", ".db_escape($inventory_account)
75                 .", ".db_escape($cogs_account).",".db_escape($adjustment_account)
76                 .", ".db_escape($wip_account).", "
77                 .db_escape($dimension_id).", ".db_escape($dimension2_id).","
78                 .db_escape($no_sale).","
79                 .db_escape($no_purchase).","
80                 .db_escape($editable).",".db_escape($vat_category).","
81                 .db_escape($depreciation_method).",".db_escape($depreciation_rate).",".db_escape($depreciation_factor)
82                 .(isset($depreciation_start) ? ",'".date2sql($depreciation_start)."','".date2sql($depreciation_start)."',".db_escape($fa_class_id) : "")
83                 .")";
84
85         db_query($sql, "The item could not be added");
86
87         $sql = "INSERT INTO ".TB_PREF."loc_stock (loc_code, stock_id)
88                 SELECT loc_code, ".db_escape($stock_id)
89                 ." FROM ".TB_PREF."locations";
90
91         db_query($sql, "The item locstock could not be added");
92
93         add_item_code($stock_id, $stock_id, $description, $category_id, 1, 0);
94 }
95
96 function delete_item($stock_id)
97 {
98         $sql="DELETE FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
99         db_query($sql, "could not delete stock item");
100
101         /*and cascade deletes in loc_stock */
102         $sql ="DELETE FROM ".TB_PREF."loc_stock WHERE stock_id=".db_escape($stock_id);
103         db_query($sql, "could not delete stock item loc stock");
104
105         /*and cascade deletes in purch_data */
106         $sql ="DELETE FROM ".TB_PREF."purch_data WHERE stock_id=".db_escape($stock_id);
107         db_query($sql, "could not delete stock item purch data");
108
109         /*and cascade deletes in prices */
110         $sql ="DELETE FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id);
111         db_query($sql, "could not delete stock item prices");
112
113         /*and cascade delete the bill of material if any */
114         $sql = "DELETE FROM ".TB_PREF."bom WHERE parent=".db_escape($stock_id);
115         db_query($sql, "could not delete stock item bom");
116
117         delete_item_kit($stock_id);
118 }
119
120 function get_item($stock_id)
121 {
122         $sql = "SELECT item.*, taxtype.name AS tax_type_name
123                 FROM ".TB_PREF."stock_master item,"
124                         .TB_PREF."item_tax_types taxtype
125                 WHERE taxtype.id=item.tax_type_id
126                 AND stock_id=".db_escape($stock_id);
127         $result = db_query($sql,"an item could not be retreived");
128
129         return db_fetch_assoc($result);
130 }
131
132 function get_items($fixed_asset = 0)
133 {
134         $sql = "SELECT * FROM ".TB_PREF."stock_master WHERE fixed_asset=".db_escape($fixed_asset);
135         return db_query($sql,"items could not be retreived");
136 }
137
138 function item_in_foreign_codes($stock_id)
139 {
140         $sqls=  array(
141         "SELECT COUNT(*) FROM "
142                 .TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) =>
143          _('Cannot delete this item because there are stock movements that refer to this item.'),
144         "SELECT COUNT(*) FROM "
145                 .TB_PREF."bom WHERE component=".db_escape($stock_id)=>
146          _('Cannot delete this item record because there are bills of material that require this part as a component.'),
147         "SELECT COUNT(*) FROM "
148                 .TB_PREF."sales_order_details WHERE stk_code=".db_escape($stock_id) =>
149          _('Cannot delete this item because there are existing sales order items for it.'),
150         "SELECT COUNT(*) FROM "
151                 .TB_PREF."purch_order_details WHERE item_code=".db_escape($stock_id)=>
152          _('Cannot delete this item because there are existing purchase order items for it.')
153         );
154
155         $msg = '';
156
157         foreach($sqls as $sql=>$err) {
158                 $result = db_query($sql, "could not query stock usage");
159                 $myrow = db_fetch_row($result);
160                 if ($myrow[0] > 0) 
161                 {
162                         $msg = $err; break;
163                 }
164         }
165         if ($msg == '') {
166
167                 $kits = get_where_used($stock_id);
168                 $num_kits = db_num_rows($kits);
169                 if ($num_kits) {
170                         $msg = _("This item cannot be deleted because some code aliases 
171                                 or foreign codes was entered for it, or there are kits defined 
172                                 using this item as component")
173                                 .':<br>';
174
175                         while($num_kits--) {
176                                 $kit = db_fetch($kits);
177                                 $msg .= "'".$kit[0]."'";
178                                 if ($num_kits) $msg .= ',';
179                         }
180
181                 }
182         }
183         return $msg;
184 }
185
186 function get_items_search($description, $type)
187 {
188         global $SysPrefs;
189
190         $sql = "SELECT COUNT(i.item_code) AS kit, i.item_code, i.description, c.description category
191                 FROM ".TB_PREF."stock_master s, ".TB_PREF."item_codes i
192                         LEFT JOIN ".TB_PREF."stock_category c ON i.category_id=c.category_id
193                 WHERE i.stock_id=s.stock_id
194                         AND !i.inactive AND !s.inactive
195                         AND (  i.item_code LIKE " . db_escape("%" . $description. "%") . " OR 
196                                 i.description LIKE " . db_escape("%" . get_post("description"). "%") . " OR 
197                                 c.description LIKE " . db_escape("%" . get_post("description"). "%") . ") ";
198
199         switch ($type) {
200                 case "sales":
201                         $sql .= " AND !s.no_sale AND mb_flag <> 'F'";
202                         break;
203                 case "manufactured":
204                         $sql .= " AND mb_flag = 'M'";
205                         break;
206         case "purchasable":
207                 $sql .= " AND NOT no_purchase AND mb_flag <> 'F' AND i.item_code=i.stock_id";
208                 break;
209                 case "costable":
210                         $sql .= " AND mb_flag <> 'D' AND mb_flag <> 'F' AND  i.item_code=i.stock_id";
211                         break;
212                 case "component":
213                         $parent = $_GET['parent'];
214                         $sql .= " AND  i.item_code=i.stock_id AND i.stock_id <> '$parent' AND mb_flag <> 'F' ";
215                         break;
216                 case "kits":
217                         $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id AND mb_flag <> 'F'";
218                         break;
219                 case "all":
220                         $sql .= " AND mb_flag <> 'F' AND i.item_code=i.stock_id";
221                         break;
222         }
223
224         if (isset($SysPrefs->max_rows_in_search))
225                 $limit = $SysPrefs->max_rows_in_search;
226         else
227                 $limit = 10;
228
229         $sql .= " GROUP BY i.item_code ORDER BY i.description LIMIT 0,".(int)($limit);
230
231         return db_query($sql, "Failed in retreiving item list.");
232 }
233