Added ajax extensions
[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 simple_page_mode(true);
20 $selected_component = $selected_id;
21 //--------------------------------------------------------------------------------------------------
22
23 //if (isset($_GET["NewItem"]))
24 //{
25 //      $_POST['stock_id'] = $_GET["NewItem"];
26 //}
27 //if (isset($_GET['stock_id']))
28 //{
29 //      $_POST['stock_id'] = $_GET['stock_id'];
30 //      $selected_parent =  $_GET['stock_id'];
31 //}
32
33 /* selected_parent could come from a post or a get */
34 /*if (isset($_GET["selected_parent"]))
35 {
36         $selected_parent = $_GET["selected_parent"];
37 }
38 else if (isset($_POST["selected_parent"]))
39 {
40         $selected_parent = $_POST["selected_parent"];
41 }
42 */
43 /* selected_component could also come from a post or a get */
44 /*if (isset($_GET["selected_component"]))
45 {
46         $selected_component = $_GET["selected_component"];
47 }
48 else
49 {
50         $selected_component = get_post("selected_component", -1);
51 }
52 */
53
54 //--------------------------------------------------------------------------------------------------
55
56 function check_for_recursive_bom($ultimate_parent, $component_to_check)
57 {
58
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 */
61
62         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
63         $result = db_query($sql,"could not check recursive bom");
64
65         if ($result != 0)
66         {
67                 while ($myrow = db_fetch_row($result))
68                 {
69                         if ($myrow[0] == $ultimate_parent)
70                         {
71                                 return 1;
72                         }
73
74                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
75                         {
76                                 return 1;
77                         }
78                 } //(while loop)
79         } //end if $result is true
80
81         return 0;
82
83 } //end of function check_for_recursive_bom
84
85 //--------------------------------------------------------------------------------------------------
86
87 function display_bom_items($selected_parent)
88 {
89         global $table_style;
90
91         $result = get_bom($selected_parent);
92 div_start('bom');
93         start_table("$table_style width=60%");
94         $th = array(_("Code"), _("Description"), _("Location"),
95                 _("Work Centre"), _("Quantity"), _("Units"),'','');
96         table_header($th);
97
98         $k = 0;
99         while ($myrow = db_fetch($result))
100         {
101
102                 alt_table_row_color($k);
103
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"));
112         end_row();
113
114         } //END WHILE LIST LOOP
115         end_table();
116 div_end();
117 }
118
119 //--------------------------------------------------------------------------------------------------
120
121 function on_submit($selected_parent, $selected_component=-1)
122 {
123         if (!check_num('quantity', 0))
124         {
125                 display_error(_("The quantity entered must be numeric and greater than zero."));
126                 set_focus('quantity');
127                 return;
128         }
129
130         if ($selected_component != -1)
131         {
132
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);
139
140                 db_query($sql,"could not update bom");
141                 display_notification(_('Selected component has been updated'));
142                 $Mode = 'RESET';
143         }
144         else
145         {
146
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
149                 component form */
150
151                 //need to check not recursive bom component of itself!
152                 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
153                 {
154
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");
162
163                         if (db_num_rows($result) == 0)
164                         {
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') . ")";
169
170                                 db_query($sql,"check failed");
171                                 display_notification(_("A new component part has been added to the bill of material for this item."));
172                                 $Mode = 'RESET';
173                         }
174                         else
175                         {
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."));
178                         }
179
180                 } //end of if its not a recursive bom
181                 else
182                 {
183                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
184                 }
185         }
186 }
187
188 //--------------------------------------------------------------------------------------------------
189
190 if ($Mode == 'Delete')
191 {
192         $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $selected_component. "'";
193         db_query($sql,"Could not delete this bom components");
194
195         display_notification(_("The component item has been deleted from this bom"));
196         $Mode = 'RESET';
197 }
198
199 if ($Mode == 'RESET')
200 {
201         $selected_component = -1;
202         unset($_POST['quantity']);
203 }
204
205 //--------------------------------------------------------------------------------------------------
206
207 start_form(false, true);
208
209 echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
210 stock_bom_items_list('selected_parent', null, false, true);
211 echo "</center><br>";
212
213 end_form();
214 if (isset($_POST['_selected_parent_update']))
215         $Ajax->activate('_page_body');
216 //--------------------------------------------------------------------------------------------------
217
218 if (get_post('selected_parent') != '')
219 { //Parent Item selected so display bom or edit component
220         $selected_parent = $_POST['selected_parent'];
221         if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
222                 on_submit($selected_parent, $selected_component);
223         //--------------------------------------------------------------------------------------
224
225 start_form();
226         display_bom_items($selected_parent);
227 //end_form();
228         //--------------------------------------------------------------------------------------
229         echo '<br>';
230 //      start_form(false, true);
231
232         start_table($table_style2);
233
234         if ($selected_component != -1)
235         {
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";
241
242                         $result = db_query($sql, "could not get bom");
243                         $myrow = db_fetch($result);
244
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"]));
248                 }
249                 hidden('component', $selected_component);
250                 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
251         }
252         else
253         {
254                 start_row();
255                 label_cell(_("Component:"));
256
257                 echo "<td>";
258                 stock_component_items_list('component', $selected_parent, null, false, true);
259                 if (get_post('_component_update')) 
260                 {
261                         $Ajax->activate('quantity');
262                 }
263                 echo "</td>";
264                 end_row();
265         }
266         hidden('selected_parent', $selected_parent);
267
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);
273
274         end_table(1);
275         submit_add_or_update_center($selected_component == -1, '', true);
276         end_form();
277 }
278 // ----------------------------------------------------------------------------------
279
280 end_page();
281
282 ?>