USt-Behandlung: Netto-Preise, VatResolver und Steuer-Ausweis im MAN-Kreis
Einwand/Entscheidung 12.06.2026: Legacy fakturierte brutto (Steuer inkludiert, z. B. 199 Euro; steuerbefreite Kunden mit Netto-Ausweis 167,23). Alle neuen Preise sind netto; die Steuer wird zur Rechnungsstellung sauber validiert und ausgewiesen. - VatResolver + VatTreatment: DE grundsaetzlich immer mit Steuer, EU nur mit (formal plausibler) USt-ID befreit (Reverse Charge inkl. Pflichthinweis), Drittlaender grundsaetzlich befreit; EU-Laenderliste + vat_rate in config/billing.php - Schema: billing_addresses.vat_id + invoice_billing_addresses.vat_id (Snapshot pro Rechnung), invoices.tax_note; Profil-Formular schreibt die vorhandene USt-ID jetzt auch an die Rechnungsadresse - ManualInvoiceService: rechnet auf Netto-Vertragsbasis (legacy_conditions.net_cents bzw. Netto-Katalogpreis) und bestimmt Steuer/is_netto/tax_note pro Rechnung ueber den VatResolver - legacy:grandfather-subscriptions: leitet net_cents aus der letzten Legacy-Rechnung ab (brutto / 1,19 bzw. is_netto-Betrag direkt); fuer DE-Bestandskunden bleibt der Bruttobetrag unveraendert (199 brutto -> 167,23 netto + 31,77 USt = 199,00) - Doku: Decision-Update 2.1 (Netto-Klarstellung), Phase-9-Plan, Checkliste, 05-DATABASE-MERGE 5.6; offen: VIES-Validierung der USt-ID Tests: VatResolverTest (Datasets fuer alle Faelle), Reverse-Charge/ EU-/Drittland-Rechnungen, Netto-Ableitung; Suite 490 passed, 4 skipped. Pint clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
1cd4d8e33a
commit
894a9436b0
19 changed files with 497 additions and 46 deletions
|
|
@ -214,16 +214,35 @@ class GrandfatherLegacySubscriptions extends Command
|
|||
'article_number' => $option['article_number'] ?? null,
|
||||
'name' => $payload['payment_option_translation']['name'] ?? null,
|
||||
'interval' => 'yearly',
|
||||
'amount_cents' => $invoice->amount_cents,
|
||||
'tax_cents' => $invoice->tax_cents,
|
||||
'total_cents' => $invoice->total_cents,
|
||||
'is_netto' => (bool) ($snapshot['is_netto'] ?? false),
|
||||
'net_cents' => $this->deriveNetCents($invoice),
|
||||
'last_total_cents' => $invoice->total_cents,
|
||||
'last_is_netto' => (bool) ($snapshot['is_netto'] ?? false),
|
||||
'source_invoice_number' => $invoice->number,
|
||||
'source_invoice_date' => $invoice->invoice_date->toDateString(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Netto-Vertragsbasis aus der letzten Legacy-Rechnung. Legacy fakturierte
|
||||
* brutto (Steuer inkludiert, z. B. 199,00 €); steuerbefreite Kunden
|
||||
* erhielten den Netto-Ausweis (`is_netto`, z. B. 167,23 €). Die neue
|
||||
* Rechnungsstellung arbeitet immer auf Netto-Basis — die Steuer wird
|
||||
* pro Rechnung über den VatResolver bestimmt.
|
||||
*/
|
||||
private function deriveNetCents(LegacyInvoice $invoice): int
|
||||
{
|
||||
$isNetto = (bool) (($invoice->raw_snapshot ?? [])['is_netto'] ?? false);
|
||||
|
||||
if ($isNetto) {
|
||||
return $invoice->total_cents;
|
||||
}
|
||||
|
||||
$vatRate = (float) config('billing.vat_rate', 0.19);
|
||||
|
||||
return (int) round($invoice->total_cents / (1 + $vatRate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Versteckter Katalog-Eintrag pro (Portal, Legacy-Artikel) — die
|
||||
* verbindlichen Beträge pro Vereinbarung liegen in legacy_conditions.
|
||||
|
|
@ -237,7 +256,8 @@ class GrandfatherLegacySubscriptions extends Command
|
|||
['article_number' => $articleNumber],
|
||||
[
|
||||
'type' => 'recurring',
|
||||
'price_cents' => $candidate['legacy_conditions']['amount_cents'],
|
||||
// Katalogpreise sind netto (Entscheidung 12.06.2026).
|
||||
'price_cents' => $candidate['legacy_conditions']['net_cents'],
|
||||
'currency' => 'EUR',
|
||||
'interval' => 'yearly',
|
||||
'is_hidden' => true,
|
||||
|
|
@ -261,12 +281,12 @@ class GrandfatherLegacySubscriptions extends Command
|
|||
private function describe(array $candidate): string
|
||||
{
|
||||
return sprintf(
|
||||
'User #%d · %s · Legacy-UPO #%d · fällig %s · %s €',
|
||||
'User #%d · %s · Legacy-UPO #%d · fällig %s · netto %s €',
|
||||
$candidate['user_id'],
|
||||
$candidate['legacy_portal'],
|
||||
$candidate['legacy_upo_id'],
|
||||
$candidate['next_due_date']->toDateString(),
|
||||
number_format($candidate['legacy_conditions']['total_cents'] / 100, 2, ',', '.'),
|
||||
number_format($candidate['legacy_conditions']['net_cents'] / 100, 2, ',', '.'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
40
app/Enums/VatTreatment.php
Normal file
40
app/Enums/VatTreatment.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
/**
|
||||
* USt-Behandlung einer Rechnung (Entscheidung 12.06.2026):
|
||||
* Deutschland grundsätzlich immer mit Steuer, EU-Ausland nur mit gültiger
|
||||
* USt-ID befreit (Reverse Charge), Drittländer grundsätzlich befreit.
|
||||
*/
|
||||
enum VatTreatment: string
|
||||
{
|
||||
case Domestic = 'domestic';
|
||||
case EuConsumer = 'eu_consumer';
|
||||
case ReverseCharge = 'reverse_charge';
|
||||
case ThirdCountry = 'third_country';
|
||||
|
||||
public function isTaxExempt(): bool
|
||||
{
|
||||
return in_array($this, [self::ReverseCharge, self::ThirdCountry], true);
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Domestic => 'Inland (Deutschland)',
|
||||
self::EuConsumer => 'EU ohne USt-ID',
|
||||
self::ReverseCharge => 'EU mit USt-ID (Reverse Charge)',
|
||||
self::ThirdCountry => 'Drittland (steuerbefreit)',
|
||||
};
|
||||
}
|
||||
|
||||
public function taxNote(): ?string
|
||||
{
|
||||
return match ($this) {
|
||||
self::ReverseCharge => 'Steuerschuldnerschaft des Leistungsempfängers (Reverse Charge, Art. 196 MwStSystRL).',
|
||||
self::ThirdCountry => 'Nicht im Inland steuerbare Leistung (§ 3a Abs. 2 UStG).',
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -20,6 +20,7 @@ class BillingAddress extends Model
|
|||
'postal_code',
|
||||
'city',
|
||||
'country_code',
|
||||
'vat_id',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ class Invoice extends Model
|
|||
'total_cents',
|
||||
'currency',
|
||||
'is_netto',
|
||||
'tax_note',
|
||||
'invoice_date',
|
||||
'due_date',
|
||||
'paid_at',
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ class InvoiceBillingAddress extends Model
|
|||
'postal_code',
|
||||
'city',
|
||||
'country_code',
|
||||
'vat_id',
|
||||
];
|
||||
|
||||
public function invoices(): HasMany
|
||||
|
|
|
|||
|
|
@ -22,14 +22,19 @@ use Illuminate\Support\Facades\Log;
|
|||
* weitergeschaltet. Neue Abschlüsse laufen ausschließlich über Stripe
|
||||
* (STR-Kreis) und werden hier bewusst ausgeklammert.
|
||||
*
|
||||
* Konditions-Overrides pro Vereinbarung über `legacy_conditions` (JSON):
|
||||
* `amount_cents`, `tax_cents`, `total_cents`, `interval` (monthly|yearly).
|
||||
* Ohne Override gilt der Netto-Preis der `payment_option` plus
|
||||
* `billing.vat_rate`.
|
||||
* Preisbasis ist immer NETTO (Entscheidung 12.06.2026): `legacy_conditions.
|
||||
* net_cents` (von der Grandfather-Migration aus den Brutto-/Netto-Beträgen
|
||||
* der letzten Legacy-Rechnung abgeleitet), sonst der Netto-Preis der
|
||||
* `payment_option`. Die Steuer wird zur Rechnungsstellung über den
|
||||
* `VatResolver` bestimmt und sauber ausgewiesen (DE immer mit Steuer,
|
||||
* EU nur mit gültiger USt-ID befreit, Drittland befreit).
|
||||
*/
|
||||
class ManualInvoiceService
|
||||
{
|
||||
public function __construct(private readonly InvoiceNumberGenerator $numbers) {}
|
||||
public function __construct(
|
||||
private readonly InvoiceNumberGenerator $numbers,
|
||||
private readonly VatResolver $vat,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return Collection<int, UserPaymentOption>
|
||||
|
|
@ -87,9 +92,14 @@ class ManualInvoiceService
|
|||
return null;
|
||||
}
|
||||
|
||||
[$amountCents, $taxCents, $totalCents] = $this->resolveAmounts($option);
|
||||
$netCents = $this->resolveNetCents($option);
|
||||
|
||||
return DB::transaction(function () use ($option, $user, $billingAddress, $amountCents, $taxCents, $totalCents, $interval, $asOf): Invoice {
|
||||
// USt zur Rechnungsstellung bestimmen: DE immer mit Steuer, EU nur
|
||||
// mit gültiger USt-ID befreit (Reverse Charge), Drittland befreit.
|
||||
$treatment = $this->vat->resolve($billingAddress->country_code, $billingAddress->vat_id);
|
||||
$taxCents = $this->vat->taxCentsFor($netCents, $treatment);
|
||||
|
||||
return DB::transaction(function () use ($option, $user, $billingAddress, $netCents, $taxCents, $treatment, $interval, $asOf): Invoice {
|
||||
// Adresse pro Rechnung einfrieren (Snapshot-Tabelle).
|
||||
$addressSnapshot = InvoiceBillingAddress::query()->create([
|
||||
'salutation_key' => $billingAddress->salutation_key,
|
||||
|
|
@ -100,6 +110,7 @@ class ManualInvoiceService
|
|||
'postal_code' => $billingAddress->postal_code,
|
||||
'city' => $billingAddress->city,
|
||||
'country_code' => $billingAddress->country_code,
|
||||
'vat_id' => $billingAddress->vat_id,
|
||||
]);
|
||||
|
||||
$invoice = Invoice::query()->create([
|
||||
|
|
@ -107,10 +118,12 @@ class ManualInvoiceService
|
|||
'invoice_billing_address_id' => $addressSnapshot->id,
|
||||
'number' => $this->numbers->nextManualNumber(),
|
||||
'status' => InvoiceStatus::Open->value,
|
||||
'amount_cents' => $amountCents,
|
||||
'amount_cents' => $netCents,
|
||||
'tax_cents' => $taxCents,
|
||||
'total_cents' => $totalCents,
|
||||
'total_cents' => $netCents + $taxCents,
|
||||
'currency' => $option->paymentOption?->currency ?? 'EUR',
|
||||
'is_netto' => $treatment->isTaxExempt(),
|
||||
'tax_note' => $treatment->taxNote(),
|
||||
'invoice_date' => $asOf,
|
||||
'due_date' => $asOf->copy()->addDays((int) config('billing.manual_due_days', 14)),
|
||||
]);
|
||||
|
|
@ -135,22 +148,14 @@ class ManualInvoiceService
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array{0: int, 1: int, 2: int} [amount_cents, tax_cents, total_cents]
|
||||
* Netto-Vertragsbasis der Vereinbarung. Alle neuen Preise sind netto;
|
||||
* für Grandfathered-Vereinbarungen liefert die Migration `net_cents`
|
||||
* (aus den Brutto-/Netto-Beträgen der letzten Legacy-Rechnung).
|
||||
*/
|
||||
private function resolveAmounts(UserPaymentOption $option): array
|
||||
private function resolveNetCents(UserPaymentOption $option): int
|
||||
{
|
||||
$conditions = $option->legacy_conditions ?? [];
|
||||
|
||||
if (isset($conditions['amount_cents'], $conditions['total_cents'])) {
|
||||
$amount = (int) $conditions['amount_cents'];
|
||||
$total = (int) $conditions['total_cents'];
|
||||
|
||||
return [$amount, (int) ($conditions['tax_cents'] ?? $total - $amount), $total];
|
||||
}
|
||||
|
||||
$amount = (int) ($conditions['amount_cents'] ?? $option->paymentOption?->price_cents ?? 0);
|
||||
$tax = (int) round($amount * (float) config('billing.vat_rate', 0.19));
|
||||
|
||||
return [$amount, $tax, $amount + $tax];
|
||||
return (int) ($conditions['net_cents'] ?? $option->paymentOption?->price_cents ?? 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
66
app/Services/Billing/VatResolver.php
Normal file
66
app/Services/Billing/VatResolver.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?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 = strtoupper((string) $countryCode);
|
||||
|
||||
if ($countryCode === '' || $countryCode === 'DE') {
|
||||
return VatTreatment::Domestic;
|
||||
}
|
||||
|
||||
if (! in_array($countryCode, (array) config('billing.eu_country_codes', []), true)) {
|
||||
return VatTreatment::ThirdCountry;
|
||||
}
|
||||
|
||||
return $this->isPlausibleVatId($vatId, $countryCode)
|
||||
? VatTreatment::ReverseCharge
|
||||
: VatTreatment::EuConsumer;
|
||||
}
|
||||
|
||||
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).
|
||||
*/
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue