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