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