Moving company data storage to company subdirectory
[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         } 
116         elseif (strlen($_POST['NewStockID']) == 0) 
117         {
118                 $input_error = 1;
119                 display_error( _('The item code cannot be empty'));
120         }
121         elseif (strstr($_POST['NewStockID'], " ") || strstr($_POST['NewStockID'],"'") || 
122                 strstr($_POST['NewStockID'], "+") || strstr($_POST['NewStockID'], "\"") || 
123                 strstr($_POST['NewStockID'], "&")) 
124         {
125                 $input_error = 1;
126                 display_error( _('The item code cannot contain any of the following characters -  & + OR a space OR quotes'));
127
128         }
129
130         if ($input_error != 1)
131         {
132
133                 if (!isset($_POST['New'])) 
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                 } 
143                 else 
144                 { //it is a NEW part
145
146                         add_item($_POST['NewStockID'], $_POST['description'],
147                                 $_POST['long_description'], $_POST['category_id'], $_POST['tax_type_id'],
148                                 $_POST['units'], $_POST['mb_flag'], $_POST['sales_account'],
149                                 $_POST['inventory_account'], $_POST['cogs_account'],
150                                 $_POST['adjustment_account'], $_POST['assembly_account'], 
151                                 $_POST['dimension_id'], $_POST['dimension2_id']);
152                 }
153                 meta_forward($_SERVER['PHP_SELF']);
154         }
155 }
156
157 //------------------------------------------------------------------------------------
158
159 function can_delete($stock_id)
160 {
161         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE stock_id='$stock_id'";
162         $result = db_query($sql, "could not query stock moves");
163         $myrow = db_fetch_row($result);
164         if ($myrow[0] > 0) 
165         {
166                 display_error(_('Cannot delete this item because there are stock movements that refer to this item.'));
167                 return false;
168         }
169
170         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE component='$stock_id'";
171         $result = db_query($sql, "could not query boms");
172         $myrow = db_fetch_row($result);
173         if ($myrow[0] > 0) 
174         {
175                 display_error(_('Cannot delete this item record because there are bills of material that require this part as a component.'));
176                 return false;
177         }
178
179         $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_order_details WHERE stk_code='$stock_id'";
180         $result = db_query($sql, "could not query sales orders");
181         $myrow = db_fetch_row($result);
182         if ($myrow[0] > 0) 
183         {
184                 display_error(_('Cannot delete this item record because there are existing sales orders for this part.'));
185                 return false;
186         }
187
188         $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_order_details WHERE item_code='$stock_id'";
189         $result = db_query($sql, "could not query purchase orders");
190         $myrow = db_fetch_row($result);
191         if ($myrow[0] > 0) 
192         {
193                 display_error(_('Cannot delete this item because there are existing purchase order items for it.'));
194                 return false;
195         }
196
197         return true;
198 }
199
200 //------------------------------------------------------------------------------------
201
202 if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
203 {
204
205         if (can_delete($_POST['NewStockID'])) {
206
207                 $stock_id = $_POST['NewStockID'];
208                 delete_item($stock_id);
209                 $filename = $comp_path . "/$user_comp/images/$stock_id.jpg";
210                 if (file_exists($filename))
211                         unlink($filename);
212                 meta_forward($_SERVER['PHP_SELF']);
213         }
214 }
215
216 //------------------------------------------------------------------------------------
217
218 start_form(true);
219
220 if (db_has_stock_items()) 
221 {
222         start_table("class='tablestyle_noborder'");
223         start_row();
224     stock_items_list_cells(_("Select an item:"), 'stock_id', null);
225     submit_cells('SelectStockItem', _("Edit Item"));
226         end_row();
227         end_table();
228 }
229
230 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a new item"), "New=1");
231 echo "<br>";
232
233 start_table("$table_style2 width=40%");
234
235 table_section_title(_("Item"));
236
237 //------------------------------------------------------------------------------------
238
239 if (!isset($_POST['NewStockID']) || isset($_POST['New'])) 
240 {
241
242 /*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*/
243
244         hidden('New', 'Yes');
245
246         text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
247
248         $company_record = get_company_prefs();
249
250     if (!isset($_POST['inventory_account']) || $_POST['inventory_account'] == "")
251         $_POST['inventory_account'] = $company_record["default_inventory_act"];
252
253     if (!isset($_POST['cogs_account']) || $_POST['cogs_account'] == "")
254         $_POST['cogs_account'] = $company_record["default_cogs_act"];
255
256         if (!isset($_POST['sales_account']) || $_POST['sales_account'] == "")
257                 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
258
259         if (!isset($_POST['adjustment_account']) || $_POST['adjustment_account'] == "")
260                 $_POST['adjustment_account'] = $company_record["default_adj_act"];
261
262         if (!isset($_POST['assembly_account']) || $_POST['assembly_account'] == "")
263                 $_POST['assembly_account'] = $company_record["default_assembly_act"];
264
265
266 else 
267 { // Must be modifying an existing item
268
269         if (!isset($_POST['New'])) 
270         {
271                 $myrow = get_item($_POST['NewStockID']);
272
273                 $_POST['long_description'] = $myrow["long_description"];
274                 $_POST['description'] = $myrow["description"];
275                 $_POST['category_id']  = $myrow["category_id"];
276                 $_POST['tax_type_id']  = $myrow["tax_type_id"];
277                 $_POST['units']  = $myrow["units"];
278                 $_POST['mb_flag']  = $myrow["mb_flag"];
279
280                 $_POST['sales_account'] =  $myrow['sales_account'];
281                 $_POST['inventory_account'] = $myrow['inventory_account'];
282                 $_POST['cogs_account'] = $myrow['cogs_account'];
283                 $_POST['adjustment_account']    = $myrow['adjustment_account'];
284                 $_POST['assembly_account']      = $myrow['assembly_account'];
285                 $_POST['dimension_id']  = $myrow['dimension_id'];
286                 $_POST['dimension2_id'] = $myrow['dimension2_id'];
287         
288                 label_row(_("Item Code:"),$_POST['NewStockID']);
289                 hidden('NewStockID', $_POST['NewStockID']);
290         }
291 }
292
293 text_row(_("Name:"), 'description', null, 52, 50);
294
295 textarea_row(_('Description:'), 'long_description', null, 45, 3);
296
297 end_table();
298 start_table("$table_style2 width=40%");
299 // Add image upload for New Item  - by Joe
300 start_row();
301 label_cells(_("Image File (.jpg)") . ":", "<input type='file' id='pic' name='pic'>");
302 // Add Image upload for New Item  - by Joe
303 if (isset($_POST['NewStockID']) && file_exists("$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg")) 
304 {
305         $stock_img_link = "<img src='$comp_path/$user_comp/images/".$_POST['NewStockID'].".jpg' width='$pic_width' height='$pic_height' border='0'>";
306
307 else 
308 {
309         $stock_img_link = "No Image";
310 }
311
312 label_cell($stock_img_link, "valign=top align=center rowspan=5");
313 end_row();
314
315 stock_categories_list_row(_("Category:"), 'category_id', null);
316
317 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
318
319 stock_item_types_list_row(_("Item Type:"), 'mb_flag', null,
320         (!isset($_POST['NewStockID']) || isset($_POST['New'])));
321
322 stock_units_list_row(_('Units of Measure:'), 'units', null,
323         (!isset($_POST['NewStockID']) || isset($_POST['New'])));
324 end_table();
325 start_table("$table_style2 width=40%");
326
327 table_section_title(_("GL Accounts"));
328
329 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
330
331 gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
332
333 if (!is_service($_POST['mb_flag'])) 
334 {
335         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
336         gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
337 }
338 else 
339 {
340         hidden('cogs_account', $_POST['cogs_account']);
341         hidden('adjustment_account', $_POST['adjustment_account']);
342 }
343
344
345 if (is_manufactured($_POST['mb_flag']))
346         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'assembly_account', $_POST['assembly_account']);
347 else
348         hidden('assembly_account', $_POST['assembly_account']);
349 $dim = get_company_pref('use_dimension');
350 if ($dim >= 1)
351 {
352         table_section_title(_("Dimensions"));
353
354         dimensions_list_row(_("Dimension")." 1", 'dimension_id', null, true, " ", false, 1);
355         if ($dim > 1)
356                 dimensions_list_row(_("Dimension")." 2", 'dimension2_id', null, true, " ", false, 2);
357 }
358 if ($dim < 1)
359         hidden('dimension_id', 0);
360 if ($dim < 2)
361         hidden('dimension2_id', 0);
362
363 end_table(1);
364
365 if (!isset($_POST['NewStockID']) || (isset($_POST['New']) && $_POST['New'] != "")) 
366 {
367         submit_center('addupdate', _("Insert New Item"));
368
369
370 else 
371 {
372         submit_center_first('addupdate', _("Update Item"));
373
374         submit_center_last('delete', _("Delete This Item"));
375 }
376
377
378 end_form();
379
380 //------------------------------------------------------------------------------------
381
382 end_page();
383 ?>