Copyright notes at top op every source file
[fa-stable.git] / taxes / db / tax_types_db.inc
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 function add_tax_type($name, $sales_gl_code, $purchasing_gl_code, $rate)
13 {
14         $sql = "INSERT INTO ".TB_PREF."tax_types (name, sales_gl_code, purchasing_gl_code, rate)
15                 VALUES (".db_escape($name).", ".db_escape($sales_gl_code)
16                 .", ".db_escape($purchasing_gl_code).", $rate)";
17
18         db_query($sql, "could not add tax type");
19 }
20
21 function update_tax_type($type_id, $name, $sales_gl_code, $purchasing_gl_code, $rate)
22 {
23         $sql = "UPDATE ".TB_PREF."tax_types SET name=".db_escape($name).",
24                 sales_gl_code=".db_escape($sales_gl_code).",
25                 purchasing_gl_code=".db_escape($purchasing_gl_code).",
26                 rate=$rate
27                 WHERE id=$type_id";
28
29         db_query($sql, "could not update tax type");
30 }
31
32 function get_all_tax_types()
33 {
34         $sql = "SELECT ".TB_PREF."tax_types.*,
35                 Chart1.account_name AS SalesAccountName,
36                 Chart2.account_name AS PurchasingAccountName
37                 FROM ".TB_PREF."tax_types, ".TB_PREF."chart_master AS Chart1,
38                 ".TB_PREF."chart_master AS Chart2
39                 WHERE ".TB_PREF."tax_types.sales_gl_code = Chart1.account_code
40                 AND ".TB_PREF."tax_types.purchasing_gl_code = Chart2.account_code";
41
42         return db_query($sql, "could not get all tax types");
43 }
44
45 function get_all_tax_types_simple()
46 {
47         $sql = "SELECT * FROM ".TB_PREF."tax_types";
48
49         return db_query($sql, "could not get all tax types");
50 }
51
52 function get_tax_type($type_id)
53 {
54         $sql = "SELECT ".TB_PREF."tax_types.*,
55                 Chart1.account_name AS SalesAccountName,
56                 Chart2.account_name AS PurchasingAccountName
57                 FROM ".TB_PREF."tax_types, ".TB_PREF."chart_master AS Chart1,
58                 ".TB_PREF."chart_master AS Chart2
59                 WHERE ".TB_PREF."tax_types.sales_gl_code = Chart1.account_code
60                 AND ".TB_PREF."tax_types.purchasing_gl_code = Chart2.account_code AND id=$type_id";
61
62         $result = db_query($sql, "could not get tax type");
63
64         return db_fetch($result);
65 }
66
67 function get_tax_type_default_rate($type_id)
68 {
69         $sql = "SELECT rate FROM ".TB_PREF."tax_types WHERE id=$type_id";
70
71         $result = db_query($sql, "could not get tax type rate");
72
73         $row = db_fetch_row($result);
74         return $row[0];
75 }
76
77 function delete_tax_type($type_id)
78 {
79         begin_transaction();
80
81         $sql = "DELETE FROM ".TB_PREF."tax_types WHERE id=$type_id";
82
83         db_query($sql, "could not delete tax type");
84
85         // also delete any item tax exemptions associated with this type
86         $sql = "DELETE FROM ".TB_PREF."item_tax_type_exemptions WHERE tax_type_id=$type_id";
87
88         db_query($sql, "could not delete item tax type exemptions");
89
90         commit_transaction();
91 }
92
93 ?>