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