Added inactive records support.
[fa-stable.git] / taxes / tax_types.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 = 3;
13 $path_to_root="..";
14
15 include($path_to_root . "/includes/session.inc");
16 page(_("Tax Types"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
20
21 simple_page_mode(true);
22 //-----------------------------------------------------------------------------------
23
24 function can_process()
25 {
26         if (strlen($_POST['name']) == 0)
27         {
28                 display_error(_("The tax type name cannot be empty."));
29                 set_focus('name');
30                 return false;
31         }
32         elseif (!check_num('rate', 0))
33         {
34                 display_error( _("The default tax rate must be numeric and not less than zero."));
35                 set_focus('rate');
36                 return false;
37         }
38
39         return true;
40 }
41
42 //-----------------------------------------------------------------------------------
43
44 if ($Mode=='ADD_ITEM' && can_process())
45 {
46
47         add_tax_type($_POST['name'], $_POST['sales_gl_code'],
48                 $_POST['purchasing_gl_code'], input_num('rate', 0));
49         display_notification(_('New tax type has been added'));
50         $Mode = 'RESET';
51 }
52
53 //-----------------------------------------------------------------------------------
54
55 if ($Mode=='UPDATE_ITEM' && can_process())
56 {
57
58         update_tax_type($selected_id, $_POST['name'],
59         $_POST['sales_gl_code'], $_POST['purchasing_gl_code'], input_num('rate'));
60         display_notification(_('Selected tax type has been updated'));
61         $Mode = 'RESET';
62 }
63
64 //-----------------------------------------------------------------------------------
65
66 function can_delete($selected_id)
67 {
68         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_group_items  WHERE tax_type_id=$selected_id";
69         $result = db_query($sql, "could not query tax groups");
70         $myrow = db_fetch_row($result);
71         if ($myrow[0] > 0)
72         {
73                 display_error(_("Cannot delete this tax type because tax groups been created referring to it."));
74
75                 return false;
76         }
77
78         return true;
79 }
80
81
82 //-----------------------------------------------------------------------------------
83
84 if ($Mode == 'Delete')
85 {
86
87         if (can_delete($selected_id))
88         {
89                 delete_tax_type($selected_id);
90                 display_notification(_('Selected tax type 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         unset($_POST);
100         $_POST['show_inactive'] = $sav;
101 }
102 //-----------------------------------------------------------------------------------
103
104 $result = get_all_tax_types(check_value('show_inactive'));
105
106 start_form();
107 start_table($table_style);
108
109 $th = array(_("Description"), _("Default Rate (%)"),
110         _("Sales GL Account"), _("Purchasing GL Account"), "", "");
111 inactive_control_column($th);
112 table_header($th);
113
114 $k = 0;
115 while ($myrow = db_fetch($result))
116 {
117
118         alt_table_row_color($k);
119
120         label_cell($myrow["name"]);
121         label_cell(percent_format($myrow["rate"]), "align=right");
122         label_cell($myrow["sales_gl_code"] . "&nbsp;" . $myrow["SalesAccountName"]);
123         label_cell($myrow["purchasing_gl_code"] . "&nbsp;" . $myrow["PurchasingAccountName"]);
124
125         inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_types', 'id');
126         edit_button_cell("Edit".$myrow["id"], _("Edit"));
127         delete_button_cell("Delete".$myrow["id"], _("Delete"));
128
129         end_row();
130 }
131
132 inactive_control_row($th);
133 end_table(1);
134 //-----------------------------------------------------------------------------------
135
136 start_table($table_style2);
137
138 if ($selected_id != -1) 
139 {
140         if ($Mode == 'Edit') {
141                 //editing an existing status code
142
143                 $myrow = get_tax_type($selected_id);
144
145                 $_POST['name']  = $myrow["name"];
146                 $_POST['rate']  = percent_format($myrow["rate"]);
147                 $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
148                 $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"];
149         }
150         hidden('selected_id', $selected_id);
151 }
152 text_row_ex(_("Description:"), 'name', 50);
153 small_amount_row(_("Default Rate:"), 'rate', '', "", "%", user_percent_dec());
154
155 gl_all_accounts_list_row(_("Sales GL Account:"), 'sales_gl_code', null);
156 gl_all_accounts_list_row(_("Purchasing GL Account:"), 'purchasing_gl_code', null);
157
158 end_table(1);
159
160 submit_add_or_update_center($selected_id == -1, '', 'both');
161
162 end_form();
163
164 end_page();
165
166 ?>