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");
17 if ($SysPrefs->use_popup_windows)
18 $js .= get_js_open_window(900, 500);
19 if (user_use_date_picker())
20 $js .= get_js_date_picker();
22 page(_($help_context = "Items"), @$_REQUEST['popup'], false, "", $js);
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/ui.inc");
26 include_once($path_to_root . "/includes/data_checks.inc");
28 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
30 $user_comp = user_company();
31 $new_item = get_post('stock_id')=='' || get_post('cancel') || get_post('clone');
32 //------------------------------------------------------------------------------------
34 if (isset($_GET['stock_id']))
36 $_POST['stock_id'] = $_GET['stock_id'];
38 $stock_id = get_post('stock_id');
39 if (list_updated('stock_id')) {
40 $_POST['NewStockID'] = $stock_id = get_post('stock_id');
42 $Ajax->activate('details');
43 $Ajax->activate('controls');
46 if (get_post('cancel')) {
47 $_POST['NewStockID'] = $stock_id = $_POST['stock_id'] = '';
49 set_focus('stock_id');
50 $Ajax->activate('_page_body');
52 if (list_updated('category_id') || list_updated('mb_flag')) {
53 $Ajax->activate('details');
56 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '')
58 $stock_id = $_POST['NewStockID'];
59 $result = $_FILES['pic']['error'];
60 $upload_file = 'Yes'; //Assume all is well to start off with
61 $filename = company_path().'/images';
62 if (!file_exists($filename))
66 $filename .= "/".item_img_name($stock_id).".jpg";
68 if ($_FILES['pic']['error'] == UPLOAD_ERR_INI_SIZE) {
69 display_error(_('The file size is over the maximum allowed.'));
72 elseif ($_FILES['pic']['error'] > 0) {
73 display_error(_('Error uploading file.'));
77 //But check for the worst
78 if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
82 //$imagetype = exif_imagetype($_FILES['pic']['tmp_name']);
83 if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG)
85 display_warning( _('Only graphics files can be uploaded'));
88 elseif (!in_array(strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
90 display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
93 elseif ( $_FILES['pic']['size'] > ($SysPrefs->max_image_size * 1024))
95 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $SysPrefs->max_image_size);
98 elseif ( $_FILES['pic']['type'] == "text/plain" )
100 display_warning( _('Only graphics files can be uploaded'));
103 elseif (file_exists($filename))
105 $result = unlink($filename);
108 display_error(_('The existing image could not be removed'));
113 if ($upload_file == 'Yes')
115 $result = move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
117 $Ajax->activate('details');
118 /* EOF Add Image upload for New Item - by Ori */
121 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."));
123 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."));
125 function clear_data()
127 unset($_POST['long_description']);
128 unset($_POST['description']);
129 unset($_POST['category_id']);
130 unset($_POST['tax_type_id']);
131 unset($_POST['units']);
132 unset($_POST['mb_flag']);
133 unset($_POST['NewStockID']);
134 unset($_POST['dimension_id']);
135 unset($_POST['dimension2_id']);
136 unset($_POST['no_sale']);
137 unset($_POST['no_purchase']);
140 //------------------------------------------------------------------------------------
142 if (isset($_POST['addupdate']))
146 if ($upload_file == 'No')
148 if (strlen($_POST['description']) == 0)
151 display_error( _('The item name must be entered.'));
152 set_focus('description');
154 elseif (strlen($_POST['NewStockID']) == 0)
157 display_error( _('The item code cannot be empty'));
158 set_focus('NewStockID');
160 elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") ||
161 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") ||
162 strstr($_POST['NewStockID'], "&") || strstr($_POST['NewStockID'], "\t"))
165 display_error( _('The item code cannot contain any of the following characters - & + OR a space OR quotes'));
166 set_focus('NewStockID');
169 elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
172 display_error( _("This item code is already assigned to stock item or sale kit."));
173 set_focus('NewStockID');
176 if ($input_error != 1)
178 if (check_value('del_image'))
180 $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
181 if (file_exists($filename))
186 { /*so its an existing one */
187 update_item($_POST['NewStockID'], $_POST['description'],
188 $_POST['long_description'], $_POST['category_id'],
189 $_POST['tax_type_id'], get_post('units'),
190 get_post('mb_flag'), $_POST['sales_account'],
191 $_POST['inventory_account'], $_POST['cogs_account'],
192 $_POST['adjustment_account'], $_POST['assembly_account'],
193 $_POST['dimension_id'], $_POST['dimension2_id'],
194 check_value('no_sale'), check_value('editable'), check_value('no_purchase'));
195 update_record_status($_POST['NewStockID'], $_POST['inactive'],
196 'stock_master', 'stock_id');
197 update_record_status($_POST['NewStockID'], $_POST['inactive'],
198 'item_codes', 'item_code');
199 set_focus('stock_id');
200 $Ajax->activate('stock_id'); // in case of status change
201 display_notification(_("Item has been updated."));
206 add_item($_POST['NewStockID'], $_POST['description'],
207 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
208 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
209 $_POST['inventory_account'], $_POST['cogs_account'],
210 $_POST['adjustment_account'], $_POST['assembly_account'],
211 $_POST['dimension_id'], $_POST['dimension2_id'],
212 check_value('no_sale'), check_value('editable'), check_value('no_purchase'));
214 display_notification(_("A new item has been added."));
215 $_POST['stock_id'] = $_POST['NewStockID'] =
216 $_POST['description'] = $_POST['long_description'] = '';
217 $_POST['no_sale'] = $_POST['editable'] = $_POST['no_purchase'] =0;
218 set_focus('NewStockID');
220 $Ajax->activate('_page_body');
224 if (get_post('clone')) {
225 unset($_POST['stock_id']);
227 unset($_POST['inactive']);
228 set_focus('NewStockID');
229 $Ajax->activate('_page_body');
232 //------------------------------------------------------------------------------------
234 function check_usage($stock_id, $dispmsg=true)
236 $msg = item_in_foreign_codes($stock_id);
239 if($dispmsg) display_error($msg);
245 //------------------------------------------------------------------------------------
247 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1)
250 if (check_usage($_POST['NewStockID'])) {
252 $stock_id = $_POST['NewStockID'];
253 delete_item($stock_id);
254 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
255 if (file_exists($filename))
257 display_notification(_("Selected item has been deleted."));
258 $_POST['stock_id'] = '';
260 set_focus('stock_id');
262 $Ajax->activate('_page_body');
266 function item_settings(&$stock_id, $new_item)
268 global $SysPrefs, $path_to_root;
270 start_outer_table(TABLESTYLE2);
274 table_section_title(_("Item"));
276 //------------------------------------------------------------------------------------
279 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
281 $_POST['inactive'] = 0;
284 { // Must be modifying an existing item
285 if (get_post('NewStockID') != get_post('stock_id') || get_post('addupdate')) { // first item display
287 $_POST['NewStockID'] = $_POST['stock_id'];
289 $myrow = get_item($_POST['NewStockID']);
291 $_POST['long_description'] = $myrow["long_description"];
292 $_POST['description'] = $myrow["description"];
293 $_POST['category_id'] = $myrow["category_id"];
294 $_POST['tax_type_id'] = $myrow["tax_type_id"];
295 $_POST['units'] = $myrow["units"];
296 $_POST['mb_flag'] = $myrow["mb_flag"];
298 $_POST['sales_account'] = $myrow['sales_account'];
299 $_POST['inventory_account'] = $myrow['inventory_account'];
300 $_POST['cogs_account'] = $myrow['cogs_account'];
301 $_POST['adjustment_account'] = $myrow['adjustment_account'];
302 $_POST['assembly_account'] = $myrow['assembly_account'];
303 $_POST['dimension_id'] = $myrow['dimension_id'];
304 $_POST['dimension2_id'] = $myrow['dimension2_id'];
305 $_POST['no_sale'] = $myrow['no_sale'];
306 $_POST['no_purchase'] = $myrow['no_purchase'];
307 $_POST['del_image'] = 0;
308 $_POST['inactive'] = $myrow["inactive"];
309 $_POST['editable'] = $myrow["editable"];
311 label_row(_("Item Code:"),$_POST['NewStockID']);
312 hidden('NewStockID', $_POST['NewStockID']);
313 set_focus('description');
316 text_row(_("Name:"), 'description', null, 52, 200);
318 textarea_row(_('Description:'), 'long_description', null, 42, 3);
320 stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item);
322 if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
324 $category_record = get_item_category($_POST['category_id']);
326 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
327 $_POST['units'] = $category_record["dflt_units"];
328 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
329 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
330 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
331 $_POST['sales_account'] = $category_record["dflt_sales_act"];
332 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
333 $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
334 $_POST['dimension_id'] = $category_record["dflt_dim1"];
335 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
336 $_POST['no_sale'] = $category_record["dflt_no_sale"];
337 $_POST['no_purchase'] = $category_record["dflt_no_purchase"];
338 $_POST['editable'] = 0;
341 $fresh_item = !isset($_POST['NewStockID']) || $new_item
342 || check_usage($_POST['stock_id'],false);
344 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
346 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, $fresh_item);
348 stock_units_list_row(_('Units of Measure:'), 'units', null, $fresh_item);
350 check_row(_("Editable description:"), 'editable');
352 check_row(_("Exclude from sales:"), 'no_sale');
354 check_row(_("Exclude from purchases:"), 'no_purchase');
358 $dim = get_company_pref('use_dimension');
361 table_section_title(_("Dimensions"));
363 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
365 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
368 hidden('dimension_id', 0);
370 hidden('dimension2_id', 0);
372 table_section_title(_("GL Accounts"));
374 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
376 if (!is_service($_POST['mb_flag']))
378 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
379 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
380 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
384 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
385 hidden('inventory_account', $_POST['inventory_account']);
386 hidden('adjustment_account', $_POST['adjustment_account']);
390 if (is_manufactured($_POST['mb_flag']))
391 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
393 hidden('assembly_account', $_POST['assembly_account']);
395 table_section_title(_("Other"));
397 // Add image upload for New Item - by Joe
398 file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
399 // Add Image upload for New Item - by Joe
400 $stock_img_link = "";
401 $check_remove_image = false;
402 if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
403 .item_img_name($_POST['NewStockID']).".jpg"))
405 // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
406 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
407 "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
408 ".jpg?nocache=".rand()."'"." height='".$SysPrefs->pic_height."' border='0'>";
409 $check_remove_image = true;
413 $stock_img_link .= _("No image");
416 label_row(" ", $stock_img_link);
417 if ($check_remove_image)
418 check_row(_("Delete Image:"), 'del_image');
420 record_status_list_row(_("Item status:"), 'inactive');
423 div_start('controls');
424 if (!isset($_POST['NewStockID']) || $new_item)
426 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
430 submit_center_first('addupdate', _("Update Item"), '',
431 @$_REQUEST['popup'] ? true : 'default');
432 submit_return('select', get_post('stock_id'),
433 _("Select this items and return to document entry."), 'default');
434 submit('clone', _("Clone This Item"), true, '', true);
435 submit('delete', _("Delete This Item"), true, '', true);
436 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
442 //--------------------------------------------------------------------------------------------
446 if (db_has_stock_items())
448 start_table(TABLESTYLE_NOBORDER);
450 stock_items_list_cells(_("Select an item:"), 'stock_id', null,
451 _('New item'), true, check_value('show_inactive'));
452 $new_item = get_post('stock_id')=='';
453 check_cells(_("Show inactive:"), 'show_inactive', null, true);
457 if (get_post('_show_inactive_update')) {
458 $Ajax->activate('stock_id');
459 set_focus('stock_id');
464 hidden('stock_id', get_post('stock_id'));
467 div_start('details');
469 $stock_id = get_post('stock_id');
471 unset($_POST['_tabs_sel']); // force settings tab for new customer
473 tabbed_content_start('tabs', array(
474 'settings' => array(_('&General settings'), $stock_id),
475 'sales_pricing' => array(_('S&ales Pricing'), $stock_id),
476 'purchase_pricing' => array(_('&Purchasing Pricing'), $stock_id),
477 'standard_cost' => array(_('Standard &Costs'), $stock_id),
478 'reorder_level' => array(_('&Reorder Levels'), (is_inventory_item($stock_id) ? $stock_id : null)),
479 'movement' => array(_('&Transactions'), $stock_id),
480 'status' => array(_('&Status'), $stock_id),
483 switch (get_post('_tabs_sel')) {
486 item_settings($stock_id, $new_item);
488 case 'sales_pricing':
489 $_GET['stock_id'] = $stock_id;
491 include_once($path_to_root."/inventory/prices.php");
493 case 'purchase_pricing':
494 $_GET['stock_id'] = $stock_id;
496 include_once($path_to_root."/inventory/purchasing_data.php");
498 case 'standard_cost':
499 $_GET['stock_id'] = $stock_id;
501 include_once($path_to_root."/inventory/cost_update.php");
503 case 'reorder_level':
504 if (!is_inventory_item($stock_id))
508 $_GET['stock_id'] = $stock_id;
510 include_once($path_to_root."/inventory/reorder_level.php");
513 $_GET['stock_id'] = $stock_id;
515 include_once($path_to_root."/inventory/inquiry/stock_movements.php");
518 $_GET['stock_id'] = $stock_id;
520 include_once($path_to_root."/inventory/inquiry/stock_status.php");
524 tabbed_content_end();
528 hidden('popup', @$_REQUEST['popup']);
531 //------------------------------------------------------------------------------------
533 end_page(@$_REQUEST['popup']);