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