Item type dispaincorrectd after change saving.
[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 (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 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."));
101
102 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."));
103
104 function clear_data()
105 {
106         unset($_POST['long_description']);
107         unset($_POST['description']);
108         unset($_POST['category_id']);
109         unset($_POST['tax_type_id']);
110         unset($_POST['units']);
111         unset($_POST['mb_flag']);
112         unset($_POST['NewStockID']);
113         unset($_POST['dimension_id']);
114         unset($_POST['dimension2_id']);
115         unset($_POST['no_sale']);
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 = company_path().'/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                         update_item($_POST['NewStockID'], $_POST['description'],
166                                 $_POST['long_description'], $_POST['category_id'], 
167                                 $_POST['tax_type_id'], get_post('units'),
168                                 get_post('mb_flag'), $_POST['sales_account'],
169                                 $_POST['inventory_account'], $_POST['cogs_account'],
170                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
171                                 $_POST['dimension_id'], $_POST['dimension2_id'],
172                                 check_value('no_sale'), check_value('editable'));
173                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
174                                 'stock_master', 'stock_id');
175                         update_record_status($_POST['NewStockID'], $_POST['inactive'],
176                                 'item_codes', 'item_code');
177                         set_focus('stock_id');
178                         $Ajax->activate('stock_id'); // in case of status change
179                         display_notification(_("Item has been updated."));
180                 } 
181                 else 
182                 { //it is a NEW part
183
184                         add_item($_POST['NewStockID'], $_POST['description'],
185                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
186                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
187                                 $_POST['inventory_account'], $_POST['cogs_account'],
188                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
189                                 $_POST['dimension_id'], $_POST['dimension2_id'],
190                                 check_value('no_sale'), check_value('editable'));
191
192                         display_notification(_("A new item has been added."));
193                         $_POST['stock_id'] = $_POST['NewStockID'] = 
194                         $_POST['description'] = $_POST['long_description'] = '';
195                         $_POST['no_sale'] = $_POST['editable'] = 0;
196                         set_focus('NewStockID');
197                 }
198                 $Ajax->activate('_page_body');
199         }
200 }
201
202 if (get_post('clone')) {
203         unset($_POST['stock_id']);
204         unset($_POST['inactive']);
205         set_focus('NewStockID');
206         $Ajax->activate('_page_body');
207 }
208
209 //------------------------------------------------------------------------------------
210
211 function check_usage($stock_id, $dispmsg=true)
212 {
213         $msg = item_in_foreign_codes($stock_id);
214
215         if ($msg != '') {
216                 if($dispmsg) display_error($msg);
217                 return false;
218         }
219         return true;
220 }
221
222 //------------------------------------------------------------------------------------
223
224 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
225 {
226
227         if (check_usage($_POST['NewStockID'])) {
228
229                 $stock_id = $_POST['NewStockID'];
230                 delete_item($stock_id);
231                 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
232                 if (file_exists($filename))
233                         unlink($filename);
234                 display_notification(_("Selected item has been deleted."));
235                 $_POST['stock_id'] = '';
236                 clear_data();
237                 set_focus('stock_id');
238                 $new_item = true;
239                 $Ajax->activate('_page_body');
240         }
241 }
242 //-------------------------------------------------------------------------------------------- 
243
244 start_form(true);
245
246 if (db_has_stock_items()) 
247 {
248         start_table(TABLESTYLE_NOBORDER);
249         start_row();
250     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
251           _('New item'), true, check_value('show_inactive'));
252         $new_item = get_post('stock_id')=='';
253         check_cells(_("Show inactive:"), 'show_inactive', null, true);
254         end_row();
255         end_table();
256
257         if (get_post('_show_inactive_update')) {
258                 $Ajax->activate('stock_id');
259                 set_focus('stock_id');
260         }
261 }
262
263 div_start('details');
264 start_outer_table(TABLESTYLE2);
265
266 table_section(1);
267
268 table_section_title(_("Item"));
269
270 //------------------------------------------------------------------------------------
271 if ($new_item) 
272 {
273         text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
274
275         $_POST['inactive'] = 0;
276
277 else 
278 { // Must be modifying an existing item
279         if (get_post('NewStockID') != get_post('stock_id')) { // first item display
280
281                 $_POST['NewStockID'] = $_POST['stock_id'];
282
283                 $myrow = get_item($_POST['NewStockID']);
284
285                 $_POST['long_description'] = $myrow["long_description"];
286                 $_POST['description'] = $myrow["description"];
287                 $_POST['category_id']  = $myrow["category_id"];
288                 $_POST['tax_type_id']  = $myrow["tax_type_id"];
289                 $_POST['units']  = $myrow["units"];
290                 $_POST['mb_flag']  = $myrow["mb_flag"];
291
292                 $_POST['sales_account'] =  $myrow['sales_account'];
293                 $_POST['inventory_account'] = $myrow['inventory_account'];
294                 $_POST['cogs_account'] = $myrow['cogs_account'];
295                 $_POST['adjustment_account']    = $myrow['adjustment_account'];
296                 $_POST['assembly_account']      = $myrow['assembly_account'];
297                 $_POST['dimension_id']  = $myrow['dimension_id'];
298                 $_POST['dimension2_id'] = $myrow['dimension2_id'];
299                 $_POST['no_sale']       = $myrow['no_sale'];
300                 $_POST['del_image'] = 0;        
301                 $_POST['inactive'] = $myrow["inactive"];
302                 $_POST['editable'] = $myrow["editable"];
303         }
304         label_row(_("Item Code:"),$_POST['NewStockID']);
305         hidden('NewStockID', $_POST['NewStockID']);
306         set_focus('description');
307 }
308
309 text_row(_("Name:"), 'description', null, 52, 200);
310
311 textarea_row(_('Description:'), 'long_description', null, 42, 3);
312
313 stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item);
314
315 if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
316
317         $category_record = get_item_category($_POST['category_id']);
318
319         $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
320         $_POST['units'] = $category_record["dflt_units"];
321         $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
322         $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
323         $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
324         $_POST['sales_account'] = $category_record["dflt_sales_act"];
325         $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
326         $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
327         $_POST['dimension_id'] = $category_record["dflt_dim1"];
328         $_POST['dimension2_id'] = $category_record["dflt_dim2"];
329         $_POST['no_sale'] = $category_record["dflt_no_sale"];
330         $_POST['editable'] = 0;
331
332 }
333 $fresh_item = !isset($_POST['NewStockID']) || $new_item 
334         || check_usage($_POST['stock_id'],false);
335
336 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
337
338 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, $fresh_item);
339
340 stock_units_list_row(_('Units of Measure:'), 'units', null, $fresh_item);
341
342 check_row(_("Editable description:"), 'editable');
343
344 check_row(_("Exclude from sales:"), 'no_sale');
345
346 table_section(2);
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_title(_("GL Accounts"));
363
364 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
365
366 if (!is_service($_POST['mb_flag'])) 
367 {
368         gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
369         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
370         gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
371 }
372 else 
373 {
374         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
375         hidden('inventory_account', $_POST['inventory_account']);
376         hidden('adjustment_account', $_POST['adjustment_account']);
377 }
378
379
380 if (is_manufactured($_POST['mb_flag']))
381         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
382 else
383         hidden('assembly_account', $_POST['assembly_account']);
384
385 table_section_title(_("Other"));
386
387 // Add image upload for New Item  - by Joe
388 file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
389 // Add Image upload for New Item  - by Joe
390 $stock_img_link = "";
391 $check_remove_image = false;
392 if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
393         .item_img_name($_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='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
398                 ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
399         $check_remove_image = true;
400
401 else 
402 {
403         $stock_img_link .= _("No image");
404 }
405
406 label_row("&nbsp;", $stock_img_link);
407 if ($check_remove_image)
408         check_row(_("Delete Image:"), 'del_image');
409         
410 record_status_list_row(_("Item status:"), 'inactive');
411 end_outer_table(1);
412 div_end();
413 div_start('controls');
414 if (!isset($_POST['NewStockID']) || $new_item) 
415 {
416         submit_center('addupdate', _("Insert New Item"), true, '', 'default');
417
418 else 
419 {
420         submit_center_first('addupdate', _("Update Item"), '', 
421                 @$_REQUEST['popup'] ? true : 'default');
422         submit_return('select', get_post('stock_id'), 
423                 _("Select this items and return to document entry."), 'default');
424         submit('clone', _("Clone This Item"), true, '', true);
425         submit('delete', _("Delete This Item"), true, '', true);
426         submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
427 }
428
429 div_end();
430 hidden('popup', @$_REQUEST['popup']);
431 end_form();
432
433 //------------------------------------------------------------------------------------
434
435 end_page();
436 ?>