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