d70493c847080d206d2442149a834f9f5bd1dd01
[fa-stable.git] / taxes / tax_types.php
1 <?php
2 $page_security = 3;
3 $path_to_root="..";
4
5 include($path_to_root . "/includes/session.inc");
6 page(_("Tax Types"));
7
8 include_once($path_to_root . "/includes/ui.inc");
9 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
10
11 if (isset($_GET['selected_id']))
12 {
13         $selected_id = $_GET['selected_id'];
14 }
15 elseif(isset($_POST['selected_id']))
16 {
17         $selected_id = $_POST['selected_id'];
18 }
19 //-----------------------------------------------------------------------------------
20
21 function can_process()
22 {
23         if (strlen($_POST['name']) == 0)
24         {
25                 display_error(_("The tax type name cannot be empty."));
26                 return false;
27         }
28         elseif (!is_numeric($_POST['rate']))
29         {
30                 display_error( _("The default tax rate must be numeric."));
31                 return false;
32         }
33         elseif ($_POST['rate'] < 0)
34         {
35                 display_error( _("The default tax rate cannot be less than zero."));
36                 return false;
37         }
38
39         return true;
40 }
41
42 //-----------------------------------------------------------------------------------
43
44 if (isset($_POST['ADD_ITEM']) && can_process())
45 {
46
47         add_tax_type($_POST['name'], $_POST['sales_gl_code'],
48                 $_POST['purchasing_gl_code'], $_POST['rate']);
49         meta_forward($_SERVER['PHP_SELF']);
50 }
51
52 //-----------------------------------------------------------------------------------
53
54 if (isset($_POST['UPDATE_ITEM']) && can_process())
55 {
56
57         update_tax_type($selected_id, $_POST['name'],
58         $_POST['sales_gl_code'], $_POST['purchasing_gl_code'], $_POST['rate']);
59         meta_forward($_SERVER['PHP_SELF']);
60 }
61
62 //-----------------------------------------------------------------------------------
63
64 function can_delete($selected_id)
65 {
66         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_group_items  WHERE tax_type_id=$selected_id";
67         $result = db_query($sql, "could not query tax groups");
68         $myrow = db_fetch_row($result);
69         if ($myrow[0] > 0)
70         {
71                 display_error(_("Cannot delete this tax type because tax groups been created referring to it."));
72                 return false;
73         }
74
75         return true;
76 }
77
78
79 //-----------------------------------------------------------------------------------
80
81 if (isset($_GET['delete']))
82 {
83
84         if (can_delete($selected_id))
85         {
86                 delete_tax_type($selected_id);
87                 meta_forward($_SERVER['PHP_SELF']);
88         }
89 }
90
91 //-----------------------------------------------------------------------------------
92
93 $result = get_all_tax_types();
94
95 start_table($table_style);
96
97 $th = array(_("Description"), _("Default Rate (%)"),
98         _("Sales GL Account"), _("Purchasing GL Account"), "", "");
99 table_header($th);
100
101 $k = 0;
102 while ($myrow = db_fetch($result))
103 {
104
105         alt_table_row_color($k);
106
107         label_cell($myrow["name"]);
108         label_cell(number_format2($myrow["rate"],user_percent_dec()), "align=right");
109         label_cell($myrow["sales_gl_code"] . "&nbsp;" . $myrow["SalesAccountName"]);
110         label_cell($myrow["purchasing_gl_code"] . "&nbsp;" . $myrow["PurchasingAccountName"]);
111
112         edit_link_cell("selected_id=".$myrow["id"]);
113         delete_link_cell("selected_id=".$myrow["id"]."&delete=1");
114
115         end_row();
116 }
117
118 end_table();
119
120 //-----------------------------------------------------------------------------------
121
122 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Tax Type"));
123
124 //-----------------------------------------------------------------------------------
125
126 start_form();
127
128 start_table($table_style2);
129
130 if (isset($selected_id))
131 {
132         //editing an existing status code
133
134         $myrow = get_tax_type($selected_id);
135
136         $_POST['name']  = $myrow["name"];
137         $_POST['rate']  = $myrow["rate"];
138         $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
139         $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"];
140
141         hidden('selected_id', $selected_id);
142 }
143 text_row_ex(_("Description:"), 'name', 50);
144 text_row_ex(_("Default Rate:"), 'rate', 10, 10, "", "%");
145
146 gl_all_accounts_list_row(_("Sales GL Account:"), 'sales_gl_code', null);
147 gl_all_accounts_list_row(_("Purchasing GL Account:"), 'purchasing_gl_code', null);
148
149 end_table(1);
150
151 submit_add_or_update_center(!isset($selected_id));
152
153 end_form();
154
155 end_page();
156
157 ?>