Bugfix [0000080] - BOM line was not editable.
[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 function check_for_recursive_bom($ultimate_parent, $component_to_check)
24 {
25
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 */
28
29         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent='$component_to_check'";
30         $result = db_query($sql,"could not check recursive bom");
31
32         if ($result != 0)
33         {
34                 while ($myrow = db_fetch_row($result))
35                 {
36                         if ($myrow[0] == $ultimate_parent)
37                         {
38                                 return 1;
39                         }
40
41                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
42                         {
43                                 return 1;
44                         }
45                 } //(while loop)
46         } //end if $result is true
47
48         return 0;
49
50 } //end of function check_for_recursive_bom
51
52 //--------------------------------------------------------------------------------------------------
53
54 function display_bom_items($selected_parent)
55 {
56         global $table_style;
57
58         $result = get_bom($selected_parent);
59 div_start('bom');
60         start_table("$table_style width=60%");
61         $th = array(_("Code"), _("Description"), _("Location"),
62                 _("Work Centre"), _("Quantity"), _("Units"),'','');
63         table_header($th);
64
65         $k = 0;
66         while ($myrow = db_fetch($result))
67         {
68
69                 alt_table_row_color($k);
70
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"));
79         end_row();
80
81         } //END WHILE LIST LOOP
82         end_table();
83 div_end();
84 }
85
86 //--------------------------------------------------------------------------------------------------
87
88 function on_submit($selected_parent, $selected_component=-1)
89 {
90         if (!check_num('quantity', 0))
91         {
92                 display_error(_("The quantity entered must be numeric and greater than zero."));
93                 set_focus('quantity');
94                 return;
95         }
96
97         if ($selected_component != -1)
98         {
99
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);
106
107                 db_query($sql,"could not update bom");
108                 display_notification(_('Selected component has been updated'));
109                 $Mode = 'RESET';
110         }
111         else
112         {
113
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
116                 component form */
117
118                 //need to check not recursive bom component of itself!
119                 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
120                 {
121
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");
129
130                         if (db_num_rows($result) == 0)
131                         {
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') . ")";
136
137                                 db_query($sql,"check failed");
138                                 display_notification(_("A new component part has been added to the bill of material for this item."));
139                                 $Mode = 'RESET';
140                         }
141                         else
142                         {
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."));
145                         }
146
147                 } //end of if its not a recursive bom
148                 else
149                 {
150                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
151                 }
152         }
153 }
154
155 //--------------------------------------------------------------------------------------------------
156
157 if ($Mode == 'Delete')
158 {
159         $sql = "DELETE FROM ".TB_PREF."bom WHERE id='" . $selected_id. "'";
160         db_query($sql,"Could not delete this bom components");
161
162         display_notification(_("The component item has been deleted from this bom"));
163         $Mode = 'RESET';
164 }
165
166 if ($Mode == 'RESET')
167 {
168         $selected_id = -1;
169         unset($_POST['quantity']);
170 }
171
172 //--------------------------------------------------------------------------------------------------
173
174 start_form(false, true);
175
176 echo "<center>" . _("Select a manufacturable item:") . "&nbsp;";
177 stock_bom_items_list('selected_parent', null, false, true);
178 echo "</center><br>";
179
180 end_form();
181 if (isset($_POST['_selected_parent_update']))
182         $Ajax->activate('_page_body');
183 //--------------------------------------------------------------------------------------------------
184
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         //--------------------------------------------------------------------------------------
191
192 start_form();
193         display_bom_items($selected_parent);
194 //end_form();
195         //--------------------------------------------------------------------------------------
196         echo '<br>';
197 //      start_form(false, true);
198
199         start_table($table_style2);
200
201         if ($selected_id != -1)
202         {
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";
208
209                         $result = db_query($sql, "could not get bom");
210                         $myrow = db_fetch($result);
211
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"]);
216                 }
217                 hidden('selected_id', $selected_id);
218         }
219         else
220         {
221                 start_row();
222                 label_cell(_("Component:"));
223
224                 echo "<td>";
225                 stock_component_items_list('component', $selected_parent, null, false, true);
226                 if (get_post('_component_update')) 
227                 {
228                         $Ajax->activate('quantity');
229                 }
230                 echo "</td>";
231                 end_row();
232         }
233         hidden('selected_parent', $selected_parent);
234
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);
240
241         end_table(1);
242         submit_add_or_update_center($selected_id == -1, '', true);
243         end_form();
244 }
245 // ----------------------------------------------------------------------------------
246
247 end_page();
248
249 ?>