Skip to content

Open-source event management and ticket selling platform β€” perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.8 KB
Raw Blame
<?php

namespace HiEvents\Services\Domain\Order\Vat;

use HiEvents\DomainObjects\Enums\CountryCode;
use HiEvents\DomainObjects\OrganizerVatSettingDomainObject;
use Illuminate\Config\Repository;
use ValueError;

class VatRateDeterminationService
{
    private float $defaultVatRate;

    private string $defaultVatCountry;

    public function __construct(
        private readonly Repository $config,
    ) {
        $this->defaultVatRate = $this->config->get('app.tax.default_vat_rate', 0.23);
        $this->defaultVatCountry = $this->config->get('app.tax.default_vat_country', CountryCode::IE->value);
    }

    public function determineVatRatePercentage(OrganizerVatSettingDomainObject $vatSetting): float
    {
        $country = $vatSetting->getVatCountryCode();

        // If no country code, user said they're not VAT registered β†’ charge default VAT
        if ($country === null) {
            return $this->defaultVatRate;
        }

        $hasVatNumber = ! empty($vatSetting->getVatNumber());
        $validated = $vatSetting->getVatValidated();

        // Try to determine if EU country, default to charging VAT if invalid country code
        try {
            $isEu = CountryCode::isEuCountry(CountryCode::from($country));
        } catch (ValueError) {
            return $this->defaultVatRate;
        }

        // 1. Default VAT country (e.g. IE) β†’ Always charge VAT, regardless of VAT number
        if ($country === $this->defaultVatCountry) {
            return $this->defaultVatRate;
        }

        // 2. If outside EU β†’ No VAT
        if (! $isEu) {
            return 0.0;
        }

        // 3. EU B2B with valid VAT β†’ Reverse charge (0%)
        if ($validated && $hasVatNumber) {
            return 0.0;
        }

        // 4. EU but no valid VAT β†’ Charge VAT
        return $this->defaultVatRate;
    }
}