Added foreign/alias item codes and sales kits support.
[fa-stable.git] / inventory / manage / items.php
1 <?php
2
3 $page_security = 11;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Items"));
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/ui.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
12
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
14
15 $user_comp = user_company();
16 $new_item = get_post('stock_id')==''; 
17 //------------------------------------------------------------------------------------
18
19 if (isset($_GET['stock_id']))
20 {
21         $_POST['stock_id'] = $stock_id = strtoupper($_GET['stock_id']);
22 }
23 else if (isset($_POST['stock_id']))
24 {
25         $stock_id = strtoupper($_POST['stock_id']);
26 }
27
28 if (list_updated('stock_id')) {
29         $_POST['NewStockID'] = get_post('stock_id');
30     clear_data();
31         $Ajax->activate('details');
32         $Ajax->activate('controls');
33 }
34 $upload_file = "";
35 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
36 {
37         $result = $_FILES['pic']['error'];
38         $upload_file = 'Yes'; //Assume all is well to start off with
39         $filename = $comp_path . "/$user_comp/images";
40         if (!file_exists($filename))
41         {
42                 mkdir($filename);
43         }       
44         $filename .= "/$stock_id.jpg";
45         
46          //But check for the worst 
47         if (strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)) != 'JPG')
48         {
49                 display_notification(_('Only jpg files are supported - a file extension of .jpg is expected'));
50                 $upload_file ='No';
51         } 
52         elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024)) 
53         { //File Size Check
54                 display_notification(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
55                 $upload_file ='No';
56         } 
57         elseif ( $_FILES['pic']['type'] == "text/plain" ) 
58         {  //File type Check
59                 display_notification( _('Only graphics files can be uploaded'));
60                 $upload_file ='No';
61         } 
62         elseif (file_exists($filename))
63         {
64                 $result = unlink($filename);
65                 if (!$result) 
66                 {
67                         display_error(_('The existing image could not be removed'));
68                         $upload_file ='No';
69                 }
70         }
71         
72         if ($upload_file == 'Yes')
73         {
74                 $result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
75         }
76  /* EOF Add Image upload for New Item  - by Ori */
77 }
78
79
80 check_db_has_stock_categories(_("There are no item categories defined in the system. At least one item category is required to add a item."));
81
82 check_db_has_item_tax_types(_("There are no item tax types defined in the system. At least one item tax type is required to add a item."));
83
84 function clear_data()
85 {
86         unset($_POST['long_description']);
87         unset($_POST['description']);
88         unset($_POST['category_id']);
89         unset($_POST['tax_type_id']);
90         unset($_POST['units']);
91         unset($_POST['mb_flag']);
92         unset($_POST['NewStockID']);
93         unset($_POST['dimension_id']);
94         unset($_POST['dimension2_id']);
95 }
96
97 //------------------------------------------------------------------------------------
98
99 if (isset($_POST['addupdate'])) 
100 {
101
102         $input_error = 0;
103         if ($upload_file == 'No')
104                 $input_error = 1;
105         if (strlen($_POST['description']) == 0) 
106         {
107                 $input_error = 1;
108                 display_error( _('The item name must be entered.'));
109                 set_focus('description');
110         } 
111         elseif (strlen($_POST['NewStockID']) == 0) 
112         {
113                 $input_error = 1;
114                 display_error( _('The item code cannot be empty'));
115                 set_focus('NewStockID');
116         }
117         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
118                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
119                 strstr($_POST['NewStockID'], "&")) 
120         {
121                 $input_error = 1;
122                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
123                 set_focus('NewStockID');
124
125         }
126         elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
127         {
128                         $input_error = 1;
129                 display_error( _("This item code is already assigned to stock item or sale kit."));
130                         set_focus('NewStockID');
131         }
132         
133         if ($input_error != 1)
134         {
135
136                 if (!$new_item) 
137                 { /*so its an existing one */
138
139                         update_item($_POST['NewStockID'], $_POST['description'],
140                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
141                                 $_POST['sales_account'], $_POST['inventory_account'], $_POST['cogs_account'],
142                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
143                                 $_POST['dimension_id'], $_POST['dimension2_id']);
144
145                         display_notification(_("Item has been updated."));
146                 } 
147                 else 
148                 { //it is a NEW part
149
150                         add_item($_POST['NewStockID'], $_POST['description'],
151                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
152                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
153                                 $_POST['inventory_account'], $_POST['cogs_account'],
154                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
155                                 $_POST['dimension_id'], $_POST['dimension2_id']);
156
157                 display_notification(_("A new item has been added."));
158                 $_POST['stock_id'] = $_POST['NewStockID'];
159                 }
160                 set_focus('stock_id');
161                 $Ajax->activate('_page_body');
162         }
163 }
164
165 //------------------------------------------------------------------------------------
166
167 function can_delete($stock_id)
168 {
169         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE stock_id='$stock_id'";
170         $result = db_query($sql, "could not query stock moves");
171         $myrow = db_fetch_row($result);
172         if ($myrow[0] > 0) 
173         {
174                 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
175                 return false;
176         }
177
178         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE component='$stock_id'";
179         $result = db_query($sql, "could not query boms");
180         $myrow = db_fetch_row($result);
181         if ($myrow[0] > 0) 
182         {
183                 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
184                 return false;
185         }
186
187         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_order_details WHERE stk_code='$stock_id'";
188         $result = db_query($sql, "could not query sales orders");
189         $myrow = db_fetch_row($result);
190         if ($myrow[0] > 0) 
191         {
192                 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
193                 return false;
194         }
195
196         $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_order_details WHERE item_code='$stock_id'";
197         $result = db_query($sql, "could not query purchase orders");
198         $myrow = db_fetch_row($result);
199         if ($myrow[0] > 0) 
200         {
201                 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
202                 return false;
203         }
204         $kits = get_where_used($stock_id);
205         $num_kits = db_num_rows($kits);
206         if ($num_kits) {
207                 $msg = _("This item cannot be deleted because some code aliases 
208                         or foreign codes was entered for it, or there are kits defined 
209                         using this item as component")
210                         .':<br>';
211
212                 while($num_kits--) {
213                         $kit = db_fetch($kits);
214                         $msg .= "'".$kit[0]."'";
215                         if ($num_kits) $msg .= ',';
216                 }
217                 display_error($msg);
218                 return false;
219         }
220         return true;
221 }
222
223 //------------------------------------------------------------------------------------
224
225 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
226 {
227
228         if (can_delete($_POST['NewStockID'])) {
229
230                 $stock_id = $_POST['NewStockID'];
231                 delete_item($stock_id);
232                 $filename = $comp_path . "/$user_comp/images/$stock_id.jpg";
233                 if (file_exists($filename))
234                         unlink($filename);
235                 display_notification(_("Selected item has been deleted."));
236                 $_POST['stock_id'] = '';
237                 clear_data();
238                 set_focus('stock_id');
239                 $Ajax->activate('_page_body');
240         }
241 }
242 //-------------------------------------------------------------------------------------------- 
243
244 if (isset($_POST['select']))
245 {
246         context_return(array('stock_id' => $_POST['stock_id']));
247 }
248
249 //------------------------------------------------------------------------------------
250
251 start_form(true);
252
253 if (db_has_stock_items()) 
254 {
255         start_table("class='tablestyle_noborder'");
256         start_row();
257     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
258           _('New item'), true);
259         $new_item = get_post('stock_id')==''; 
260         end_row();
261         end_table();
262 }
263
264 div_start('details');
265 start_table("$table_style2 width=40%");
266
267 table_section_title(_("Item"));
268
269 //------------------------------------------------------------------------------------
270
271 if ($new_item) 
272 {
273
274 /*If the page was called without $_POST['NewStockID'] passed to page then assume a new item is to be entered show a form with a part Code field other wise the form showing the fields with the existing entries against the part will show for editing with only a hidden stock_id field. New is set to flag that the page may have called itself and still be entering a new part, in which case the page needs to know not to go looking up details for an existing part*/
275
276         text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
277
278         $company_record = get_company_prefs();
279
280     if (!isset($_POST['inventory_account']) || $_POST['inventory_account'] == "")
281         $_POST['inventory_account'] = $company_record["default_inventory_act"];
282
283     if (!isset($_POST['cogs_account']) || $_POST['cogs_account'] == "")
284         $_POST['cogs_account'] = $company_record["default_cogs_act"];
285
286         if (!isset($_POST['sales_account']) || $_POST['sales_account'] == "")
287                 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
288
289         if (!isset($_POST['adjustment_account']) || $_POST['adjustment_account'] == "")
290                 $_POST['adjustment_account'] = $company_record["default_adj_act"];
291
292         if (!isset($_POST['assembly_account']) || $_POST['assembly_account'] == "")
293                 $_POST['assembly_account'] = $company_record["default_assembly_act"];
294
295
296 else 
297 { // Must be modifying an existing item
298                 $_POST['NewStockID'] = $_POST['stock_id'];
299
300                 $myrow = get_item($_POST['NewStockID']);
301
302                 $_POST['long_description'] = $myrow["long_description"];
303                 $_POST['description'] = $myrow["description"];
304                 $_POST['category_id']  = $myrow["category_id"];
305                 $_POST['tax_type_id']  = $myrow["tax_type_id"];
306                 $_POST['units']  = $myrow["units"];
307                 $_POST['mb_flag']  = $myrow["mb_flag"];
308
309                 $_POST['sales_account'] =  $myrow['sales_account'];
310                 $_POST['inventory_account'] = $myrow['inventory_account'];
311                 $_POST['cogs_account'] = $myrow['cogs_account'];
312                 $_POST['adjustment_account']    = $myrow['adjustment_account'];
313                 $_POST['assembly_account']      = $myrow['assembly_account'];
314                 $_POST['dimension_id']  = $myrow['dimension_id'];
315                 $_POST['dimension2_id'] = $myrow['dimension2_id'];
316         
317                 label_row(_("Item Code:"),$_POST['NewStockID']);
318                 hidden('NewStockID', $_POST['NewStockID']);
319                 set_focus('description');
320 }
321
322 text_row(_("Name:"), 'description', null, 52, 50);
323
324 textarea_row(_('Description:'), 'long_description', null, 45, 3);
325
326 end_table();
327 start_table("$table_style2 width=40%");
328 // Add image upload for New Item  - by Joe
329 start_row();
330 label_cells(_("Image File (.jpg)") . ":", "<input type='file' id='pic' name='pic'>");
331 // Add Image upload for New Item  - by Joe
332 $stock_img_link = "<img id='item_img' alt = '[";
333 if (isset($_POST['NewStockID']) && file_exists("$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg")) 
334 {
335  // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
336         $stock_img_link .= $_POST['NewStockID'].".jpg".
337         "]' src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg?nocache=".rand()."'";
338
339 else 
340 {
341         $stock_img_link .= _("No image"). "]'";
342 }
343 $stock_img_link .= " width='$pic_width' height='$pic_height' border='0'>";
344
345 label_cell($stock_img_link, "valign=top align=center rowspan=5");
346 end_row();
347
348 stock_categories_list_row(_("Category:"), 'category_id', null);
349
350 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
351
352 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
353         (!isset($_POST['NewStockID']) || $new_item));
354
355 stock_units_list_row(_('Units of Measure:'), 'units', null,
356         (!isset($_POST['NewStockID']) || $new_item));
357 end_table();
358 start_table("$table_style2 width=40%");
359
360 table_section_title(_("GL Accounts"));
361
362 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
363
364 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
365
366 if (!is_service($_POST['mb_flag'])) 
367 {
368         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
369         gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
370 }
371 else 
372 {
373         hidden('cogs_account', $_POST['cogs_account']);
374         hidden('adjustment_account', $_POST['adjustment_account']);
375 }
376
377
378 if (is_manufactured($_POST['mb_flag']))
379         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
380 else
381         hidden('assembly_account', $_POST['assembly_account']);
382 $dim = get_company_pref('use_dimension');
383 if ($dim >= 1)
384 {
385         table_section_title(_("Dimensions"));
386
387         dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
388         if ($dim > 1)
389                 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
390 }
391 if ($dim < 1)
392         hidden('dimension_id', 0);
393 if ($dim < 2)
394         hidden('dimension2_id', 0);
395
396 end_table(1);
397 div_end();
398 div_start('controls');
399 if (!isset($_POST['NewStockID']) || $new_item) 
400 {
401         submit_center('addupdate', _("Insert New Item"), true, '', true);
402
403 else 
404 {
405         submit_center_first('addupdate', _("Update Item"), '', true);
406         submit_return('select', _("Return"), _("Select this items and return to document entry."), true);
407         submit_center_last('delete', _("Delete This Item"), '', true);
408 }
409
410 div_end();
411 end_form();
412
413 //------------------------------------------------------------------------------------
414
415 end_page();
416 ?>