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 function check_for_recursive_bom($ultimate_parent, $component_to_check)
26 /* returns true ie 1 if the bom contains the parent part as a component
27 ie the bom is recursive otherwise false ie 0 */
29 $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
30 $result = db_query($sql,"could not check recursive bom");
34 while ($myrow = db_fetch_row($result))
36 if ($myrow[0] == $ultimate_parent)
41 if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
46 } //end if $result is true
50 } //end of function check_for_recursive_bom
52 //--------------------------------------------------------------------------------------------------
54 function display_bom_items($selected_parent)
58 $result = get_bom($selected_parent);
60 start_table("$table_style width=60%");
61 $th = array(_("Code"), _("Description"), _("Location"),
62 _("Work Centre"), _("Quantity"), _("Units"),'','');
66 while ($myrow = db_fetch($result))
69 alt_table_row_color($k);
71 label_cell($myrow["component"]);
72 label_cell($myrow["description"]);
73 label_cell($myrow["location_name"]);
74 label_cell($myrow["WorkCentreDescription"]);
75 qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
76 label_cell($myrow["units"]);
77 edit_button_cell("Edit".$myrow['id'], _("Edit"));
78 edit_button_cell("Delete".$myrow['id'], _("Delete"));
81 } //END WHILE LIST LOOP
86 //--------------------------------------------------------------------------------------------------
88 function on_submit($selected_parent, $selected_component=-1)
90 if (!check_num('quantity', 0))
92 display_error(_("The quantity entered must be numeric and greater than zero."));
93 set_focus('quantity');
97 if ($selected_component != -1)
100 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
101 loc_code='" . $_POST['loc_code'] . "',
102 quantity= " . input_num('quantity') . "
103 WHERE parent='" . $selected_parent . "'
104 AND id='" . $selected_component . "'";
105 check_db_error("Could not update this bom component", $sql);
107 db_query($sql,"could not update bom");
108 display_notification(_('Selected component has been updated'));
114 /*Selected component is null cos no item selected on first time round
115 so must be adding a record must be Submitting new entries in the new
118 //need to check not recursive bom component of itself!
119 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
122 /*Now check to see that the component is not already on the bom */
123 $sql = "SELECT component FROM ".TB_PREF."bom
124 WHERE parent='$selected_parent'
125 AND component='" . $_POST['component'] . "'
126 AND workcentre_added='" . $_POST['workcentre_added'] . "'
127 AND loc_code='" . $_POST['loc_code'] . "'" ;
128 $result = db_query($sql,"check failed");
130 if (db_num_rows($result) == 0)
132 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
133 VALUES ('$selected_parent', '" . $_POST['component'] . "', '"
134 . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', "
135 . input_num('quantity') . ")";
137 db_query($sql,"check failed");
138 display_notification(_("A new component part has been added to the bill of material for this item."));
143 /*The component must already be on the bom */
144 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."));
147 } //end of if its not a recursive bom
150 display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
155 //--------------------------------------------------------------------------------------------------
157 if ($Mode == 'Delete')
159 $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $selected_id. "'";
160 db_query($sql,"Could not delete this bom components");
162 display_notification(_("The component item has been deleted from this bom"));
166 if ($Mode == 'RESET')
169 unset($_POST['quantity']);
172 //--------------------------------------------------------------------------------------------------
174 start_form(false, true);
176 echo "<center>" . _("Select a manufacturable item:") . " ";
177 stock_bom_items_list('selected_parent', null, false, true);
178 echo "</center><br>";
181 if (isset($_POST['_selected_parent_update']))
182 $Ajax->activate('_page_body');
183 //--------------------------------------------------------------------------------------------------
185 if (get_post('selected_parent') != '')
186 { //Parent Item selected so display bom or edit component
187 $selected_parent = $_POST['selected_parent'];
188 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
189 on_submit($selected_parent, $selected_id);
190 //--------------------------------------------------------------------------------------
193 display_bom_items($selected_parent);
195 //--------------------------------------------------------------------------------------
197 // start_form(false, true);
199 start_table($table_style2);
201 if ($selected_id != -1)
203 if ($Mode == 'Edit') {
204 //editing a selected component from the link to the line item
205 $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
206 WHERE id='$selected_id'
207 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
209 $result = db_query($sql, "could not get bom");
210 $myrow = db_fetch($result);
212 $_POST['loc_code'] = $myrow["loc_code"];
213 $_POST['workcentre_added'] = $myrow["workcentre_added"];
214 $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
215 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
217 hidden('selected_id', $selected_id);
222 label_cell(_("Component:"));
225 stock_component_items_list('component', $selected_parent, null, false, true);
226 if (get_post('_component_update'))
228 $Ajax->activate('quantity');
233 hidden('selected_parent', $selected_parent);
235 locations_list_row(_("Location to Draw From:"), 'loc_code', null);
236 workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
237 $dec = get_qty_dec(get_post('component'));
238 $_POST['quantity'] = number_format2(input_num('quantity',1), $dec);
239 qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
242 submit_add_or_update_center($selected_id == -1, '', true);
245 // ----------------------------------------------------------------------------------