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 include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
13 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
14 include_once($path_to_root . "/taxes/db/item_tax_types_db.inc");
16 //---------------------------------------------------------------------------------
18 // returns the tax cost for a given item
20 function get_tax_value_for_item($stock_id, $price, $tax_group)
22 // if price is zero, then can't be taxed
26 $ret_tax_array = get_tax_group_items_as_array($tax_group);
28 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
29 if ($tax_array == null)
35 foreach ($tax_array as $taxitem) {
36 $tax_multiplier += $taxitem["rate"];
39 return $price * ($tax_multiplier / 100);
42 //---------------------------------------------------------------------------------
44 // returns the price of a given item minus any included taxes
45 // for item $stock_id with line price $price,
46 // with applicable tax rates $tax_group_array or group id $tax_group
49 function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
51 // if price is zero, then can't be taxed !
55 if ($tax_included==0) return $price;
57 // if array already read, then make a copy and use that
59 $ret_tax_array = $tax_group_array;
61 $ret_tax_array = get_tax_group_items_as_array($tax_group);
63 //print_r($ret_tax_array);
65 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
66 // if no exemptions or taxgroup is empty, then no included/excluded taxes
67 if ($tax_array == null)
74 foreach ($tax_array as $taxitem)
76 $tax_multiplier += $taxitem["rate"];
79 return round($price / (1 + ($tax_multiplier / 100)), user_price_dec());
82 // Full price (incl. VAT) for item $stock_id with line price $price,
83 // with tax rates $tax_group_array or applicable group $tax_group
85 function get_full_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
87 // if price is zero, then can't be taxed !
91 if ($tax_included==1) return $price;
93 // if array already read, then make a copy and use that
95 $ret_tax_array = $tax_group_array;
97 $ret_tax_array = get_tax_group_items_as_array($tax_group);
99 //print_r($ret_tax_array);
101 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
102 // if no exemptions or taxgroup is empty, then no included/excluded taxes
103 if ($tax_array == null)
108 // loop for all items
110 foreach ($tax_array as $taxitem)
112 $tax_multiplier += $taxitem["rate"];
115 return round($price * (1 + ($tax_multiplier / 100)), user_price_dec());
118 //---------------------------------------------------------------------------------
119 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate)
121 function get_taxes_for_item($stock_id, $tax_group_items_array)
123 $item_tax_type = get_item_tax_type_for_item($stock_id);
125 // if the item is exempt from all taxes then return 0
126 if ($item_tax_type["exempt"])
129 // get the exemptions for this item tax type
130 $item_tax_type_exemptions_db = get_item_tax_type_exemptions($item_tax_type["id"]);
132 // read them all into an array to minimize db querying
133 $item_tax_type_exemptions = array();
134 while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db))
136 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
139 $ret_tax_array = array();
141 // if any of the taxes of the tax group are in the exemptions, then skip
142 foreach ($tax_group_items_array as $tax_group_item)
147 // if it's in the exemptions, skip
148 foreach ($item_tax_type_exemptions as $exemption)
150 if (($tax_group_item['tax_type_id'] == $exemption))
159 $index = $tax_group_item['tax_type_id'];
160 $ret_tax_array[$index] = $tax_group_item;
164 return $ret_tax_array;
166 //-----------------------------------------------------------------------------------
167 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value)
169 function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_included=null, $tax_items_array=null)
171 // first create and set an array with all the tax types of the tax group
172 if($tax_items_array!=null)
173 $ret_tax_array = $tax_items_array;
175 $ret_tax_array = get_tax_group_items_as_array($tax_group);
177 foreach($ret_tax_array as $k=>$t)
178 $ret_tax_array[$k]['Net'] = 0;
180 // loop for all items
181 for ($i = 0; $i < count($items); $i++)
183 $item_taxes = get_taxes_for_item($items[$i], $ret_tax_array);
185 if ($item_taxes != null)
187 foreach ($item_taxes as $item_tax)
189 $index = $item_tax['tax_type_id'];
190 if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
191 //$ret_tax_array[$index]['Value'] += round($prices[$i] * $item_tax['rate']
192 // / ($item_tax['rate'] + 100), user_price_dec());
193 $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / ($item_tax['rate'] + 100));
194 $ret_tax_array[$index]['Net'] += ($prices[$i] * 100 / ($item_tax['rate'] + 100));
196 //$ret_tax_array[$index]['Value'] +=
197 // round($prices[$i] * $item_tax['rate'] / 100, user_price_dec());
198 $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / 100);
199 $ret_tax_array[$index]['Net'] += $prices[$i];
205 // add the shipping taxes, only if non-zero, and only if tax group taxes shipping
206 if ($shipping_cost != 0)
208 $item_taxes = get_shipping_tax_as_array();
209 if ($item_taxes != null)
211 foreach ($item_taxes as $item_tax)
213 $index = $item_tax['tax_type_id'];
214 if(isset($ret_tax_array[$index])) {
215 if($tax_included==1) {// 2008-11-26 Joe Hunt Taxes are stored without roundings
216 //$ret_tax_array[$index]['Value'] += round($shipping_cost * $item_tax['rate']
217 // / ($item_tax['rate'] + 100), user_price_dec());
218 $ret_tax_array[$index]['Value'] += ($shipping_cost * $item_tax['rate'] / ($item_tax['rate'] + 100));
219 $ret_tax_array[$index]['Net'] += ($shipping_cost * 100 / ($item_tax['rate'] + 100));
221 //$ret_tax_array[$index]['Value'] +=
222 // round($shipping_cost * $item_tax['rate'] / 100, user_price_dec());
223 $ret_tax_array[$index]['Value'] += ($shipping_cost * $item_tax['rate'] / 100);
224 $ret_tax_array[$index]['Net'] += $shipping_cost;
231 //print_r($ret_tax_array);
233 return $ret_tax_array;
236 // Get all taxes for given tax_type_id. This can be used when
237 // no tax group exemptions are active (local sales).
238 // To be used in quick entries.
240 function get_taxes_for_item_tax($tax_type_id)
242 $item_tax_type = get_item_tax_type($tax_type_id);
244 // if the item is exempt from all taxes then return 0
245 if ($item_tax_type["exempt"])
248 // get the exemptions for this item tax type
249 $item_tax_type_exemptions_db = get_item_tax_type_exemptions($tax_type_id);
251 // read them all into an array to minimize db querying
252 $item_tax_type_exemptions = array();
253 while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db))
255 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
258 $ret_tax_array = array();
260 $tax_rates = get_all_tax_types_simple();
261 // if any of the taxes are in the exemptions, then skip
262 while($tax_rate = db_fetch($tax_rates))
267 // if it's in the exemptions, skip
268 foreach ($item_tax_type_exemptions as $exemption)
270 if (($tax_rate['id'] == $exemption))
279 $index = $tax_rate['id'];
280 $ret_tax_array[$index] = $tax_rate;
284 return $ret_tax_array;
287 function is_tax_account($account_code)
289 $sql= "SELECT id FROM ".TB_PREF."tax_types WHERE
290 sales_gl_code='$account_code' OR purchasing_gl_code='$account_code'";
291 $result = db_query($sql, "checking account is tax account");
292 if (db_num_rows($result) > 0) {
293 $acct = db_fetch($result);