Fixed typo causing error when adding new tax type.
[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                 set_focus('name');
27                 return false;
28         }
29         elseif (!check_num('rate', 0))
30         {
31                 display_error( _("The default tax rate must be numeric and not less than zero."));
32                 set_focus('rate');
33                 return false;
34         }
35
36         return true;
37 }
38
39 //-----------------------------------------------------------------------------------
40
41 if (isset($_POST['ADD_ITEM']) && can_process())
42 {
43
44         add_tax_type($_POST['name'], $_POST['sales_gl_code'],
45                 $_POST['purchasing_gl_code'], input_num('rate'));
46         meta_forward($_SERVER['PHP_SELF']);
47 }
48
49 //-----------------------------------------------------------------------------------
50
51 if (isset($_POST['UPDATE_ITEM']) && can_process())
52 {
53
54         update_tax_type($selected_id, $_POST['name'],
55         $_POST['sales_gl_code'], $_POST['purchasing_gl_code'], input_num('rate'));
56         meta_forward($_SERVER['PHP_SELF']);
57 }
58
59 //-----------------------------------------------------------------------------------
60
61 function can_delete($selected_id)
62 {
63         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_group_items  WHERE tax_type_id=$selected_id";
64         $result = db_query($sql, "could not query tax groups");
65         $myrow = db_fetch_row($result);
66         if ($myrow[0] > 0)
67         {
68                 display_error(_("Cannot delete this tax type because tax groups been created referring to it."));
69
70                 return false;
71         }
72
73         return true;
74 }
75
76
77 //-----------------------------------------------------------------------------------
78
79 if (isset($_GET['delete']))
80 {
81
82         if (can_delete($selected_id))
83         {
84                 delete_tax_type($selected_id);
85                 meta_forward($_SERVER['PHP_SELF']);
86         }
87 }
88
89 //-----------------------------------------------------------------------------------
90
91 $result = get_all_tax_types();
92
93 start_table($table_style);
94
95 $th = array(_("Description"), _("Default Rate (%)"),
96         _("Sales GL Account"), _("Purchasing GL Account"), "", "");
97 table_header($th);
98
99 $k = 0;
100 while ($myrow = db_fetch($result))
101 {
102
103         alt_table_row_color($k);
104
105         label_cell($myrow["name"]);
106         label_cell(percent_format($myrow["rate"]), "align=right");
107         label_cell($myrow["sales_gl_code"] . "&nbsp;" . $myrow["SalesAccountName"]);
108         label_cell($myrow["purchasing_gl_code"] . "&nbsp;" . $myrow["PurchasingAccountName"]);
109
110         edit_link_cell("selected_id=".$myrow["id"]);
111         delete_link_cell("selected_id=".$myrow["id"]."&delete=1");
112
113         end_row();
114 }
115
116 end_table();
117
118 //-----------------------------------------------------------------------------------
119
120 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Tax Type"));
121
122 //-----------------------------------------------------------------------------------
123
124 start_form();
125
126 start_table($table_style2);
127
128 if (isset($selected_id))
129 {
130         //editing an existing status code
131
132         $myrow = get_tax_type($selected_id);
133
134         $_POST['name']  = $myrow["name"];
135         $_POST['rate']  = percent_format($myrow["rate"]);
136         $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
137         $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"];
138
139         hidden('selected_id', $selected_id);
140 }
141 text_row_ex(_("Description:"), 'name', 50);
142 small_amount_row(_("Default Rate:"), 'rate', '', "", "%", user_percent_dec());
143
144 gl_all_accounts_list_row(_("Sales GL Account:"), 'sales_gl_code', null);
145 gl_all_accounts_list_row(_("Purchasing GL Account:"), 'purchasing_gl_code', null);
146
147 end_table(1);
148
149 submit_add_or_update_center(!isset($selected_id));
150
151 end_form();
152
153 end_page();
154
155 ?>