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