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");
17 function add_tax_group($name, $tax_shipping, $taxes, $rates)
21 if($tax_shipping) // only one tax group for shipping
22 clear_shipping_tax_group();
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");
29 add_tax_group_items($id, $taxes, $rates);
34 function update_tax_group($id, $name, $tax_shipping, $taxes, $rates)
38 if($tax_shipping) // only one tax group for shipping
39 clear_shipping_tax_group();
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");
44 delete_tax_group_items($id);
45 add_tax_group_items($id, $taxes, $rates);
50 function get_all_tax_groups($all=false)
52 $sql = "SELECT * FROM ".TB_PREF."tax_groups";
53 if (!$all) $sql .= " WHERE !inactive";
55 return db_query($sql, "could not get all tax group");
58 function get_tax_group($type_id)
60 $sql = "SELECT * FROM ".TB_PREF."tax_groups WHERE id=".db_escape($type_id);
62 $result = db_query($sql, "could not get tax group");
64 return db_fetch($result);
67 function delete_tax_group($id)
71 $sql = "DELETE FROM ".TB_PREF."tax_groups WHERE id=".db_escape($id);
73 db_query($sql, "could not delete tax group");
75 delete_tax_group_items($id);
80 function add_tax_group_items($id, $items, $rates)
82 for ($i=0; $i < count($items); $i++)
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");
90 function delete_tax_group_items($id)
92 $sql = "DELETE FROM ".TB_PREF."tax_group_items WHERE tax_group_id=".db_escape($id);
94 db_query($sql, "could not delete item tax group items");
98 // Return all tax types with rate value updated according to tax group selected
99 // Call the function without arg to find shipment group taxes.
101 function get_tax_group_rates($group_id=null)
103 global $suppress_tax_rates;
106 "SELECT t.id as tax_type_id,"
107 .(isset($suppress_tax_rates) && $suppress_tax_rates == 1
108 ? "t.name as tax_type_name,"
109 : "CONCAT(t.name, ' (', t.rate, '%)') as tax_type_name,")
111 t.purchasing_gl_code,
112 IF(g.tax_type_id, t.rate, NULL) as rate
113 FROM ".TB_PREF."tax_types t
114 LEFT JOIN ".TB_PREF."tax_group_items g ON t.id=g.tax_type_id
115 AND g.tax_group_id=". ($group_id ? db_escape($group_id) : "(SELECT id FROM ".TB_PREF."tax_groups WHERE tax_shipping=1)")
116 ." WHERE !t.inactive";
118 return db_query($sql, "cannot get tax types as array");
121 function get_tax_group_items_as_array($id)
123 $active_taxes = $ret_tax_array = array();
125 $tax_group_items = get_tax_group_rates($id);
127 while ($tax_group_item = db_fetch_assoc($tax_group_items))
129 $tax_group_item['Value'] = 0;
130 $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
133 return $ret_tax_array;
137 function get_shipping_tax_as_array()
139 $active_taxes = $ret_tax_array = array();
141 $tax_group_items = get_tax_group_rates();
143 while ($tax_group_item = db_fetch_assoc($tax_group_items))
145 $tax_group_item['Value'] = 0;
146 $ret_tax_array[$tax_group_item['tax_type_id']] = $tax_group_item;
149 return $ret_tax_array;