phpstan lief mit 390 Meldungen außerhalb der Baseline, der größte Teil davon Falschmeldungen. Ursache: Larastan löste keinen einzigen Cast auf, weil alle Models ihre Casts über die casts()-METHODE deklarieren (Laravel-11-Stil). Laravel führt die erst zur Laufzeit mit $casts zusammen, Larastan instanziiert Models aber ohne Konstruktor — $pressRelease->status galt deshalb als string und jeder Enum-Vergleich als "immer falsch". Dafür gibt es den Schalter parseModelCastsMethod, dessen Larastan-Default false ist. Zweite Sammelursache waren fehlende Generics an den Relations-Methoden: Ohne sie landete phpstan auf Illuminate\Database\Eloquent\Model und meldete jede Modell-Eigenschaft und jeden eigenen Scope als unbekannt. Alle 102 Relationen tragen jetzt ihr @return <Related, $this>; das verbessert nebenbei die Vervollständigung in der IDE. Zusammen fällt die Zahl von 390 auf 169. Die neu erzeugte Baseline umfasst 166 Einträge, überwiegend Closure-Parameter, die als blankes Builder typisiert sind, sowie zwei nachgeprüfte Falschmeldungs-Gruppen: die Concerns unter app/Livewire werden ausschließlich aus Volt-Komponenten eingebunden, die phpstan nicht analysiert, und Cashier typisiert findBillable() gegen den TRAIT Billable, was phpstan nicht mit User vereinbaren kann — die Laufzeitprüfung dort ist richtig und bleibt. Was die Analyse an echten toten Zweigen zutage gefördert hat, ist mit entfernt: eine doppelte Null-Prüfung im Nachweis-PDF, Prüfungen gegen NOT-NULL-Spalten in der Rechnungsstellung und im Vorschau-Controller, eine immer wahre Bedingung im Logo-Pfad und in RetireAgencyCompanyType, ein überflüssiges ?? [] auf einer Symfony-Methode, die immer ein Array liefert, sowie der unerreichbare null-Arm in publicPortalLinks(). MigrateLegacyMedia deklariert seine Medientypen jetzt als Literal-Union, womit das match ohne default nachweislich vollständig ist statt nur faktisch. Damit das nicht wieder abdriftet — die Baseline stand seit Mai unverändert — läuft die Analyse ab jetzt als eigener Schritt in der Test-Pipeline. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
173 lines
6.8 KiB
PHP
173 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Billing;
|
|
|
|
use App\Enums\InvoiceStatus;
|
|
use App\Enums\UserPaymentOptionStatus;
|
|
use App\Models\Invoice;
|
|
use App\Models\InvoiceBillingAddress;
|
|
use App\Models\UserPaymentOption;
|
|
use App\Services\Billing\Concerns\LocksModelForUpdate;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Manueller Rechnungskreis (MAN-) für laufende Legacy-Zahlungen.
|
|
*
|
|
* Aktive Alt-Vereinbarungen leben in `user_payment_options` ohne
|
|
* `stripe_subscription_id`. Wie im Legacy-System wird im Hintergrund
|
|
* geprüft, wann eine Rechnung fällig ist (`current_period_end` erreicht),
|
|
* dann eine Rechnung im MAN-Kreis ausgestellt und die Periode
|
|
* weitergeschaltet. Neue Abschlüsse laufen ausschließlich über Stripe
|
|
* (STR-Kreis) und werden hier bewusst ausgeklammert.
|
|
*
|
|
* 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
|
|
{
|
|
use LocksModelForUpdate;
|
|
|
|
public function __construct(
|
|
private readonly InvoiceNumberGenerator $numbers,
|
|
private readonly VatResolver $vat,
|
|
) {}
|
|
|
|
/**
|
|
* @return Collection<int, UserPaymentOption>
|
|
*/
|
|
public function duePaymentOptions(?Carbon $asOf = null, int $limit = 50): Collection
|
|
{
|
|
$asOf = $asOf ?? today();
|
|
|
|
return UserPaymentOption::query()
|
|
->whereIn('status', [
|
|
UserPaymentOptionStatus::Active->value,
|
|
UserPaymentOptionStatus::Grandfathered->value,
|
|
])
|
|
->whereNull('stripe_subscription_id')
|
|
->whereDate('current_period_end', '<=', $asOf)
|
|
->orderBy('current_period_end')
|
|
->limit($limit)
|
|
->with(['user.billingAddress', 'paymentOption'])
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Stellt die fällige Rechnung für eine Vereinbarung aus und schaltet die
|
|
* Periode weiter. Gibt die Rechnung zurück oder null, wenn die
|
|
* Vereinbarung (noch) nicht abrechenbar ist — dann bleibt die Periode
|
|
* unverändert und der nächste Lauf versucht es erneut.
|
|
*/
|
|
public function invoiceFor(UserPaymentOption $option, ?Carbon $asOf = null): ?Invoice
|
|
{
|
|
$asOf = $asOf ?? today();
|
|
|
|
return $this->withLockedByKey($option, function (UserPaymentOption $lockedOption) use ($asOf): ?Invoice {
|
|
if (
|
|
! in_array($lockedOption->status, [
|
|
UserPaymentOptionStatus::Active,
|
|
UserPaymentOptionStatus::Grandfathered,
|
|
], true)
|
|
|| $lockedOption->stripe_subscription_id !== null
|
|
|| $lockedOption->current_period_end->isAfter($asOf)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
$lockedOption->loadMissing(['user.billingAddress', 'paymentOption']);
|
|
$user = $lockedOption->user;
|
|
$interval = $this->billingInterval($lockedOption);
|
|
|
|
if (! $user) {
|
|
Log::warning('MAN-Rechnung übersprungen: Vereinbarung ohne User.', ['user_payment_option_id' => $lockedOption->id]);
|
|
|
|
return null;
|
|
}
|
|
|
|
if (! $interval) {
|
|
Log::warning('MAN-Rechnung übersprungen: kein abrechenbares Intervall.', ['user_payment_option_id' => $lockedOption->id]);
|
|
|
|
return null;
|
|
}
|
|
|
|
$billingAddress = $user->billingAddress;
|
|
|
|
if (! $billingAddress) {
|
|
Log::warning('MAN-Rechnung übersprungen: User ohne Rechnungsadresse.', [
|
|
'user_payment_option_id' => $lockedOption->id,
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
|
|
$netCents = $this->resolveNetCents($lockedOption);
|
|
$treatment = $this->vat->resolve($billingAddress->country_code, $billingAddress->vat_id);
|
|
$taxCents = $this->vat->taxCentsFor($netCents, $treatment);
|
|
|
|
// Adresse pro Rechnung einfrieren (Snapshot-Tabelle).
|
|
$addressSnapshot = InvoiceBillingAddress::query()->create([
|
|
'salutation_key' => $billingAddress->salutation_key,
|
|
'title' => $billingAddress->title,
|
|
'company' => $billingAddress->company,
|
|
'name' => $billingAddress->name,
|
|
'address1' => $billingAddress->address1,
|
|
'address2' => $billingAddress->address2,
|
|
'postal_code' => $billingAddress->postal_code,
|
|
'city' => $billingAddress->city,
|
|
'country_code' => $billingAddress->country_code,
|
|
'vat_id' => $billingAddress->vat_id,
|
|
]);
|
|
|
|
$invoice = Invoice::query()->create([
|
|
'user_id' => $user->id,
|
|
'invoice_billing_address_id' => $addressSnapshot->id,
|
|
'number' => $this->numbers->nextManualNumber(),
|
|
'status' => InvoiceStatus::Open->value,
|
|
'amount_cents' => $netCents,
|
|
'tax_cents' => $taxCents,
|
|
'total_cents' => $netCents + $taxCents,
|
|
'currency' => $lockedOption->paymentOption?->currency ?? config('billing.default_currency'),
|
|
'is_netto' => $treatment->isTaxExempt(),
|
|
'tax_note' => $treatment->taxNote(),
|
|
'invoice_date' => $asOf,
|
|
'due_date' => $asOf->copy()->addDays((int) config('billing.manual_due_days', 14)),
|
|
]);
|
|
|
|
$lockedOption->update([
|
|
'current_period_start' => $lockedOption->current_period_end,
|
|
'current_period_end' => $interval === 'yearly'
|
|
? $lockedOption->current_period_end->copy()->addYear()
|
|
: $lockedOption->current_period_end->copy()->addMonth(),
|
|
]);
|
|
|
|
return $invoice;
|
|
});
|
|
}
|
|
|
|
private function billingInterval(UserPaymentOption $option): ?string
|
|
{
|
|
$interval = $option->legacy_conditions['interval']
|
|
?? $option->paymentOption?->interval;
|
|
|
|
return in_array($interval, ['monthly', 'yearly'], true) ? $interval : null;
|
|
}
|
|
|
|
/**
|
|
* 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 resolveNetCents(UserPaymentOption $option): int
|
|
{
|
|
$conditions = $option->legacy_conditions ?? [];
|
|
|
|
return (int) ($conditions['net_cents'] ?? $option->paymentOption?->price_cents ?? 0);
|
|
}
|
|
}
|