Fixed page update after item search.
[fa-stable.git] / inventory / manage / items.php
1 <?php
2
3 $page_security = 11;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Items"));
8
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");
12
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
14
15 $user_comp = user_company();
16 $new_item = get_post('stock_id')==''; 
17 //------------------------------------------------------------------------------------
18
19 if (isset($_GET['stock_id']))
20 {
21         $stock_id = strtoupper($_GET['stock_id']);
22 }
23 else if (isset($_POST['stock_id']))
24 {
25         $stock_id = strtoupper($_POST['stock_id']);
26 }
27
28 if (list_updated('stock_id')) {
29         $_POST['NewStockID'] = get_post('stock_id');
30         $Ajax->activate('details');
31         $Ajax->activate('controls');
32 //      unset($_POST['New']);
33 }
34 $upload_file = "";
35 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
36 {
37         $result = $_FILES['pic']['error'];
38         $upload_file = 'Yes'; //Assume all is well to start off with
39         $filename = $comp_path . "/$user_comp/images";
40         if (!file_exists($filename))
41         {
42                 mkdir($filename);
43         }       
44         $filename .= "/$stock_id.jpg";
45         
46          //But check for the worst 
47         if (strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)) != 'JPG')
48         {
49                 display_notification(_('Only jpg files are supported - a file extension of .jpg is expected'));
50                 $upload_file ='No';
51         } 
52         elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024)) 
53         { //File Size Check
54                 display_notification(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
55                 $upload_file ='No';
56         } 
57         elseif ( $_FILES['pic']['type'] == "text/plain" ) 
58         {  //File type Check
59                 display_notification( _('Only graphics files can be uploaded'));
60                 $upload_file ='No';
61         } 
62         elseif (file_exists($filename))
63         {
64                 $result = unlink($filename);
65                 if (!$result) 
66                 {
67                         display_error(_('The existing image could not be removed'));
68                         $upload_file ='No';
69                 }
70         }
71         
72         if ($upload_file == 'Yes')
73         {
74                 $result  =  move_uploaded_file($_FILES['pic']['tmp_name'], $filename);
75         }
76  /* EOF Add Image upload for New Item  - by Ori */
77 }
78
79
80 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."));
81
82 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."));
83
84 function clear_data()
85 {
86 //      global $new_item;
87         
88         unset($_POST['long_description']);
89         unset($_POST['description']);
90         unset($_POST['category_id']);
91         unset($_POST['tax_type_id']);
92         unset($_POST['units']);
93         unset($_POST['mb_flag']);
94         unset($_POST['NewStockID']);
95         unset($_POST['dimension_id']);
96         unset($_POST['dimension2_id']);
97 //      $new_item = true;
98 }
99
100 //------------------------------------------------------------------------------------
101
102 if (isset($_POST['addupdate'])) 
103 {
104
105         $input_error = 0;
106         if ($upload_file == 'No')
107                 $input_error = 1;
108         if (strlen($_POST['description']) == 0) 
109         {
110                 $input_error = 1;
111                 display_error( _('The item name must be entered.'));
112                 set_focus('description');
113         } 
114         elseif (strlen($_POST['NewStockID']) == 0) 
115         {
116                 $input_error = 1;
117                 display_error( _('The item code cannot be empty'));
118                 set_focus('NewStockID');
119         }
120         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
121                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
122                 strstr($_POST['NewStockID'], "&")) 
123         {
124                 $input_error = 1;
125                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
126                 set_focus('NewStockID');
127
128         }
129
130         if ($input_error != 1)
131         {
132
133                 if (!$new_item) 
134                 { /*so its an existing one */
135
136                         update_item($_POST['NewStockID'], $_POST['description'],
137                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
138                                 $_POST['sales_account'], $_POST['inventory_account'], $_POST['cogs_account'],
139                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
140                                 $_POST['dimension_id'], $_POST['dimension2_id']);
141
142                         display_notification(_("Item has been updated."));
143                 } 
144                 else 
145                 { //it is a NEW part
146
147                         add_item($_POST['NewStockID'], $_POST['description'],
148                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
149                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
150                                 $_POST['inventory_account'], $_POST['cogs_account'],
151                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
152                                 $_POST['dimension_id'], $_POST['dimension2_id']);
153
154                 display_notification(_("A new item has been added."));
155                 $_POST['stock_id'] = $_POST['NewStockID'];
156 //              $new_item = false;
157                 }
158                 set_focus('stock_id');
159                 $Ajax->activate('_page_body');
160         }
161 }
162
163 //------------------------------------------------------------------------------------
164
165 function can_delete($stock_id)
166 {
167         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE stock_id='$stock_id'";
168         $result = db_query($sql, "could not query stock moves");
169         $myrow = db_fetch_row($result);
170         if ($myrow[0] > 0) 
171         {
172                 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
173                 return false;
174         }
175
176         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE component='$stock_id'";
177         $result = db_query($sql, "could not query boms");
178         $myrow = db_fetch_row($result);
179         if ($myrow[0] > 0) 
180         {
181                 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
182                 return false;
183         }
184
185         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_order_details WHERE stk_code='$stock_id'";
186         $result = db_query($sql, "could not query sales orders");
187         $myrow = db_fetch_row($result);
188         if ($myrow[0] > 0) 
189         {
190                 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
191                 return false;
192         }
193
194         $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_order_details WHERE item_code='$stock_id'";
195         $result = db_query($sql, "could not query purchase orders");
196         $myrow = db_fetch_row($result);
197         if ($myrow[0] > 0) 
198         {
199                 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
200                 return false;
201         }
202
203         return true;
204 }
205
206 //------------------------------------------------------------------------------------
207
208 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
209 {
210
211         if (can_delete($_POST['NewStockID'])) {
212
213                 $stock_id = $_POST['NewStockID'];
214                 delete_item($stock_id);
215                 $filename = $comp_path . "/$user_comp/images/$stock_id.jpg";
216                 if (file_exists($filename))
217                         unlink($filename);
218                 display_notification(_("Selected item has been deleted."));
219 //              $new_item = true;
220                 $_POST['stock_id'] = '';
221                 clear_data();
222                 set_focus('stock_id');
223                 $Ajax->activate('_page_body');
224 //              meta_forward($_SERVER['PHP_SELF']);
225         }
226 }
227
228 //------------------------------------------------------------------------------------
229
230 start_form(true);
231
232 if (db_has_stock_items()) 
233 {
234         start_table("class='tablestyle_noborder'");
235         start_row();
236     stock_items_list_cells(_("Select an item:"), 'stock_id', null,
237           _('New item'), true);
238         $new_item = get_post('stock_id')==''; 
239         end_row();
240         end_table();
241 }
242
243 //hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new item"), "New=1");
244 //echo "<br>";
245
246 div_start('details');
247 start_table("$table_style2 width=40%");
248
249 table_section_title(_("Item"));
250
251 //------------------------------------------------------------------------------------
252
253 if ($new_item) 
254 {
255
256 /*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*/
257
258     clear_data();
259         text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
260
261         $company_record = get_company_prefs();
262
263     if (!isset($_POST['inventory_account']) || $_POST['inventory_account'] == "")
264         $_POST['inventory_account'] = $company_record["default_inventory_act"];
265
266     if (!isset($_POST['cogs_account']) || $_POST['cogs_account'] == "")
267         $_POST['cogs_account'] = $company_record["default_cogs_act"];
268
269         if (!isset($_POST['sales_account']) || $_POST['sales_account'] == "")
270                 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
271
272         if (!isset($_POST['adjustment_account']) || $_POST['adjustment_account'] == "")
273                 $_POST['adjustment_account'] = $company_record["default_adj_act"];
274
275         if (!isset($_POST['assembly_account']) || $_POST['assembly_account'] == "")
276                 $_POST['assembly_account'] = $company_record["default_assembly_act"];
277
278
279 else 
280 { // Must be modifying an existing item
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         
300                 label_row(_("Item Code:"),$_POST['NewStockID']);
301                 hidden('NewStockID', $_POST['NewStockID']);
302                 set_focus('description');
303 }
304
305 text_row(_("Name:"), 'description', null, 52, 50);
306
307 textarea_row(_('Description:'), 'long_description', null, 45, 3);
308
309 end_table();
310 start_table("$table_style2 width=40%");
311 // Add image upload for New Item  - by Joe
312 start_row();
313 label_cells(_("Image File (.jpg)") . ":", "<input type='file' id='pic' name='pic'>");
314 // Add Image upload for New Item  - by Joe
315 $stock_img_link = "<img id='item_img' alt = '[";
316 if (isset($_POST['NewStockID']) && file_exists("$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg")) 
317 {
318  // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
319         $stock_img_link .= $_POST['NewStockID'].".jpg".
320         "]' src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg?nocache=".rand()."'";
321
322 else 
323 {
324         $stock_img_link .= _("No image"). "]'";
325 }
326 $stock_img_link .= " width='$pic_width' height='$pic_height' border='0'>";
327
328 label_cell($stock_img_link, "valign=top align=center rowspan=5");
329 end_row();
330
331 stock_categories_list_row(_("Category:"), 'category_id', null);
332
333 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
334
335 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
336         (!isset($_POST['NewStockID']) || $new_item));
337
338 stock_units_list_row(_('Units of Measure:'), 'units', null,
339         (!isset($_POST['NewStockID']) || $new_item));
340 end_table();
341 start_table("$table_style2 width=40%");
342
343 table_section_title(_("GL Accounts"));
344
345 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
346
347 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
348
349 if (!is_service($_POST['mb_flag'])) 
350 {
351         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
352         gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
353 }
354 else 
355 {
356         hidden('cogs_account', $_POST['cogs_account']);
357         hidden('adjustment_account', $_POST['adjustment_account']);
358 }
359
360
361 if (is_manufactured($_POST['mb_flag']))
362         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
363 else
364         hidden('assembly_account', $_POST['assembly_account']);
365 $dim = get_company_pref('use_dimension');
366 if ($dim >= 1)
367 {
368         table_section_title(_("Dimensions"));
369
370         dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
371         if ($dim > 1)
372                 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
373 }
374 if ($dim < 1)
375         hidden('dimension_id', 0);
376 if ($dim < 2)
377         hidden('dimension2_id', 0);
378
379 end_table(1);
380 div_end();
381 div_start('controls');
382 if (!isset($_POST['NewStockID']) || $new_item) 
383 {
384         submit_center('addupdate', _("Insert New Item"), true, '', true);
385
386 else 
387 {
388         submit_center_first('addupdate', _("Update Item"), '', true);
389
390         submit_center_last('delete', _("Delete This Item"), '', true);
391 }
392
393 div_end();
394 end_form();
395
396 //------------------------------------------------------------------------------------
397
398 end_page();
399 ?>