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