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