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