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 ***********************************************************************/
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
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");
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
24 $user_comp = user_company();
25 $new_item = get_post('stock_id')=='' || get_post('cancel');
26 //------------------------------------------------------------------------------------
28 if (isset($_GET['stock_id']))
30 $_POST['stock_id'] = $stock_id = strtoupper($_GET['stock_id']);
32 else if (isset($_POST['stock_id']))
34 $stock_id = strtoupper($_POST['stock_id']);
37 if (list_updated('stock_id')) {
38 $_POST['NewStockID'] = get_post('stock_id');
40 $Ajax->activate('details');
41 $Ajax->activate('controls');
44 if (get_post('cancel')) {
45 $_POST['NewStockID'] = $_POST['stock_id'] = '';
47 set_focus('stock_id');
48 $Ajax->activate('_page_body');
51 if (list_updated('category_id') || list_updated('mb_flag')) {
52 $Ajax->activate('details');
55 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '')
57 $result = $_FILES['pic']['error'];
58 $upload_file = 'Yes'; //Assume all is well to start off with
59 $filename = $comp_path . "/$user_comp/images";
60 if (!file_exists($filename))
64 $filename .= "/$stock_id.jpg";
66 //But check for the worst
67 if (strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)) != 'JPG')
69 display_warning(_('Only jpg files are supported - a file extension of .jpg is expected'));
72 elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024))
74 display_warning(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
77 elseif ( $_FILES['pic']['type'] == "text/plain" )
79 display_warning( _('Only graphics files can be uploaded'));
82 elseif (file_exists($filename))
84 $result = unlink($filename);
87 display_error(_('The existing image could not be removed'));
92 if ($upload_file == 'Yes')
94 $result = move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
96 /* EOF Add Image upload for New Item - by Ori */
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."));
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."));
104 function clear_data()
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']);
117 //------------------------------------------------------------------------------------
119 if (isset($_POST['addupdate']))
123 if ($upload_file == 'No')
125 if (strlen($_POST['description']) == 0)
128 display_error( _('The item name must be entered.'));
129 set_focus('description');
131 elseif (strlen($_POST['NewStockID']) == 0)
134 display_error( _('The item code cannot be empty'));
135 set_focus('NewStockID');
137 elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") ||
138 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") ||
139 strstr($_POST['NewStockID'], "&"))
142 display_error( _('The item code cannot contain any of the following characters - & + OR a space OR quotes'));
143 set_focus('NewStockID');
146 elseif ($new_item && db_num_rows(get_item_kit($_POST['NewStockID'])))
149 display_error( _("This item code is already assigned to stock item or sale kit."));
150 set_focus('NewStockID');
153 if ($input_error != 1)
157 { /*so its an existing one */
159 update_item($_POST['NewStockID'], $_POST['description'],
160 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
161 $_POST['sales_account'], $_POST['inventory_account'], $_POST['cogs_account'],
162 $_POST['adjustment_account'], $_POST['assembly_account'],
163 $_POST['dimension_id'], $_POST['dimension2_id']);
165 display_notification(_("Item has been updated."));
170 add_item($_POST['NewStockID'], $_POST['description'],
171 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
172 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
173 $_POST['inventory_account'], $_POST['cogs_account'],
174 $_POST['adjustment_account'], $_POST['assembly_account'],
175 $_POST['dimension_id'], $_POST['dimension2_id']);
177 display_notification(_("A new item has been added."));
178 $_POST['stock_id'] = $_POST['NewStockID'] = '';
180 set_focus('stock_id');
181 $Ajax->activate('_page_body');
185 //------------------------------------------------------------------------------------
187 function can_delete($stock_id)
189 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE stock_id='$stock_id'";
190 $result = db_query($sql, "could not query stock moves");
191 $myrow = db_fetch_row($result);
194 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
198 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE component='$stock_id'";
199 $result = db_query($sql, "could not query boms");
200 $myrow = db_fetch_row($result);
203 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
207 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_order_details WHERE stk_code='$stock_id'";
208 $result = db_query($sql, "could not query sales orders");
209 $myrow = db_fetch_row($result);
212 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
216 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_order_details WHERE item_code='$stock_id'";
217 $result = db_query($sql, "could not query purchase orders");
218 $myrow = db_fetch_row($result);
221 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
224 $kits = get_where_used($stock_id);
225 $num_kits = db_num_rows($kits);
227 $msg = _("This item cannot be deleted because some code aliases
228 or foreign codes was entered for it, or there are kits defined
229 using this item as component")
233 $kit = db_fetch($kits);
234 $msg .= "'".$kit[0]."'";
235 if ($num_kits) $msg .= ',';
243 //------------------------------------------------------------------------------------
245 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1)
248 if (can_delete($_POST['NewStockID'])) {
250 $stock_id = $_POST['NewStockID'];
251 delete_item($stock_id);
252 $filename = $comp_path . "/$user_comp/images/$stock_id.jpg";
253 if (file_exists($filename))
255 display_notification(_("Selected item has been deleted."));
256 $_POST['stock_id'] = '';
258 set_focus('stock_id');
260 $Ajax->activate('_page_body');
263 //--------------------------------------------------------------------------------------------
265 if (isset($_POST['select']))
267 context_return(array('stock_id' => $_POST['stock_id']));
271 //------------------------------------------------------------------------------------
275 if (db_has_stock_items())
277 start_table("class='tablestyle_noborder'");
279 stock_items_list_cells(_("Select an item:"), 'stock_id', null,
280 _('New item'), true);
281 $new_item = get_post('stock_id')=='';
286 div_start('details');
287 start_outer_table($table_style2, 5);
291 table_section_title(_("Item"));
293 //------------------------------------------------------------------------------------
297 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
300 { // Must be modifying an existing item
301 $_POST['NewStockID'] = $_POST['stock_id'];
303 $myrow = get_item($_POST['NewStockID']);
305 $_POST['long_description'] = $myrow["long_description"];
306 $_POST['description'] = $myrow["description"];
307 $_POST['category_id'] = $myrow["category_id"];
308 $_POST['tax_type_id'] = $myrow["tax_type_id"];
309 $_POST['units'] = $myrow["units"];
310 $_POST['mb_flag'] = $myrow["mb_flag"];
312 $_POST['sales_account'] = $myrow['sales_account'];
313 $_POST['inventory_account'] = $myrow['inventory_account'];
314 $_POST['cogs_account'] = $myrow['cogs_account'];
315 $_POST['adjustment_account'] = $myrow['adjustment_account'];
316 $_POST['assembly_account'] = $myrow['assembly_account'];
317 $_POST['dimension_id'] = $myrow['dimension_id'];
318 $_POST['dimension2_id'] = $myrow['dimension2_id'];
320 label_row(_("Item Code:"),$_POST['NewStockID']);
321 hidden('NewStockID', $_POST['NewStockID']);
322 set_focus('description');
325 text_row(_("Name:"), 'description', null, 52, 50);
327 textarea_row(_('Description:'), 'long_description', null, 42, 3);
329 stock_categories_list_row(_("Category:"), 'category_id', null, $new_item);
331 if ($new_item && (list_updated('category_id') || !isset($_POST['units']))) {
333 $category_record = get_item_category($_POST['category_id']);
335 $_POST['tax_type_id'] = $category_record["dflt_tax_type"];
336 $_POST['units'] = $category_record["dflt_units"];
337 $_POST['mb_flag'] = $category_record["dflt_mb_flag"];
338 $_POST['inventory_account'] = $category_record["dflt_inventory_act"];
339 $_POST['cogs_account'] = $category_record["dflt_cogs_act"];
340 $_POST['sales_account'] = $category_record["dflt_sales_act"];
341 $_POST['adjustment_account'] = $category_record["dflt_adjustment_act"];
342 $_POST['assembly_account'] = $category_record["dflt_assembly_act"];
343 $_POST['dimension_id'] = $category_record["dflt_dim1"];
344 $_POST['dimension2_id'] = $category_record["dflt_dim2"];
347 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
349 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
350 (!isset($_POST['NewStockID']) || $new_item));
352 stock_units_list_row(_('Units of Measure:'), 'units', null,
353 (!isset($_POST['NewStockID']) || $new_item));
355 $dim = get_company_pref('use_dimension');
358 table_section_title(_("Dimensions"));
360 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
362 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
365 hidden('dimension_id', 0);
367 hidden('dimension2_id', 0);
371 table_section_title(_("GL Accounts"));
373 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
375 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
377 if (!is_service($_POST['mb_flag']))
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 hidden('cogs_account', $_POST['cogs_account']);
385 hidden('adjustment_account', $_POST['adjustment_account']);
389 if (is_manufactured($_POST['mb_flag']))
390 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
392 hidden('assembly_account', $_POST['assembly_account']);
394 table_section_title(_("Picture"));
396 // Add image upload for New Item - by Joe
397 label_row(_("Image File (.jpg)") . ":", "<input type='file' id='pic' name='pic'>");
398 // Add Image upload for New Item - by Joe
399 $stock_img_link = "";
400 if (isset($_POST['NewStockID']) && file_exists("$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg"))
402 // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
403 $stock_img_link .= "<img id='item_img' alt = '[".$_POST['NewStockID'].".jpg".
404 "]' src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg?nocache=".rand()."'".
405 " width='$pic_width' height='$pic_height' border='0'>";
409 $stock_img_link .= _("No image");
412 label_row(" ", $stock_img_link);
416 div_start('controls');
417 if (!isset($_POST['NewStockID']) || $new_item)
419 submit_center('addupdate', _("Insert New Item"), true, '', 'default');
423 submit_center_first('addupdate', _("Update Item"), '',
424 count($_SESSION['Context']) ? true : 'default');
425 submit_return('select', _("Return"), _("Select this items and return to document entry."), 'default');
426 submit('delete', _("Delete This Item"), true, '', true);
427 submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
433 //------------------------------------------------------------------------------------