Changed license type to GPLv3 in top of files
[fa-stable.git] / taxes / db / tax_groups_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 clear_shipping_tax_group() {
13           $sql = "UPDATE ".TB_PREF."tax_groups SET tax_shipping=0 WHERE 1";
14           db_query($sql, "could not update tax_shipping fields");         
15 }
16
17 function add_tax_group($name, $tax_shipping, $taxes, $rates)
18 {
19         begin_transaction();
20
21         if($tax_shipping)       // only one tax group for shipping
22           clear_shipping_tax_group();
23                 
24         $sql = "INSERT INTO ".TB_PREF."tax_groups (name, tax_shipping) VALUES (".db_escape($name).", $tax_shipping)";
25         db_query($sql, "could not add tax group");
26         
27         $id = db_insert_id();
28         
29         add_tax_group_items($id, $taxes, $rates);       
30         
31         commit_transaction();   
32 }
33
34 function update_tax_group($id, $name, $tax_shipping, $taxes, $rates)
35 {
36         begin_transaction();    
37
38         if($tax_shipping)       // only one tax group for shipping
39           clear_shipping_tax_group();
40         
41     $sql = "UPDATE ".TB_PREF."tax_groups SET name=".db_escape($name).",tax_shipping=$tax_shipping WHERE id=$id";
42         db_query($sql, "could not update tax group");
43         
44         delete_tax_group_items($id);
45         add_tax_group_items($id, $taxes, $rates);       
46         
47         commit_transaction();                   
48 }
49
50 function get_all_tax_groups()
51 {
52         $sql = "SELECT * FROM ".TB_PREF."tax_groups";
53         
54         return db_query($sql, "could not get all tax group");
55
56
57 function get_tax_group($type_id)
58 {
59         $sql = "SELECT * FROM ".TB_PREF."tax_groups WHERE id=$type_id";
60         
61         $result = db_query($sql, "could not get tax group");
62         
63         return db_fetch($result);
64 }
65
66 function delete_tax_group($id)
67 {
68         begin_transaction();
69                 
70         $sql = "DELETE FROM ".TB_PREF."tax_groups WHERE id=$id";
71                 
72         db_query($sql, "could not delete tax group");
73         
74         delete_tax_group_items($id);    
75         
76         commit_transaction();
77 }
78
79 function add_tax_group_items($id, $items, $rates)
80 {
81         for ($i=0; $i < count($items); $i++) 
82         {
83                 $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, rate)
84                         VALUES ($id,  " . $items[$i] . ", " . $rates[$i] .")";
85                 db_query($sql, "could not add item tax group item");                                    
86         }               
87 }
88
89 function delete_tax_group_items($id)
90 {
91         $sql = "DELETE FROM ".TB_PREF."tax_group_items WHERE tax_group_id=$id";
92         
93         db_query($sql, "could not delete item tax group items");                                        
94 }
95
96 function get_tax_group_items($id)
97 {
98         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, 
99                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
100                 FROM ".TB_PREF."tax_group_items, ".TB_PREF."tax_types 
101                 WHERE tax_group_id=$id
102                         AND ".TB_PREF."tax_types.id=tax_type_id";
103         
104         return db_query($sql, "could not get item tax type group items");
105 }
106
107 function get_tax_group_items_as_array($id)
108 {
109         $ret_tax_array = array();
110         
111         $tax_group_items = get_tax_group_items($id);
112         
113         while ($tax_group_item = db_fetch($tax_group_items)) 
114         {
115                 $index = $tax_group_item['tax_type_id'];
116                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
117                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
118                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
119                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
120                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
121                 $ret_tax_array[$index]['Value'] = 0;
122         }
123         
124         return $ret_tax_array;
125 }
126
127 function get_shipping_tax_group_items()
128 {
129
130         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, 
131                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
132                 FROM " .TB_PREF."tax_group_items, ".TB_PREF."tax_types, ".TB_PREF."tax_groups
133                 WHERE " .TB_PREF."tax_groups.tax_shipping=1
134                 AND " .TB_PREF."tax_groups.id=tax_group_id
135                 AND ".TB_PREF."tax_types.id=tax_type_id";
136         return db_query($sql, "could not get shipping tax group items");
137 }
138
139 function get_shipping_tax_as_array()
140 {
141         $ret_tax_array = array();
142
143         
144         $tax_group_items = get_shipping_tax_group_items();
145
146         while ($tax_group_item = db_fetch($tax_group_items)) 
147         {
148                 $index = $tax_group_item['tax_type_id'];
149                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
150                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
151                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
152                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
153                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
154                 $ret_tax_array[$index]['Value'] = 0;
155         }
156         
157         return $ret_tax_array;
158 }
159 ?>