Added inactive records support.
[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 General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-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($all=false)
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         if (!$all) $sql .= " AND !".TB_PREF."tax_types.inactive";
43         return db_query($sql, "could not get all tax types");
44 }
45
46 function get_all_tax_types_simple()
47 {
48         $sql = "SELECT * FROM ".TB_PREF."tax_types";
49
50         return db_query($sql, "could not get all tax types");
51 }
52
53 function get_tax_type($type_id)
54 {
55         $sql = "SELECT ".TB_PREF."tax_types.*,
56                 Chart1.account_name AS SalesAccountName,
57                 Chart2.account_name AS PurchasingAccountName
58                 FROM ".TB_PREF."tax_types, ".TB_PREF."chart_master AS Chart1,
59                 ".TB_PREF."chart_master AS Chart2
60                 WHERE ".TB_PREF."tax_types.sales_gl_code = Chart1.account_code
61                 AND ".TB_PREF."tax_types.purchasing_gl_code = Chart2.account_code AND id=$type_id";
62
63         $result = db_query($sql, "could not get tax type");
64
65         return db_fetch($result);
66 }
67
68 function get_tax_type_default_rate($type_id)
69 {
70         $sql = "SELECT rate FROM ".TB_PREF."tax_types WHERE id=$type_id";
71
72         $result = db_query($sql, "could not get tax type rate");
73
74         $row = db_fetch_row($result);
75         return $row[0];
76 }
77
78 function delete_tax_type($type_id)
79 {
80         begin_transaction();
81
82         $sql = "DELETE FROM ".TB_PREF."tax_types WHERE id=$type_id";
83
84         db_query($sql, "could not delete tax type");
85
86         // also delete any item tax exemptions associated with this type
87         $sql = "DELETE FROM ".TB_PREF."item_tax_type_exemptions WHERE tax_type_id=$type_id";
88
89         db_query($sql, "could not delete item tax type exemptions");
90
91         commit_transaction();
92 }
93
94 ?>