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