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