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