New tax system implementation.
[fa-stable.git] / inventory / manage / item_categories.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_ITEMCATEGORY';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 if (isset($_GET['FixedAsset'])) {
17   $page_security = 'SA_ASSETCATEGORY';
18   $help_context = "Fixed Assets Categories";
19   $_POST['mb_flag'] = 'F';
20 }
21 else {
22   $help_context = "Item Categories";
23 }
24
25 $js = "";
26 if ($SysPrefs->use_popup_windows && $SysPrefs->use_popup_search)
27         $js .= get_js_open_window(900, 500);
28
29 page(_($help_context), false, false, "", $js);
30
31 include_once($path_to_root . "/includes/ui.inc");
32
33 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
34
35 simple_page_mode(true);
36 //----------------------------------------------------------------------------------
37
38 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
39 {
40
41         //initialise no input errors assumed initially before we test
42         $input_error = 0;
43
44         if (strlen($_POST['description']) == 0) 
45         {
46                 $input_error = 1;
47                 display_error(_("The item category description cannot be empty."));
48                 set_focus('description');
49         }
50
51         if ($input_error !=1)
52         {
53         if ($selected_id != -1) 
54         {
55                     update_item_category($selected_id, $_POST['description'],
56                                 $_POST['tax_type_id'],  $_POST['sales_account'], 
57                                 $_POST['cogs_account'], $_POST['inventory_account'], 
58                                 $_POST['adjustment_account'], $_POST['wip_account'],
59                                 $_POST['units'], $_POST['mb_flag'],     $_POST['dim1'], $_POST['dim2'],
60                                 check_value('no_sale'), check_value('no_purchase'), $_POST['vat_category']);
61                         display_notification(_('Selected item category has been updated'));
62         } 
63         else 
64         {
65                     add_item_category($_POST['description'],
66                                 $_POST['tax_type_id'],  $_POST['sales_account'], 
67                                 $_POST['cogs_account'], $_POST['inventory_account'], 
68                                 $_POST['adjustment_account'], $_POST['wip_account'], 
69                                 $_POST['units'], $_POST['mb_flag'],     $_POST['dim1'], 
70                                 $_POST['dim2'], check_value('no_sale'), check_value('no_purchase'), $_POST['vat_category']);
71                         display_notification(_('New item category has been added'));
72         }
73                 $Mode = 'RESET';
74         }
75 }
76
77 //---------------------------------------------------------------------------------- 
78
79 if ($Mode == 'Delete')
80 {
81
82         // PREVENT DELETES IF DEPENDENT RECORDS IN 'stock_master'
83         if (key_in_foreign_table($selected_id, 'stock_master', 'category_id'))
84         {
85                 display_error(_("Cannot delete this item category because items have been created using this item category."));
86         } 
87         else 
88         {
89                 delete_item_category($selected_id);
90                 display_notification(_('Selected item category has been deleted'));
91         }
92         $Mode = 'RESET';
93 }
94
95 if ($Mode == 'RESET')
96 {
97         $selected_id = -1;
98         $sav = get_post('show_inactive');
99     $mb_flag = get_post('mb_flag');
100         unset($_POST);
101         $_POST['show_inactive'] = $sav;
102         if (is_fixed_asset($mb_flag))
103                 $_POST['mb_flag'] = 'F';
104 }
105 if (list_updated('mb_flag')) {
106         $Ajax->activate('details');
107 }
108
109 //----------------------------------------------------------------------------------
110 $fixed_asset = is_fixed_asset(get_post('mb_flag'));
111
112 $result = get_item_categories(check_value('show_inactive'), $fixed_asset);
113
114 start_form();
115 start_table(TABLESTYLE, "width='80%'");
116 if ($fixed_asset) {
117         $th = array(_("Name"), _("Tax type"), _("Units"), _("Sales Act"),
118                 _("Asset Account"), _("Deprecation Cost Account"),
119                 _("Depreciation/Disposal Account"), "", "");
120 } else {
121         $th = array(_("Name"), _("Tax type"), _("Units"), _("VAT Category"), _("Type"), _("Sales Act"),
122                 _("Inventory Account"), _("COGS Account"), _("Adjustment Account"),
123                 _("Assembly Account"), "", "");
124 }
125 inactive_control_column($th);
126
127 table_header($th);
128 $k = 0; //row colour counter
129
130 while ($myrow = db_fetch($result)) 
131 {
132         
133         alt_table_row_color($k);
134
135         label_cell($myrow["description"]);
136         label_cell($myrow["tax_name"]);
137         label_cell($myrow["dflt_units"], "align=center");
138         if (!$fixed_asset) {
139                 label_cell($vat_categories[$myrow["vat_category"]]);
140                 label_cell($stock_types[$myrow["dflt_mb_flag"]]);
141         }
142         label_cell($myrow["dflt_sales_act"], "align=center");
143         label_cell($myrow["dflt_inventory_act"], "align=center");
144         label_cell($myrow["dflt_cogs_act"], "align=center");
145         label_cell($myrow["dflt_adjustment_act"], "align=center");
146         if (!$fixed_asset)
147                 label_cell($myrow["dflt_wip_act"], "align=center");
148         inactive_control_cell($myrow["category_id"], $myrow["inactive"], 'stock_category', 'category_id');
149         edit_button_cell("Edit".$myrow["category_id"], _("Edit"));
150         delete_button_cell("Delete".$myrow["category_id"], _("Delete"));
151         end_row();
152 }
153
154 inactive_control_row($th);
155 end_table();
156 echo '<br>';
157 //----------------------------------------------------------------------------------
158
159 div_start('details');
160 start_table(TABLESTYLE2);
161
162 if ($selected_id != -1) 
163 {
164         if ($Mode == 'Edit') {
165                 //editing an existing item category
166                 $myrow = get_item_category($selected_id);
167
168                 $_POST['category_id'] = $myrow["category_id"];
169                 $_POST['description']  = $myrow["description"];
170                 $_POST['tax_type_id']  = $myrow["dflt_tax_type"];
171                 $_POST['sales_account']  = $myrow["dflt_sales_act"];
172                 $_POST['cogs_account']  = $myrow["dflt_cogs_act"];
173                 $_POST['inventory_account']  = $myrow["dflt_inventory_act"];
174                 $_POST['adjustment_account']  = $myrow["dflt_adjustment_act"];
175                 $_POST['wip_account']  = $myrow["dflt_wip_act"];
176                 $_POST['units']  = $myrow["dflt_units"];
177                 $_POST['mb_flag']  = $myrow["dflt_mb_flag"];
178                 $_POST['dim1']  = $myrow["dflt_dim1"];
179                 $_POST['dim2']  = $myrow["dflt_dim2"];
180                 $_POST['no_sale']  = $myrow["dflt_no_sale"];
181                 $_POST['no_purchase']  = $myrow["dflt_no_purchase"];
182                 $_POST['vat_category']  = $myrow["vat_category"];
183         } 
184         hidden('selected_id', $selected_id);
185         hidden('category_id');
186 } else if ($Mode != 'CLONE') {
187                 $_POST['long_description'] = '';
188                 $_POST['description'] = '';
189                 $_POST['no_sale']  = 0;
190                 $_POST['no_purchase']  = 0;
191
192                 $company_record = get_company_prefs();
193
194     if (get_post('inventory_account') == "")
195         $_POST['inventory_account'] = $company_record["default_inventory_act"];
196
197     if (get_post('cogs_account') == "")
198         $_POST['cogs_account'] = $company_record["default_cogs_act"];
199
200         if (get_post('sales_account') == "")
201                 $_POST['sales_account'] = $company_record["default_inv_sales_act"];
202
203         if (get_post('adjustment_account') == "")
204                 $_POST['adjustment_account'] = $company_record["default_adj_act"];
205
206         if (get_post('wip_account') == "")
207                 $_POST['wip_account'] = $company_record["default_wip_act"];
208
209 }
210
211 text_row(_("Category Name:"), 'description', null, 30, 30);  
212
213 table_section_title(_("Default values for new items"));
214
215 item_tax_types_list_row(_("Item Tax Type:"), 'tax_type_id', null);
216
217 if (is_fixed_asset(get_post('mb_flag')))
218         hidden('mb_flag', 'F');
219 else
220         stock_item_types_list_row(_("Item Type:"), 'mb_flag', null, true);
221
222 stock_units_list_row(_("Units of Measure:"), 'units', null);
223
224 if (is_fixed_asset($_POST['mb_flag']))
225   hidden('vat_category', VC_ASSETS);
226 else
227   vat_category_list_row(_("VAT category:"), 'vat_category',null, true, false, $selected_id!=-1);
228
229 if (is_fixed_asset($_POST['mb_flag'])) 
230         hidden('no_sale', 0);
231 else
232         check_row(_("Exclude from sales:"), 'no_sale');
233
234 check_row(_("Exclude from purchases:"), 'no_purchase');
235
236 gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
237
238 if (is_service($_POST['mb_flag']))
239 {
240         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
241         hidden('inventory_account', $_POST['inventory_account']);
242         hidden('adjustment_account', $_POST['adjustment_account']);
243 }
244 elseif (is_fixed_asset($_POST['mb_flag'])) 
245 {
246         gl_all_accounts_list_row(_("Asset account:"), 'inventory_account', $_POST['inventory_account']);
247         gl_all_accounts_list_row(_("Depreciation cost account:"), 'cogs_account', $_POST['cogs_account']);
248         gl_all_accounts_list_row(_("Depreciation/Disposal account:"), 'adjustment_account', $_POST['adjustment_account']);
249 }
250 else
251 {
252         gl_all_accounts_list_row(_("Inventory Account:"), 'inventory_account', $_POST['inventory_account']);
253
254         gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'cogs_account', $_POST['cogs_account']);
255         gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'adjustment_account', $_POST['adjustment_account']);
256 }
257
258 if (is_manufactured($_POST['mb_flag']))
259         gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'wip_account', $_POST['wip_account']);
260 else
261         hidden('wip_account', $_POST['wip_account']);
262
263 $dim = get_company_pref('use_dimension');
264 if ($dim >= 1)
265 {
266         dimensions_list_row(_("Dimension")." 1", 'dim1', null, true, " ", false, 1);
267         if ($dim > 1)
268                 dimensions_list_row(_("Dimension")." 2", 'dim2', null, true, " ", false, 2);
269 }
270 if ($dim < 1)
271         hidden('dim1', 0);
272 if ($dim < 2)
273         hidden('dim2', 0);
274
275 end_table(1);
276 div_end();
277 submit_add_or_update_center($selected_id == -1, '', 'both', true);
278
279 end_form();
280
281 end_page();
282