Fixed regression in Sales Kits.
[fa-stable.git] / inventory / manage / sales_kits.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_SALESKIT';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Sales Kits & Alias Codes"));
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_stock_items(_("There are no items defined in the system."));
25
26 simple_page_mode(true);
27 /*
28 if (isset($_GET['item_code']))
29 {
30         $_POST['item_code'] = $_GET['item_code'];
31         $selected_kit =  $_GET['item_code'];
32 }
33 */
34 //--------------------------------------------------------------------------------------------------
35 function display_kit_items($selected_kit)
36 {
37         $result = get_item_kit($selected_kit);
38         div_start('bom');
39         start_table(TABLESTYLE, "width='60%'");
40         $th = array(_("Stock Item"), _("Description"), _("Quantity"), _("Units"),
41                 '','');
42         table_header($th);
43
44         $k = 0;
45         while ($myrow = db_fetch($result))
46         {
47
48                 alt_table_row_color($k);
49
50                 label_cell($myrow["stock_id"]);
51                 label_cell($myrow["comp_name"]);
52         qty_cell($myrow["quantity"], false, 
53                         $myrow["units"] == '' ? 0 : get_qty_dec($myrow["comp_name"]));
54         label_cell($myrow["units"] == '' ? _('kit') : $myrow["units"]);
55                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
56                 delete_button_cell("Delete".$myrow['id'], _("Delete"));
57         end_row();
58
59         } //END WHILE LIST LOOP
60         end_table();
61 div_end();
62 }
63
64 //--------------------------------------------------------------------------------------------------
65
66 function update_component($kit_code, $selected_item)
67 {
68         global $Mode, $Ajax, $selected_kit;
69         
70         if (!check_num('quantity', 0))
71         {
72                 display_error(_("The quantity entered must be numeric and greater than zero."));
73                 set_focus('quantity');
74                 return;
75         }
76         elseif (get_post('description') == '')
77         {
78         display_error( _("Item code description cannot be empty."));
79                 set_focus('description');
80                 return;
81         }
82         elseif ($selected_item == -1)   // adding new item or new alias/kit
83         {
84                 if (get_post('item_code') == '') { // New kit/alias definition
85                         $kit = get_item_kit($kit_code);
86                 if (db_num_rows($kit)) {
87                                 $input_error = 1;
88                         display_error( _("This item code is already assigned to stock item or sale kit."));
89                                 set_focus('kit_code');
90                                 return;
91                         }
92                         if ($kit_code == '') {
93                         display_error( _("Kit/alias code cannot be empty."));
94                                 set_focus('kit_code');
95                                 return;
96                         }
97                 }
98         }
99
100         if (check_item_in_kit($selected_item, $kit_code, get_post('component'), true)) {
101                 display_error(_("The selected component contains directly or on any lower level the kit under edition. Recursive kits are not allowed."));
102                 set_focus('component');
103                 return;
104         }
105
106                 /*Now check to see that the component is not already in the kit */
107         if (check_item_in_kit($selected_item, $kit_code, get_post('component'))) {
108                 display_error(_("The selected component is already in this kit. You can modify it's quantity but it cannot appear more than once in the same kit."));
109                 set_focus('component');
110                 return;
111         }
112         if ($selected_item == -1) { // new item alias/kit
113                 if (get_post('item_code') == '') {
114                         $selected_kit = $_POST['item_code'] = $kit_code;
115                         $msg = _("New alias code has been created.");
116                 } 
117                  else
118                         $msg =_("New component has been added to selected kit.");
119
120                 add_item_code( $kit_code, get_post('component'), get_post('description'),
121                          get_post('category'), input_num('quantity'), 0);
122                 display_notification($msg);
123
124         } else {
125                 $props = get_kit_props(get_post('item_code'));
126                 update_item_code($selected_item, $kit_code, get_post('component'),
127                         $props['description'], $props['category_id'], input_num('quantity'), 0);
128                 display_notification(_("Component of selected kit has been updated."));
129         }
130         $Mode = 'RESET';
131         $Ajax->activate('_page_body');
132 }
133
134 //--------------------------------------------------------------------------------------------------
135
136 if (get_post('update_name')) {
137         update_kit_props(get_post('item_code'), get_post('description'), get_post('category'));
138         display_notification(_('Kit common properties has been updated'));
139         $Ajax->activate('_page_body');
140 }
141
142 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
143         update_component($_POST['item_code'], $selected_id);
144
145 if ($Mode == 'Delete')
146 {
147         // Before removing last component from selected kit check 
148         // if selected kit is not included in any other kit. 
149         // 
150         $other_kits = get_where_used($_POST['item_code']);
151         $num_kits = db_num_rows($other_kits);
152
153         $kit = get_item_kit($_POST['item_code']);
154         if ((db_num_rows($kit) == 1) && $num_kits) {
155
156                 $msg = _("This item cannot be deleted because it is the last item in the kit used by following kits")
157                         .':<br>';
158
159                 while($num_kits--) {
160                         $kit = db_fetch($other_kits);
161                         $msg .= "'".$kit[0]."'";
162                         if ($num_kits) $msg .= ',';
163                 }
164                 display_error($msg);
165         } else {
166                 delete_item_code($selected_id);
167                 display_notification(_("The component item has been deleted from this bom"));
168                 $Mode = 'RESET';
169         }
170 }
171
172 if ($Mode == 'RESET')
173 {
174         $selected_id = -1;
175         unset($_POST['quantity']);
176         unset($_POST['component']);
177 }
178 //--------------------------------------------------------------------------------------------------
179
180 start_form();
181
182 echo "<center>" . _("Select a sale kit:") . "&nbsp;";
183 echo sales_kits_list('item_code', null, _('New kit'), true);
184 echo "</center><br>";
185 $props = get_kit_props($_POST['item_code']);
186
187 if (list_updated('item_code')) {
188         if (get_post('item_code') == '')
189                 $_POST['description'] = '';
190         $Ajax->activate('_page_body');
191 }
192
193 $selected_kit = $_POST['item_code'];
194 //----------------------------------------------------------------------------------
195 if (get_post('item_code') == '') {
196 // New sales kit entry
197         start_table(TABLESTYLE2);
198         text_row(_("Alias/kit code:"), 'kit_code', null, 20, 21);
199 } else
200 {
201          // Kit selected so display bom or edit component
202         $_POST['description'] = $props['description'];
203         $_POST['category'] = $props['category_id'];
204         start_table(TABLESTYLE2);
205         text_row(_("Description:"), 'description', null, 50, 200);
206         stock_categories_list_row(_("Category:"), 'category', null);
207         submit_row('update_name', _("Update"), false, 'align=center colspan=2', _('Update kit/alias name'), true);
208         end_row();
209         end_table(1);
210         display_kit_items($selected_kit);
211         echo '<br>';
212         start_table(TABLESTYLE2);
213 }
214
215         if ($Mode == 'Edit') {
216                 $myrow = get_item_code($selected_id);
217                 $_POST['component'] = $myrow["stock_id"];
218                 $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["stock_id"]));
219         }
220         hidden("selected_id", $selected_id);
221         
222         sales_local_items_list_row(_("Component:"),'component', null, false, true);
223
224 //      if (get_post('description') == '')
225 //              $_POST['description'] = get_kit_name($_POST['component']);
226         if (get_post('item_code') == '') { // new kit/alias
227                 if ($Mode!='ADD_ITEM' && $Mode!='UPDATE_ITEM') {
228                         $_POST['description'] = $props['description'];
229                         $_POST['category'] = $props['category_id'];
230                 }
231                 text_row(_("Description:"), 'description', null, 50, 200);
232                 stock_categories_list_row(_("Category:"), 'category', null);
233         }
234         $res = get_item_edit_info(get_post('component'));
235         $dec =  $res["decimals"] == '' ? 0 : $res["decimals"];
236         $units = $res["units"] == '' ? _('kits') : $res["units"];
237         if (list_updated('component')) 
238         {
239                 $_POST['quantity'] = number_format2(1, $dec);
240                 $Ajax->activate('quantity');
241                 $Ajax->activate('category');
242         }
243         
244         qty_row(_("Quantity:"), 'quantity', number_format2(1, $dec), '', $units, $dec);
245
246         end_table(1);
247         submit_add_or_update_center($selected_id == -1, '', 'both');
248         end_form();
249 //----------------------------------------------------------------------------------
250
251 end_page();
252