Changed so that average item material price is automatic updated whenever a PO Delive...
[fa-stable.git] / manufacturing / manage / bom_edit.php
1 <?php
2
3 $page_security = 9;
4 $path_to_root="../..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 page(_("Bill Of Materials"));
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 . "/includes/manufacturing.inc");
14
15 check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));
16
17 check_db_has_workcentres(_("There are no work centres defined in the system. BOMs require at least one work centre be defined."));
18
19 //--------------------------------------------------------------------------------------------------
20
21 if (isset($_GET["NewItem"]))
22 {
23         $_POST['stock_id'] = $_GET["NewItem"];
24         if (isset($_GET['item']) && isset($_GET['qty']))
25                 add_material_cost($_GET["NewItem"], $_GET['item'], $_GET['qty'], false);
26
27 }
28 if (isset($_GET['stock_id']))
29 {
30         $_POST['stock_id'] = $_GET['stock_id'];
31         $selected_parent =  $_GET['stock_id'];
32 }
33
34 /* selected_parent could come from a post or a get */
35 if (isset($_GET["selected_parent"]))
36 {
37         $selected_parent = $_GET["selected_parent"];
38 }
39 else if (isset($_POST["selected_parent"]))
40 {
41         $selected_parent = $_POST["selected_parent"];
42 }
43 /* selected_component could also come from a post or a get */
44 if (isset($_GET["selected_component"]))
45 {
46         $selected_component = $_GET["selected_component"];
47 }
48 elseif (isset($_POST["selected_component"]))
49 {
50         $selected_component = $_POST["selected_component"];
51 }
52
53 function add_material_cost($parent, $item, $n, $add=true)
54 {
55         $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$parent'";
56         $result = db_query($sql);
57         $myrow = db_fetch($result);
58         $material_cost = $myrow['material_cost'];
59         $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$item'";
60         $result = db_query($sql);
61         $myrow = db_fetch($result);
62         $material_cost2 = $myrow['material_cost'];
63         if ($add)
64                 $material_cost += ($material_cost2 * $n);
65         else
66                 $material_cost -= ($material_cost2 * $n);
67         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=$material_cost
68                 WHERE stock_id='$parent'";
69         db_query($sql,"The cost details for the inventory item could not be updated");
70 }
71
72 //--------------------------------------------------------------------------------------------------
73
74 function check_for_recursive_bom($ultimate_parent, $component_to_check)
75 {
76
77         /* returns true ie 1 if the bom contains the parent part as a component
78         ie the bom is recursive otherwise false ie 0 */
79
80         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
81         $result = db_query($sql,"could not check recursive bom");
82
83         if ($result != 0)
84         {
85                 while ($myrow = db_fetch_row($result))
86                 {
87                         if ($myrow[0] == $ultimate_parent)
88                         {
89                                 return 1;
90                         }
91
92                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
93                         {
94                                 return 1;
95                         }
96                 } //(while loop)
97         } //end if $result is true
98
99         return 0;
100
101 } //end of function check_for_recursive_bom
102
103 //--------------------------------------------------------------------------------------------------
104
105 function display_bom_items($selected_parent)
106 {
107         global $table_style;
108
109         $result = get_bom($selected_parent);
110
111         start_table("$table_style width=60%");
112         $th = array(_("Code"), _("Description"), _("Location"),
113                 _("Work Centre"), _("Quantity"), _("Units"));
114         table_header($th);
115
116         $k = 0;
117         while ($myrow = db_fetch($result))
118         {
119
120                 alt_table_row_color($k);
121
122                 label_cell($myrow["component"]);
123                 label_cell($myrow["description"]);
124         label_cell($myrow["location_name"]);
125         label_cell($myrow["WorkCentreDescription"]);
126         label_cell($myrow["quantity"]);
127         label_cell($myrow["units"]);
128         edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]."&item=".$myrow['component']."&qty=".$myrow['quantity']);
129         delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']."&item=".$myrow['component']."&qty=".$myrow['quantity']);
130         end_row();
131
132         } //END WHILE LIST LOOP
133         end_table();
134 }
135
136 //--------------------------------------------------------------------------------------------------
137
138 function on_submit($selected_parent, $selected_component)
139 {
140         if (!is_numeric($_POST['quantity']))
141         {
142                 display_error(_("The quantity entered must be numeric."));
143                 return;
144         }
145
146         if ($_POST['quantity'] <= 0)
147         {
148                 display_error(_("The quantity entered must be greater than zero."));
149                 return;
150         }
151
152
153         if (isset($selected_parent) && isset($selected_component))
154         {
155
156                 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
157                         loc_code='" . $_POST['loc_code'] . "',
158                         quantity= " . $_POST['quantity'] . "
159                         WHERE parent='" . $selected_parent . "'
160                         AND id='" . $selected_component . "'";
161                 check_db_error("Could not update this bom component", $sql);
162
163                 add_material_cost($selected_parent, $_POST['item'], $_POST['quantity'], true);
164
165                 db_query($sql,"could not update bom");
166
167         }
168         elseif (!isset($selected_component) && isset($selected_parent))
169         {
170
171                 /*Selected component is null cos no item selected on first time round so must be                                adding a record must be Submitting new entries in the new component form */
172
173                 //need to check not recursive bom component of itself!
174                 If (!check_for_recursive_bom($selected_parent, $_POST['component']))
175                 {
176
177                         /*Now check to see that the component is not already on the bom */
178                         $sql = "SELECT component FROM ".TB_PREF."bom
179                                 WHERE parent='$selected_parent'
180                                 AND component='" . $_POST['component'] . "'
181                                 AND workcentre_added='" . $_POST['workcentre_added'] . "'
182                                 AND loc_code='" . $_POST['loc_code'] . "'" ;
183                         $result = db_query($sql,"check failed");
184
185                         if (db_num_rows($result) == 0)
186                         {
187                                 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
188                                         VALUES ('$selected_parent', '" . $_POST['component'] . "', '" . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', " . $_POST['quantity'] . ")";
189
190                                 db_query($sql,"check failed");
191
192                                 add_material_cost($selected_parent, $_POST['component'], $_POST['quantity'], true);
193                                 //$msg = _("A new component part has been added to the bill of material for this item.");
194
195                         }
196                         else
197                         {
198                                 /*The component must already be on the bom */
199                                 display_error(_("The selected component is already on this bom. You can modify it's quantity but it cannot appear more than once on the same bom."));
200                         }
201
202                 } //end of if its not a recursive bom
203                 else
204                 {
205                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
206                 }
207         }
208 }
209
210 //--------------------------------------------------------------------------------------------------
211
212 if (isset($_GET['delete']))
213 {
214         $sql = "DELETE FROM ".TB_PREF."bom WHERE id='".$_GET['delete']."'";
215         db_query($sql,"Could not delete this bom components");
216
217         add_material_cost($_GET['stock_id'], $_GET['item'], $_GET['qty'], false);
218
219         display_note(_("The component item has been deleted from this bom."));
220
221 }
222
223 //--------------------------------------------------------------------------------------------------
224
225 start_form(false, true);
226 //echo $msg;
227
228 echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
229 stock_bom_items_list('stock_id', null, false, true);
230
231 end_form();
232
233 //--------------------------------------------------------------------------------------------------
234
235 if (isset($_POST['stock_id']))
236 { //Parent Item selected so display bom or edit component
237         $selected_parent = $_POST['stock_id'];
238
239         if (isset($selected_parent) && isset($_POST['Submit']))
240                 on_submit($selected_parent, $selected_component);
241
242         //--------------------------------------------------------------------------------------
243
244         display_bom_items($selected_parent);
245
246         if (isset($selected_parent) && isset($selected_component))
247         {
248                 hyperlink_params($_SERVER['PHP_SELF'], _("Add a new Component"), "NewItem=$selected_parent");
249         }
250
251         //--------------------------------------------------------------------------------------
252
253         start_form(false, true, $_SERVER['PHP_SELF'] . "?" . SID . "NewItem=" . $selected_parent);
254
255         start_table($table_style2);
256
257         if (isset($selected_component))
258         {
259                 //editing a selected component from the link to the line item
260                 $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
261                         WHERE id='$selected_component'
262                         AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
263
264                 $result = db_query($sql, "could not get bom");
265                 $myrow = db_fetch($result);
266
267                 $_POST['loc_code'] = $myrow["loc_code"];
268                 $_POST['workcentre_added']  = $myrow["workcentre_added"];
269                 $_POST['quantity'] = $myrow["quantity"];
270
271                 hidden('item', $myrow["component"]);
272                 hidden('selected_parent', $selected_parent);
273                 hidden('selected_component', $selected_component);
274                 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
275
276         }
277         else
278         { //end of if $selected_component
279
280                 hidden('selected_parent', $selected_parent);
281
282                 start_row();
283                 label_cell(_("Component:"));
284
285                 echo "<td>";
286                 stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);
287                 echo "</td>";
288                 end_row();
289         }
290
291         locations_list_row(_("Location to Draw From:"), 'loc_code', null);
292         workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
293
294         if (!isset($_POST['quantity']))
295         {
296                 $_POST['quantity'] = 1;
297         }
298         text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);
299
300         end_table(1);
301         submit_center('Submit', _("Add/Update"));
302
303         end_form();
304 }
305
306 // ----------------------------------------------------------------------------------
307
308 end_page();
309
310 ?>