d06b84d321a6f525bfb6e6d1c0ed5f4355ec1c66
[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 }
25 if (isset($_GET['stock_id']))
26 {
27         $_POST['stock_id'] = $_GET['stock_id'];
28         $selected_parent =  $_GET['stock_id'];
29 }
30
31 /* selected_parent could come from a post or a get */
32 if (isset($_GET["selected_parent"]))
33 {
34         $selected_parent = $_GET["selected_parent"];
35 }
36 else if (isset($_POST["selected_parent"]))
37 {
38         $selected_parent = $_POST["selected_parent"];
39 }
40 /* selected_component could also come from a post or a get */
41 if (isset($_GET["selected_component"]))
42 {
43         $selected_component = $_GET["selected_component"];
44 }
45 elseif (isset($_POST["selected_component"]))
46 {
47         $selected_component = $_POST["selected_component"];
48 }
49
50
51 //--------------------------------------------------------------------------------------------------
52
53 function check_for_recursive_bom($ultimate_parent, $component_to_check)
54 {
55
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 */
58
59         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
60         $result = db_query($sql,"could not check recursive bom");
61
62         if ($result != 0)
63         {
64                 while ($myrow = db_fetch_row($result))
65                 {
66                         if ($myrow[0] == $ultimate_parent)
67                         {
68                                 return 1;
69                         }
70
71                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
72                         {
73                                 return 1;
74                         }
75                 } //(while loop)
76         } //end if $result is true
77
78         return 0;
79
80 } //end of function check_for_recursive_bom
81
82 //--------------------------------------------------------------------------------------------------
83
84 function display_bom_items($selected_parent)
85 {
86         global $table_style;
87
88         $result = get_bom($selected_parent);
89 div_start('bom');
90         start_table("$table_style width=60%");
91         $th = array(_("Code"), _("Description"), _("Location"),
92                 _("Work Centre"), _("Quantity"), _("Units"),'','');
93         table_header($th);
94
95         $k = 0;
96         while ($myrow = db_fetch($result))
97         {
98
99                 alt_table_row_color($k);
100
101                 label_cell($myrow["component"]);
102                 label_cell($myrow["description"]);
103         label_cell($myrow["location_name"]);
104         label_cell($myrow["WorkCentreDescription"]);
105         qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
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']);
109         end_row();
110
111         } //END WHILE LIST LOOP
112         end_table();
113 div_end();
114 }
115
116 //--------------------------------------------------------------------------------------------------
117
118 function on_submit($selected_parent, $selected_component=null)
119 {
120         if (!check_num('quantity', 0))
121         {
122                 display_error(_("The quantity entered must be numeric and greater than zero."));
123                 set_focus('quantity');
124                 return;
125         }
126
127         if (isset($selected_parent) && isset($selected_component))
128         {
129
130                 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added='" . $_POST['workcentre_added'] . "',
131                         loc_code='" . $_POST['loc_code'] . "',
132                         quantity= " . input_num('quantity') . "
133                         WHERE parent='" . $selected_parent . "'
134                         AND id='" . $selected_component . "'";
135                 check_db_error("Could not update this bom component", $sql);
136
137                 db_query($sql,"could not update bom");
138
139         }
140         elseif (!isset($selected_component) && isset($selected_parent))
141         {
142
143                 /*Selected component is null cos no item selected on first time round
144                 so must be adding a record must be Submitting new entries in the new
145                 component form */
146
147                 //need to check not recursive bom component of itself!
148                 If (!check_for_recursive_bom($selected_parent, $_POST['component']))
149                 {
150
151                         /*Now check to see that the component is not already on the bom */
152                         $sql = "SELECT component FROM ".TB_PREF."bom
153                                 WHERE parent='$selected_parent'
154                                 AND component='" . $_POST['component'] . "'
155                                 AND workcentre_added='" . $_POST['workcentre_added'] . "'
156                                 AND loc_code='" . $_POST['loc_code'] . "'" ;
157                         $result = db_query($sql,"check failed");
158
159                         if (db_num_rows($result) == 0)
160                         {
161                                 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
162                                         VALUES ('$selected_parent', '" . $_POST['component'] . "', '"
163                                         . $_POST['workcentre_added'] . "', '" . $_POST['loc_code'] . "', "
164                                         . input_num('quantity') . ")";
165
166                                 db_query($sql,"check failed");
167
168                                 //$msg = _("A new component part has been added to the bill of material for this item.");
169
170                         }
171                         else
172                         {
173                                 /*The component must already be on the bom */
174                                 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."));
175                         }
176
177                 } //end of if its not a recursive bom
178                 else
179                 {
180                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
181                 }
182         }
183 }
184
185 //--------------------------------------------------------------------------------------------------
186
187 if (isset($_GET['delete']))
188 {
189
190         $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $_GET['delete']. "'";
191         db_query($sql,"Could not delete this bom components");
192
193         display_note(_("The component item has been deleted from this bom."));
194
195 }
196
197 //--------------------------------------------------------------------------------------------------
198
199 start_form(false, true);
200 //echo $msg;
201
202 echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
203 stock_bom_items_list('stock_id', null, false, true);
204 echo "</center>";
205
206 end_form();
207
208 if (isset($_POST['_stock_id_update']))
209         $Ajax->activate('bom');
210 //--------------------------------------------------------------------------------------------------
211
212 if (isset($_POST['stock_id']))
213 { //Parent Item selected so display bom or edit component
214         $selected_parent = $_POST['stock_id'];
215         if (isset($selected_parent) && isset($_POST['Submit'])) {
216           if(isset($selected_component))
217                 on_submit($selected_parent, $selected_component);
218           else
219                 on_submit($selected_parent);
220         }
221         //--------------------------------------------------------------------------------------
222
223         display_bom_items($selected_parent);
224
225         if (isset($selected_parent) && isset($selected_component))
226         {
227                 hyperlink_params($_SERVER['PHP_SELF'], _("Add a new Component"), "NewItem=$selected_parent");
228         }
229
230         //--------------------------------------------------------------------------------------
231
232         start_form(false, true, $_SERVER['PHP_SELF'] . "?" . SID . "NewItem=" . $selected_parent);
233
234         start_table($table_style2);
235
236         if (isset($selected_component))
237         {
238                 //editing a selected component from the link to the line item
239                 $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM ".TB_PREF."bom,".TB_PREF."stock_master
240                         WHERE id='$selected_component'
241                         AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
242
243                 $result = db_query($sql, "could not get bom");
244                 $myrow = db_fetch($result);
245
246                 $_POST['loc_code'] = $myrow["loc_code"];
247                 $_POST['workcentre_added']  = $myrow["workcentre_added"];
248                 $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
249
250                 hidden('selected_parent', $selected_parent);
251                 hidden('selected_component', $selected_component);
252                 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
253
254         }
255         else
256         { //end of if $selected_component
257
258                 hidden('selected_parent', $selected_parent);
259
260                 start_row();
261                 label_cell(_("Component:"));
262
263                 echo "<td>";
264                 stock_component_items_list('component', $selected_parent, null, false, true);
265                 echo "</td>";
266                 end_row();
267         }
268
269         locations_list_row(_("Location to Draw From:"), 'loc_code', null);
270         workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
271         $dec = get_qty_dec($_POST['component']);
272         if (!isset($_POST['quantity']))
273         {
274                 $_POST['quantity'] = number_format2(1, $dec);
275         }
276         qty_row(_("Quantity:"), 'quantity', $_POST['quantity'], null, null, $dec);
277
278         end_table(1);
279         submit_center('Submit', _("Add/Update"));
280
281         end_form();
282 }
283
284 // ----------------------------------------------------------------------------------
285
286 end_page();
287
288 ?>