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