New tax system implementation.
[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 add_tax_group($name, $taxes, $tax_shippings, $tax_area)
13 {
14         begin_transaction();
15
16         $sql = "INSERT INTO ".TB_PREF."tax_groups (name, tax_area) VALUES (".db_escape($name).",".db_escape($tax_area).")";
17         db_query($sql, "could not add tax group");
18         
19         $id = db_insert_id();
20         
21         add_tax_group_items($id, $taxes, $tax_shippings);
22         
23         commit_transaction();   
24 }
25
26 function update_tax_group($id, $name, $taxes, $tax_shippings, $tax_area)
27 {
28         begin_transaction();    
29
30     $sql = "UPDATE ".TB_PREF."tax_groups SET name=".db_escape($name).",tax_area=".db_escape($tax_area)." WHERE id=".db_escape($id);
31         db_query($sql, "could not update tax group");
32         
33         delete_tax_group_items($id);
34         add_tax_group_items($id, $taxes, $tax_shippings);       
35         
36         commit_transaction();                   
37 }
38
39 function get_all_tax_groups($all=false)
40 {
41         $sql = "SELECT * FROM ".TB_PREF."tax_groups";
42         if (!$all) $sql .= " WHERE !inactive";
43         
44         return db_query($sql, "could not get all tax group");
45
46
47 function get_tax_group($type_id)
48 {
49         $sql = "SELECT * FROM ".TB_PREF."tax_groups WHERE id=".db_escape($type_id);
50         
51         $result = db_query($sql, "could not get tax group");
52
53         return db_fetch($result);
54 }
55
56 function delete_tax_group($id)
57 {
58         begin_transaction();
59                 
60         $sql = "DELETE FROM ".TB_PREF."tax_groups WHERE id=".db_escape($id);
61                 
62         db_query($sql, "could not delete tax group");
63         
64         delete_tax_group_items($id);    
65         
66         commit_transaction();
67 }
68
69 function add_tax_group_items($id, $items, $tax_shippings)
70 {
71         for ($i=0; $i < count($items); $i++) 
72         {
73                 $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, tax_shipping)
74                         VALUES (".db_escape($id).",  ".db_escape($items[$i]).", " . $tax_shippings[$i] .")";
75                 db_query($sql, "could not add item tax group item");                                    
76         }               
77 }
78
79 function delete_tax_group_items($id)
80 {
81         $sql = "DELETE FROM ".TB_PREF."tax_group_items WHERE tax_group_id=".db_escape($id);
82         
83         db_query($sql, "could not delete item tax group items");                                        
84 }
85
86 //
87 //      Return all tax types with rate value updated according to tax group selected
88 //      Call the function without arg to find shipment group taxes.
89 //
90 function get_tax_group_rates($group_id=null, $tax_shipping=false)
91 {
92         global $SysPrefs;
93
94         $sql = 
95         "SELECT t.id as tax_type_id,"
96                 .($SysPrefs->suppress_tax_rates() == 1 ? "t.name as tax_type_name,"
97                         : "CONCAT(t.name, ' (', t.rate, '%)') as tax_type_name,")
98                 ."t.sales_gl_code,
99                   t.purchasing_gl_code,
100                   IF(g.tax_type_id, t.rate, NULL) as rate,
101                   g.tax_shipping
102                 FROM ".TB_PREF."tax_types t 
103                   LEFT JOIN ".TB_PREF."tax_group_items g ON t.id=g.tax_type_id
104         AND g.tax_group_id=". ($group_id ? db_escape($group_id) : "(SELECT MIN(id) FROM ".TB_PREF."tax_groups)")                  
105         . " WHERE !t.inactive";
106         if ($tax_shipping)
107                 $sql .= " AND g.tax_shipping=1";
108
109         return db_query($sql, "cannot get tax types as array");
110 }
111
112 function get_tax_group_items_as_array($id)
113 {
114         $ret_tax_array = array();
115         
116         $tax_group_items = get_tax_group_rates($id);
117         
118         while ($tax_group_item = db_fetch($tax_group_items)) 
119         {
120                 $tax_group_item['Value'] = 0;
121                 $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
122
123         }
124         
125         return $ret_tax_array;
126 }
127
128 function get_tax_group_data($id)
129 {
130         $data = get_tax_group($id);
131
132         $data['taxes'] = get_tax_group_items_as_array($id);
133
134         return $data;
135 }
136
137 /*
138         Heuristic function to find default domestic tax_group.
139 */
140 function find_domestic_tax_group()
141 {
142
143         $sql =  "SELECT id, cnt FROM 
144                 (SELECT g.id, count(*) cnt 
145                         FROM ".TB_PREF."tax_group_items i
146                         LEFT JOIN ".TB_PREF."tax_groups g ON g.id=i.tax_group_id
147                 WHERE tax_shipping=0 AND tax_area=0 AND !inactive
148                 GROUP BY g.id) cnts
149                 ORDER by cnt DESC";
150         $result = db_query($sql, "cannot get domestic group id");
151         $group = db_fetch($result);
152         return $group['id'];
153 }
154
155 function get_shipping_tax_as_array($id=null)
156 {
157         $ret_tax_array = array();
158
159         $tax_group_items = get_tax_group_rates($id, true);
160
161         while ($tax_group_item = db_fetch($tax_group_items)) 
162         {
163                 $tax_group_item['Value'] = 0;
164                 $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
165         }
166         
167         return $ret_tax_array;
168 }