webhub/vat

kdeldycke's VAT rates wrapped in a PHP library

0.7.0 2019-03-26 08:56 UTC

This package is auto-updated.

Last update: 2024-04-26 20:39:52 UTC


README

Build Status codecov Latest Stable Version Total Downloads License

This is a wrapper of kdeldycke/vat-rates.

🛠 Usage

Installation

composer require webhub/vat

Basic use

use Webhub\Vat\Rates;

$rate = (new Rates)->in('NL')->current()->get();

$rate->rate(); // 0.21
$rate->currencyCode(); // 'EUR'

$rate = (new Rates)->in('BE')->at('1990-01-01')->get();
$rate->rate(); // 0.12

Rates

A Rates instance is a collection of rates.

Rates can be filtered:

  • ->in(string $territory) a territory like DE or NL
  • ->at(string|Carbon $when) a date like 2018-01-05, supports Carbon
  • ->current() alias for ->at(Carbon::now())
  • ->type(string $type) rate type, currently the database only contains standard rates.

When one rate remains, the ->get() : Rate method retrieves it, otherwise it throws. Obtain all rates through ->all() : array.

Rate

A Rate has:

  • ->rate() : string decimal fraction representation of the rate, e.g. '0.20' for 20%
  • ->rateType() : string type of the rate, currently standard.
  • ->(start|stop)Date() : Carbon Carbon date instance of first valid day and subsequent first not-valid day.
  • ->currencyCode() : string currency like SEK or EUR
  • ->description() : ?string optional description of the rate

Rates proxies method calls to the underlying Rate if it exists and is unique.

// with ->get()
(new Rates)->in('DE')->current()->get()->rate();
// equals shorter:
(new Rates)->in('DE')->current()->rate();

// non unique
(new Rates)->in('FR')->rate();  // throws AmbiguousResultException
(new Rates)->in('XX')->get();   // throws NoResultException

Rate array access

A Rate implements ArrayAccess, so when using with for example Laravel's Collection, this is perfectly possible:

collect((new Rates)->in('NL')->all())
  ->sortBy('start_date')
  ->pluck('rate', 'start_date');

📝 Compiling a new dataset

Data is obtained from kdeldycke/vat-rates and written to a PHP file data.php that is included in Rates.

composer install
composer run build  // runs Generator::generate()

🇪🇺