3 include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
4 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
5 include_once($path_to_root . "/taxes/db/item_tax_types_db.inc");
7 //---------------------------------------------------------------------------------
9 // returns the tax cost for a given item
11 function get_tax_value_for_item($stock_id, $price, $tax_group)
13 // if price is zero, then can't be taxed
17 $ret_tax_array = get_tax_group_items_as_array($tax_group);
19 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
20 if ($tax_array == null)
26 foreach ($tax_array as $taxitem) {
27 $tax_multiplier += $taxitem["rate"];
30 return $price * ($tax_multiplier / 100);
33 //---------------------------------------------------------------------------------
35 // returns the price of a given item minus any included taxes
36 // for item $stock_id with line price $price,
37 // with applicable tax rates $tax_group_array or group id $tax_group
40 function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
42 // if price is zero, then can't be taxed !
46 if ($tax_included==0) return $price;
48 // if array already read, then make a copy and use that
50 $ret_tax_array = $tax_group_array;
52 $ret_tax_array = get_tax_group_items_as_array($tax_group);
54 //print_r($ret_tax_array);
56 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
57 // if no exemptions or taxgroup is empty, then no included/excluded taxes
58 if ($tax_array == null)
65 foreach ($tax_array as $taxitem)
67 $tax_multiplier += $taxitem["rate"];
70 return $price / (1 + ($tax_multiplier / 100));
73 // Full price (incl. VAT) for item $stock_id with line price $price,
74 // with tax rates $tax_group_array or applicable group $tax_group
76 function get_full_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
78 // if price is zero, then can't be taxed !
82 if ($tax_included==1) return $price;
84 // if array already read, then make a copy and use that
86 $ret_tax_array = $tax_group_array;
88 $ret_tax_array = get_tax_group_items_as_array($tax_group);
90 //print_r($ret_tax_array);
92 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
93 // if no exemptions or taxgroup is empty, then no included/excluded taxes
94 if ($tax_array == null)
101 foreach ($tax_array as $taxitem)
103 $tax_multiplier += $taxitem["rate"];
106 return $price * (1 + ($tax_multiplier / 100));
109 //---------------------------------------------------------------------------------
110 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate)
112 function get_taxes_for_item($stock_id, $tax_group_items_array)
114 $item_tax_type = get_item_tax_type_for_item($stock_id);
116 // if the item is exempt from all taxes then return 0
117 if ($item_tax_type["exempt"])
120 // get the exemptions for this item tax type
121 $item_tax_type_exemptions_db = get_item_tax_type_exemptions($item_tax_type["id"]);
123 // read them all into an array to minimize db querying
124 $item_tax_type_exemptions = array();
125 while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db))
127 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
130 $ret_tax_array = array();
132 // if any of the taxes of the tax group are in the exemptions, then skip
133 foreach ($tax_group_items_array as $tax_group_item)
138 // if it's in the exemptions, skip
139 foreach ($item_tax_type_exemptions as $exemption)
141 if (($tax_group_item['tax_type_id'] == $exemption))
150 $index = $tax_group_item['tax_type_id'];
151 $ret_tax_array[$index] = $tax_group_item;
155 return $ret_tax_array;
157 //-----------------------------------------------------------------------------------
158 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value)
160 function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_included=null, $tax_items_array=null)
162 // first create and set an array with all the tax types of the tax group
163 if($tax_items_array!=null)
164 $ret_tax_array = $tax_items_array;
166 $ret_tax_array = get_tax_group_items_as_array($tax_group);
168 // loop for all items
169 for ($i = 0; $i < count($items); $i++)
171 $item_taxes = get_taxes_for_item($items[$i], $ret_tax_array);
173 if ($item_taxes != null)
175 foreach ($item_taxes as $item_tax)
177 $index = $item_tax['tax_type_id'];
179 $ret_tax_array[$index]['Value'] += $prices[$i] * $item_tax['rate']
180 / ($item_tax['rate'] + 100);
182 $ret_tax_array[$index]['Value'] += $prices[$i] * $item_tax['rate'] / 100;
187 // add the shipping taxes, only if non-zero, and only if tax group taxes shipping
188 if ($shipping_cost != 0)
190 $item_taxes = get_shipping_tax_as_array();
191 if ($item_taxes != null)
193 foreach ($item_taxes as $item_tax)
195 $index = $item_tax['tax_type_id'];
196 if(isset($ret_tax_array[$index])) {
198 $ret_tax_array[$index]['Value'] += $shipping_cost * $item_tax['rate']
199 / ($item_tax['rate'] + 100);
201 $ret_tax_array[$index]['Value'] += $shipping_cost * $item_tax['rate'] / 100;
207 //print_r($ret_tax_array);
209 return $ret_tax_array;