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:
Kevin Adametz 2026-06-12 10:58:43 +00:00
parent 1cd4d8e33a
commit 894a9436b0
19 changed files with 497 additions and 46 deletions

View file

@ -73,7 +73,9 @@ test('an active recurring legacy agreement is migrated as grandfathered with the
expect($agreement->current_period_start->toDateString())->toBe('2025-08-01');
expect($agreement->current_period_end->toDateString())->toBe('2026-08-01');
expect($agreement->legacy_conditions['interval'])->toBe('yearly');
expect($agreement->legacy_conditions['total_cents'])->toBe(4900);
// Legacy fakturierte brutto: 49,00 € inkl. USt → Netto-Basis 41,18 €.
expect($agreement->legacy_conditions['net_cents'])->toBe(4118);
expect($agreement->legacy_conditions['last_total_cents'])->toBe(4900);
expect($agreement->legacy_conditions['legacy_user_payment_option_id'])->toBe(42);
$catalog = PaymentOption::query()->where('article_number', 'LEGACY-PE-PK-01')->sole();
@ -111,7 +113,20 @@ test('the latest invoice per agreement wins', function () {
$agreement = UserPaymentOption::sole();
expect($agreement->current_period_end->toDateString())->toBe('2026-08-01');
expect($agreement->legacy_conditions['total_cents'])->toBe(4900);
expect($agreement->legacy_conditions['last_total_cents'])->toBe(4900);
});
test('a legacy net invoice keeps its amount as the net base', function () {
$user = User::factory()->create();
legacyArchiveInvoice($user, [
'amount_cents' => 16723,
'total_cents' => 16723,
'raw_snapshot' => ['is_netto' => true],
]);
$this->artisan(GrandfatherLegacySubscriptions::class, ['--no-report' => true])->assertSuccessful();
expect(UserPaymentOption::sole()->legacy_conditions['net_cents'])->toBe(16723);
});
test('re-running updates the agreement instead of duplicating it (pre-relaunch replay)', function () {
@ -156,11 +171,12 @@ test('dry-run writes nothing', function () {
expect(PaymentOption::query()->where('article_number', 'like', 'LEGACY-%')->count())->toBe(0);
});
test('after migration the MAN circle invoices a due legacy agreement with the legacy amounts', function () {
test('after migration the MAN circle invoices a due legacy agreement with proper VAT', function () {
$user = User::factory()->create();
BillingAddress::factory()->create(['user_id' => $user->id]);
BillingAddress::factory()->create(['user_id' => $user->id, 'country_code' => 'DE']);
// Fällig: next_due_date liegt vor dem Stichtag (überfälliger Jahreskunde).
// Fällig: next_due_date liegt vor dem Stichtag (überfälliger Jahreskunde,
// Legacy-Brutto 199,00 €).
legacyArchiveInvoice($user, [
'invoice_date' => '2025-05-14',
'amount_cents' => 19900,
@ -178,10 +194,13 @@ test('after migration the MAN circle invoices a due legacy agreement with the le
$invoice = $service->invoiceFor($due->first());
// DE-Kunde: Netto 167,23 € + 19 % USt = 199,00 € — Brutto bleibt wie im
// Legacy, die Steuer wird jetzt aber sauber ausgewiesen.
expect($invoice->number)->toBe('MAN-00001');
expect($invoice->amount_cents)->toBe(19900);
expect($invoice->tax_cents)->toBe(0);
expect($invoice->amount_cents)->toBe(16723);
expect($invoice->tax_cents)->toBe(3177);
expect($invoice->total_cents)->toBe(19900);
expect($invoice->is_netto)->toBeFalse();
// Periode jährlich weitergeschaltet.
expect($due->first()->fresh()->current_period_end->toDateString())->toBe('2027-05-14');

View file

@ -34,7 +34,7 @@ function manualAgreement(array $overrides = [], array $optionOverrides = []): Us
]);
}
test('a due manual agreement gets a MAN invoice and the period advances', function () {
test('a due manual agreement gets a MAN invoice with German VAT and the period advances', function () {
Carbon::setTestNow('2026-06-12 08:00:00');
$agreement = manualAgreement([
@ -46,9 +46,12 @@ test('a due manual agreement gets a MAN invoice and the period advances', functi
expect($invoice)->not->toBeNull();
expect($invoice->number)->toBe('MAN-00001');
expect($invoice->status)->toBe(InvoiceStatus::Open);
// Netto-Preisbasis 49,00 € + 19 % USt (Adresse DE).
expect($invoice->amount_cents)->toBe(4900);
expect($invoice->tax_cents)->toBe(931);
expect($invoice->total_cents)->toBe(5831);
expect($invoice->is_netto)->toBeFalse();
expect($invoice->tax_note)->toBeNull();
expect($invoice->due_date->toDateString())->toBe('2026-06-26');
$fresh = $agreement->fresh();
@ -58,15 +61,13 @@ test('a due manual agreement gets a MAN invoice and the period advances', functi
Carbon::setTestNow();
});
test('legacy_conditions override amounts and interval', function () {
test('legacy_conditions provide the net base and interval', function () {
Carbon::setTestNow('2026-06-12 08:00:00');
$agreement = manualAgreement([
'current_period_end' => '2026-06-10',
'legacy_conditions' => [
'amount_cents' => 10000,
'tax_cents' => 1900,
'total_cents' => 11900,
'net_cents' => 10000,
'interval' => 'yearly',
],
]);
@ -81,6 +82,43 @@ test('legacy_conditions override amounts and interval', function () {
Carbon::setTestNow();
});
test('an EU agreement with a valid vat id is invoiced tax-free as reverse charge', function () {
$agreement = manualAgreement();
$agreement->user->billingAddress->update(['country_code' => 'AT', 'vat_id' => 'ATU12345678']);
$invoice = app(ManualInvoiceService::class)->invoiceFor($agreement);
expect($invoice->amount_cents)->toBe(4900);
expect($invoice->tax_cents)->toBe(0);
expect($invoice->total_cents)->toBe(4900);
expect($invoice->is_netto)->toBeTrue();
expect($invoice->tax_note)->toContain('Reverse Charge');
expect($invoice->invoiceBillingAddress->vat_id)->toBe('ATU12345678');
});
test('an EU agreement without a vat id is invoiced with German VAT', function () {
$agreement = manualAgreement();
$agreement->user->billingAddress->update(['country_code' => 'AT', 'vat_id' => null]);
$invoice = app(ManualInvoiceService::class)->invoiceFor($agreement);
expect($invoice->tax_cents)->toBe(931);
expect($invoice->total_cents)->toBe(5831);
expect($invoice->is_netto)->toBeFalse();
});
test('a third-country agreement is invoiced tax-free', function () {
$agreement = manualAgreement();
$agreement->user->billingAddress->update(['country_code' => 'CH', 'vat_id' => null]);
$invoice = app(ManualInvoiceService::class)->invoiceFor($agreement);
expect($invoice->tax_cents)->toBe(0);
expect($invoice->total_cents)->toBe(4900);
expect($invoice->is_netto)->toBeTrue();
expect($invoice->tax_note)->toContain('Nicht im Inland steuerbar');
});
test('the invoice freezes the billing address as a snapshot', function () {
$agreement = manualAgreement();
$agreement->user->billingAddress->update(['name' => 'Alpha GmbH', 'city' => 'Berlin']);

View file

@ -0,0 +1,33 @@
<?php
use App\Enums\VatTreatment;
use App\Services\Billing\VatResolver;
test('vat treatment follows the 12.06. decision rules', function (?string $country, ?string $vatId, VatTreatment $expected) {
expect(app(VatResolver::class)->resolve($country, $vatId))->toBe($expected);
})->with([
'Deutschland immer mit Steuer' => ['DE', null, VatTreatment::Domestic],
'Deutschland auch mit USt-ID mit Steuer' => ['DE', 'DE123456789', VatTreatment::Domestic],
'EU mit gültiger USt-ID → Reverse Charge' => ['AT', 'ATU12345678', VatTreatment::ReverseCharge],
'EU ohne USt-ID → mit Steuer' => ['AT', null, VatTreatment::EuConsumer],
'EU mit fremdländischer USt-ID → mit Steuer' => ['AT', 'DE123456789', VatTreatment::EuConsumer],
'Griechenland mit EL-Präfix' => ['GR', 'EL123456789', VatTreatment::ReverseCharge],
'Drittland grundsätzlich befreit' => ['CH', null, VatTreatment::ThirdCountry],
'Drittland auch mit ID befreit' => ['US', 'US-TAX-1', VatTreatment::ThirdCountry],
'Fehlendes Land wie Inland behandeln' => [null, null, VatTreatment::Domestic],
]);
test('tax cents are derived from the treatment', function () {
$resolver = app(VatResolver::class);
expect($resolver->taxCentsFor(16723, VatTreatment::Domestic))->toBe(3177);
expect($resolver->taxCentsFor(16723, VatTreatment::EuConsumer))->toBe(3177);
expect($resolver->taxCentsFor(16723, VatTreatment::ReverseCharge))->toBe(0);
expect($resolver->taxCentsFor(16723, VatTreatment::ThirdCountry))->toBe(0);
});
test('exempt treatments carry a legal tax note', function () {
expect(VatTreatment::ReverseCharge->taxNote())->toContain('Reverse Charge');
expect(VatTreatment::ThirdCountry->taxNote())->toContain('Nicht im Inland steuerbar');
expect(VatTreatment::Domestic->taxNote())->toBeNull();
});