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