Block 5 — MAN-Abrechnung F06 (E6): Ein explizites Legacy-Vertragsende (grandfathered_until) deckelt jetzt Auswahl und Sperr-Nachprüfung: Beginnt die nächste Periode nach dem Vertragsende, gibt es keine Rechnung mehr. Neuer täglicher Befehl billing:expire-grandfathering (04:15, vor dem Rechnungslauf) setzt abgelaufene Posten auf gekündigt und schickt je Kunde eine Mail (GrandfatheringExpired). Auf der Dev-Kopie trägt kein Posten ein Vertragsende; der Fehler war latent. F16: Nicht abrechenbare Vereinbarungen bekommen einen Sperrvermerk (billing_hold_reason/-at), sichtbar auf der Admin-Zahlungsseite, und stehen im Rechnungslauf hinten an. Der Lauf arbeitet den fälligen Bestand in Seiten ab; --limit deckelt die erstellten Rechnungen (500), nicht die betrachteten Fälle. Vorher blockierten 50 unvollständige Fälle die 51. dauerhaft, und der Durchsatz war auf 50 am Tag begrenzt. Block 6 — Entwicklung F24: Lint-Workflow prüft mit pint --test statt zu formatieren; keine Schreibrechte mehr. F27: composer run dev startet beide Vite-Server (npm run dev:all). F28: docs/README.md als historischer Stand markiert, toter Link auf das Hub-Flux-Archiv korrigiert, aktueller Einstieg dev/README.md. F26: Login-Modal übernimmt den Fokus, hält Tab im Dialog und gibt den Fokus beim Schließen an den Auslöser zurück – ohne neue Abhängigkeit. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
120 lines
3.8 KiB
PHP
120 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\UserPaymentOptionStatus;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class UserPaymentOption extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'payment_option_id',
|
|
'status',
|
|
'grandfathered_until',
|
|
'legacy_conditions',
|
|
'current_period_start',
|
|
'current_period_end',
|
|
'billing_hold_reason',
|
|
'billing_hold_at',
|
|
'stripe_subscription_id',
|
|
'cancelled_at',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => UserPaymentOptionStatus::class,
|
|
'grandfathered_until' => 'date',
|
|
'legacy_conditions' => 'array',
|
|
'current_period_start' => 'date',
|
|
'current_period_end' => 'date',
|
|
'billing_hold_at' => 'datetime',
|
|
'cancelled_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<PaymentOption, $this>
|
|
*/
|
|
public function paymentOption(): BelongsTo
|
|
{
|
|
return $this->belongsTo(PaymentOption::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany<Company, $this>
|
|
*/
|
|
public function companies(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Company::class, 'user_payment_option_company')
|
|
->withPivot('is_active')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<UserPayment, $this>
|
|
*/
|
|
public function payments(): HasMany
|
|
{
|
|
return $this->hasMany(UserPayment::class);
|
|
}
|
|
|
|
/**
|
|
* Aktuell gültige Zahlungsoption (period-aware): aktives Abo innerhalb der
|
|
* laufenden Periode ODER laufender Bestandsschutz. Single Source of Truth
|
|
* für „aktive Zahlungsoption" in API-Gate, Buchungs- und Kontingent-Checks.
|
|
*/
|
|
public function scopeActiveNow(Builder $query): Builder
|
|
{
|
|
return $query->where(function (Builder $query): void {
|
|
$query
|
|
->where(function (Builder $active): void {
|
|
$active
|
|
->where('status', UserPaymentOptionStatus::Active->value)
|
|
->whereDate('current_period_start', '<=', now())
|
|
->whereDate('current_period_end', '>=', now());
|
|
})
|
|
->orWhere(fn (Builder $grandfathered): Builder => $grandfathered->grandfatheredNow());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Laufender Bestandsschutz. `grandfathered_until` ist das explizite
|
|
* Legacy-Vertragsende (valid_until_date) und deckelt, wenn gesetzt.
|
|
* Bei offenen Dauervereinbarungen ist es NULL (Regelfall im Import) —
|
|
* dann zählt die laufende, vom MAN-Rechnungslauf fortgerollte Periode
|
|
* (`current_period_end` = next_due_date). Fix 06.07.2026: vorher galt
|
|
* NULL pauschal als abgelaufen, wodurch praktisch kein Legacy-Kunde
|
|
* eine aktive Zahlungsoption hatte („Buchung erforderlich").
|
|
*/
|
|
public function scopeGrandfatheredNow(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->where('status', UserPaymentOptionStatus::Grandfathered->value)
|
|
->where(function (Builder $query): void {
|
|
$query
|
|
->whereDate('grandfathered_until', '>=', now())
|
|
->orWhere(function (Builder $rolling): void {
|
|
$rolling
|
|
->whereNull('grandfathered_until')
|
|
->whereDate('current_period_end', '>=', now());
|
|
});
|
|
});
|
|
}
|
|
}
|