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