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"];
25 if (isset($_GET['stock_id']))
27 $_POST['stock_id'] = $_GET['stock_id'];
28 $selected_parent = $_GET['stock_id'];
31 /* selected_parent could come from a post or a get */
32 if (isset($_GET["selected_parent"]))
34 $selected_parent = $_GET["selected_parent"];
36 else if (isset($_POST["selected_parent"]))
38 $selected_parent = $_POST["selected_parent"];
40 /* selected_component could also come from a post or a get */
41 if (isset($_GET["selected_component"]))
43 $selected_component = $_GET["selected_component"];
45 elseif (isset($_POST["selected_component"]))
47 $selected_component = $_POST["selected_component"];
51 //--------------------------------------------------------------------------------------------------
53 function check_for_recursive_bom($ultimate_parent, $component_to_check, $db)
56 /* returns true ie 1 if the bom contains the parent part as a component
57 ie the bom is recursive otherwise false ie 0 */
59 $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
60 $result = db_query($sql,"could not check recursive bom");
64 while ($myrow = db_fetch_row($result))
66 if ($myrow[0] == $ultimate_parent)
71 if (check_for_recursive_bom($ultimate_parent, $myrow[0], &$db))
76 } //end if $result is true
80 } //end of function check_for_recursive_bom
82 //--------------------------------------------------------------------------------------------------
84 function display_bom_items($selected_parent)
88 $result = get_bom($selected_parent);
90 start_table("$table_style width=60%");
91 $th = array(_("Code"), _("Description"), _("Location"),
92 _("Work Centre"), _("Quantity"), _("Units"));
96 while ($myrow = db_fetch($result))
99 alt_table_row_color($k);
101 label_cell($myrow["component"]);
102 label_cell($myrow["description"]);
103 label_cell($myrow["location_name"]);
104 label_cell($myrow["WorkCentreDescription"]);
105 label_cell($myrow["quantity"]);
106 label_cell($myrow["units"]);
107 edit_link_cell(SID . "NewItem=$selected_parent&selected_component=" . $myrow["id"]);
108 delete_link_cell(SID . "delete=" . $myrow["id"]. "&stock_id=" . $_POST['stock_id']);
111 } //END WHILE LIST LOOP
115 //--------------------------------------------------------------------------------------------------
117 function on_submit($selected_parent, $selected_component)
121 if (!is_numeric($_POST['quantity']))
123 display_error(_("The quantity entered must be numeric."));
127 if ($_POST['quantity'] <= 0)
129 display_error(_("The quantity entered must be greater than zero."));
134 if (isset($selected_parent) && isset($selected_component))
137 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
138 loc_code='" . $_POST['loc_code'] . "',
139 quantity= " . $_POST['quantity'] . "
140 WHERE parent='" . $selected_parent . "'
141 AND id='" . $selected_component . "'";
142 check_db_error("Could not update this bom component", $sql);
144 db_query($sql,"could not update bom");
147 elseif (!isset($selected_component) && isset($selected_parent))
150 /*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 */
152 //need to check not recursive bom component of itself!
153 If (!check_for_recursive_bom($selected_parent, $_POST['component'], &$db))
156 /*Now check to see that the component is not already on the bom */
157 $sql = "SELECT component FROM ".TB_PREF."bom
158 WHERE parent='$selected_parent'
159 AND component='" . $_POST['component'] . "'
160 AND workcentre_added='" . $_POST['workcentre_added'] . "'
161 AND loc_code='" . $_POST['loc_code'] . "'" ;
162 $result = db_query($sql,"check failed");
164 if (db_num_rows($result) == 0)
166 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
167 VALUES ('$selected_parent', '" . $_POST['component'] . "', '" . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', " . $_POST['quantity'] . ")";
169 db_query($sql,"check failed");
171 //$msg = _("A new component part has been added to the bill of material for this item.");
176 /*The component must already be on the bom */
177 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."));
180 } //end of if its not a recursive bom
183 display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
188 //--------------------------------------------------------------------------------------------------
190 if (isset($_GET['delete']))
193 $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $_GET['delete']. "'";
194 db_query($sql,"Could not delete this bom components");
196 display_note(_("The component item has been deleted from this bom."));
200 //--------------------------------------------------------------------------------------------------
202 start_form(false, true);
205 echo "<center>" . _("Select a manufacturable item:") . " ";
206 stock_bom_items_list('stock_id', null, false, true);
210 //--------------------------------------------------------------------------------------------------
212 if (isset($_POST['stock_id']))
213 { //Parent Item selected so display bom or edit component
214 $selected_parent = $_POST['stock_id'];
216 if (isset($selected_parent) && isset($_POST['Submit']))
217 on_submit($selected_parent, $selected_component);
219 //--------------------------------------------------------------------------------------
221 display_bom_items($selected_parent);
223 if (isset($selected_parent) && isset($selected_component))
225 hyperlink_params($_SERVER['PHP_SELF'], _("Add a new Component"), "NewItem=$selected_parent");
228 //--------------------------------------------------------------------------------------
230 start_form(false, true, $_SERVER['PHP_SELF'] . "?" . SID . "NewItem=" . $selected_parent);
232 start_table($table_style2);
234 if (isset($selected_component))
236 //editing a selected component from the link to the line item
237 $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
238 WHERE id='$selected_component'
239 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
241 $result = db_query($sql, "could not get bom");
242 $myrow = db_fetch($result);
244 $_POST['loc_code'] = $myrow["loc_code"];
245 $_POST['workcentre_added'] = $myrow["workcentre_added"];
246 $_POST['quantity'] = $myrow["quantity"];
248 hidden('selected_parent', $selected_parent);
249 hidden('selected_component', $selected_component);
250 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
254 { //end of if $selected_component
256 hidden('selected_parent', $selected_parent);
259 label_cell(_("Component:"));
262 stock_component_items_list('component', $selected_parent, $_POST['component'], false, true);
267 locations_list_row(_("Location to Draw From:"), 'loc_code', null);
268 workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
270 if (!isset($_POST['quantity']))
272 $_POST['quantity'] = 1;
274 text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 10, 18);
277 submit_center('Submit', _("Add/Update"));
282 // ----------------------------------------------------------------------------------