e963d5634f53192d22aaf161ad1e68b6e8f3ab88
[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 display_bom_items($selected_parent)
66 {
67         global $table_style;
68
69         $result = get_bom($selected_parent);
70         div_start('bom');
71         start_table("$table_style width=60%");
72         $th = array(_("Code"), _("Description"), _("Location"),
73                 _("Work Centre"), _("Quantity"), _("Units"),'','');
74         table_header($th);
75
76         $k = 0;
77         while ($myrow = db_fetch($result))
78         {
79
80                 alt_table_row_color($k);
81
82                 label_cell($myrow["component"]);
83                 label_cell($myrow["description"]);
84         label_cell($myrow["location_name"]);
85         label_cell($myrow["WorkCentreDescription"]);
86         qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
87         label_cell($myrow["units"]);
88                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
89                 delete_button_cell("Delete".$myrow['id'], _("Delete"));
90         end_row();
91
92         } //END WHILE LIST LOOP
93         end_table();
94         div_end();
95 }
96
97 //--------------------------------------------------------------------------------------------------
98
99 function on_submit($selected_parent, $selected_component=-1)
100 {
101         if (!check_num('quantity', 0))
102         {
103                 display_error(_("The quantity entered must be numeric and greater than zero."));
104                 set_focus('quantity');
105                 return;
106         }
107
108         if ($selected_component != -1)
109         {
110                 update_bom($selected_parent, $selected_component, $_POST['workcentre_added'], $_POST['loc_code'],
111                         input_num('quantity'));
112                 display_notification(_('Selected component has been updated'));
113                 $Mode = 'RESET';
114         }
115         else
116         {
117
118                 /*Selected component is null cos no item selected on first time round
119                 so must be adding a record must be Submitting new entries in the new
120                 component form */
121
122                 //need to check not recursive bom component of itself!
123                 if (!check_for_recursive_bom($selected_parent, $_POST['component']))
124                 {
125
126                         /*Now check to see that the component is not already on the bom */
127                         if (!is_component_already_on_bom($_POST['component'], $_POST['workcentre_added'],
128                                 $_POST['loc_code'], $selected_parent))
129                         {
130                                 add_bom($selected_parent, $_POST['component'], $_POST['workcentre_added'],
131                                         $_POST['loc_code'], input_num('quantity'));
132                                 display_notification(_("A new component part has been added to the bill of material for this item."));
133                                 $Mode = 'RESET';
134                         }
135                         else
136                         {
137                                 /*The component must already be on the bom */
138                                 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."));
139                         }
140
141                 } //end of if its not a recursive bom
142                 else
143                 {
144                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
145                 }
146         }
147 }
148
149 //--------------------------------------------------------------------------------------------------
150
151 if ($Mode == 'Delete')
152 {
153         delete_bom($selected_id);
154
155         display_notification(_("The component item has been deleted from this bom"));
156         $Mode = 'RESET';
157 }
158
159 if ($Mode == 'RESET')
160 {
161         $selected_id = -1;
162         unset($_POST['quantity']);
163 }
164
165 //--------------------------------------------------------------------------------------------------
166
167 start_form();
168
169 start_form(false, true);
170 start_table("class='tablestyle_noborder'");
171 stock_manufactured_items_list_row(_("Select a manufacturable item:"), 'stock_id', null, false, true);
172 if (list_updated('stock_id'))
173         $Ajax->activate('_page_body');
174 end_table();
175 br();
176
177 end_form();
178 //--------------------------------------------------------------------------------------------------
179
180 if (get_post('stock_id') != '')
181 { //Parent Item selected so display bom or edit component
182         $selected_parent = $_POST['stock_id'];
183         if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
184                 on_submit($selected_parent, $selected_id);
185         //--------------------------------------------------------------------------------------
186
187 start_form();
188         display_bom_items($selected_parent);
189         //--------------------------------------------------------------------------------------
190         echo '<br>';
191
192         start_table($table_style2);
193
194         if ($selected_id != -1)
195         {
196                 if ($Mode == 'Edit') {
197                         //editing a selected component from the link to the line item
198                         $myrow = get_component_from_bom($selected_id);
199
200                         $_POST['loc_code'] = $myrow["loc_code"];
201                         $_POST['component'] = $myrow["component"]; // by Tom Moulton
202                         $_POST['workcentre_added']  = $myrow["workcentre_added"];
203                         $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
204                         label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
205                 }
206                 hidden('selected_id', $selected_id);
207         }
208         else
209         {
210                 start_row();
211                 label_cell(_("Component:"));
212
213                 echo "<td>";
214                 echo stock_component_items_list('component', $selected_parent, null, false, true);
215                 if (get_post('_component_update')) 
216                 {
217                         $Ajax->activate('quantity');
218                 }
219                 echo "</td>";
220                 end_row();
221         }
222         hidden('stock_id', $selected_parent);
223
224         locations_list_row(_("Location to Draw From:"), 'loc_code', null);
225         workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
226         $dec = get_qty_dec(get_post('component'));
227         $_POST['quantity'] = number_format2(input_num('quantity',1), $dec);
228         qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
229
230         end_table(1);
231         submit_add_or_update_center($selected_id == -1, '', 'both');
232         end_form();
233 }
234 // ----------------------------------------------------------------------------------
235
236 end_page();
237
238 ?>