Calculate
do_action( 'vikrestaurants_calculate_tax', TaxRule $rule, float $total, object $data, array $options )
Fires while calculating the taxes with a custom math operation.
Description
Triggers a hook to let the plugins calculate the taxes by using custom math operators, which might have been registered through the vikrestaurants_load_tax_operators
action.
The calculated amounts should be registered within the $data argument, simply by increasing/decreasing the following properties:
net
- the total net amount;tax
- the total tax amount;gross
- the total gross amount;breakdown
- an array describing the applied taxes.
In example, in case of excluded taxes, it is appropriate to do the following:
// assume $tax has been calculated by our custom rule
$data->total += $tax;
$data->tax += $tax;
For included taxes, the tax and net properties should be updated instead:
// assume $tax has been calculated by our custom rule
$data->net -= $tax;
$data->tax += $tax;
At the end of the process, it is appropriate to register the description of the taxes within the breakdown. Unless it is needed to apply custom descriptions, the following code should be enough:
// assume $tax is the amount calculated by our custom rule
$rule->addBreakdown($tax, $data->breakdown);
Parameters
- $rule
-
(TaxRule) The instance holding the tax rule settings. This class is part of the
E4J\VikRestaurants\Taxing
namespace. - $total
-
(float) The total amount to use for taxes calculation.
- $data
-
(object) The object holding the totals to return.
- $options
-
(array) An array of configuration options.
subject
- the entity to which the TAX refers. When specified, the$id_tax
argument is equals to the ID of the given subject (e.g. takeaway.item with ID 7). In that case, the system will load the tax ID assigned to the specified record.lang
- the language tag specified by the user.id_user
- the ID of the customer that is going to purchase something.
Example
The example below calculates the taxes for the EUVAT math operator, which have been registered through vikrestaurants_load_tax_operators
.
/**
* Trigger hook to let plugins apply the taxes by using custom rules.
*
* @param TaxRule $rule The rule instance.
* @param float $total The total amount to use.
* @param object $data An object containing the tax details.
* @param array $options An array of options.
*/
add_action('vikappointments_calculate_tax', function($rule, $total, $data, $options) {
// make sure we are applying the taxes for the correct operator
if ($rule->operator != 'euvat') {
return;
}
$vatnum = $country = null;
if (isset($options['id_user'])) {
// get customer details
$customer = VikRestaurants::getCustomer($options['id_user']);
if ($customer) {
// take the VAT number and country (2-letters code) specified by the customer
$vatnum = $customer->vatnum;
$country = $customer->country_code;
}
}
// first of all make sure we have a VAT number to check
if ($vatnum) {
// VAT number has been specified, now lets check whether
// it is valid or not through the VIES service
$valid = false;
/**
* @todo connect to VIES and validate VAT
*/
if ($valid) {
// VAT number seems to be valid, ignore taxes
return;
}
}
// VAT number empty or invalid, apply taxes through the default "add" rule
// get rule properties
$ruleData = $rule->getProperties();
// replace operator with default "+%"
$ruleData['operator'] = 'add';
// create new rule instance
$rule = E4J\VikRestaurants\Taxing\TaxRule::getInstance($ruleData);
// complete tax calculation
$rule->calculate($total, $data, $options);
}, 10, 4);
Changelog
Version | Description |
---|---|
1.3 | Introduced. |