05e36f9235de5b367a16f8763d63e50f00790c91
[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, $rates, $tax_shippings)
13 {
14         begin_transaction();
15
16         $sql = "INSERT INTO ".TB_PREF."tax_groups (name) VALUES (".db_escape($name).")";
17         db_query($sql, "could not add tax group");
18         
19         $id = db_insert_id();
20         
21         add_tax_group_items($id, $taxes, $rates, $tax_shippings);       
22         
23         commit_transaction();   
24 }
25
26 function update_tax_group($id, $name, $taxes, $rates, $tax_shippings)
27 {
28         begin_transaction();    
29
30     $sql = "UPDATE ".TB_PREF."tax_groups SET name=".db_escape($name)." 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, $rates, $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, $rates, $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, rate, tax_shipping)
74                         VALUES (".db_escape($id).",  ".db_escape($items[$i]).", " . $rates[$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 function get_tax_group_items($id)
87 {
88         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate, 
89                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
90                 FROM ".TB_PREF."tax_group_items, ".TB_PREF."tax_types 
91                 WHERE tax_group_id=".db_escape($id)."
92                         AND ".TB_PREF."tax_types.id=tax_type_id";
93         
94         return db_query($sql, "could not get item tax type group items");
95 }
96
97 function get_tax_group_items_as_array($id)
98 {
99         $ret_tax_array = array();
100         
101         $tax_group_items = get_tax_group_items($id);
102         
103         while ($tax_group_item = db_fetch($tax_group_items)) 
104         {
105                 $index = $tax_group_item['tax_type_id'];
106                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
107                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
108                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
109                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
110                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
111                 $ret_tax_array[$index]['Value'] = 0;
112         }
113         
114         return $ret_tax_array;
115 }
116
117 function get_shipping_tax_group_items($tax_group)
118 {
119
120         $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name, ".TB_PREF."tax_types.rate,
121                 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code  
122                 FROM " .TB_PREF."tax_group_items, ".TB_PREF."tax_types
123                 WHERE " .TB_PREF."tax_group_items.tax_shipping=1
124                 AND tax_group_id=$tax_group
125                 AND ".TB_PREF."tax_types.id=tax_type_id";
126         return db_query($sql, "could not get shipping tax group items");
127 }
128
129 function get_shipping_tax_as_array($tax_group)
130 {
131         $ret_tax_array = array();
132
133         
134         $tax_group_items = get_shipping_tax_group_items($tax_group);
135
136         while ($tax_group_item = db_fetch($tax_group_items)) 
137         {
138                 $index = $tax_group_item['tax_type_id'];
139                 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
140                 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
141                 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
142                 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
143                 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
144                 $ret_tax_array[$index]['Value'] = 0;
145         }
146         
147         return $ret_tax_array;
148 }
149 ?>