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