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