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