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");
16 if ($use_popup_windows)
17 $js .= get_js_open_window(900, 500);
19 page(_($help_context = "Items"), @$_REQUEST['popup'], false, "", $js);
21 include_once($path_to_root . "/includes/date_functions.inc");
22 include_once($path_to_root . "/includes/ui.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
27 $user_comp = user_company();
28 $new_item = get_post('stock_id')=='' || get_post('cancel') || get_post('clone');
29 //------------------------------------------------------------------------------------
31 if (isset($_GET['stock_id']))
33 $_POST['stock_id'] = $stock_id = $_GET['stock_id'];
35 elseif (isset($_POST['stock_id']))
37 $stock_id = $_POST['stock_id'];
39 if (list_updated('stock_id')) {
40 $_POST['NewStockID'] = get_post('stock_id');
42 $Ajax->activate('details');
43 $Ajax->activate('controls');
46 if (get_post('cancel')) {
47 $_POST['NewStockID'] = $_POST['stock_id'] = '';
49 set_focus('stock_id');
50 $Ajax->activate('_page_body');
53 if (list_updated('category_id') || list_updated('mb_flag')) {
54 $Ajax->activate('details');
57 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '')
59 $stock_id = $_POST['NewStockID'];
60 $result = $_FILES['pic']['error'];
61 $upload_file = 'Yes'; //Assume all is well to start off with
62 $filename = company_path().'/images';
63 if (!file_exists($filename))
67 $filename .= "/".item_img_name($stock_id).".jpg";
69 //But check for the worst
70 if ((list($width, $height, $type, $attr) = getimagesize($_FILES['pic']['tmp_name'])) !== false)
74 //$imagetype = exif_imagetype($_FILES['pic']['tmp_name']);
75 if ($imagetype != IMAGETYPE_GIF && $imagetype != IMAGETYPE_JPEG && $imagetype != IMAGETYPE_PNG)
77 display_warning( _('Only graphics files can be uploaded'));
80 elseif (@strtoupper(substr(trim($_FILES['pic']['name']), @in_array(strlen($_FILES['pic']['name']) - 3)), array('JPG','PNG','GIF')))
82 display_warning(_('Only graphics files are supported - a file extension of .jpg, .png or .gif is expected'));
85 elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024))
87 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
90 elseif (file_exists($filename))
92 $result = unlink($filename);
95 display_error(_('The existing image could not be removed'));
100 if ($upload_file == 'Yes')
102 $result = move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
104 $Ajax->activate('details');
105 /* EOF Add Image upload for New Item - by Ori */
108 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."));
110 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."));
112 function clear_data()
114 unset($_POST['long_description']);
115 unset($_POST['description']);
116 unset($_POST['category_id']);
117 unset($_POST['tax_type_id']);
118 unset($_POST['units']);
119 unset($_POST['mb_flag']);
120 unset($_POST['NewStockID']);
121 unset($_POST['dimension_id']);
122 unset($_POST['dimension2_id']);
123 unset($_POST['no_sale']);
126 //------------------------------------------------------------------------------------
128 if (isset($_POST['addupdate']))
132 if ($upload_file == 'No')
134 if (strlen($_POST['description']) == 0)
137 display_error( _('The item name must be entered.'));
138 set_focus('description');
140 elseif (strlen($_POST['NewStockID']) == 0)
143 display_error( _('The item code cannot be empty'));
144 set_focus('NewStockID');
146 elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") ||
147 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") ||
148 strstr($_POST['NewStockID'], "&") || strstr($_POST['NewStockID'], "\t"))
151 display_error( _('The item code cannot contain any of the following characters - & + OR a space OR quotes'));
152 set_focus('NewStockID');
155 elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
158 display_error( _("This item code is already assigned to stock item or sale kit."));
159 set_focus('NewStockID');
162 if ($input_error != 1)
164 if (check_value('del_image'))
166 $filename = company_path().'/images/'.item_img_name($_POST['NewStockID']).".jpg";
167 if (file_exists($filename))
172 { /*so its an existing one */
173 update_item($_POST['NewStockID'], $_POST['description'],
174 $_POST['long_description'], $_POST['category_id'],
175 $_POST['tax_type_id'], get_post('units'),
176 get_post('mb_flag'), $_POST['sales_account'],
177 $_POST['inventory_account'], $_POST['cogs_account'],
178 $_POST['adjustment_account'], $_POST['assembly_account'],
179 $_POST['dimension_id'], $_POST['dimension2_id'],
180 check_value('no_sale'), check_value('editable'));
181 update_record_status($_POST['NewStockID'], $_POST['inactive'],
182 'stock_master', 'stock_id');
183 update_record_status($_POST['NewStockID'], $_POST['inactive'],
184 'item_codes', 'item_code');
185 set_focus('stock_id');
186 $Ajax->activate('stock_id'); // in case of status change
187 display_notification(_("Item has been updated."));
192 add_item($_POST['NewStockID'], $_POST['description'],
193 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
194 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
195 $_POST['inventory_account'], $_POST['cogs_account'],
196 $_POST['adjustment_account'], $_POST['assembly_account'],
197 $_POST['dimension_id'], $_POST['dimension2_id'],
198 check_value('no_sale'), check_value('editable'));
200 display_notification(_("A new item has been added."));
201 $_POST['stock_id'] = $_POST['NewStockID'] =
202 $_POST['description'] = $_POST['long_description'] = '';
203 $_POST['no_sale'] = $_POST['editable'] = 0;
204 set_focus('NewStockID');
206 $Ajax->activate('_page_body');
210 if (get_post('clone')) {
211 unset($_POST['stock_id']);
212 unset($_POST['inactive']);
213 set_focus('NewStockID');
214 $Ajax->activate('_page_body');
217 //------------------------------------------------------------------------------------
219 function check_usage($stock_id, $dispmsg=true)
221 $msg = item_in_foreign_codes($stock_id);
224 if($dispmsg) display_error($msg);
230 //------------------------------------------------------------------------------------
232 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1)
235 if (check_usage($_POST['NewStockID'])) {
237 $stock_id = $_POST['NewStockID'];
238 delete_item($stock_id);
239 $filename = company_path().'/images/'.item_img_name($stock_id).".jpg";
240 if (file_exists($filename))
242 display_notification(_("Selected item has been deleted."));
243 $_POST['stock_id'] = '';
245 set_focus('stock_id');
247 $Ajax->activate('_page_body');
250 //--------------------------------------------------------------------------------------------
254 if (db_has_stock_items())
256 start_table(TABLESTYLE_NOBORDER);
258 stock_items_list_cells(_("Select an item:"), 'stock_id', null,
259 _('New item'), true, check_value('show_inactive'));
260 $new_item = get_post('stock_id')=='';
261 check_cells(_("Show inactive:"), 'show_inactive', null, true);
265 if (get_post('_show_inactive_update')) {
266 $Ajax->activate('stock_id');
267 set_focus('stock_id');
271 div_start('details');
272 start_outer_table(TABLESTYLE2);
276 table_section_title(_("Item"));
278 //------------------------------------------------------------------------------------
281 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
283 $_POST['inactive'] = 0;
286 { // Must be modifying an existing item
287 if (get_post('NewStockID') != get_post('stock_id') || get_post('addupdate')) { // first item display
289 $_POST['NewStockID'] = $_POST['stock_id'];
291 $myrow = get_item($_POST['NewStockID']);
293 $_POST['long_description'] = $myrow["long_description"];
294 $_POST['description'] = $myrow["description"];
295 $_POST['category_id'] = $myrow["category_id"];
296 $_POST['tax_type_id'] = $myrow["tax_type_id"];
297 $_POST['units'] = $myrow["units"];
298 $_POST['mb_flag'] = $myrow["mb_flag"];
300 $_POST['sales_account'] = $myrow['sales_account'];
301 $_POST['inventory_account'] = $myrow['inventory_account'];
302 $_POST['cogs_account'] = $myrow['cogs_account'];
303 $_POST['adjustment_account'] = $myrow['adjustment_account'];
304 $_POST['assembly_account'] = $myrow['assembly_account'];
305 $_POST['dimension_id'] = $myrow['dimension_id'];
306 $_POST['dimension2_id'] = $myrow['dimension2_id'];
307 $_POST['no_sale'] = $myrow['no_sale'];
308 $_POST['del_image'] = 0;
309 $_POST['inactive'] = $myrow["inactive"];
310 $_POST['editable'] = $myrow["editable"];
312 label_row(_("Item Code:"),$_POST['NewStockID']);
313 hidden('NewStockID', $_POST['NewStockID']);
314 set_focus('description');
317 text_row(_("Name:"), 'description', null, 52, 200);
319 textarea_row(_('Description:'), 'long_description', null, 42, 3);
321 stock_categories_list_row(_("Category:"), 'category_id', null, false, $new_item);
323 if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
325 $category_record = get_item_category($_POST['category_id']);
327 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
328 $_POST['units'] = $category_record["dflt_units"];
329 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
330 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
331 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
332 $_POST['sales_account'] = $category_record["dflt_sales_act"];
333 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
334 $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
335 $_POST['dimension_id'] = $category_record["dflt_dim1"];
336 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
337 $_POST['no_sale'] = $category_record["dflt_no_sale"];
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');
356 if (!@$_REQUEST['popup'])
359 echo '<td class="label"> </td><td>';
360 echo viewer_link('<b>'. _('Inventory Item Movement').'</b>', "inventory/inquiry/stock_movements.php?stock_id=$stock_id&popup=1");
364 echo '<td class="label"> </td><td>';
365 echo viewer_link('<b>'. _('Inventory Item Status').'</b>', "inventory/inquiry/stock_status.php?stock_id=$stock_id&popup=1");
373 $dim = get_company_pref('use_dimension');
376 table_section_title(_("Dimensions"));
378 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
380 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
383 hidden('dimension_id', 0);
385 hidden('dimension2_id', 0);
387 table_section_title(_("GL Accounts"));
389 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
391 if (!is_service($_POST['mb_flag']))
393 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
394 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
395 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
399 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
400 hidden('inventory_account', $_POST['inventory_account']);
401 hidden('adjustment_account', $_POST['adjustment_account']);
405 if (is_manufactured($_POST['mb_flag']))
406 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
408 hidden('assembly_account', $_POST['assembly_account']);
410 table_section_title(_("Other"));
412 // Add image upload for New Item - by Joe
413 file_row(_("Image File (.jpg)") . ":", 'pic', 'pic');
414 // Add Image upload for New Item - by Joe
415 $stock_img_link = "";
416 $check_remove_image = false;
417 if (isset($_POST['NewStockID']) && file_exists(company_path().'/images/'
418 .item_img_name($_POST['NewStockID']).".jpg"))
420 // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
421 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
422 "]' src='".company_path().'/images/'.item_img_name($_POST['NewStockID']).
423 ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
424 $check_remove_image = true;
428 $stock_img_link .= _("No image");
431 label_row(" ", $stock_img_link);
432 if ($check_remove_image)
433 check_row(_("Delete Image:"), 'del_image');
435 record_status_list_row(_("Item status:"), 'inactive');
438 div_start('controls');
439 if (!isset($_POST['NewStockID']) || $new_item)
441 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
445 submit_center_first('addupdate', _("Update Item"), '',
446 @$_REQUEST['popup'] ? true : 'default');
447 submit_return('select', get_post('stock_id'),
448 _("Select this items and return to document entry."), 'default');
449 submit('clone', _("Clone This Item"), true, '', true);
450 submit('delete', _("Delete This Item"), true, '', true);
451 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
455 hidden('popup', @$_REQUEST['popup']);
458 //------------------------------------------------------------------------------------