5 include($path_to_root . "/includes/session.inc");
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/ui.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
15 $user_comp = user_company();
16 //------------------------------------------------------------------------------------
18 if (isset($_GET['stock_id']))
20 $stock_id = strtoupper($_GET['stock_id']);
22 else if (isset($_POST['stock_id']))
24 $stock_id = strtoupper($_POST['stock_id']);
27 if (isset($_GET['New']) || !isset($_POST['NewStockID']))
32 if (isset($_POST['SelectStockItem']))
34 $_POST['NewStockID'] = $_POST['stock_id'];
38 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '')
40 $result = $_FILES['pic']['error'];
41 $upload_file = 'Yes'; //Assume all is well to start off with
42 $filename = $comp_path . "/$user_comp/images";
43 if (!file_exists($filename))
47 $filename .= "/$stock_id.jpg";
49 //But check for the worst
50 if (strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)) != 'JPG')
52 display_notification(_('Only jpg files are supported - a file extension of .jpg is expected'));
55 elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024))
57 display_notification(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
60 elseif ( $_FILES['pic']['type'] == "text/plain" )
62 display_notification( _('Only graphics files can be uploaded'));
65 elseif (file_exists($filename))
67 display_notification(_('Attempting to overwrite an existing item image'));
68 $result = unlink($filename);
71 display_error(_('The existing image could not be removed'));
76 if ($upload_file == 'Yes')
78 $result = move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
79 $message = ($result)?_('File url') ."<a href='$filename'>$filename</a>" : "Somthing is wrong with uploading a file.";
81 /* EOF Add Image upload for New Item - by Ori */
85 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."));
87 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."));
91 unset($_POST['long_description']);
92 unset($_POST['description']);
93 unset($_POST['category_id']);
94 unset($_POST['tax_type_id']);
95 unset($_POST['units']);
96 unset($_POST['mb_flag']);
97 unset($_POST['NewStockID']);
98 unset($_POST['dimension_id']);
99 unset($_POST['dimension2_id']);
103 //------------------------------------------------------------------------------------
105 if (isset($_POST['addupdate']))
109 if ($upload_file == 'No')
111 if (strlen($_POST['description']) == 0)
114 display_error( _('The item name must be entered.'));
115 set_focus('description');
117 elseif (strlen($_POST['NewStockID']) == 0)
120 display_error( _('The item code cannot be empty'));
121 set_focus('NewStockID');
123 elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") ||
124 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") ||
125 strstr($_POST['NewStockID'], "&"))
128 display_error( _('The item code cannot contain any of the following characters - & + OR a space OR quotes'));
129 set_focus('NewStockID');
133 if ($input_error != 1)
136 if (!isset($_POST['New']))
137 { /*so its an existing one */
139 update_item($_POST['NewStockID'], $_POST['description'],
140 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
141 $_POST['sales_account'], $_POST['inventory_account'], $_POST['cogs_account'],
142 $_POST['adjustment_account'], $_POST['assembly_account'],
143 $_POST['dimension_id'], $_POST['dimension2_id']);
149 add_item($_POST['NewStockID'], $_POST['description'],
150 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
151 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
152 $_POST['inventory_account'], $_POST['cogs_account'],
153 $_POST['adjustment_account'], $_POST['assembly_account'],
154 $_POST['dimension_id'], $_POST['dimension2_id']);
156 meta_forward($_SERVER['PHP_SELF']);
160 //------------------------------------------------------------------------------------
162 function can_delete($stock_id)
164 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE stock_id='$stock_id'";
165 $result = db_query($sql, "could not query stock moves");
166 $myrow = db_fetch_row($result);
169 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
173 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE component='$stock_id'";
174 $result = db_query($sql, "could not query boms");
175 $myrow = db_fetch_row($result);
178 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
182 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_order_details WHERE stk_code='$stock_id'";
183 $result = db_query($sql, "could not query sales orders");
184 $myrow = db_fetch_row($result);
187 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
191 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_order_details WHERE item_code='$stock_id'";
192 $result = db_query($sql, "could not query purchase orders");
193 $myrow = db_fetch_row($result);
196 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
203 //------------------------------------------------------------------------------------
205 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1)
208 if (can_delete($_POST['NewStockID'])) {
210 $stock_id = $_POST['NewStockID'];
211 delete_item($stock_id);
212 $filename = $comp_path . "/$user_comp/images/$stock_id.jpg";
213 if (file_exists($filename))
215 meta_forward($_SERVER['PHP_SELF']);
219 //------------------------------------------------------------------------------------
223 if (db_has_stock_items())
225 start_table("class='tablestyle_noborder'");
227 stock_items_list_cells(_("Select an item:"), 'stock_id', null);
228 submit_cells('SelectStockItem', _("Edit Item"));
233 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new item"), "New=1");
236 start_table("$table_style2 width=40%");
238 table_section_title(_("Item"));
240 //------------------------------------------------------------------------------------
242 if (!isset($_POST['NewStockID']) || isset($_POST['New']))
245 /*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*/
247 hidden('New', 'Yes');
249 text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
251 $company_record = get_company_prefs();
253 if (!isset($_POST['inventory_account']) || $_POST['inventory_account'] == "")
254 $_POST['inventory_account'] = $company_record["default_inventory_act"];
256 if (!isset($_POST['cogs_account']) || $_POST['cogs_account'] == "")
257 $_POST['cogs_account'] = $company_record["default_cogs_act"];
259 if (!isset($_POST['sales_account']) || $_POST['sales_account'] == "")
260 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
262 if (!isset($_POST['adjustment_account']) || $_POST['adjustment_account'] == "")
263 $_POST['adjustment_account'] = $company_record["default_adj_act"];
265 if (!isset($_POST['assembly_account']) || $_POST['assembly_account'] == "")
266 $_POST['assembly_account'] = $company_record["default_assembly_act"];
270 { // Must be modifying an existing item
272 if (!isset($_POST['New']))
274 $myrow = get_item($_POST['NewStockID']);
276 $_POST['long_description'] = $myrow["long_description"];
277 $_POST['description'] = $myrow["description"];
278 $_POST['category_id'] = $myrow["category_id"];
279 $_POST['tax_type_id'] = $myrow["tax_type_id"];
280 $_POST['units'] = $myrow["units"];
281 $_POST['mb_flag'] = $myrow["mb_flag"];
283 $_POST['sales_account'] = $myrow['sales_account'];
284 $_POST['inventory_account'] = $myrow['inventory_account'];
285 $_POST['cogs_account'] = $myrow['cogs_account'];
286 $_POST['adjustment_account'] = $myrow['adjustment_account'];
287 $_POST['assembly_account'] = $myrow['assembly_account'];
288 $_POST['dimension_id'] = $myrow['dimension_id'];
289 $_POST['dimension2_id'] = $myrow['dimension2_id'];
291 label_row(_("Item Code:"),$_POST['NewStockID']);
292 hidden('NewStockID', $_POST['NewStockID']);
296 text_row(_("Name:"), 'description', null, 52, 50);
298 textarea_row(_('Description:'), 'long_description', null, 45, 3);
301 start_table("$table_style2 width=40%");
302 // Add image upload for New Item - by Joe
304 label_cells(_("Image File (.jpg)") . ":", "<input type='file' id='pic' name='pic'>");
305 // Add Image upload for New Item - by Joe
306 if (isset($_POST['NewStockID']) && file_exists("$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg"))
308 $stock_img_link = "<img src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg' width='$pic_width' height='$pic_height' border='0'>";
312 $stock_img_link = "No Image";
315 label_cell($stock_img_link, "valign=top align=center rowspan=5");
318 stock_categories_list_row(_("Category:"), 'category_id', null);
320 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
322 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
323 (!isset($_POST['NewStockID']) || isset($_POST['New'])));
325 stock_units_list_row(_('Units of Measure:'), 'units', null,
326 (!isset($_POST['NewStockID']) || isset($_POST['New'])));
328 start_table("$table_style2 width=40%");
330 table_section_title(_("GL Accounts"));
332 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
334 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
336 if (!is_service($_POST['mb_flag']))
338 gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
339 gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
343 hidden('cogs_account', $_POST['cogs_account']);
344 hidden('adjustment_account', $_POST['adjustment_account']);
348 if (is_manufactured($_POST['mb_flag']))
349 gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
351 hidden('assembly_account', $_POST['assembly_account']);
352 $dim = get_company_pref('use_dimension');
355 table_section_title(_("Dimensions"));
357 dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
359 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
362 hidden('dimension_id', 0);
364 hidden('dimension2_id', 0);
368 if (!isset($_POST['NewStockID']) || (isset($_POST['New']) && $_POST['New'] != ""))
370 submit_center('addupdate', _("Insert New Item"));
375 submit_center_first('addupdate', _("Update Item"));
377 submit_center_last('delete', _("Delete This Item"));
383 //------------------------------------------------------------------------------------