0004641: items->transactions should not show QOH for service items. @Braathwaate...
[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 if (isset($_GET['FixedAsset'])) {
23   $page_security = 'SA_ASSET';
24   $_SESSION['page_title'] = _($help_context = "Fixed Assets");
25   $_POST['mb_flag'] = 'F';
26   $_POST['fixed_asset']  = 1;
27 }
28 else {
29   $_SESSION['page_title'] = _($help_context = "Items");
30         if (!get_post('fixed_asset'))
31                 $_POST['fixed_asset']  = 0;
32 }
33
34
35 page($_SESSION['page_title'], false, false, "", $js);
36
37 include_once($path_to_root . "/includes/date_functions.inc");
38 include_once($path_to_root . "/includes/ui.inc");
39 include_once($path_to_root . "/includes/data_checks.inc");
40
41 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
42 include_once($path_to_root . "/fixed_assets/includes/fixed_assets_db.inc");
43
44 $user_comp = user_company();
45 $new_item = get_post('stock_id')=='' || get_post('cancel') || get_post('clone'); 
46 //------------------------------------------------------------------------------------
47
48 if (isset($_GET['stock_id']))
49 {
50         $_POST['stock_id'] = $_GET['stock_id'];
51 }
52 $stock_id = get_post('stock_id');
53 if (list_updated('stock_id')) {
54         $_POST['NewStockID'] = $stock_id = get_post('stock_id');
55     clear_data();
56         $Ajax->activate('details');
57         $Ajax->activate('controls');
58 }
59
60 if (get_post('cancel')) {
61         $_POST['NewStockID'] = $stock_id = $_POST['stock_id'] = '';
62     clear_data();
63         set_focus('stock_id');
64         $Ajax->activate('_page_body');
65 }
66 if (list_updated('category_id') || list_updated('mb_flag') || list_updated('fa_class_id') || list_updated('depreciation_method')) {
67         $Ajax->activate('details');
68 }
69 $upload_file = "";
70 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
71 {
72         $stock_id = $_POST['NewStockID'];
73         $result = $_FILES['pic']['error'];
74         $upload_file = 'Yes'; //Assume all is well to start off with
75         $filename = company_path().'/images';
76         if (!file_exists($filename))
77         {
78                 mkdir($filename);
79         }       
80         $filename .= "/".item_img_name($stock_id).".jpg";
81
82   if ($_FILES['pic']['error'] == UPLOAD_ERR_INI_SIZE) {
83     display_error(_('The file size is over the maximum allowed.'));
84                 $upload_file ='No';
85   }
86   elseif ($_FILES['pic']['error'] > 0) {
87                 display_error(_('Error uploading file.'));
88                 $upload_file ='No';
89   }
90         
91         //But check for the worst 
92         if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
93                 $imagetype = $type;
94         else
95                 $imagetype = false;
96
97         if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG)
98         {       //File type Check
99                 display_warning( _('Only graphics files can be uploaded'));
100                 $upload_file ='No';
101         }
102         elseif (!in_array(strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
103         {
104                 display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
105                 $upload_file ='No';
106         } 
107         elseif ( $_FILES['pic']['size'] > ($SysPrefs->max_image_size * 1024)) 
108         { //File Size Check
109                 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $SysPrefs->max_image_size);
110                 $upload_file ='No';
111         } 
112         elseif ( $_FILES['pic']['type'] == "text/plain" ) 
113         {  //File type Check
114                 display_warning( _('Only graphics files can be uploaded'));
115         $upload_file ='No';
116         } 
117         elseif (file_exists($filename))
118         {
119                 $result = unlink($filename);
120                 if (!$result) 
121                 {
122                         display_error(_('The existing image could not be removed'));
123                         $upload_file ='No';
124                 }
125         }
126         
127         if ($upload_file == 'Yes')
128         {
129                 $result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
130         }
131         $Ajax->activate('details');
132  /* EOF Add Image upload for New Item  - by Ori */
133 }
134
135 if (get_post('fixed_asset')) {
136         check_db_has_fixed_asset_categories(_("There are no fixed asset categories defined in the system. At least one fixed asset category is required to add a fixed asset."));
137         check_db_has_fixed_asset_classes(_("There are no fixed asset classes defined in the system. At least one fixed asset class is required to add a fixed asset."));
138 } else
139         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."));
140
141 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."));
142
143 function clear_data()
144 {
145         unset($_POST['long_description']);
146         unset($_POST['description']);
147         unset($_POST['category_id']);
148         unset($_POST['tax_type_id']);
149         unset($_POST['units']);
150         unset($_POST['mb_flag']);
151         unset($_POST['NewStockID']);
152         unset($_POST['dimension_id']);
153         unset($_POST['dimension2_id']);
154         unset($_POST['no_sale']);
155         unset($_POST['no_purchase']);
156         unset($_POST['depreciation_method']);
157         unset($_POST['depreciation_rate']);
158         unset($_POST['depreciation_factor']);
159         unset($_POST['depreciation_start']);
160 }
161
162 //------------------------------------------------------------------------------------
163
164 if (isset($_POST['addupdate'])) 
165 {
166
167         $input_error = 0;
168         if ($upload_file == 'No')
169                 $input_error = 1;
170         if (strlen($_POST['description']) == 0) 
171         {
172                 $input_error = 1;
173                 display_error( _('The item name must be entered.'));
174                 set_focus('description');
175         } 
176         elseif (strlen($_POST['NewStockID']) == 0) 
177         {
178                 $input_error = 1;
179                 display_error( _('The item code cannot be empty'));
180                 set_focus('NewStockID');
181         }
182         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
183                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
184                 strstr($_POST['NewStockID'], "&") || strstr($_POST['NewStockID'], "\t")) 
185         {
186                 $input_error = 1;
187                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
188                 set_focus('NewStockID');
189
190         }
191         elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
192         {
193                         $input_error = 1;
194                 display_error( _("This item code is already assigned to stock item or sale kit."));
195                         set_focus('NewStockID');
196         }
197         
198   if (get_post('fixed_asset')) {
199     if ($_POST['depreciation_rate'] > 100) {
200       $_POST['depreciation_rate'] = 100;
201     }
202     elseif ($_POST['depreciation_rate'] < 0) {
203       $_POST['depreciation_rate'] = 0;
204     }
205     $move_row = get_fixed_asset_move($_POST['NewStockID'], ST_SUPPRECEIVE);
206     if (isset($_POST['depreciation_start']) && strtotime($_POST['depreciation_start']) < strtotime($move_row['tran_date'])) {
207       display_warning(_('The depracation cannot start before the fixed asset purchase date'));
208     }
209   }
210         
211         if ($input_error != 1)
212         {
213                 if (check_value('del_image'))
214                 {
215                         $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
216                         if (file_exists($filename))
217                                 unlink($filename);
218                 }
219                 
220                 if (!$new_item) 
221                 { /*so its an existing one */
222                         update_item($_POST['NewStockID'], $_POST['description'],
223                                 $_POST['long_description'], $_POST['category_id'], 
224                                 $_POST['tax_type_id'], get_post('units'),
225                                 get_post('fixed_asset') ? 'F' : get_post('mb_flag'), $_POST['sales_account'],
226                                 $_POST['inventory_account'], $_POST['cogs_account'],
227                                 $_POST['adjustment_account'], $_POST['wip_account'], 
228                                 $_POST['dimension_id'], $_POST['dimension2_id'],
229                                 check_value('no_sale'), check_value('editable'), check_value('no_purchase'),
230                                 get_post('depreciation_method'), input_num('depreciation_rate'), input_num('depreciation_factor'), get_post('depreciation_start'),
231                                 get_post('fa_class_id'));
232
233                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
234                                 'stock_master', 'stock_id');
235                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
236                                 'item_codes', 'item_code');
237                         set_focus('stock_id');
238                         $Ajax->activate('stock_id'); // in case of status change
239                         display_notification(_("Item has been updated."));
240                 } 
241                 else 
242                 { //it is a NEW part
243
244                         add_item($_POST['NewStockID'], $_POST['description'],
245                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
246                                 $_POST['units'], get_post('fixed_asset') ? 'F' : get_post('mb_flag'), $_POST['sales_account'],
247                                 $_POST['inventory_account'], $_POST['cogs_account'],
248                                 $_POST['adjustment_account'], $_POST['wip_account'], 
249                                 $_POST['dimension_id'], $_POST['dimension2_id'],
250                                 check_value('no_sale'), check_value('editable'), check_value('no_purchase'),
251                                 get_post('depreciation_method'), input_num('depreciation_rate'), input_num('depreciation_factor'), get_post('depreciation_start'),
252                                 get_post('fa_class_id'));
253
254                         display_notification(_("A new item has been added."));
255                         $_POST['stock_id'] = $_POST['NewStockID'] = 
256                         $_POST['description'] = $_POST['long_description'] = '';
257                         $_POST['no_sale'] = $_POST['editable'] = $_POST['no_purchase'] =0;
258                         set_focus('NewStockID');
259                 }
260                 $Ajax->activate('_page_body');
261         }
262 }
263
264 if (get_post('clone')) {
265         unset($_POST['stock_id']);
266         $stock_id = '';
267         unset($_POST['inactive']);
268         set_focus('NewStockID');
269         $Ajax->activate('_page_body');
270 }
271
272 //------------------------------------------------------------------------------------
273
274 function check_usage($stock_id, $dispmsg=true)
275 {
276         $msg = item_in_foreign_codes($stock_id);
277
278         if ($msg != '') {
279                 if($dispmsg) display_error($msg);
280                 return false;
281         }
282         return true;
283 }
284
285 //------------------------------------------------------------------------------------
286
287 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
288 {
289
290         if (check_usage($_POST['NewStockID'])) {
291
292                 $stock_id = $_POST['NewStockID'];
293                 delete_item($stock_id);
294                 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
295                 if (file_exists($filename))
296                         unlink($filename);
297                 display_notification(_("Selected item has been deleted."));
298                 $_POST['stock_id'] = '';
299                 clear_data();
300                 set_focus('stock_id');
301                 $new_item = true;
302                 $Ajax->activate('_page_body');
303         }
304 }
305
306 function item_settings(&$stock_id, $new_item) 
307 {
308         global $SysPrefs, $path_to_root, $page_nested, $depreciation_methods;
309
310         start_outer_table(TABLESTYLE2);
311
312         table_section(1);
313
314         table_section_title(_("General Settings"));
315
316         //------------------------------------------------------------------------------------
317         if ($new_item) 
318         {
319                 $tmpCodeID=null;
320                 $post_label = null;
321                 if (!empty($SysPrefs->prefs['barcodes_on_stock']))
322                 {
323                         $post_label = '<button class="ajaxsubmit" type="submit" aspect=\'default\'  name="generateBarcode"  id="generateBarcode" value="Generate Barcode EAN8"> '._("Generate EAN-8 Barcode").' </button>';
324                         if (isset($_POST['generateBarcode']))
325                         {
326                                 $tmpCodeID=generateBarcode();
327                                 $_POST['NewStockID'] = $tmpCodeID;
328                         }
329                 }       
330                 text_row(_("Item Code:"), 'NewStockID', $tmpCodeID, 21, 20, null, "", $post_label);
331                 $_POST['inactive'] = 0;
332         } 
333         else 
334         { // Must be modifying an existing item
335                 if (get_post('NewStockID') != get_post('stock_id') || get_post('addupdate')) { // first item display
336
337                         $_POST['NewStockID'] = $_POST['stock_id'];
338
339                         $myrow = get_item($_POST['NewStockID']);
340
341                         $_POST['long_description'] = $myrow["long_description"];
342                         $_POST['description'] = $myrow["description"];
343                         $_POST['category_id']  = $myrow["category_id"];
344                         $_POST['tax_type_id']  = $myrow["tax_type_id"];
345                         $_POST['units']  = $myrow["units"];
346                         $_POST['mb_flag']  = $myrow["mb_flag"];
347
348                         $_POST['depreciation_method'] = $myrow['depreciation_method'];
349                         $_POST['depreciation_rate'] = number_format2($myrow['depreciation_rate'], 1);
350                         $_POST['depreciation_factor'] = number_format2($myrow['depreciation_factor'], 1);
351                         $_POST['depreciation_start'] = sql2date($myrow['depreciation_start']);
352                         $_POST['depreciation_date'] = sql2date($myrow['depreciation_date']);
353                         $_POST['fa_class_id'] = $myrow['fa_class_id'];
354                         $_POST['material_cost'] = $myrow['material_cost'];
355                         $_POST['purchase_cost'] = $myrow['purchase_cost'];
356                         
357                         $_POST['sales_account'] =  $myrow['sales_account'];
358                         $_POST['inventory_account'] = $myrow['inventory_account'];
359                         $_POST['cogs_account'] = $myrow['cogs_account'];
360                         $_POST['adjustment_account']    = $myrow['adjustment_account'];
361                         $_POST['wip_account']   = $myrow['wip_account'];
362                         $_POST['dimension_id']  = $myrow['dimension_id'];
363                         $_POST['dimension2_id'] = $myrow['dimension2_id'];
364                         $_POST['no_sale']       = $myrow['no_sale'];
365                         $_POST['no_purchase']   = $myrow['no_purchase'];
366                         $_POST['del_image'] = 0;
367                         $_POST['inactive'] = $myrow["inactive"];
368                         $_POST['editable'] = $myrow["editable"];
369                 }
370                 label_row(_("Item Code:"),$_POST['NewStockID']);
371                 hidden('NewStockID', $_POST['NewStockID']);
372                 set_focus('description');
373         }
374         $fixed_asset = get_post('fixed_asset');
375
376         text_row(_("Name:"), 'description', null, 52, 200);
377
378         textarea_row(_('Description:'), 'long_description', null, 42, 3);
379
380         stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item, $fixed_asset);
381
382         if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
383
384                 $category_record = get_item_category($_POST['category_id']);
385
386                 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
387                 $_POST['units'] = $category_record["dflt_units"];
388                 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
389                 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
390                 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
391                 $_POST['sales_account'] = $category_record["dflt_sales_act"];
392                 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
393                 $_POST['wip_account'] = $category_record["dflt_wip_act"];
394                 $_POST['dimension_id'] = $category_record["dflt_dim1"];
395                 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
396                 $_POST['no_sale'] = $category_record["dflt_no_sale"];
397                 $_POST['no_purchase'] = $category_record["dflt_no_purchase"];
398                 $_POST['editable'] = 0;
399
400         }
401         $fresh_item = !isset($_POST['NewStockID']) || $new_item 
402                 || check_usage($_POST['stock_id'],false);
403
404         // show inactive item tax type in selector only if already set.
405   item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null, !$new_item && item_type_inactive(get_post('tax_type_id')));
406
407         if (!get_post('fixed_asset'))
408                 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, $fresh_item);
409
410         stock_units_list_row(_('Units of Measure:'), 'units', null, $fresh_item);
411
412         check_row(_("Editable description:"), 'editable');
413
414         if (get_post('fixed_asset'))
415                 hidden('no_sale', 0);
416         else
417                 check_row(_("Exclude from sales:"), 'no_sale');
418
419         check_row(_("Exclude from purchases:"), 'no_purchase');
420
421         if (get_post('fixed_asset')) {
422                 table_section_title(_("Depreciation"));
423
424                 fixed_asset_classes_list_row(_("Fixed Asset Class").':', 'fa_class_id', null, false, true);
425
426                 array_selector_row(_("Depreciation Method").":", "depreciation_method", null, $depreciation_methods, array('select_submit'=> true));
427
428                 if (!isset($_POST['depreciation_rate']) || (list_updated('fa_class_id') || list_updated('depreciation_method'))) {
429                         $class_row = get_fixed_asset_class($_POST['fa_class_id']);
430                         $_POST['depreciation_rate'] = get_post('depreciation_method') == 'N' ? ceil(100/$class_row['depreciation_rate'])
431                                 : $class_row['depreciation_rate'];
432                 }
433
434                 if ($_POST['depreciation_method'] == 'O')
435                 {
436                         hidden('depreciation_rate', 100);
437                         label_row(_("Depreciation Rate").':', "100 %");
438                 }
439                 elseif ($_POST['depreciation_method'] == 'N')
440                 {
441                         small_amount_row(_("Depreciation Years").':', 'depreciation_rate', null, null, _('years'), 0);
442                 }
443                 elseif ($_POST['depreciation_method'] == 'D')
444                         small_amount_row(_("Base Rate").':', 'depreciation_rate', null, null, '%', user_percent_dec());
445                 else
446                         small_amount_row(_("Depreciation Rate").':', 'depreciation_rate', null, null, '%', user_percent_dec());
447
448                 if ($_POST['depreciation_method'] == 'D')
449                         small_amount_row(_("Rate multiplier").':', 'depreciation_factor', null, null, '', 2);
450
451                 // do not allow to change the depreciation start after this item has been depreciated
452                 if ($new_item || $_POST['depreciation_start'] == $_POST['depreciation_date'])
453                         date_row(_("Depreciation Start").':', 'depreciation_start', null, null, 1 - date('j'));
454                 else {
455                         hidden('depreciation_start');
456                         label_row(_("Depreciation Start").':', $_POST['depreciation_start']);
457                         label_row(_("Last Depreciation").':', $_POST['depreciation_date']==$_POST['depreciation_start'] ? _("None") :  $_POST['depreciation_date']);
458                 }
459                 hidden('depreciation_date');
460         }
461         table_section(2);
462
463         $dim = get_company_pref('use_dimension');
464         if ($dim >= 1)
465         {
466                 table_section_title(_("Dimensions"));
467
468                 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
469                 if ($dim > 1)
470                         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
471         }
472         if ($dim < 1)
473                 hidden('dimension_id', 0);
474         if ($dim < 2)
475                 hidden('dimension2_id', 0);
476
477         table_section_title(_("GL Accounts"));
478
479         gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
480
481         if (get_post('fixed_asset')) {
482                 gl_all_accounts_list_row(_("Asset account:"), 'inventory_account', $_POST['inventory_account']);
483                 gl_all_accounts_list_row(_("Depreciation cost account:"), 'cogs_account', $_POST['cogs_account']);
484                 gl_all_accounts_list_row(_("Depreciation/Disposal account:"), 'adjustment_account', $_POST['adjustment_account']);
485         }
486         elseif (!is_service($_POST['mb_flag'])) 
487         {
488                 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
489                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
490                 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
491         }
492         else 
493         {
494                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
495                 hidden('inventory_account', $_POST['inventory_account']);
496                 hidden('adjustment_account', $_POST['adjustment_account']);
497         }
498
499
500         if (is_manufactured($_POST['mb_flag']))
501                 gl_all_accounts_list_row(_("WIP Account:"), 'wip_account', $_POST['wip_account']);
502         else
503                 hidden('wip_account', $_POST['wip_account']);
504
505         table_section_title(_("Other"));
506
507         // Add image upload for New Item  - by Joe
508         file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
509         // Add Image upload for New Item  - by Joe
510         $stock_img_link = "";
511         $check_remove_image = false;
512         if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
513                 .item_img_name($_POST['NewStockID']).".jpg")) 
514         {
515          // 31/08/08 - rand() call is necessary here to avoid caching problems.
516                 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
517                         "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
518                         ".jpg?nocache=".rand()."'"." height='".$SysPrefs->pic_height."' border='0'>";
519                 $check_remove_image = true;
520         } 
521         else 
522         {
523                 $stock_img_link .= _("No image");
524         }
525
526         label_row("&nbsp;", $stock_img_link);
527         if ($check_remove_image)
528                 check_row(_("Delete Image:"), 'del_image');
529
530         record_status_list_row(_("Item status:"), 'inactive');
531         if (get_post('fixed_asset')) {
532                 table_section_title(_("Values"));
533                 if (!$new_item) {
534                         hidden('material_cost');
535                         hidden('purchase_cost');
536                         label_row(_("Initial Value").":", price_format($_POST['purchase_cost']), "", "align='right'");
537                         label_row(_("Depreciations").":", price_format($_POST['purchase_cost'] - $_POST['material_cost']), "", "align='right'");
538                         label_row(_("Current Value").':', price_format($_POST['material_cost']), "", "align='right'");
539                 }
540         }
541         end_outer_table(1);
542
543         div_start('controls');
544         if (@$_REQUEST['popup']) hidden('popup', 1);
545         if (!isset($_POST['NewStockID']) || $new_item) 
546         {
547                 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
548         } 
549         else 
550         {
551                 submit_center_first('addupdate', _("Update Item"), '', 
552                         $page_nested ? true : 'default');
553                 submit_return('select', get_post('stock_id'), 
554                         _("Select this items and return to document entry."));
555                 submit('clone', _("Clone This Item"), true, '', true);
556                 submit('delete', _("Delete This Item"), true, '', true);
557                 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
558         }
559
560         div_end();
561 }
562
563 //-------------------------------------------------------------------------------------------- 
564
565 start_form(true);
566
567 if (db_has_stock_items()) 
568 {
569         start_table(TABLESTYLE_NOBORDER);
570         start_row();
571     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
572           _('New item'), true, check_value('show_inactive'), false, array('fixed_asset' => get_post('fixed_asset')));
573         $new_item = get_post('stock_id')=='';
574         check_cells(_("Show inactive:"), 'show_inactive', null, true);
575         end_row();
576         end_table();
577
578         if (get_post('_show_inactive_update')) {
579                 $Ajax->activate('stock_id');
580                 set_focus('stock_id');
581         }
582 }
583 else
584 {
585         hidden('stock_id', get_post('stock_id'));
586 }
587
588 div_start('details');
589
590 $stock_id = get_post('stock_id');
591 if (!$stock_id)
592         unset($_POST['_tabs_sel']); // force settings tab for new customer
593
594 $tabs = (get_post('fixed_asset'))
595         ? array(
596                 'settings' => array(_('&General settings'), $stock_id),
597                 'movement' => array(_('&Transactions'), $stock_id) )
598         : array(
599                 'settings' => array(_('&General settings'), $stock_id),
600                 'sales_pricing' => array(_('S&ales Pricing'), (user_check_access('SA_SALESPRICE') ? $stock_id : null)),
601                 'purchase_pricing' => array(_('&Purchasing Pricing'), (user_check_access('SA_PURCHASEPRICING') ? $stock_id : null)),
602                 'standard_cost' => array(_('Standard &Costs'), (user_check_access('SA_STANDARDCOST') ? $stock_id : null)),
603                 'reorder_level' => array(_('&Reorder Levels'), (is_inventory_item($stock_id) && 
604                         user_check_access('SA_REORDER') ? $stock_id : null)),
605                 'movement' => array(_('&Transactions'), (user_check_access('SA_ITEMSTRANSVIEW') && is_inventory_item($stock_id) ? 
606                         $stock_id : null)),
607                 'status' => array(_('&Status'), (user_check_access('SA_ITEMSSTATVIEW') ? $stock_id : null)),
608         );
609
610 tabbed_content_start('tabs', $tabs);
611
612         switch (get_post('_tabs_sel')) {
613                 default:
614                 case 'settings':
615                         item_settings($stock_id, $new_item); 
616                         break;
617                 case 'sales_pricing':
618                         $_GET['stock_id'] = $stock_id;
619                         $_GET['page_level'] = 1;
620                         include_once($path_to_root."/inventory/prices.php");
621                         break;
622                 case 'purchase_pricing':
623                         $_GET['stock_id'] = $stock_id;
624                         $_GET['page_level'] = 1;
625                         include_once($path_to_root."/inventory/purchasing_data.php");
626                         break;
627                 case 'standard_cost':
628                         $_GET['stock_id'] = $stock_id;
629                         $_GET['page_level'] = 1;
630                         include_once($path_to_root."/inventory/cost_update.php");
631                         break;
632                 case 'reorder_level':
633                         if (!is_inventory_item($stock_id))
634                                 break;
635                         $_GET['page_level'] = 1;
636                         $_GET['stock_id'] = $stock_id;
637                         include_once($path_to_root."/inventory/reorder_level.php");
638                         break;
639                 case 'movement':
640                         if (!is_inventory_item($stock_id))
641                                 break;
642                         $_GET['stock_id'] = $stock_id;
643                         include_once($path_to_root."/inventory/inquiry/stock_movements.php");
644                         break;
645                 case 'status':
646                         $_GET['stock_id'] = $stock_id;
647                         include_once($path_to_root."/inventory/inquiry/stock_status.php");
648                         break;
649         };
650
651 br();
652 tabbed_content_end();
653
654 div_end();
655
656 hidden('fixed_asset', get_post('fixed_asset'));
657
658 if (get_post('fixed_asset'))
659         hidden('mb_flag', 'F');
660
661 end_form();
662
663 //------------------------------------------------------------------------------------
664
665 end_page();
666
667 function generateBarcode() {
668         $tmpBarcodeID = "";
669         $tmpCountTrys = 0;
670         while ($tmpBarcodeID == "")     {
671                 srand ((double) microtime( )*1000000);
672                 $random_1  = rand(1,9);
673                 $random_2  = rand(0,9);
674                 $random_3  = rand(0,9);
675                 $random_4  = rand(0,9);
676                 $random_5  = rand(0,9);
677                 $random_6  = rand(0,9);
678                 $random_7  = rand(0,9);
679                 //$random_8  = rand(0,9);
680
681                         // http://stackoverflow.com/questions/1136642/ean-8-how-to-calculate-checksum-digit
682                 $sum1 = $random_2 + $random_4 + $random_6; 
683                 $sum2 = 3 * ($random_1  + $random_3  + $random_5  + $random_7 );
684                 $checksum_value = $sum1 + $sum2;
685
686                 $checksum_digit = 10 - ($checksum_value % 10);
687                 if ($checksum_digit == 10) 
688                         $checksum_digit = 0;
689
690                 $random_8  = $checksum_digit;
691
692                 $tmpBarcodeID = $random_1 . $random_2 . $random_3 . $random_4 . $random_5 . $random_6 . $random_7 . $random_8;
693
694                 // LETS CHECK TO SEE IF THIS NUMBER HAS EVER BEEN USED
695                 $query = "SELECT stock_id FROM ".TB_PREF."stock_master WHERE stock_id='" . $tmpBarcodeID . "'";
696                 $arr_stock = db_fetch(db_query($query));
697   
698                 if (  !$arr_stock['stock_id'] ) {
699                         return $tmpBarcodeID;
700                 }
701                 $tmpBarcodeID = "";      
702         }
703 }