presseportale/app/Services/Billing/VatResolver.php
Kevin Adametz b255d90721 Optimierung 3.5: Kleinere Zentralisierungen (VAT, findByEmail, Waehrung, Provisioning)
- VatResolver::normalizeCountryCode()/isEuCountry() als Single Source of Truth;
  resolve(), VatIdValidationService und StripeCheckoutService nutzen jetzt die
  zentrale EU-Pruefung statt eigener config-Lookups.
- User::findByEmail() buendelt die LOWER(email)-Lookups (ContactAccessService,
  SocialAuthService). Bleibt bewusst case-insensitiv, da E-Mails nicht
  normalisiert gespeichert werden; der indexfreundliche Direktvergleich
  braucht erst eine Schreib-Normalisierung (offen, dokumentiert).
- config('billing.default_currency') ersetzt 8 verstreute 'EUR'/'eur'-Literale
  (Stripe-Calls via strtolower). Vereinheitlicht nebenbei die zuvor uneinheitlich
  gespeicherte SinglePurchase.currency.
- Neuer CustomerAccountProvisioner::provisionVerifiedCustomer() ist die
  gemeinsame Provisioning-Methode (verifiziert + aktiv + customer-Rolle) fuer
  ContactAccessService und SocialAuthService.
- Tests: UserFindByEmailTest, erweiterte VatResolverTest.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:14:05 +00:00

85 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\Billing;
use App\Enums\VatTreatment;
/**
* Bestimmt die USt-Behandlung einer Rechnung anhand der Rechnungsadresse
* (Entscheidung 12.06.2026):
*
* - Deutschland → grundsätzlich immer mit Steuer (`billing.vat_rate`)
* - EU-Ausland → nur mit gültiger USt-ID befreit (Reverse Charge),
* sonst mit Steuer
* - Drittland → grundsätzlich steuerbefreit
*
* „Gültig" heißt aktuell: USt-ID vorhanden und formal plausibel
* (Ländercode + Kennziffer). Eine echte VIES-Validierung ist als
* Folgeschritt vorgesehen (siehe Phase-9-Plan).
*/
class VatResolver
{
public function resolve(?string $countryCode, ?string $vatId = null): VatTreatment
{
$countryCode = $this->normalizeCountryCode($countryCode);
if ($countryCode === '' || $countryCode === 'DE') {
return VatTreatment::Domestic;
}
if (! $this->isEuCountry($countryCode)) {
return VatTreatment::ThirdCountry;
}
return $this->isPlausibleVatId($vatId, $countryCode)
? VatTreatment::ReverseCharge
: VatTreatment::EuConsumer;
}
/**
* Normalisiert einen Ländercode auf den ISO-3166-1-alpha-2-Großbuchstaben.
* Single Source of Truth, damit alle USt-Pfade dieselbe Schreibweise nutzen.
*/
public function normalizeCountryCode(?string $countryCode): string
{
return strtoupper((string) $countryCode);
}
/**
* Ist der (bereits normalisierte) Ländercode ein EU-Mitgliedstaat? DE zählt
* hier bewusst NICHT mit (Inland, siehe config `billing.eu_country_codes`).
*/
public function isEuCountry(string $countryCode): bool
{
return in_array($countryCode, (array) config('billing.eu_country_codes', []), true);
}
public function rateFor(VatTreatment $treatment): float
{
return $treatment->isTaxExempt() ? 0.0 : (float) config('billing.vat_rate', 0.19);
}
public function taxCentsFor(int $netCents, VatTreatment $treatment): int
{
return (int) round($netCents * $this->rateFor($treatment));
}
/**
* Formale Plausibilität: beginnt mit dem Ländercode der Adresse und
* trägt danach 2–13 alphanumerische Zeichen (EU-Formatrahmen).
* Public, damit der VatIdValidationService dieselbe Definition nutzt.
*/
public function isPlausibleVatId(?string $vatId, string $countryCode): bool
{
$vatId = strtoupper(preg_replace('/\s+/', '', (string) $vatId) ?? '');
if ($vatId === '') {
return false;
}
// Griechenland nutzt das Präfix EL statt GR.
$expectedPrefix = $countryCode === 'GR' ? 'EL' : $countryCode;
return (bool) preg_match('/^'.preg_quote($expectedPrefix, '/').'[A-Z0-9]{2,13}$/', $vatId);
}
}