Corected inadequate shipping tax calculations.
[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
37 function get_tax_free_price_for_item($stock_id, $price, $tax_group, $tax_group_array=null)
38 {
39         // if price is zero, then can't be taxed !
40         if ($price == 0)
41                 return 0;
42
43         // if array already read, then make a copy and use that
44         if ($tax_group_array)
45                 $ret_tax_array = $tax_group_array;
46         else
47                 $ret_tax_array = get_tax_group_items_as_array($tax_group);
48         
49         //print_r($ret_tax_array);
50
51         $tax_array = get_taxes_for_item($stock_id, $ret_tax_array);
52         // if no exemptions or taxgroup is empty, then no included/excluded taxes
53         if ($tax_array == null)
54                 return $price;
55         
56         $tax_multiplier = 0;    
57
58         // loop for all items
59
60         foreach ($tax_array as $taxitem) 
61         {
62                 if ($taxitem["included_in_price"])
63                         $tax_multiplier += $taxitem["rate"];
64         }
65         
66         return $price / (1 + ($tax_multiplier / 100));  
67 }
68
69 //---------------------------------------------------------------------------------
70
71
72 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate)
73
74 function get_taxes_for_item($stock_id, $tax_group_items_array)
75 {
76         $item_tax_type = get_item_tax_type_for_item($stock_id);
77         
78         // if the item is exempt from all taxes then return 0
79         if ($item_tax_type["exempt"])
80                 return null;
81                 
82         // get the exemptions for this item tax type
83         $item_tax_type_exemptions_db = get_item_tax_type_exemptions($item_tax_type["id"]);
84         
85         // read them all into an array to minimize db querying
86         $item_tax_type_exemptions = array();
87         while ($item_tax_type_exemp = db_fetch($item_tax_type_exemptions_db)) 
88         {
89                 $item_tax_type_exemptions[] = $item_tax_type_exemp["tax_type_id"];
90         }
91         
92         $ret_tax_array = array();
93         
94         // if any of the taxes of the tax group are in the exemptions, then skip
95         foreach ($tax_group_items_array as $tax_group_item) 
96         { 
97                 
98                 $skip = false;                  
99                 
100                 // if it's in the exemptions, skip
101                 foreach ($item_tax_type_exemptions as $exemption) 
102                 {
103                         if (($tax_group_item['tax_type_id'] == $exemption)) 
104                         {
105                         $skip = true;
106                         break;
107                         }
108                 }
109                 
110                 if (!$skip) 
111                 {
112                         $index = $tax_group_item['tax_type_id'];
113                         $ret_tax_array[$index] = $tax_group_item;
114                 }
115         }
116         
117         return $ret_tax_array;
118 }
119
120 //-----------------------------------------------------------------------------------
121
122 // return an array of (tax_type_id, tax_type_name, sales_gl_code, purchasing_gl_code, rate, included_in_price, Value) 
123
124 function get_tax_for_items($items, $prices, $shipping_cost, $tax_group)
125 {
126         // first create and set an array with all the tax types of the tax group
127         $ret_tax_array = get_tax_group_items_as_array($tax_group);
128         
129         // loop for all items
130         for ($i = 0; $i < count($items); $i++)
131         {
132                 $item_taxes = get_taxes_for_item($items[$i], $ret_tax_array);
133
134                 if ($item_taxes != null) 
135                 {
136                         foreach ($item_taxes as $item_tax) 
137                         {
138                                 $index = $item_tax['tax_type_id'];
139                                 $ret_tax_array[$index]['Value'] += $prices[$i] * $item_tax['rate'] / 100;
140                         }
141                 }
142         }
143         
144         // add the shipping taxes, only if non-zero, and only if tax group taxes shipping
145         if ($shipping_cost != 0) 
146         {
147                 $item_taxes = get_shipping_tax_as_array();
148                 if ($item_taxes != null) 
149                 {
150                         foreach ($item_taxes as $item_tax) 
151                         {
152                                 $index = $item_tax['tax_type_id'];
153                                 if(!isset($ret_tax_array[$index]))
154                                     $ret_tax_array[$index] = $item_tax;
155                                 $ret_tax_array[$index]['Value'] += $shipping_cost * $item_tax['rate'] / 100;
156                         }
157                 }
158         }
159         
160         //print_r($ret_tax_array);
161
162         return $ret_tax_array;
163 }
164
165
166 ?>