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