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