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 simple_page_mode(true);
20 $selected_component = $selected_id;
21 //--------------------------------------------------------------------------------------------------
23 //if (isset($_GET["NewItem"]))
25 // $_POST['stock_id'] = $_GET["NewItem"];
27 if (isset($_GET['stock_id']))
29 $_POST['stock_id'] = $_GET['stock_id'];
30 $selected_parent = $_GET['stock_id'];
33 /* selected_parent could come from a post or a get */
34 /*if (isset($_GET["selected_parent"]))
36 $selected_parent = $_GET["selected_parent"];
38 else if (isset($_POST["selected_parent"]))
40 $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"];
50 $selected_component = get_post("selected_component", -1);
54 //--------------------------------------------------------------------------------------------------
56 function check_for_recursive_bom($ultimate_parent, $component_to_check)
59 /* returns true ie 1 if the bom contains the parent part as a component
60 ie the bom is recursive otherwise false ie 0 */
62 $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
63 $result = db_query($sql,"could not check recursive bom");
67 while ($myrow = db_fetch_row($result))
69 if ($myrow[0] == $ultimate_parent)
74 if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
79 } //end if $result is true
83 } //end of function check_for_recursive_bom
85 //--------------------------------------------------------------------------------------------------
87 function display_bom_items($selected_parent)
91 $result = get_bom($selected_parent);
93 start_table("$table_style width=60%");
94 $th = array(_("Code"), _("Description"), _("Location"),
95 _("Work Centre"), _("Quantity"), _("Units"),'','');
99 while ($myrow = db_fetch($result))
102 alt_table_row_color($k);
104 label_cell($myrow["component"]);
105 label_cell($myrow["description"]);
106 label_cell($myrow["location_name"]);
107 label_cell($myrow["WorkCentreDescription"]);
108 qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
109 label_cell($myrow["units"]);
110 edit_button_cell("Edit".$myrow['id'], _("Edit"));
111 edit_button_cell("Delete".$myrow['id'], _("Delete"));
114 } //END WHILE LIST LOOP
119 //--------------------------------------------------------------------------------------------------
121 function on_submit($selected_parent, $selected_component=-1)
123 if (!check_num('quantity', 0))
125 display_error(_("The quantity entered must be numeric and greater than zero."));
126 set_focus('quantity');
130 if ($selected_component != -1)
133 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
134 loc_code='" . $_POST['loc_code'] . "',
135 quantity= " . input_num('quantity') . "
136 WHERE parent='" . $selected_parent . "'
137 AND id='" . $selected_component . "'";
138 check_db_error("Could not update this bom component", $sql);
140 db_query($sql,"could not update bom");
141 display_notification(_('Selected component has been updated'));
147 /*Selected component is null cos no item selected on first time round
148 so must be adding a record must be Submitting new entries in the new
151 //need to check not recursive bom component of itself!
152 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
155 /*Now check to see that the component is not already on the bom */
156 $sql = "SELECT component FROM ".TB_PREF."bom
157 WHERE parent='$selected_parent'
158 AND component='" . $_POST['component'] . "'
159 AND workcentre_added='" . $_POST['workcentre_added'] . "'
160 AND loc_code='" . $_POST['loc_code'] . "'" ;
161 $result = db_query($sql,"check failed");
163 if (db_num_rows($result) == 0)
165 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
166 VALUES ('$selected_parent', '" . $_POST['component'] . "', '"
167 . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', "
168 . input_num('quantity') . ")";
170 db_query($sql,"check failed");
171 display_notification(_("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 ($Mode == 'Delete')
192 $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $selected_component. "'";
193 db_query($sql,"Could not delete this bom components");
195 display_notification(_("The component item has been deleted from this bom"));
199 if ($Mode == 'RESET')
201 $selected_component = -1;
202 unset($_POST['quantity']);
205 //--------------------------------------------------------------------------------------------------
207 start_form(false, true);
209 echo "<center>" . _("Select a manufacturable item:") . " ";
210 stock_bom_items_list('stock_id', null, false, true);
211 echo "</center><br>";
214 if (isset($_POST['_stock_id_update']))
215 $Ajax->activate('_page_body');
216 //--------------------------------------------------------------------------------------------------
218 if (get_post('stock_id') != '')
219 { //Parent Item selected so display bom or edit component
220 $selected_parent = $_POST['stock_id'];
221 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
222 on_submit($selected_parent, $selected_component);
223 //--------------------------------------------------------------------------------------
226 display_bom_items($selected_parent);
228 //--------------------------------------------------------------------------------------
230 // start_form(false, true);
232 start_table($table_style2);
234 if ($selected_component != -1)
236 if ($Mode == 'Edit') {
237 //editing a selected component from the link to the line item
238 $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
239 WHERE id='$selected_component'
240 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
242 $result = db_query($sql, "could not get bom");
243 $myrow = db_fetch($result);
245 $_POST['loc_code'] = $myrow["loc_code"];
246 $_POST['workcentre_added'] = $myrow["workcentre_added"];
247 $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
249 hidden('component', $selected_component);
250 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
255 label_cell(_("Component:"));
258 stock_component_items_list('component', $selected_parent, null, false, true);
259 if (get_post('_component_update'))
261 $Ajax->activate('quantity');
266 hidden('stock_id', $selected_parent);
268 locations_list_row(_("Location to Draw From:"), 'loc_code', null);
269 workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
270 $dec = get_qty_dec(get_post('component'));
271 $_POST['quantity'] = number_format2(input_num('quantity',1), $dec);
272 qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
275 submit_add_or_update_center($selected_component == -1, '', true);
278 // ----------------------------------------------------------------------------------