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