5 include_once($path_to_root . "/includes/session.inc");
7 page(_("Bill Of Materials"));
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");
13 include_once($path_to_root . "/includes/manufacturing.inc");
15 check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));
17 check_db_has_workcentres(_("There are no work centres defined in the system. BOMs require at least one work centre be defined."));
19 //--------------------------------------------------------------------------------------------------
21 if (isset($_GET["NewItem"]))
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);
28 if (isset($_GET['stock_id']))
30 $_POST['stock_id'] = $_GET['stock_id'];
31 $selected_parent = $_GET['stock_id'];
34 /* selected_parent could come from a post or a get */
35 if (isset($_GET["selected_parent"]))
37 $selected_parent = $_GET["selected_parent"];
39 else if (isset($_POST["selected_parent"]))
41 $selected_parent = $_POST["selected_parent"];
43 /* selected_component could also come from a post or a get */
44 if (isset($_GET["selected_component"]))
46 $selected_component = $_GET["selected_component"];
48 elseif (isset($_POST["selected_component"]))
50 $selected_component = $_POST["selected_component"];
53 function add_material_cost($parent, $item, $n, $add=true)
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'];
64 $material_cost += ($material_cost2 * $n);
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");
72 //--------------------------------------------------------------------------------------------------
74 function check_for_recursive_bom($ultimate_parent, $component_to_check)
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 */
80 $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
81 $result = db_query($sql,"could not check recursive bom");
85 while ($myrow = db_fetch_row($result))
87 if ($myrow[0] == $ultimate_parent)
92 if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
97 } //end if $result is true
101 } //end of function check_for_recursive_bom
103 //--------------------------------------------------------------------------------------------------
105 function display_bom_items($selected_parent)
109 $result = get_bom($selected_parent);
111 start_table("$table_style width=60%");
112 $th = array(_("Code"), _("Description"), _("Location"),
113 _("Work Centre"), _("Quantity"), _("Units"));
117 while ($myrow = db_fetch($result))
120 alt_table_row_color($k);
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']);
132 } //END WHILE LIST LOOP
136 //--------------------------------------------------------------------------------------------------
138 function on_submit($selected_parent, $selected_component)
140 if (!is_numeric($_POST['quantity']))
142 display_error(_("The quantity entered must be numeric."));
146 if ($_POST['quantity'] <= 0)
148 display_error(_("The quantity entered must be greater than zero."));
153 if (isset($selected_parent) && isset($selected_component))
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);
163 add_material_cost($selected_parent, $_POST['item'], $_POST['quantity'], true);
165 db_query($sql,"could not update bom");
168 elseif (!isset($selected_component) && isset($selected_parent))
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 */
173 //need to check not recursive bom component of itself!
174 If (!check_for_recursive_bom($selected_parent, $_POST['component']))
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");
185 if (db_num_rows($result) == 0)
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'] . ")";
190 db_query($sql,"check failed");
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.");
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."));
202 } //end of if its not a recursive bom
205 display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
210 //--------------------------------------------------------------------------------------------------
212 if (isset($_GET['delete']))
214 $sql = "DELETE FROM ".TB_PREF."bom WHERE id='".$_GET['delete']."'";
215 db_query($sql,"Could not delete this bom components");
217 add_material_cost($_GET['stock_id'], $_GET['item'], $_GET['qty'], false);
219 display_note(_("The component item has been deleted from this bom."));
223 //--------------------------------------------------------------------------------------------------
225 start_form(false, true);
228 echo "<center>" . _("Select a manufacturable item:") . " ";
229 stock_bom_items_list('stock_id', null, false, true);
233 //--------------------------------------------------------------------------------------------------
235 if (isset($_POST['stock_id']))
236 { //Parent Item selected so display bom or edit component
237 $selected_parent = $_POST['stock_id'];
239 if (isset($selected_parent) && isset($_POST['Submit']))
240 on_submit($selected_parent, $selected_component);
242 //--------------------------------------------------------------------------------------
244 display_bom_items($selected_parent);
246 if (isset($selected_parent) && isset($selected_component))
248 hyperlink_params($_SERVER['PHP_SELF'], _("Add a new Component"), "NewItem=$selected_parent");
251 //--------------------------------------------------------------------------------------
253 start_form(false, true, $_SERVER['PHP_SELF'] . "?" . SID . "NewItem=" . $selected_parent);
255 start_table($table_style2);
257 if (isset($selected_component))
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";
264 $result = db_query($sql, "could not get bom");
265 $myrow = db_fetch($result);
267 $_POST['loc_code'] = $myrow["loc_code"];
268 $_POST['workcentre_added'] = $myrow["workcentre_added"];
269 $_POST['quantity'] = $myrow["quantity"];
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"]);
278 { //end of if $selected_component
280 hidden('selected_parent', $selected_parent);
283 label_cell(_("Component:"));
286 stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);
291 locations_list_row(_("Location to Draw From:"), 'loc_code', null);
292 workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
294 if (!isset($_POST['quantity']))
296 $_POST['quantity'] = 1;
298 text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);
301 submit_center('Submit', _("Add/Update"));
306 // ----------------------------------------------------------------------------------