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