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