Italian (Italy)

apply_filters( 'vikappointments_calculate_tax', bool $status, VAPTaxRule $rule, float $total, object $data, array $options )

Fires while calculating the taxes with a custom math operation.


Description

Triggers a hook to let plugins calculate the taxes by using custom math operators, which might have been registered through the vikappointments_load_tax_operators filter.

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

$status

(bool)  True on success, false otherwise.

$rule

(VAPTaxRule)  The instance holding the tax rule settings.

$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. service 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 (missing when id_employee is specified).
  • id_employee - the ID of the employee that is going to purchase a subscription (missing when id_user is specified).

Example

The example below calculates the taxes for the EUVAT math operator, which have been registered through vikappointments_load_tax_operators.

/** 
 * Trigger hook to let plugins apply the taxes by using custom rules.
 *
 * @param  boolean     $status   True on success, false otherwise.
 * @param  VAPTaxRule  $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_filter('vikappointments_calculate_tax', function($status, $rule, $total, $data, $options)
{
    // make sure we are applying the taxes for the correct operator
    if ($rule->operator != 'euvat')
    {
        return $status;
    }

    $vatnum = $country = null;

    if (isset($options['id_user']))
    {
        // get customer details
        $customer = VikAppointments::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;
        }
    }
    else
    {
        // get employee details
        $employee = JModelVAP::getInstance('employee')->getItem($options['id_employee'], true);
        // decode billing details
        $billing = json_decode($employee->billing_json);

        if (!empty($billing->vat))
        {
            // take the VAT number and country (2-letters code) specified by the employee
            $vatnum  = $billing->vat;
            $country = $billing->country;
        }
    }

    // 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 $status;
        }
    }

    // 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 = VAPTaxRule::getInstance($ruleData);
    // complete tax calculation
    $rule->calculate($total, $data, $options);

    return $status;
}, 10, 5);

Changelog

Version Description
1.2 Introduced.
Ultimo aggiornamento: 2021-10-08
Utile?
Potrebbe interessarti: