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