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