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