addeeandra/vat-calculator

1.0.0 2025-01-11 11:07 UTC

This package is auto-updated.

Last update: 2025-01-11 11:26:46 UTC


README

VAT Calculator

Configurable VAT Rate & Calculator

GitHub Workflow Status (main) Latest Stable Version

Vat Calculator ID

This library is a simple VAT calculator.

What's in this library?

  • VAT Rule 11%
  • VAT Rule 12% with custom base (e.g. 11/12)
  • VAT Calculator, when given VAT Rule, you can
    • Calculate VAT of an amount (e.g. AmountExclVat=100_000 => 11_000)
    • Extract VAT from an amount (e.g. AmountInclVat=111_000 => 11_000)
    • Get total amount include VAT (e.g. AmountExclVat=100_000 => 111_000)
    • Get amount exclude VAT from (e.g. AmountInclVat=111_000 => 100_000)

Installation

You can install this package by simply running composer require addeeandra/vat-calculator

How to use

To use this library is simple.

use \Addeeandra\VatCalculator\Rules;
use \Addeeandra\VatCalculator\Builders;

# To use PPN 11% Rule
$rule = new Rules\Vat12Rule();
$rule->calculate(100_000); // 11_000 (float)
$rule->calculator()->vatInAmount(111_000); // 11_000 (float)

# To use PPN 12% Rule
$rule = new Rules\Vat12Rule();
$rule->calculate(100_000); // 11_000 (float)
$rule->calculator()->vatInAmount(111_000); // 11_000 (float)

See VatRuleTest and VatCalculatorTest for more examples.

Need Custom VAT Rule?

You can make custom VAT Rule using VatRuleBuilder considering how fluid is our country's regulation.

image

// new VAT Rate 12% without base 11/12
$newVatRule = \Addeeandra\VatCalculator\Builders\VatRuleBuilder::make()
    ->rate(12) // 12%
    ->build();

// new VAT Rate 12% with base 15/12 which equals to 15%
$newVatRule = \Addeeandra\VatCalculator\Builders\VatRuleBuilder::make()
    ->rate(12) // 12%
    ->base(fn (int|float $amount) => $amount * 15 / 12) // whoops, it's actually 15% :)
    ->build();

License

MIT License