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).", $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=$tax_shipping WHERE id=$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()
52 $sql = "SELECT * FROM ".TB_PREF."tax_groups";
54 return db_query($sql, "could not get all tax group");
57 function get_tax_group($type_id)
59 $sql = "SELECT * FROM ".TB_PREF."tax_groups WHERE id=$type_id";
61 $result = db_query($sql, "could not get tax group");
63 return db_fetch($result);
66 function delete_tax_group($id)
70 $sql = "DELETE FROM ".TB_PREF."tax_groups WHERE id=$id";
72 db_query($sql, "could not delete tax group");
74 delete_tax_group_items($id);
79 function add_tax_group_items($id, $items, $rates)
81 for ($i=0; $i < count($items); $i++)
83 $sql = "INSERT INTO ".TB_PREF."tax_group_items (tax_group_id, tax_type_id, rate)
84 VALUES ($id, " . $items[$i] . ", " . $rates[$i] .")";
85 db_query($sql, "could not add item tax group item");
89 function delete_tax_group_items($id)
91 $sql = "DELETE FROM ".TB_PREF."tax_group_items WHERE tax_group_id=$id";
93 db_query($sql, "could not delete item tax group items");
96 function get_tax_group_items($id)
98 $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name,
99 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code
100 FROM ".TB_PREF."tax_group_items, ".TB_PREF."tax_types
101 WHERE tax_group_id=$id
102 AND ".TB_PREF."tax_types.id=tax_type_id";
104 return db_query($sql, "could not get item tax type group items");
107 function get_tax_group_items_as_array($id)
109 $ret_tax_array = array();
111 $tax_group_items = get_tax_group_items($id);
113 while ($tax_group_item = db_fetch($tax_group_items))
115 $index = $tax_group_item['tax_type_id'];
116 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
117 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
118 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
119 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
120 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
121 $ret_tax_array[$index]['Value'] = 0;
124 return $ret_tax_array;
127 function get_shipping_tax_group_items()
130 $sql = "SELECT ".TB_PREF."tax_group_items.*, ".TB_PREF."tax_types.name AS tax_type_name,
131 ".TB_PREF."tax_types.sales_gl_code, ".TB_PREF."tax_types.purchasing_gl_code
132 FROM " .TB_PREF."tax_group_items, ".TB_PREF."tax_types, ".TB_PREF."tax_groups
133 WHERE " .TB_PREF."tax_groups.tax_shipping=1
134 AND " .TB_PREF."tax_groups.id=tax_group_id
135 AND ".TB_PREF."tax_types.id=tax_type_id";
136 return db_query($sql, "could not get shipping tax group items");
139 function get_shipping_tax_as_array()
141 $ret_tax_array = array();
144 $tax_group_items = get_shipping_tax_group_items();
146 while ($tax_group_item = db_fetch($tax_group_items))
148 $index = $tax_group_item['tax_type_id'];
149 $ret_tax_array[$index]['tax_type_id'] = $tax_group_item['tax_type_id'];
150 $ret_tax_array[$index]['tax_type_name'] = $tax_group_item['tax_type_name'];
151 $ret_tax_array[$index]['sales_gl_code'] = $tax_group_item['sales_gl_code'];
152 $ret_tax_array[$index]['purchasing_gl_code'] = $tax_group_item['purchasing_gl_code'];
153 $ret_tax_array[$index]['rate'] = $tax_group_item['rate'];
154 $ret_tax_array[$index]['Value'] = 0;
157 return $ret_tax_array;