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