Tabbed dialogs allowed non access pages.
[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 $js = "";
16 if ($use_popup_windows)
17         $js .= get_js_open_window(900, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20         
21 page(_($help_context = "Items"), @$_REQUEST['popup'], false, "", $js);
22
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/ui.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26
27 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
28
29 $user_comp = user_company();
30 $new_item = get_post('stock_id')=='' || get_post('cancel') || get_post('clone'); 
31 //------------------------------------------------------------------------------------
32
33 if (isset($_GET['stock_id']))
34 {
35         $_POST['stock_id'] = $_GET['stock_id'];
36 }
37 $stock_id = get_post('stock_id');
38 if (list_updated('stock_id')) {
39         $_POST['NewStockID'] = $stock_id = get_post('stock_id');
40     clear_data();
41         $Ajax->activate('details');
42         $Ajax->activate('controls');
43 }
44
45 if (get_post('cancel')) {
46         $_POST['NewStockID'] = $stock_id = $_POST['stock_id'] = '';
47     clear_data();
48         set_focus('stock_id');
49         $Ajax->activate('_page_body');
50 }
51 if (list_updated('category_id') || list_updated('mb_flag')) {
52         $Ajax->activate('details');
53 }
54 $upload_file = "";
55 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
56 {
57         $stock_id = $_POST['NewStockID'];
58         $result = $_FILES['pic']['error'];
59         $upload_file = 'Yes'; //Assume all is well to start off with
60         $filename = company_path().'/images';
61         if (!file_exists($filename))
62         {
63                 mkdir($filename);
64         }       
65         $filename .= "/".item_img_name($stock_id).".jpg";
66         
67         //But check for the worst 
68         if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
69                 $imagetype = $type;
70         else
71                 $imagetype = false;
72         //$imagetype = exif_imagetype($_FILES['pic']['tmp_name']);
73         if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG)
74         {       //File type Check
75                 display_warning( _('Only graphics files can be uploaded'));
76                 $upload_file ='No';
77         }
78         elseif (!in_array(strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
79         {
80                 display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
81                 $upload_file ='No';
82         } 
83         elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024)) 
84         { //File Size Check
85                 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
86                 $upload_file ='No';
87         } 
88         elseif (file_exists($filename))
89         {
90                 $result = unlink($filename);
91                 if (!$result) 
92                 {
93                         display_error(_('The existing image could not be removed'));
94                         $upload_file ='No';
95                 }
96         }
97         
98         if ($upload_file == 'Yes')
99         {
100                 $result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
101         }
102         $Ajax->activate('details');
103  /* EOF Add Image upload for New Item  - by Ori */
104 }
105
106 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."));
107
108 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."));
109
110 function clear_data()
111 {
112         unset($_POST['long_description']);
113         unset($_POST['description']);
114         unset($_POST['category_id']);
115         unset($_POST['tax_type_id']);
116         unset($_POST['units']);
117         unset($_POST['mb_flag']);
118         unset($_POST['NewStockID']);
119         unset($_POST['dimension_id']);
120         unset($_POST['dimension2_id']);
121         unset($_POST['no_sale']);
122 }
123
124 //------------------------------------------------------------------------------------
125
126 if (isset($_POST['addupdate'])) 
127 {
128
129         $input_error = 0;
130         if ($upload_file == 'No')
131                 $input_error = 1;
132         if (strlen($_POST['description']) == 0) 
133         {
134                 $input_error = 1;
135                 display_error( _('The item name must be entered.'));
136                 set_focus('description');
137         } 
138         elseif (strlen($_POST['NewStockID']) == 0) 
139         {
140                 $input_error = 1;
141                 display_error( _('The item code cannot be empty'));
142                 set_focus('NewStockID');
143         }
144         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
145                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
146                 strstr($_POST['NewStockID'], "&") || strstr($_POST['NewStockID'], "\t")) 
147         {
148                 $input_error = 1;
149                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
150                 set_focus('NewStockID');
151
152         }
153         elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
154         {
155                         $input_error = 1;
156                 display_error( _("This item code is already assigned to stock item or sale kit."));
157                         set_focus('NewStockID');
158         }
159         
160         if ($input_error != 1)
161         {
162                 if (check_value('del_image'))
163                 {
164                         $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
165                         if (file_exists($filename))
166                                 unlink($filename);
167                 }
168                 
169                 if (!$new_item) 
170                 { /*so its an existing one */
171                         update_item($_POST['NewStockID'], $_POST['description'],
172                                 $_POST['long_description'], $_POST['category_id'], 
173                                 $_POST['tax_type_id'], get_post('units'),
174                                 get_post('mb_flag'), $_POST['sales_account'],
175                                 $_POST['inventory_account'], $_POST['cogs_account'],
176                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
177                                 $_POST['dimension_id'], $_POST['dimension2_id'],
178                                 check_value('no_sale'), check_value('editable'));
179                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
180                                 'stock_master', 'stock_id');
181                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
182                                 'item_codes', 'item_code');
183                         set_focus('stock_id');
184                         $Ajax->activate('stock_id'); // in case of status change
185                         display_notification(_("Item has been updated."));
186                 } 
187                 else 
188                 { //it is a NEW part
189
190                         add_item($_POST['NewStockID'], $_POST['description'],
191                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
192                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
193                                 $_POST['inventory_account'], $_POST['cogs_account'],
194                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
195                                 $_POST['dimension_id'], $_POST['dimension2_id'],
196                                 check_value('no_sale'), check_value('editable'));
197
198                         display_notification(_("A new item has been added."));
199                         $_POST['stock_id'] = $_POST['NewStockID'] = 
200                         $_POST['description'] = $_POST['long_description'] = '';
201                         $_POST['no_sale'] = $_POST['editable'] = 0;
202                         set_focus('NewStockID');
203                 }
204                 $Ajax->activate('_page_body');
205         }
206 }
207
208 if (get_post('clone')) {
209         unset($_POST['stock_id']);
210         $stock_id = '';
211         unset($_POST['inactive']);
212         set_focus('NewStockID');
213         $Ajax->activate('_page_body');
214 }
215
216 //------------------------------------------------------------------------------------
217
218 function check_usage($stock_id, $dispmsg=true)
219 {
220         $msg = item_in_foreign_codes($stock_id);
221
222         if ($msg != '') {
223                 if($dispmsg) display_error($msg);
224                 return false;
225         }
226         return true;
227 }
228
229 //------------------------------------------------------------------------------------
230
231 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
232 {
233
234         if (check_usage($_POST['NewStockID'])) {
235
236                 $stock_id = $_POST['NewStockID'];
237                 delete_item($stock_id);
238                 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
239                 if (file_exists($filename))
240                         unlink($filename);
241                 display_notification(_("Selected item has been deleted."));
242                 $_POST['stock_id'] = '';
243                 clear_data();
244                 set_focus('stock_id');
245                 $new_item = true;
246                 $Ajax->activate('_page_body');
247         }
248 }
249
250 function item_settings(&$stock_id) 
251 {
252         global $SysPrefs, $path_to_root, $new_item, $pic_height;
253
254         start_outer_table(TABLESTYLE2);
255
256         table_section(1);
257
258         table_section_title(_("Item"));
259
260         //------------------------------------------------------------------------------------
261         if ($new_item) 
262         {
263                 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
264
265                 $_POST['inactive'] = 0;
266         } 
267         else 
268         { // Must be modifying an existing item
269                 if (get_post('NewStockID') != get_post('stock_id') || get_post('addupdate')) { // first item display
270
271                         $_POST['NewStockID'] = $_POST['stock_id'];
272
273                         $myrow = get_item($_POST['NewStockID']);
274
275                         $_POST['long_description'] = $myrow["long_description"];
276                         $_POST['description'] = $myrow["description"];
277                         $_POST['category_id']  = $myrow["category_id"];
278                         $_POST['tax_type_id']  = $myrow["tax_type_id"];
279                         $_POST['units']  = $myrow["units"];
280                         $_POST['mb_flag']  = $myrow["mb_flag"];
281
282                         $_POST['sales_account'] =  $myrow['sales_account'];
283                         $_POST['inventory_account'] = $myrow['inventory_account'];
284                         $_POST['cogs_account'] = $myrow['cogs_account'];
285                         $_POST['adjustment_account']    = $myrow['adjustment_account'];
286                         $_POST['assembly_account']      = $myrow['assembly_account'];
287                         $_POST['dimension_id']  = $myrow['dimension_id'];
288                         $_POST['dimension2_id'] = $myrow['dimension2_id'];
289                         $_POST['no_sale']       = $myrow['no_sale'];
290                         $_POST['del_image'] = 0;        
291                         $_POST['inactive'] = $myrow["inactive"];
292                         $_POST['editable'] = $myrow["editable"];
293                 }
294                 label_row(_("Item Code:"),$_POST['NewStockID']);
295                 hidden('NewStockID', $_POST['NewStockID']);
296                 set_focus('description');
297         }
298
299         text_row(_("Name:"), 'description', null, 52, 200);
300
301         textarea_row(_('Description:'), 'long_description', null, 42, 3);
302
303         stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item);
304
305         if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
306
307                 $category_record = get_item_category($_POST['category_id']);
308
309                 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
310                 $_POST['units'] = $category_record["dflt_units"];
311                 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
312                 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
313                 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
314                 $_POST['sales_account'] = $category_record["dflt_sales_act"];
315                 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
316                 $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
317                 $_POST['dimension_id'] = $category_record["dflt_dim1"];
318                 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
319                 $_POST['no_sale'] = $category_record["dflt_no_sale"];
320                 $_POST['editable'] = 0;
321
322         }
323         $fresh_item = !isset($_POST['NewStockID']) || $new_item 
324                 || check_usage($_POST['stock_id'],false);
325
326         item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
327
328         stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, $fresh_item);
329
330         stock_units_list_row(_('Units of Measure:'), 'units', null, $fresh_item);
331
332         check_row(_("Editable description:"), 'editable');
333
334         check_row(_("Exclude from sales:"), 'no_sale');
335
336         table_section(2);
337
338         $dim = get_company_pref('use_dimension');
339         if ($dim >= 1)
340         {
341                 table_section_title(_("Dimensions"));
342
343                 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
344                 if ($dim > 1)
345                         dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
346         }
347         if ($dim < 1)
348                 hidden('dimension_id', 0);
349         if ($dim < 2)
350                 hidden('dimension2_id', 0);
351
352         table_section_title(_("GL Accounts"));
353
354         gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
355
356         if (!is_service($_POST['mb_flag'])) 
357         {
358                 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
359                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
360                 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
361         }
362         else 
363         {
364                 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
365                 hidden('inventory_account', $_POST['inventory_account']);
366                 hidden('adjustment_account', $_POST['adjustment_account']);
367         }
368
369
370         if (is_manufactured($_POST['mb_flag']))
371                 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
372         else
373                 hidden('assembly_account', $_POST['assembly_account']);
374
375         table_section_title(_("Other"));
376
377         // Add image upload for New Item  - by Joe
378         file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
379         // Add Image upload for New Item  - by Joe
380         $stock_img_link = "";
381         $check_remove_image = false;
382         if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
383                 .item_img_name($_POST['NewStockID']).".jpg")) 
384         {
385          // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
386                 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
387                         "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
388                         ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
389                 $check_remove_image = true;
390         } 
391         else 
392         {
393                 $stock_img_link .= _("No image");
394         }
395
396         label_row("&nbsp;", $stock_img_link);
397         if ($check_remove_image)
398                 check_row(_("Delete Image:"), 'del_image');
399
400         record_status_list_row(_("Item status:"), 'inactive');
401         end_outer_table(1);
402
403         div_start('controls');
404         if (!isset($_POST['NewStockID']) || $new_item) 
405         {
406                 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
407         } 
408         else 
409         {
410                 submit_center_first('addupdate', _("Update Item"), '', 
411                         @$_REQUEST['popup'] ? true : 'default');
412                 submit_return('select', get_post('stock_id'), 
413                         _("Select this items and return to document entry."), 'default');
414                 submit('clone', _("Clone This Item"), true, '', true);
415                 submit('delete', _("Delete This Item"), true, '', true);
416                 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
417         }
418
419         div_end();
420 }
421
422 //-------------------------------------------------------------------------------------------- 
423
424 start_form(true);
425
426 if (db_has_stock_items()) 
427 {
428         start_table(TABLESTYLE_NOBORDER);
429         start_row();
430     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
431           _('New item'), true, check_value('show_inactive'));
432         $new_item = get_post('stock_id')=='';
433         check_cells(_("Show inactive:"), 'show_inactive', null, true);
434         end_row();
435         end_table();
436
437         if (get_post('_show_inactive_update')) {
438                 $Ajax->activate('stock_id');
439                 set_focus('stock_id');
440         }
441 }
442 else
443 {
444         hidden('stock_id', get_post('stock_id'));
445 }
446
447 div_start('details');
448
449 $stock_id = get_post('stock_id');
450 if (!$stock_id)
451         unset($_POST['_tabs_sel']); // force settings tab for new customer
452
453 tabbed_content_start('tabs', array(
454                 'settings' => array(_('&General settings'), $stock_id),
455                 'sales_pricing' => array(_('S&ales Pricing'), 
456                         ($_SESSION["wa_current_user"]->can_access_page('SA_SALESPRICE') ? $stock_id : null)),
457                 'purchase_pricing' => array(_('&Purchasing Pricing'), 
458                         ($_SESSION["wa_current_user"]->can_access_page('SA_PURCHASEPRICING') ? $stock_id : null)),
459                 'standard_cost' => array(_('Standard &Costs'), 
460                         ($_SESSION["wa_current_user"]->can_access_page('SA_STANDARDCOST') ? $stock_id : null)),
461                 'reorder_level' => array(_('&Reorder Levels'), (is_inventory_item($stock_id) && 
462                         $_SESSION["wa_current_user"]->can_access_page('SA_REORDER') ? $stock_id : null)),
463                 'movement' => array(_('&Transactions'), 
464                         ($_SESSION["wa_current_user"]->can_access_page('SA_ITEMSTRANSVIEW') ? $stock_id : null)),
465                 'status' => array(_('&Status'), $stock_id),
466                 //'status' => array(_('&Status'), 
467                 //      ($_SESSION["wa_current_user"]->can_access_page('SA_ITEMSSTATVIEW') ? $stock_id : null)),
468         ));
469         
470         switch (get_post('_tabs_sel')) {
471                 default:
472                 case 'settings':
473                         item_settings($stock_id); 
474                         break;
475                 case 'sales_pricing':
476                         $_GET['stock_id'] = $stock_id;
477                         $_GET['popup'] = 1;
478                         include_once($path_to_root."/inventory/prices.php");
479                         break;
480                 case 'purchase_pricing':
481                         $_GET['stock_id'] = $stock_id;
482                         $_GET['popup'] = 1;
483                         include_once($path_to_root."/inventory/purchasing_data.php");
484                         break;
485                 case 'standard_cost':
486                         $_GET['stock_id'] = $stock_id;
487                         $_GET['popup'] = 1;
488                         include_once($path_to_root."/inventory/cost_update.php");
489                         break;
490                 case 'reorder_level':
491                         if (!is_inventory_item($stock_id))
492                         {
493                                 break;
494                         }       
495                         $_GET['stock_id'] = $stock_id;
496                         $_GET['popup'] = 1;
497                         include_once($path_to_root."/inventory/reorder_level.php");
498                         break;
499                 case 'movement':
500                         $_GET['stock_id'] = $stock_id;
501                         $_GET['popup'] = 1;
502                         include_once($path_to_root."/inventory/inquiry/stock_movements.php");
503                         break;
504                 case 'status':
505                         $_GET['stock_id'] = $stock_id;
506                         $_GET['popup'] = 1;
507                         include_once($path_to_root."/inventory/inquiry/stock_status.php");
508                         break;
509         };
510 br();
511 tabbed_content_end();
512
513 div_end();
514
515
516 hidden('popup', @$_REQUEST['popup']);
517 end_form();
518
519 //------------------------------------------------------------------------------------
520
521 end_page(@$_REQUEST['popup']);
522 ?>