Update from usntable branch.
[fa-stable.git] / manufacturing / manage / bom_edit.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_BOM';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Bill Of Materials"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/includes/manufacturing.inc");
23
24 check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));
25
26 check_db_has_workcentres(_("There are no work centres defined in the system. BOMs require at least one work centre be defined."));
27
28 simple_page_mode(true);
29 $selected_component = $selected_id;
30 //--------------------------------------------------------------------------------------------------
31
32 //if (isset($_GET["NewItem"]))
33 //{
34 //      $_POST['stock_id'] = $_GET["NewItem"];
35 //}
36 if (isset($_GET['stock_id']))
37 {
38         $_POST['stock_id'] = $_GET['stock_id'];
39         $selected_parent =  $_GET['stock_id'];
40 }
41
42 /* selected_parent could come from a post or a get */
43 /*if (isset($_GET["selected_parent"]))
44 {
45         $selected_parent = $_GET["selected_parent"];
46 }
47 else if (isset($_POST["selected_parent"]))
48 {
49         $selected_parent = $_POST["selected_parent"];
50 }
51 */
52 /* selected_component could also come from a post or a get */
53 /*if (isset($_GET["selected_component"]))
54 {
55         $selected_component = $_GET["selected_component"];
56 }
57 else
58 {
59         $selected_component = get_post("selected_component", -1);
60 }
61 */
62
63 //--------------------------------------------------------------------------------------------------
64
65 function check_for_recursive_bom($ultimate_parent, $component_to_check)
66 {
67
68         /* returns true ie 1 if the bom contains the parent part as a component
69         ie the bom is recursive otherwise false ie 0 */
70
71         $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
72         $result = db_query($sql,"could not check recursive bom");
73
74         if ($result != 0)
75         {
76                 while ($myrow = db_fetch_row($result))
77                 {
78                         if ($myrow[0] == $ultimate_parent)
79                         {
80                                 return 1;
81                         }
82
83                         if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
84                         {
85                                 return 1;
86                         }
87                 } //(while loop)
88         } //end if $result is true
89
90         return 0;
91
92 } //end of function check_for_recursive_bom
93
94 //--------------------------------------------------------------------------------------------------
95
96 function display_bom_items($selected_parent)
97 {
98         global $table_style;
99
100         $result = get_bom($selected_parent);
101 div_start('bom');
102         start_table("$table_style width=60%");
103         $th = array(_("Code"), _("Description"), _("Location"),
104                 _("Work Centre"), _("Quantity"), _("Units"),'','');
105         table_header($th);
106
107         $k = 0;
108         while ($myrow = db_fetch($result))
109         {
110
111                 alt_table_row_color($k);
112
113                 label_cell($myrow["component"]);
114                 label_cell($myrow["description"]);
115         label_cell($myrow["location_name"]);
116         label_cell($myrow["WorkCentreDescription"]);
117         qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
118         label_cell($myrow["units"]);
119                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
120                 delete_button_cell("Delete".$myrow['id'], _("Delete"));
121         end_row();
122
123         } //END WHILE LIST LOOP
124         end_table();
125 div_end();
126 }
127
128 //--------------------------------------------------------------------------------------------------
129
130 function on_submit($selected_parent, $selected_component=-1)
131 {
132         if (!check_num('quantity', 0))
133         {
134                 display_error(_("The quantity entered must be numeric and greater than zero."));
135                 set_focus('quantity');
136                 return;
137         }
138
139         if ($selected_component != -1)
140         {
141
142                 $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($_POST['workcentre_added'])
143                  . ",loc_code=".db_escape($_POST['loc_code']) . ",
144                         quantity= " . input_num('quantity') . "
145                         WHERE parent=".db_escape($selected_parent) . "
146                         AND id=".db_escape($selected_component);
147                 check_db_error("Could not update this bom component", $sql);
148
149                 db_query($sql,"could not update bom");
150                 display_notification(_('Selected component has been updated'));
151                 $Mode = 'RESET';
152         }
153         else
154         {
155
156                 /*Selected component is null cos no item selected on first time round
157                 so must be adding a record must be Submitting new entries in the new
158                 component form */
159
160                 //need to check not recursive bom component of itself!
161                 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
162                 {
163
164                         /*Now check to see that the component is not already on the bom */
165                         $sql = "SELECT component FROM ".TB_PREF."bom
166                                 WHERE parent=".db_escape($selected_parent)."
167                                 AND component=".db_escape($_POST['component']) . "
168                                 AND workcentre_added=".db_escape($_POST['workcentre_added']) . "
169                                 AND loc_code=".db_escape($_POST['loc_code']);
170                         $result = db_query($sql,"check failed");
171
172                         if (db_num_rows($result) == 0)
173                         {
174                                 $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
175                                         VALUES (".db_escape($selected_parent).", ".db_escape($_POST['component']) . ","
176                                         .db_escape($_POST['workcentre_added']) . ", ".db_escape($_POST['loc_code']) . ", "
177                                         . input_num('quantity') . ")";
178
179                                 db_query($sql,"check failed");
180                                 display_notification(_("A new component part has been added to the bill of material for this item."));
181                                 $Mode = 'RESET';
182                         }
183                         else
184                         {
185                                 /*The component must already be on the bom */
186                                 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."));
187                         }
188
189                 } //end of if its not a recursive bom
190                 else
191                 {
192                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
193                 }
194         }
195 }
196
197 //--------------------------------------------------------------------------------------------------
198
199 if ($Mode == 'Delete')
200 {
201         $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
202         db_query($sql,"Could not delete this bom components");
203
204         display_notification(_("The component item has been deleted from this bom"));
205         $Mode = 'RESET';
206 }
207
208 if ($Mode == 'RESET')
209 {
210         $selected_id = -1;
211         unset($_POST['quantity']);
212 }
213
214 //--------------------------------------------------------------------------------------------------
215
216 start_form();
217
218 start_form(false, true);
219 start_table("class='tablestyle_noborder'");
220 stock_manufactured_items_list_row(_("Select a manufacturable item:"), 'stock_id', null, false, true);
221 if (list_updated('stock_id'))
222         $Ajax->activate('_page_body');
223 end_table();
224 br();
225
226 end_form();
227 //--------------------------------------------------------------------------------------------------
228
229 if (get_post('stock_id') != '')
230 { //Parent Item selected so display bom or edit component
231         $selected_parent = $_POST['stock_id'];
232         if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
233                 on_submit($selected_parent, $selected_id);
234         //--------------------------------------------------------------------------------------
235
236 start_form();
237         display_bom_items($selected_parent);
238         //--------------------------------------------------------------------------------------
239         echo '<br>';
240
241         start_table($table_style2);
242
243         if ($selected_id != -1)
244         {
245                 if ($Mode == 'Edit') {
246                         //editing a selected component from the link to the line item
247                         $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM "
248                                 .TB_PREF."bom,".TB_PREF."stock_master
249                                 WHERE id=".db_escape($selected_id)."
250                                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
251
252                         $result = db_query($sql, "could not get bom");
253                         $myrow = db_fetch($result);
254
255                         $_POST['loc_code'] = $myrow["loc_code"];
256                         $_POST['component'] = $myrow["component"]; // by Tom Moulton
257                         $_POST['workcentre_added']  = $myrow["workcentre_added"];
258                         $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
259                 label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
260                 }
261                 hidden('selected_id', $selected_id);
262         }
263         else
264         {
265                 start_row();
266                 label_cell(_("Component:"));
267
268                 echo "<td>";
269                 echo stock_component_items_list('component', $selected_parent, null, false, true);
270                 if (get_post('_component_update')) 
271                 {
272                         $Ajax->activate('quantity');
273                 }
274                 echo "</td>";
275                 end_row();
276         }
277         hidden('stock_id', $selected_parent);
278
279         locations_list_row(_("Location to Draw From:"), 'loc_code', null);
280         workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
281         $dec = get_qty_dec(get_post('component'));
282         $_POST['quantity'] = number_format2(input_num('quantity',1), $dec);
283         qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
284
285         end_table(1);
286         submit_add_or_update_center($selected_id == -1, '', 'both');
287         end_form();
288 }
289 // ----------------------------------------------------------------------------------
290
291 end_page();
292
293 ?>