Partial (inventory related) include files cleanup.
[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 check_db_has_bom_stock_items(_("There are no manufactured or kit items defined in the system."));
23
24 check_db_has_workcentres(_("There are no work centres defined in the system. BOMs require at least one work centre be defined."));
25
26 simple_page_mode(true);
27 $selected_component = $selected_id;
28 //--------------------------------------------------------------------------------------------------
29
30 //if (isset($_GET["NewItem"]))
31 //{
32 //      $_POST['stock_id'] = $_GET["NewItem"];
33 //}
34 if (isset($_GET['stock_id']))
35 {
36         $_POST['stock_id'] = $_GET['stock_id'];
37         $selected_parent =  $_GET['stock_id'];
38 }
39
40 /* selected_parent could come from a post or a get */
41 /*if (isset($_GET["selected_parent"]))
42 {
43         $selected_parent = $_GET["selected_parent"];
44 }
45 else if (isset($_POST["selected_parent"]))
46 {
47         $selected_parent = $_POST["selected_parent"];
48 }
49 */
50 /* selected_component could also come from a post or a get */
51 /*if (isset($_GET["selected_component"]))
52 {
53         $selected_component = $_GET["selected_component"];
54 }
55 else
56 {
57         $selected_component = get_post("selected_component", -1);
58 }
59 */
60
61 //--------------------------------------------------------------------------------------------------
62
63 function display_bom_items($selected_parent)
64 {
65         $result = get_bom($selected_parent);
66         div_start('bom');
67         start_table(TABLESTYLE, "width='60%'");
68         $th = array(_("Code"), _("Description"), _("Location"),
69                 _("Work Centre"), _("Quantity"), _("Units"),'','');
70         table_header($th);
71
72         $k = 0;
73         while ($myrow = db_fetch($result))
74         {
75
76                 alt_table_row_color($k);
77
78                 label_cell($myrow["component"]);
79                 label_cell($myrow["description"]);
80         label_cell($myrow["location_name"]);
81         label_cell($myrow["WorkCentreDescription"]);
82         qty_cell($myrow["quantity"], false, get_qty_dec($myrow["component"]));
83         label_cell($myrow["units"]);
84                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
85                 delete_button_cell("Delete".$myrow['id'], _("Delete"));
86         end_row();
87
88         } //END WHILE LIST LOOP
89         end_table();
90         div_end();
91 }
92
93 //--------------------------------------------------------------------------------------------------
94
95 function on_submit($selected_parent, $selected_component=-1)
96 {
97         if (!check_num('quantity', 0))
98         {
99                 display_error(_("The quantity entered must be numeric and greater than zero."));
100                 set_focus('quantity');
101                 return;
102         }
103
104         if ($selected_component != -1)
105         {
106                 update_bom($selected_parent, $selected_component, $_POST['workcentre_added'], $_POST['loc_code'],
107                         input_num('quantity'));
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                         if (!is_component_already_on_bom($_POST['component'], $_POST['workcentre_added'],
124                                 $_POST['loc_code'], $selected_parent))
125                         {
126                                 add_bom($selected_parent, $_POST['component'], $_POST['workcentre_added'],
127                                         $_POST['loc_code'], input_num('quantity'));
128                                 display_notification(_("A new component part has been added to the bill of material for this item."));
129                                 $Mode = 'RESET';
130                         }
131                         else
132                         {
133                                 /*The component must already be on the bom */
134                                 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."));
135                         }
136
137                 } //end of if its not a recursive bom
138                 else
139                 {
140                         display_error(_("The selected component is a parent of the current item. Recursive BOMs are not allowed."));
141                 }
142         }
143 }
144
145 //--------------------------------------------------------------------------------------------------
146
147 if ($Mode == 'Delete')
148 {
149         delete_bom($selected_id);
150
151         display_notification(_("The component item has been deleted from this bom"));
152         $Mode = 'RESET';
153 }
154
155 if ($Mode == 'RESET')
156 {
157         $selected_id = -1;
158         unset($_POST['quantity']);
159 }
160
161 //--------------------------------------------------------------------------------------------------
162
163 start_form();
164
165 start_form(false, true);
166 start_table(TABLESTYLE_NOBORDER);
167 start_row();
168 stock_manufactured_items_list_cells(_("Select a manufacturable item:"), 'stock_id', null, false, true);
169 end_row();
170 if (list_updated('stock_id'))
171         $Ajax->activate('_page_body');
172 end_table();
173 br();
174
175 end_form();
176 //--------------------------------------------------------------------------------------------------
177
178 if (get_post('stock_id') != '')
179 { //Parent Item selected so display bom or edit component
180         $selected_parent = $_POST['stock_id'];
181         if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
182                 on_submit($selected_parent, $selected_id);
183         //--------------------------------------------------------------------------------------
184
185 start_form();
186         display_bom_items($selected_parent);
187         //--------------------------------------------------------------------------------------
188         echo '<br>';
189
190         start_table(TABLESTYLE2);
191
192         if ($selected_id != -1)
193         {
194                 if ($Mode == 'Edit') {
195                         //editing a selected component from the link to the line item
196                         $myrow = get_component_from_bom($selected_id);
197
198                         $_POST['loc_code'] = $myrow["loc_code"];
199                         $_POST['component'] = $myrow["component"]; // by Tom Moulton
200                         $_POST['workcentre_added']  = $myrow["workcentre_added"];
201                         $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
202                         label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
203                 }
204                 hidden('selected_id', $selected_id);
205         }
206         else
207         {
208                 start_row();
209                 label_cell(_("Component:"), "class='label'");
210
211                 echo "<td>";
212                 echo stock_component_items_list('component', $selected_parent, null, false, true);
213                 if (get_post('_component_update')) 
214                 {
215                         $Ajax->activate('quantity');
216                 }
217                 echo "</td>";
218                 end_row();
219         }
220         hidden('stock_id', $selected_parent);
221
222         locations_list_row(_("Location to Draw From:"), 'loc_code', null);
223         workcenter_list_row(_("Work Centre Added:"), 'workcentre_added', null);
224         $dec = get_qty_dec(get_post('component'));
225         $_POST['quantity'] = number_format2(input_num('quantity',1), $dec);
226         qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
227
228         end_table(1);
229         submit_add_or_update_center($selected_id == -1, '', 'both');
230         end_form();
231 }
232 // ----------------------------------------------------------------------------------
233
234 end_page();
235