commerceguys / tax

A PHP 5.5+ tax library.
MIT License
276 stars 39 forks source link

tax

Build Status

A PHP 5.5+ tax management library.

Features:

Requires commerceguys/zone.

The backstory behind the library design can be found in this blog post.

Don't see your country's tax types and rates in the dataset? Send us a PR!

Data model

Zone 1-1 TaxType 1-n TaxRate 1-n TaxRateAmount

Each tax type has a zone and one or more tax rates. Each tax rate has one or more tax rate amounts.

Example:

The base interfaces don't impose setters, since they aren't needed by the service classes. Extended interfaces (TaxTypeEntityInterface, (TaxRateEntityInterface, (TaxRateAmountEntityInterface) are provided for that purpose, as well as matching TaxType, TaxRate and TaxRateAmount classes that can be used as examples or mapped by Doctrine.

Tax resolvers

The process of finding the most suitable tax type/rate/amount for the given taxable object is called resolving. Along with the Taxable object, a Context object containing customer and store information is also passed to the system.

Tax is resolved in three steps:

  1. Resolve the tax types.
  2. Resolve the tax rate for each resolved tax type.
  3. Get the tax rate amount for each resolved tax rate (by calling $rate->getAmount($date)).

Tax types and tax rates are resolved by invoking registered resolvers (sorted by priority) until one of them returns a result.

Included tax type resolvers:

Included tax rate resolvers:

Users would create a custom resolver for:

Usage example:

use CommerceGuys\Tax\Repository\TaxTypeRepository;
use CommerceGuys\Tax\Resolver\TaxType\ChainTaxTypeResolver;
use CommerceGuys\Tax\Resolver\TaxType\CanadaTaxTypeResolver;
use CommerceGuys\Tax\Resolver\TaxType\EuTaxTypeResolver;
use CommerceGuys\Tax\Resolver\TaxType\DefaultTaxTypeResolver;
use CommerceGuys\Tax\Resolver\TaxRate\ChainTaxRateResolver;
use CommerceGuys\Tax\Resolver\TaxRate\DefaultTaxRateResolver;
use CommerceGuys\Tax\Resolver\TaxResolver;

// The repository, and the resolvers are usualy initialized by the
// container, this is just a verbose example.
$taxTypeRepository = new TaxTypeRepository();
$chainTaxTypeResolver = new ChainTaxTypeResolver();
$chainTaxTypeResolver->addResolver(new CanadaTaxTypeResolver($taxTypeRepository));
$chainTaxTypeResolver->addResolver(new EuTaxTypeResolver($taxTypeRepository));
$chainTaxTypeResolver->addResolver(new DefaultTaxTypeResolver($taxTypeRepository));
$chainTaxRateResolver = new ChainTaxRateResolver();
$chainTaxRateResolver->addResolver(new DefaultTaxRateResolver());
$resolver = new TaxResolver($chainTaxTypeResolver, $chainTaxRateResolver);

// You can also provide the customer's tax number (e.g. VAT number needed
// to trigger Intra-Community supply rules in EU), list of additional countries
// where the store is registered to collect tax, a different calculation date.
$context = new Context($customerAddress, $storeAddress);

$amounts = $resolver->resolveAmounts($taxable, $context);
// More rarely, if only the types or rates are needed:
$rates = $resolver->resolveRates($taxable, $context);
$types = $resolver->resolveTypes($taxable, $context);

Credits