5c217aa275bb0457c67a8285b57bf0ff4f509fca
[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).", ".db_escape($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=".db_escape($tax_shipping)." WHERE id=".db_escape($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($all=false)
51 {
52         $sql = "SELECT * FROM ".TB_PREF."tax_groups";
53         if (!$all) $sql .= " WHERE !inactive";
54         
55         return db_query($sql, "could not get all tax group");
56
57
58 function get_tax_group($type_id)
59 {
60         $sql = "SELECT * FROM ".TB_PREF."tax_groups WHERE id=".db_escape($type_id);
61         
62         $result = db_query($sql, "could not get tax group");
63         
64         return db_fetch($result);
65 }
66
67 function delete_tax_group($id)
68 {
69         begin_transaction();
70                 
71         $sql = "DELETE FROM ".TB_PREF."tax_groups WHERE id=".db_escape($id);
72                 
73         db_query($sql, "could not delete tax group");
74         
75         delete_tax_group_items($id);    
76         
77         commit_transaction();
78 }
79
80 function add_tax_group_items($id, $items, $rates)
81 {
82         for ($i=0; $i < count($items); $i++) 
83         {
84                 $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, rate)
85                         VALUES (".db_escape($id).",  ".db_escape($items[$i]).", " . $rates[$i] .")";
86                 db_query($sql, "could not add item tax group item");                                    
87         }               
88 }
89
90 function delete_tax_group_items($id)
91 {
92         $sql = "DELETE FROM ".TB_PREF."tax_group_items WHERE tax_group_id=".db_escape($id);
93         
94         db_query($sql, "could not delete item tax group items");                                        
95 }
96
97 function get_tax_group_items($id)
98 {
99         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate, 
100                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
101                 FROM ".TB_PREF."tax_group_items, ".TB_PREF."tax_types 
102                 WHERE tax_group_id=".db_escape($id)."
103                         AND ".TB_PREF."tax_types.id=tax_type_id";
104         
105         return db_query($sql, "could not get item tax type group items");
106 }
107
108 function get_tax_group_items_as_array($id)
109 {
110         $ret_tax_array = array();
111         
112         $tax_group_items = get_tax_group_items($id);
113         
114         while ($tax_group_item = db_fetch($tax_group_items)) 
115         {
116                 $index = $tax_group_item['tax_type_id'];
117                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
118                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
119                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
120                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
121                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
122                 $ret_tax_array[$index]['Value'] = 0;
123         }
124         
125         return $ret_tax_array;
126 }
127
128 function get_shipping_tax_group_items()
129 {
130
131         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate,
132                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
133                 FROM " .TB_PREF."tax_group_items, ".TB_PREF."tax_types, ".TB_PREF."tax_groups
134                 WHERE " .TB_PREF."tax_groups.tax_shipping=1
135                 AND " .TB_PREF."tax_groups.id=tax_group_id
136                 AND ".TB_PREF."tax_types.id=tax_type_id";
137         return db_query($sql, "could not get shipping tax group items");
138 }
139
140 function get_shipping_tax_as_array()
141 {
142         $ret_tax_array = array();
143
144         
145         $tax_group_items = get_shipping_tax_group_items();
146
147         while ($tax_group_item = db_fetch($tax_group_items)) 
148         {
149                 $index = $tax_group_item['tax_type_id'];
150                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
151                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
152                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
153                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
154                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
155                 $ret_tax_array[$index]['Value'] = 0;
156         }
157         
158         return $ret_tax_array;
159 }
160 ?>