a7c538361c001d1628f03fc5d40f4ac1eb475f3c
[fa-stable.git] / taxes / tax_types.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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'));
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         unset($_POST);
99 }
100 //-----------------------------------------------------------------------------------
101
102 $result = get_all_tax_types();
103
104 start_form();
105 start_table($table_style);
106
107 $th = array(_("Description"), _("Default Rate (%)"),
108         _("Sales GL Account"), _("Purchasing GL Account"), "", "");
109 table_header($th);
110
111 $k = 0;
112 while ($myrow = db_fetch($result))
113 {
114
115         alt_table_row_color($k);
116
117         label_cell($myrow["name"]);
118         label_cell(percent_format($myrow["rate"]), "align=right");
119         label_cell($myrow["sales_gl_code"] . "&nbsp;" . $myrow["SalesAccountName"]);
120         label_cell($myrow["purchasing_gl_code"] . "&nbsp;" . $myrow["PurchasingAccountName"]);
121
122         edit_button_cell("Edit".$myrow["id"], _("Edit"));
123         delete_button_cell("Delete".$myrow["id"], _("Delete"));
124
125         end_row();
126 }
127
128 end_table();
129
130 end_form();
131 echo '<br>';
132
133 //-----------------------------------------------------------------------------------
134
135 start_form();
136
137 start_table($table_style2);
138
139 if ($selected_id != -1) 
140 {
141         if ($Mode == 'Edit') {
142                 //editing an existing status code
143
144                 $myrow = get_tax_type($selected_id);
145
146                 $_POST['name']  = $myrow["name"];
147                 $_POST['rate']  = percent_format($myrow["rate"]);
148                 $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
149                 $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"];
150         }
151         hidden('selected_id', $selected_id);
152 }
153 text_row_ex(_("Description:"), 'name', 50);
154 small_amount_row(_("Default Rate:"), 'rate', '', "", "%", user_percent_dec());
155
156 gl_all_accounts_list_row(_("Sales GL Account:"), 'sales_gl_code', null);
157 gl_all_accounts_list_row(_("Purchasing GL Account:"), 'purchasing_gl_code', null);
158
159 end_table(1);
160
161 submit_add_or_update_center($selected_id == -1, '', true);
162
163 end_form();
164
165 end_page();
166
167 ?>