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