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