Focus set to invalid field after submit check fail
[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 //------------------------------------------------------------------------------------
17
18 if (isset($_GET['stock_id']))
19 {
20         $stock_id = strtoupper($_GET['stock_id']);
21 }
22 else if (isset($_POST['stock_id']))
23 {
24         $stock_id = strtoupper($_POST['stock_id']);
25 }
26
27 if (isset($_GET['New']) || !isset($_POST['NewStockID'])) 
28 {
29         $_POST['New'] = "1";
30 }
31
32 if (isset($_POST['SelectStockItem'])) 
33 {
34         $_POST['NewStockID'] = $_POST['stock_id'];
35         unset($_POST['New']);
36 }
37 $upload_file = "";
38 if (isset($_FILES['pic']) && $_FILES['pic']['name'] != '') 
39 {
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))
44         {
45                 mkdir($filename);
46         }       
47         $filename .= "$stock_id.jpg";
48         
49          //But check for the worst 
50         if (strtoupper(substr(trim($_FILES['pic']['name']), strlen($_FILES['pic']['name']) - 3)) != 'JPG')
51         {
52                 display_notification(_('Only jpg files are supported - a file extension of .jpg is expected'));
53                 $upload_file ='No';
54         } 
55         elseif ( $_FILES['pic']['size'] > ($max_image_size * 1024)) 
56         { //File Size Check
57                 display_notification(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $max_image_size);
58                 $upload_file ='No';
59         } 
60         elseif ( $_FILES['pic']['type'] == "text/plain" ) 
61         {  //File type Check
62                 display_notification( _('Only graphics files can be uploaded'));
63                 $upload_file ='No';
64         } 
65         elseif (file_exists($filename))
66         {
67                 display_notification(_('Attempting to overwrite an existing item image'));
68                 $result = unlink($filename);
69                 if (!$result) 
70                 {
71                         display_error(_('The existing image could not be removed'));
72                         $upload_file ='No';
73                 }
74         }
75         
76         if ($upload_file == 'Yes')
77         {
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.";
80         }
81  /* EOF Add Image upload for New Item  - by Ori */
82 }
83
84
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."));
86
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."));
88
89 function clear_data()
90 {
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']);
100         $_POST['New'] = "1";
101 }
102
103 //------------------------------------------------------------------------------------
104
105 if (isset($_POST['addupdate'])) 
106 {
107
108         $input_error = 0;
109         if ($upload_file == 'No')
110                 $input_error = 1;
111         if (strlen($_POST['description']) == 0) 
112         {
113                 $input_error = 1;
114                 display_error( _('The item name must be entered.'));
115                 set_focus('description');
116         } 
117         elseif (strlen($_POST['NewStockID']) == 0) 
118         {
119                 $input_error = 1;
120                 display_error( _('The item code cannot be empty'));
121                 set_focus('NewStockID');
122         }
123         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
124                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
125                 strstr($_POST['NewStockID'], "&")) 
126         {
127                 $input_error = 1;
128                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
129                 set_focus('NewStockID');
130
131         }
132
133         if ($input_error != 1)
134         {
135
136                 if (!isset($_POST['New'])) 
137                 { /*so its an existing one */
138
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']);
144
145                 } 
146                 else 
147                 { //it is a NEW part
148
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']);
155                 }
156                 meta_forward($_SERVER['PHP_SELF']);
157         }
158 }
159
160 //------------------------------------------------------------------------------------
161
162 function can_delete($stock_id)
163 {
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);
167         if ($myrow[0] > 0) 
168         {
169                 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
170                 return false;
171         }
172
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);
176         if ($myrow[0] > 0) 
177         {
178                 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
179                 return false;
180         }
181
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);
185         if ($myrow[0] > 0) 
186         {
187                 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
188                 return false;
189         }
190
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);
194         if ($myrow[0] > 0) 
195         {
196                 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
197                 return false;
198         }
199
200         return true;
201 }
202
203 //------------------------------------------------------------------------------------
204
205 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
206 {
207
208         if (can_delete($_POST['NewStockID'])) {
209
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))
214                         unlink($filename);
215                 meta_forward($_SERVER['PHP_SELF']);
216         }
217 }
218
219 //------------------------------------------------------------------------------------
220
221 start_form(true);
222
223 if (db_has_stock_items()) 
224 {
225         start_table("class='tablestyle_noborder'");
226         start_row();
227     stock_items_list_cells(_("Select an item:"), 'stock_id', null);
228     submit_cells('SelectStockItem', _("Edit Item"));
229         end_row();
230         end_table();
231 }
232
233 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new item"), "New=1");
234 echo "<br>";
235
236 start_table("$table_style2 width=40%");
237
238 table_section_title(_("Item"));
239
240 //------------------------------------------------------------------------------------
241
242 if (!isset($_POST['NewStockID']) || isset($_POST['New'])) 
243 {
244
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*/
246
247         hidden('New', 'Yes');
248
249         text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
250
251         $company_record = get_company_prefs();
252
253     if (!isset($_POST['inventory_account']) || $_POST['inventory_account'] == "")
254         $_POST['inventory_account'] = $company_record["default_inventory_act"];
255
256     if (!isset($_POST['cogs_account']) || $_POST['cogs_account'] == "")
257         $_POST['cogs_account'] = $company_record["default_cogs_act"];
258
259         if (!isset($_POST['sales_account']) || $_POST['sales_account'] == "")
260                 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
261
262         if (!isset($_POST['adjustment_account']) || $_POST['adjustment_account'] == "")
263                 $_POST['adjustment_account'] = $company_record["default_adj_act"];
264
265         if (!isset($_POST['assembly_account']) || $_POST['assembly_account'] == "")
266                 $_POST['assembly_account'] = $company_record["default_assembly_act"];
267
268
269 else 
270 { // Must be modifying an existing item
271
272         if (!isset($_POST['New'])) 
273         {
274                 $myrow = get_item($_POST['NewStockID']);
275
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"];
282
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'];
290         
291                 label_row(_("Item Code:"),$_POST['NewStockID']);
292                 hidden('NewStockID', $_POST['NewStockID']);
293         }
294 }
295
296 text_row(_("Name:"), 'description', null, 52, 50);
297
298 textarea_row(_('Description:'), 'long_description', null, 45, 3);
299
300 end_table();
301 start_table("$table_style2 width=40%");
302 // Add image upload for New Item  - by Joe
303 start_row();
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")) 
307 {
308         $stock_img_link = "<img src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg' width='$pic_width' height='$pic_height' border='0'>";
309
310 else 
311 {
312         $stock_img_link = "No Image";
313 }
314
315 label_cell($stock_img_link, "valign=top align=center rowspan=5");
316 end_row();
317
318 stock_categories_list_row(_("Category:"), 'category_id', null);
319
320 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
321
322 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
323         (!isset($_POST['NewStockID']) || isset($_POST['New'])));
324
325 stock_units_list_row(_('Units of Measure:'), 'units', null,
326         (!isset($_POST['NewStockID']) || isset($_POST['New'])));
327 end_table();
328 start_table("$table_style2 width=40%");
329
330 table_section_title(_("GL Accounts"));
331
332 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
333
334 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
335
336 if (!is_service($_POST['mb_flag'])) 
337 {
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']);
340 }
341 else 
342 {
343         hidden('cogs_account', $_POST['cogs_account']);
344         hidden('adjustment_account', $_POST['adjustment_account']);
345 }
346
347
348 if (is_manufactured($_POST['mb_flag']))
349         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
350 else
351         hidden('assembly_account', $_POST['assembly_account']);
352 $dim = get_company_pref('use_dimension');
353 if ($dim >= 1)
354 {
355         table_section_title(_("Dimensions"));
356
357         dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
358         if ($dim > 1)
359                 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
360 }
361 if ($dim < 1)
362         hidden('dimension_id', 0);
363 if ($dim < 2)
364         hidden('dimension2_id', 0);
365
366 end_table(1);
367
368 if (!isset($_POST['NewStockID']) || (isset($_POST['New']) && $_POST['New'] != "")) 
369 {
370         submit_center('addupdate', _("Insert New Item"));
371
372
373 else 
374 {
375         submit_center_first('addupdate', _("Update Item"));
376
377         submit_center_last('delete', _("Delete This Item"));
378 }
379
380
381 end_form();
382
383 //------------------------------------------------------------------------------------
384
385 end_page();
386 ?>