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
37 function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_group_array=null)
39 // if price is zero, then can't be taxed !
43 // if array already read, then make a copy and use that
45 $ret_tax_array = $tax_group_array;
47 $ret_tax_array = get_tax_group_items_as_array($tax_group);
49 //print_r($ret_tax_array);
51 $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
52 // if no exemptions or taxgroup is empty, then no included/excluded taxes
53 if ($tax_array == null)
60 foreach ($tax_array as $taxitem)
62 if ($taxitem["included_in_price"])
63 $tax_multiplier += $taxitem["rate"];
66 return $price / (1 + ($tax_multiplier / 100));
69 //---------------------------------------------------------------------------------
72 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate)
74 function get_taxes_for_item($stock_id, $tax_group_items_array)
76 $item_tax_type = get_item_tax_type_for_item($stock_id);
78 // if the item is exempt from all taxes then return 0
79 if ($item_tax_type["exempt"])
82 // get the exemptions for this item tax type
83 $item_tax_type_exemptions_db = get_item_tax_type_exemptions($item_tax_type["id"]);
85 // read them all into an array to minimize db querying
86 $item_tax_type_exemptions = array();
87 while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db))
89 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
92 $ret_tax_array = array();
94 // if any of the taxes of the tax group are in the exemptions, then skip
95 foreach ($tax_group_items_array as $tax_group_item)
100 // if it's in the exemptions, skip
101 foreach ($item_tax_type_exemptions as $exemption)
103 if (($tax_group_item['tax_type_id'] == $exemption))
112 $index = $tax_group_item['tax_type_id'];
113 $ret_tax_array[$index] = $tax_group_item;
117 return $ret_tax_array;
120 //-----------------------------------------------------------------------------------
122 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value)
124 function get_tax_for_items($items, $prices, $shipping_cost, $tax_group)
126 // first create and set an array with all the tax types of the tax group
127 $ret_tax_array = get_tax_group_items_as_array($tax_group);
129 // loop for all items
130 for ($i = 0; $i < count($items); $i++)
132 $item_taxes = get_taxes_for_item($items[$i], $ret_tax_array);
133 if ($item_taxes != null)
135 foreach ($item_taxes as $item_tax)
137 $index = $item_tax['tax_type_id'];
138 $ret_tax_array[$index]['Value'] += $prices[$i] * $item_tax['rate'] / 100;
143 // add the shipping taxes, only if non-zero, and only if tax group taxes shipping
144 if ($shipping_cost != 0)
146 $tax_group = get_tax_group($tax_group);
147 if ($tax_group['tax_shipping'])
149 foreach ($ret_tax_array as $item_tax)
151 $index = $item_tax['tax_type_id'];
152 $ret_tax_array[$index]['Value'] += $shipping_cost * $item_tax['rate'] / 100;
157 //print_r($ret_tax_array);
159 return $ret_tax_array;