Cleanup comments. Small bugfixes. Third run
[fa-stable.git] / inventory / manage / items.php
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 $page_security = 'SA_ITEM';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 $js = "";
17 if ($SysPrefs->use_popup_windows)
18         $js .= get_js_open_window(900, 500);
19 if (user_use_date_picker())
20         $js .= get_js_date_picker();
21
22 page(_($help_context = "Items"), @$_REQUEST['popup'], false, "", $js);
23
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/ui.inc");
26 include_once($path_to_root . "/includes/data_checks.inc");
27
28 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
29
30 $user_comp = user_company();
31 $new_item = get_post('stock_id')=='' || get_post('cancel') || get_post('clone'); 
32 //------------------------------------------------------------------------------------
33
34 if (isset($_GET['stock_id']))
35 {
36         $_POST['stock_id'] = $_GET['stock_id'];
37 }
38 $stock_id = get_post('stock_id');
39 if (list_updated('stock_id')) {
40         $_POST['NewStockID'] = $stock_id = get_post('stock_id');
41     clear_data();
42         $Ajax->activate('details');
43         $Ajax->activate('controls');
44 }
45
46 if (get_post('cancel')) {
47         $_POST['NewStockID'] = $stock_id = $_POST['stock_id'] = '';
48     clear_data();
49         set_focus('stock_id');
50         $Ajax->activate('_page_body');
51 }
52 if (list_updated('category_id') || list_updated('mb_flag')) {
53         $Ajax->activate('details');
54 }
55 $upload_file = "";
56 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
57 {
58         $stock_id = $_POST['NewStockID'];
59         $result = $_FILES['pic']['error'];
60         $upload_file = 'Yes'; //Assume all is well to start off with
61         $filename = company_path().'/images';
62         if (!file_exists($filename))
63         {
64                 mkdir($filename);
65         }       
66         $filename .= "/".item_img_name($stock_id).".jpg";
67
68   if ($_FILES['pic']['error'] == UPLOAD_ERR_INI_SIZE) {
69     display_error(_('The file size is over the maximum allowed.'));
70                 $upload_file ='No';
71   }
72   elseif ($_FILES['pic']['error'] > 0) {
73                 display_error(_('Error uploading file.'));
74                 $upload_file ='No';
75   }
76         
77         //But check for the worst 
78         if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
79                 $imagetype = $type;
80         else
81                 $imagetype = false;
82
83         if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG)
84         {       //File type Check
85                 display_warning( _('Only graphics files can be uploaded'));
86                 $upload_file ='No';
87         }
88         elseif (!in_array(strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
89         {
90                 display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
91                 $upload_file ='No';
92         } 
93         elseif ( $_FILES['pic']['size'] > ($SysPrefs->max_image_size * 1024)) 
94         { //File Size Check
95                 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $SysPrefs->max_image_size);
96                 $upload_file ='No';
97         } 
98         elseif ( $_FILES['pic']['type'] == "text/plain" ) 
99         {  //File type Check
100                 display_warning( _('Only graphics files can be uploaded'));
101         $upload_file ='No';
102         } 
103         elseif (file_exists($filename))
104         {
105                 $result = unlink($filename);
106                 if (!$result) 
107                 {
108                         display_error(_('The existing image could not be removed'));
109                         $upload_file ='No';
110                 }
111         }
112         
113         if ($upload_file == 'Yes')
114         {
115                 $result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
116         }
117         $Ajax->activate('details');
118  /* EOF Add Image upload for New Item  - by Ori */
119 }
120
121 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."));
122
123 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."));
124
125 function clear_data()
126 {
127         unset($_POST['long_description']);
128         unset($_POST['description']);
129         unset($_POST['category_id']);
130         unset($_POST['tax_type_id']);
131         unset($_POST['units']);
132         unset($_POST['mb_flag']);
133         unset($_POST['NewStockID']);
134         unset($_POST['dimension_id']);
135         unset($_POST['dimension2_id']);
136         unset($_POST['no_sale']);
137         unset($_POST['no_purchase']);
138 }
139
140 //------------------------------------------------------------------------------------
141
142 if (isset($_POST['addupdate'])) 
143 {
144
145         $input_error = 0;
146         if ($upload_file == 'No')
147                 $input_error = 1;
148         if (strlen($_POST['description']) == 0) 
149         {
150                 $input_error = 1;
151                 display_error( _('The item name must be entered.'));
152                 set_focus('description');
153         } 
154         elseif (strlen($_POST['NewStockID']) == 0) 
155         {
156                 $input_error = 1;
157                 display_error( _('The item code cannot be empty'));
158                 set_focus('NewStockID');
159         }
160         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
161                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
162                 strstr($_POST['NewStockID'], "&") || strstr($_POST['NewStockID'], "\t")) 
163         {
164                 $input_error = 1;
165                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
166                 set_focus('NewStockID');
167
168         }
169         elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
170         {
171                         $input_error = 1;
172                 display_error( _("This item code is already assigned to stock item or sale kit."));
173                         set_focus('NewStockID');
174         }
175         
176         if ($input_error != 1)
177         {
178                 if (check_value('del_image'))
179                 {
180                         $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
181                         if (file_exists($filename))
182                                 unlink($filename);
183                 }
184                 
185                 if (!$new_item) 
186                 { /*so its an existing one */
187                         update_item($_POST['NewStockID'], $_POST['description'],
188                                 $_POST['long_description'], $_POST['category_id'], 
189                                 $_POST['tax_type_id'], get_post('units'),
190                                 get_post('mb_flag'), $_POST['sales_account'],
191                                 $_POST['inventory_account'], $_POST['cogs_account'],
192                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
193                                 $_POST['dimension_id'], $_POST['dimension2_id'],
194                                 check_value('no_sale'), check_value('editable'), check_value('no_purchase'));
195                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
196                                 'stock_master', 'stock_id');
197                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
198                                 'item_codes', 'item_code');
199                         set_focus('stock_id');
200                         $Ajax->activate('stock_id'); // in case of status change
201                         display_notification(_("Item has been updated."));
202                 } 
203                 else 
204                 { //it is a NEW part
205
206                         add_item($_POST['NewStockID'], $_POST['description'],
207                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
208                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
209                                 $_POST['inventory_account'], $_POST['cogs_account'],
210                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
211                                 $_POST['dimension_id'], $_POST['dimension2_id'],
212                                 check_value('no_sale'), check_value('editable'), check_value('no_purchase'));
213
214                         display_notification(_("A new item has been added."));
215                         $_POST['stock_id'] = $_POST['NewStockID'] = 
216                         $_POST['description'] = $_POST['long_description'] = '';
217                         $_POST['no_sale'] = $_POST['editable'] = $_POST['no_purchase'] =0;
218                         set_focus('NewStockID');
219                 }
220                 $Ajax->activate('_page_body');
221         }
222 }
223
224 if (get_post('clone')) {
225         unset($_POST['stock_id']);
226         $stock_id = '';
227         unset($_POST['inactive']);
228         set_focus('NewStockID');
229         $Ajax->activate('_page_body');
230 }
231
232 //------------------------------------------------------------------------------------
233
234 function check_usage($stock_id, $dispmsg=true)
235 {
236         $msg = item_in_foreign_codes($stock_id);
237
238         if ($msg != '') {
239                 if($dispmsg) display_error($msg);
240                 return false;
241         }
242         return true;
243 }
244
245 //------------------------------------------------------------------------------------
246
247 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
248 {
249
250         if (check_usage($_POST['NewStockID'])) {
251
252                 $stock_id = $_POST['NewStockID'];
253                 delete_item($stock_id);
254                 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
255                 if (file_exists($filename))
256                         unlink($filename);
257                 display_notification(_("Selected item has been deleted."));
258                 $_POST['stock_id'] = '';
259                 clear_data();
260                 set_focus('stock_id');
261                 $new_item = true;
262                 $Ajax->activate('_page_body');
263         }
264 }
265
266 function item_settings(&$stock_id, $new_item) 
267 {
268         global $SysPrefs, $path_to_root;
269
270         start_outer_table(TABLESTYLE2);
271
272         table_section(1);
273
274         table_section_title(_("General Settings"));
275
276         //------------------------------------------------------------------------------------
277         if ($new_item) 
278         {
279                 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
280
281                 $_POST['inactive'] = 0;
282         } 
283         else 
284         { // Must be modifying an existing item
285                 if (get_post('NewStockID') != get_post('stock_id') || get_post('addupdate')) { // first item display
286
287                         $_POST['NewStockID'] = $_POST['stock_id'];
288
289                         $myrow = get_item($_POST['NewStockID']);
290
291                         $_POST['long_description'] = $myrow["long_description"];
292                         $_POST['description'] = $myrow["description"];
293                         $_POST['category_id']  = $myrow["category_id"];
294                         $_POST['tax_type_id']  = $myrow["tax_type_id"];
295                         $_POST['units']  = $myrow["units"];
296                         $_POST['mb_flag']  = $myrow["mb_flag"];
297
298                         $_POST['sales_account'] =  $myrow['sales_account'];
299                         $_POST['inventory_account'] = $myrow['inventory_account'];
300                         $_POST['cogs_account'] = $myrow['cogs_account'];
301                         $_POST['adjustment_account']    = $myrow['adjustment_account'];
302                         $_POST['assembly_account']      = $myrow['assembly_account'];
303                         $_POST['dimension_id']  = $myrow['dimension_id'];
304                         $_POST['dimension2_id'] = $myrow['dimension2_id'];
305                         $_POST['no_sale']       = $myrow['no_sale'];
306                         $_POST['no_purchase']   = $myrow['no_purchase'];
307                         $_POST['del_image'] = 0;
308                         $_POST['inactive'] = $myrow["inactive"];
309                         $_POST['editable'] = $myrow["editable"];
310                 }
311                 label_row(_("Item Code:"),$_POST['NewStockID']);
312                 hidden('NewStockID', $_POST['NewStockID']);
313                 set_focus('description');
314         }
315
316         text_row(_("Name:"), 'description', null, 52, 200);
317
318         textarea_row(_('Description:'), 'long_description', null, 42, 3);
319
320         stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item);
321
322         if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
323
324                 $category_record = get_item_category($_POST['category_id']);
325
326                 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
327                 $_POST['units'] = $category_record["dflt_units"];
328                 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
329                 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
330                 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
331                 $_POST['sales_account'] = $category_record["dflt_sales_act"];
332                 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
333                 $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
334                 $_POST['dimension_id'] = $category_record["dflt_dim1"];
335                 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
336                 $_POST['no_sale'] = $category_record["dflt_no_sale"];
337                 $_POST['no_purchase'] = $category_record["dflt_no_purchase"];
338                 $_POST['editable'] = 0;
339
340         }
341         $fresh_item = !isset($_POST['NewStockID']) || $new_item 
342                 || check_usage($_POST['stock_id'],false);
343
344         item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
345
346         stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, $fresh_item);
347
348         stock_units_list_row(_('Units of Measure:'), 'units', null, $fresh_item);
349
350         check_row(_("Editable description:"), 'editable');
351
352         check_row(_("Exclude from sales:"), 'no_sale');
353
354         check_row(_("Exclude from purchases:"), 'no_purchase');
355
356         table_section(2);
357
358         $dim = get_company_pref('use_dimension');
359         if ($dim >= 1)
360         {
361                 table_section_title(_("Dimensions"));
362
363                 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
364                 if ($dim > 1)
365                         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
366         }
367         if ($dim < 1)
368                 hidden('dimension_id', 0);
369         if ($dim < 2)
370                 hidden('dimension2_id', 0);
371
372         table_section_title(_("GL Accounts"));
373
374         gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
375
376         if (!is_service($_POST['mb_flag'])) 
377         {
378                 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
379                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
380                 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
381         }
382         else 
383         {
384                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
385                 hidden('inventory_account', $_POST['inventory_account']);
386                 hidden('adjustment_account', $_POST['adjustment_account']);
387         }
388
389
390         if (is_manufactured($_POST['mb_flag']))
391                 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
392         else
393                 hidden('assembly_account', $_POST['assembly_account']);
394
395         table_section_title(_("Other"));
396
397         // Add image upload for New Item  - by Joe
398         file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
399         // Add Image upload for New Item  - by Joe
400         $stock_img_link = "";
401         $check_remove_image = false;
402         if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
403                 .item_img_name($_POST['NewStockID']).".jpg")) 
404         {
405          // 31/08/08 - rand() call is necessary here to avoid caching problems.
406                 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
407                         "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
408                         ".jpg?nocache=".rand()."'"." height='".$SysPrefs->pic_height."' border='0'>";
409                 $check_remove_image = true;
410         } 
411         else 
412         {
413                 $stock_img_link .= _("No image");
414         }
415
416         label_row("&nbsp;", $stock_img_link);
417         if ($check_remove_image)
418                 check_row(_("Delete Image:"), 'del_image');
419
420         record_status_list_row(_("Item status:"), 'inactive');
421         end_outer_table(1);
422
423         div_start('controls');
424         if (!isset($_POST['NewStockID']) || $new_item) 
425         {
426                 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
427         } 
428         else 
429         {
430                 submit_center_first('addupdate', _("Update Item"), '', 
431                         @$_REQUEST['popup'] ? true : 'default');
432                 submit_return('select', get_post('stock_id'), 
433                         _("Select this items and return to document entry."), 'default');
434                 submit('clone', _("Clone This Item"), true, '', true);
435                 submit('delete', _("Delete This Item"), true, '', true);
436                 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
437         }
438
439         div_end();
440 }
441
442 //-------------------------------------------------------------------------------------------- 
443
444 start_form(true);
445
446 if (db_has_stock_items()) 
447 {
448         start_table(TABLESTYLE_NOBORDER);
449         start_row();
450     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
451           _('New item'), true, check_value('show_inactive'));
452         $new_item = get_post('stock_id')=='';
453         check_cells(_("Show inactive:"), 'show_inactive', null, true);
454         end_row();
455         end_table();
456
457         if (get_post('_show_inactive_update')) {
458                 $Ajax->activate('stock_id');
459                 set_focus('stock_id');
460         }
461 }
462 else
463 {
464         hidden('stock_id', get_post('stock_id'));
465 }
466
467 div_start('details');
468
469 $stock_id = get_post('stock_id');
470 if (!$stock_id)
471         unset($_POST['_tabs_sel']); // force settings tab for new customer
472
473 tabbed_content_start('tabs', array(
474                 'settings' => array(_('&General settings'), $stock_id),
475                 'sales_pricing' => array(_('S&ales Pricing'), $stock_id),
476                 'purchase_pricing' => array(_('&Purchasing Pricing'), $stock_id),
477                 'standard_cost' => array(_('Standard &Costs'), $stock_id),
478                 'reorder_level' => array(_('&Reorder Levels'), (is_inventory_item($stock_id) ? $stock_id : null)),
479                 'movement' => array(_('&Transactions'), $stock_id),
480                 'status' => array(_('&Status'), $stock_id),
481         ));
482         
483         switch (get_post('_tabs_sel')) {
484                 default:
485                 case 'settings':
486                         item_settings($stock_id, $new_item); 
487                         break;
488                 case 'sales_pricing':
489                         $_GET['stock_id'] = $stock_id;
490                         $_GET['popup'] = 1;
491                         include_once($path_to_root."/inventory/prices.php");
492                         break;
493                 case 'purchase_pricing':
494                         $_GET['stock_id'] = $stock_id;
495                         $_GET['popup'] = 1;
496                         include_once($path_to_root."/inventory/purchasing_data.php");
497                         break;
498                 case 'standard_cost':
499                         $_GET['stock_id'] = $stock_id;
500                         $_GET['popup'] = 1;
501                         include_once($path_to_root."/inventory/cost_update.php");
502                         break;
503                 case 'reorder_level':
504                         if (!is_inventory_item($stock_id))
505                         {
506                                 break;
507                         }       
508                         $_GET['stock_id'] = $stock_id;
509                         $_GET['popup'] = 1;
510                         include_once($path_to_root."/inventory/reorder_level.php");
511                         break;
512                 case 'movement':
513                         $_GET['stock_id'] = $stock_id;
514                         $_GET['popup'] = 1;
515                         include_once($path_to_root."/inventory/inquiry/stock_movements.php");
516                         break;
517                 case 'status':
518                         $_GET['stock_id'] = $stock_id;
519                         $_GET['popup'] = 1;
520                         include_once($path_to_root."/inventory/inquiry/stock_status.php");
521                         break;
522         };
523 br();
524 tabbed_content_end();
525
526 div_end();
527
528 hidden('popup', @$_REQUEST['popup']);
529 end_form();
530
531 //------------------------------------------------------------------------------------
532
533 end_page(@$_REQUEST['popup']);