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