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