1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Rules integration for line items.
|
6
|
*
|
7
|
* @addtogroup rules
|
8
|
* @{
|
9
|
*/
|
10
|
|
11
|
|
12
|
/**
|
13
|
* Implements hook_rules_action_info().
|
14
|
*/
|
15
|
function commerce_tax_rules_action_info() {
|
16
|
$actions = array();
|
17
|
|
18
|
$actions['commerce_tax_remove_taxes'] = array(
|
19
|
'label' => t('Remove taxes applied to a line item'),
|
20
|
'parameter' => array(
|
21
|
'commerce_line_item' => array(
|
22
|
'type' => 'commerce_line_item',
|
23
|
'label' => t('Line item'),
|
24
|
'wrapped' => TRUE,
|
25
|
'save' => TRUE,
|
26
|
'restriction' => 'selector',
|
27
|
),
|
28
|
'increase_base_price' => array(
|
29
|
'type' => 'boolean',
|
30
|
'label' => t('Increase the base price amount by the amount of display inclusive taxes removed.'),
|
31
|
'description' => t('If left unchecked, a price of £2.95 including £0.49 VAT will be reduced to £2.46. If checked it will be set to £2.95 with no tax included.'),
|
32
|
'default value' => FALSE,
|
33
|
'restriction' => 'input',
|
34
|
),
|
35
|
'tax_rates' => array(
|
36
|
'type' => 'list<text>',
|
37
|
'label' => t('Limit removal to only a certain set of tax rates'),
|
38
|
'description' => t('If no tax rates are selected, all tax rates will be removed from the line item. Use ctrl + click (or command + click) to deselect a tax rate.'),
|
39
|
'options list' => 'commerce_tax_rate_titles',
|
40
|
'optional' => TRUE,
|
41
|
'restriction' => 'input',
|
42
|
),
|
43
|
),
|
44
|
'group' => t('Commerce Tax'),
|
45
|
);
|
46
|
|
47
|
if (count(commerce_tax_rates()) > 0) {
|
48
|
$actions['commerce_tax_rate_apply'] = array(
|
49
|
'label' => t('Apply a tax rate to a line item'),
|
50
|
'parameter' => array(
|
51
|
'commerce_line_item' => array(
|
52
|
'type' => 'commerce_line_item',
|
53
|
'label' => t('Line item'),
|
54
|
),
|
55
|
'tax_rate_name' => array(
|
56
|
'type' => 'text',
|
57
|
'label' => t('Tax rate'),
|
58
|
'options list' => 'commerce_tax_rate_titles',
|
59
|
),
|
60
|
),
|
61
|
'provides' => array(
|
62
|
'applied_tax' => array(
|
63
|
'type' => 'commerce_price',
|
64
|
'label' => t('Applied tax'),
|
65
|
),
|
66
|
),
|
67
|
'group' => t('Commerce Tax'),
|
68
|
'callbacks' => array(
|
69
|
'execute' => 'commerce_tax_rate_rules_apply',
|
70
|
),
|
71
|
);
|
72
|
}
|
73
|
|
74
|
if (count(commerce_tax_types()) > 0) {
|
75
|
$actions['commerce_tax_calculate_by_type'] = array(
|
76
|
'label' => t('Calculate taxes for a line item'),
|
77
|
'parameter' => array(
|
78
|
'commerce_line_item' => array(
|
79
|
'type' => 'commerce_line_item',
|
80
|
'label' => t('Line item'),
|
81
|
),
|
82
|
'tax_type_name' => array(
|
83
|
'type' => 'text',
|
84
|
'label' => t('Tax type'),
|
85
|
'options list' => 'commerce_tax_type_titles',
|
86
|
),
|
87
|
),
|
88
|
'group' => t('Commerce Tax'),
|
89
|
);
|
90
|
}
|
91
|
|
92
|
return $actions;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Rules actions: removes all taxes applied to a line item.
|
97
|
*/
|
98
|
function commerce_tax_remove_taxes($line_item_wrapper, $increase_base_price, $tax_rates) {
|
99
|
// Load all the tax component types to look for matching price components in
|
100
|
// the line item's unit price. Filter the list by tax rates that should be
|
101
|
// removed if only some were specified.
|
102
|
$component_types = commerce_tax_commerce_price_component_type_info();
|
103
|
|
104
|
if (!empty($tax_rates)) {
|
105
|
foreach ($component_types as $name => $component_type) {
|
106
|
if (!in_array($component_type['tax_rate'], $tax_rates)) {
|
107
|
unset($component_types[$name]);
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
// Loop over the price components in the line item's unit price.
|
113
|
$price = $line_item_wrapper->commerce_unit_price->value();
|
114
|
$new_components = array();
|
115
|
|
116
|
foreach ($price['data']['components'] as $key => $component) {
|
117
|
// Look for components that match one of the defined tax price components.
|
118
|
if (in_array($component['name'], array_keys($component_types))) {
|
119
|
// Remove it from the components array.
|
120
|
unset($price['data']['components'][$key]);
|
121
|
|
122
|
// If the component was marked as "included" in the price amount, update
|
123
|
// the price amount to reflect the difference.
|
124
|
if (!empty($component['included'])) {
|
125
|
$price['amount'] -= $component['price']['amount'];
|
126
|
|
127
|
// If the user opted to increase the base price by the amount of the
|
128
|
// display inclusive taxes removed, add them back as new price components.
|
129
|
if (!empty($increase_base_price)) {
|
130
|
$price['data']['components'][] = array(
|
131
|
'name' => 'base_price',
|
132
|
'price' => array(
|
133
|
'amount' => $component['price']['amount'],
|
134
|
'currency_code' => $component['price']['currency_code'],
|
135
|
'data' => array(),
|
136
|
),
|
137
|
'included' => TRUE,
|
138
|
);
|
139
|
|
140
|
$price['amount'] += $component['price']['amount'];
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
|
146
|
$line_item_wrapper->commerce_unit_price = $price;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Rules action: loads and applies a tax rate to the given line item.
|
151
|
*/
|
152
|
function commerce_tax_rate_rules_apply($line_item, $name) {
|
153
|
if ($tax_rate = commerce_tax_rate_load($name)) {
|
154
|
$tax_price = commerce_tax_rate_apply($tax_rate, $line_item);
|
155
|
|
156
|
// If tax was applied, return the price array as a new variable for use in
|
157
|
// subsequent actions.
|
158
|
if ($tax_price) {
|
159
|
return array('applied_tax' => $tax_price);
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Rules action: checks for the application of each tax rate of a certain type.
|
166
|
*/
|
167
|
function commerce_tax_calculate_by_type($line_item, $tax_type_name) {
|
168
|
if ($tax_type = commerce_tax_type_load($tax_type_name)) {
|
169
|
commerce_tax_type_calculate_rates($tax_type, $line_item);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* @}
|
175
|
*/
|