Merging latest changes from stable branch up to 2.3.24
[fa-stable.git] / inventory / manage / item_codes.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_FORITEMCODE';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Foreign Item 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_purchasable_items(_("There are no inventory items defined in the system."));
23
24 simple_page_mode(true);
25 //--------------------------------------------------------------------------------------------------
26
27 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
28 {
29
30         $input_error = 0;
31         if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
32         {
33         $input_error = 1;
34         display_error( _("There is no item selected."));
35                 set_focus('stock_id');
36         }
37         elseif (!input_num('quantity'))
38         {
39         $input_error = 1;
40         display_error( _("The price entered was not positive number."));
41                 set_focus('quantity');
42         }
43         elseif ($_POST['description'] == '')
44         {
45         $input_error = 1;
46         display_error( _("Item code description cannot be empty."));
47                 set_focus('description');
48         }
49         elseif($selected_id == -1)
50         {
51                 $kit = get_item_kit($_POST['item_code']);
52         if (db_num_rows($kit)) {
53                         $input_error = 1;
54                 display_error( _("This item code is already assigned to stock item or sale kit."));
55                         set_focus('item_code');
56                 }
57         }
58         
59         if ($input_error == 0)
60         {
61         if ($Mode == 'ADD_ITEM') 
62         {
63                         add_item_code($_POST['item_code'], $_POST['stock_id'],
64                                 $_POST['description'], $_POST['category_id'], $_POST['quantity'], 1); 
65
66                 display_notification(_("New item code has been added."));
67         } else
68         {
69                         update_item_code($selected_id, $_POST['item_code'], $_POST['stock_id'],
70                                 $_POST['description'], $_POST['category_id'], $_POST['quantity'], 1); 
71
72                 display_notification(_("Item code has been updated."));
73         }
74                 $Mode = 'RESET';
75         }
76 }
77
78 //--------------------------------------------------------------------------------------------------
79
80 if ($Mode == 'Delete')
81 {
82         delete_item_code($selected_id);
83         
84         display_notification(_("Item code has been sucessfully deleted."));
85         $Mode = 'RESET';
86 }
87
88 if ($Mode == 'RESET')
89 {
90         $selected_id = -1;
91         unset($_POST);
92 }
93
94 if (list_updated('stock_id')) 
95         $Ajax->activate('_page_body');
96
97 //--------------------------------------------------------------------------------------------------
98
99 start_form();
100
101 if (!isset($_POST['stock_id']))
102         $_POST['stock_id'] = get_global_stock_item();
103
104 echo "<center>" . _("Item:"). "&nbsp;";
105 //Chaitanya : Manufcatured item visible
106 echo stock_items_list('stock_id', $_POST['stock_id'], false, true);
107 //echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
108
109 echo "<hr></center>";
110
111 set_global_stock_item($_POST['stock_id']);
112
113 $result = get_item_code_dflts($_POST['stock_id']);
114 $dec = $result['decimals'];
115 $units = $result['units'];
116 $dflt_desc = $result['description'];
117 $dflt_cat = $result['category_id'];
118
119 $result = get_all_item_codes($_POST['stock_id']);
120 div_start('code_table');
121         start_table(TABLESTYLE, "width='60%'");
122
123         $th = array(_("EAN/UPC Code"), _("Quantity"), _("Units"), 
124                 _("Description"),_("Category"), "", "");
125
126         table_header($th);
127
128         $k = $j = 0; //row colour counter
129
130         while ($myrow = db_fetch($result))
131         {
132                         alt_table_row_color($k);
133
134             label_cell($myrow["item_code"]);
135             qty_cell($myrow["quantity"], $dec);
136             label_cell($units);
137             label_cell($myrow["description"]);
138             label_cell($myrow["cat_name"]);
139                         edit_button_cell("Edit".$myrow['id'], _("Edit"));
140                         edit_button_cell("Delete".$myrow['id'], _("Delete"));
141             end_row();
142
143             $j++;
144             If ($j == 12)
145             {
146                 $j = 1;
147                         table_header($th);
148             } //end of page full new headings
149         } //end of while loop
150
151         end_table();
152 div_end();
153
154 //-----------------------------------------------------------------------------------------------
155
156 if ($selected_id != '') {
157         if ($Mode =='Edit')
158         {
159                 $myrow = get_item_code($selected_id);
160                 $_POST['item_code'] = $myrow["item_code"];
161                 $_POST['quantity'] = $myrow["quantity"];
162                 $_POST['description'] = $myrow["description"];
163                 $_POST['category_id'] = $myrow["category_id"];
164         }
165         hidden('selected_id', $selected_id);
166 } else {
167         $_POST['quantity'] = 1;
168         $_POST['description'] = $dflt_desc;
169         $_POST['category_id'] = $dflt_cat;
170 }
171
172 echo "<br>";
173 start_table(TABLESTYLE2);
174
175 hidden('code_id', $selected_id);
176
177 text_row(_("UPC/EAN code:"), 'item_code', null, 20, 21);
178 qty_row(_("Quantity:"), 'quantity', null, '', $units, $dec);
179 text_row(_("Description:"), 'description', null, 50, 200);
180 stock_categories_list_row(_("Category:"), 'category_id', null);
181
182 end_table(1);
183
184 submit_add_or_update_center($selected_id == -1, '', 'both');
185
186 end_form();
187 end_page();
188