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