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