Rounding errors in tax calculations for tax_included sales fixed again.
[fa-stable.git] / taxes / tax_calc.inc
1 <?php
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");
15
16 //---------------------------------------------------------------------------------
17
18 // returns the price of a given item minus any included taxes
19 // for item $stock_id with line price $price,
20 // with applicable tax rates $tax_group_array or group id $tax_group
21
22 function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
23 {
24         // if price is zero, then can't be taxed !
25         if ($price == 0)
26                 return 0;
27
28         if ($tax_included==0) return $price;
29         
30         // if array already read, then make a copy and use that
31         if ($tax_group_array)
32                 $ret_tax_array = $tax_group_array;
33         else
34                 $ret_tax_array = get_tax_group_items_as_array($tax_group);
35         
36         //print_r($ret_tax_array);
37
38         $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
39         // if no exemptions or taxgroup is empty, then no included/excluded taxes
40         if ($tax_array == null)
41                 return $price;
42
43         // to avoid rounding errors we have to just subtract taxes from tax_included price.
44         $tax_multiplier = 0;
45         foreach ($tax_array as $taxitem) 
46         {
47                 $tax_multiplier += $taxitem["rate"];
48         }
49
50         $tax = 0;
51         foreach ($tax_array as $taxitem)
52         {
53                 $tax += round($price*$taxitem['rate']/(100+$tax_multiplier), user_price_dec());
54         }
55         return $price-$tax;
56 }
57 //
58 //      Full price (incl. VAT) for item $stock_id with line price $price,
59 //      with tax rates $tax_group_array or applicable group $tax_group
60 //
61 function get_full_price_for_item($stock_id, $price, $tax_group, $tax_included, $tax_group_array=null)
62 {
63         // if price is zero, then can't be taxed !
64         if ($price == 0)
65                 return 0;
66
67         if ($tax_included==1) return $price;
68
69         // if array already read, then make a copy and use that
70         if ($tax_group_array)
71                 $ret_tax_array = $tax_group_array;
72         else
73                 $ret_tax_array = get_tax_group_items_as_array($tax_group);
74         
75         //print_r($ret_tax_array);
76
77         $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
78         // if no exemptions or taxgroup is empty, then no included/excluded taxes
79         if ($tax_array == null)
80                 return $price;
81         
82         $tax_multiplier = 0;
83
84         // loop for all items
85
86         foreach ($tax_array as $taxitem) 
87         {
88                         $tax_multiplier += $taxitem["rate"];
89         }
90         
91         return round($price * (1 + ($tax_multiplier / 100)),  user_price_dec());
92 }
93
94 //---------------------------------------------------------------------------------
95 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate)
96
97 function get_taxes_for_item($stock_id, $tax_group_items_array)
98 {
99         $item_tax_type = get_item_tax_type_for_item($stock_id);
100         
101         // if the item is exempt from all taxes then return 0
102         if ($item_tax_type["exempt"])
103                 return null;
104                 
105         // get the exemptions for this item tax type
106         $item_tax_type_exemptions_db = get_item_tax_type_exemptions($item_tax_type["id"]);
107         
108         // read them all into an array to minimize db querying
109         $item_tax_type_exemptions = array();
110         while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db)) 
111         {
112                 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
113         }
114         
115         $ret_tax_array = array();
116         
117         // if any of the taxes of the tax group are in the exemptions, then skip
118         foreach ($tax_group_items_array as $tax_group_item) 
119         { 
120                 
121                 $skip = false;
122                 
123                 // if it's in the exemptions, skip
124                 foreach ($item_tax_type_exemptions as $exemption) 
125                 {
126                         if (($tax_group_item['tax_type_id'] == $exemption)) 
127                         {
128                         $skip = true;
129                         break;
130                         }
131                 }
132                 
133                 if (!$skip) 
134                 {
135                         $index = $tax_group_item['tax_type_id'];
136                         $ret_tax_array[$index] = $tax_group_item;
137                 }
138         }
139         
140         return $ret_tax_array;
141 }
142 //-----------------------------------------------------------------------------------
143 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value) 
144
145 function get_tax_for_items($items, $prices, $shipping_cost, $tax_group, $tax_included=null, $tax_items_array=null)
146 {
147         // first create and set an array with all the tax types of the tax group
148         if($tax_items_array!=null)
149           $ret_tax_array = $tax_items_array;
150         else
151           $ret_tax_array = get_tax_group_items_as_array($tax_group);
152
153         foreach($ret_tax_array as $k=>$t)
154                 $ret_tax_array[$k]['Net'] = 0;
155
156         $dec = user_price_dec();
157         // loop for all items
158         for ($i = 0; $i < count($items); $i++)
159         {
160                 $item_taxes = get_taxes_for_item($items[$i], $ret_tax_array);
161                 if ($item_taxes != null) 
162                 {
163                         $tax_multiplier = 0;
164                         foreach ($item_taxes as $taxitem) 
165                         {
166                                 $tax_multiplier += $taxitem['rate'];
167                         }
168                         foreach ($item_taxes as $item_tax) 
169                         {
170                                 if ($item_tax['rate'] !== null) {
171                                         $index = $item_tax['tax_type_id'];
172                                         if ($tax_included == 1) {
173                                           $ret_tax_array[$index]['Value'] += round2($prices[$i]*$item_tax['rate']/(100+$tax_multiplier), $dec);
174                                           $ret_tax_array[$index]['Net'] += round2($prices[$i]*100/(100+$tax_multiplier), $dec);
175                                         } else {
176                                           $ret_tax_array[$index]['Value'] += ($prices[$i] * $item_tax['rate'] / 100);
177                                           $ret_tax_array[$index]['Net'] += $prices[$i];
178                                         }
179                                 }
180                         }
181                 }
182         }
183
184         // add the shipping taxes, only if non-zero, and only if tax group taxes shipping
185         if ($shipping_cost != 0) 
186         {
187                 $item_taxes = get_shipping_tax_as_array();
188                 if ($item_taxes != null) 
189                 {
190                         if ($tax_included == 1)
191                         {
192                                 $tax_rate = 0;
193                                 foreach ($item_taxes as $item_tax)
194                                 {
195                                         $index = $item_tax['tax_type_id'];
196                                         if(isset($ret_tax_array[$index])) {
197                                                 $tax_rate += $item_tax['rate'];
198                                         }
199                                 }
200                                 $shipping_net = round2($shipping_cost*100/(100+$tax_rate), $dec);
201                         }
202                         foreach ($item_taxes as $item_tax) 
203                         {
204                                 $index = $item_tax['tax_type_id'];
205                                 if ($item_tax['rate'] !== null && $ret_tax_array[$index]['rate'] !== null) {
206                                           if($tax_included==1) {
207                                             $ret_tax_array[$index]['Value'] += round2($shipping_cost*$item_tax['rate']/(100+$tax_rate), $dec);
208                                             $ret_tax_array[$index]['Net'] += $shipping_net;
209                                           } else {
210                                                 $ret_tax_array[$index]['Value'] += ($shipping_cost * $item_tax['rate'] / 100);
211                                             $ret_tax_array[$index]['Net'] += $shipping_cost;
212                                           }
213                                 }
214                         }
215                 }
216         }
217         //print_r($ret_tax_array);
218
219         return $ret_tax_array;
220 }
221
222 ?>