Konsolidierter Entwicklungsplan aus den zwei Analyse-Dokumenten plus
Umsetzung der Phase-0-Massnahmen (Korrektheit/Security, je mit Tests):
0.1 BasicAuth: timing-sicherer Vergleich (hash_equals, beide Felder ohne
Short-Circuit); Enabled-Check zur Laufzeit via config('auth.basic.*')
statt env() im Bootstrap-Closure (greift jetzt unter config:cache);
Config-Block + .env.example. Fund: auth.basic.* war nie als Config
definiert, Gate war faktisch unkonfigurierbar.
0.2 Legacy-Invoice-"latest"-Abfrage zentralisiert (LegacyInvoice::
scopeLatestFirst + User::latestLegacyInvoice); beide Consumer N+1-frei
umgestellt; unterschiedliches null-Verhalten bewusst erhalten.
0.3 Payment-Option-Eligibility vereinheitlicht (period-aware, strikt):
UserPaymentOption::scopeActiveNow/grandfatheredNow + User::
hasActivePaymentOption; hasActiveBooking/hasUnlimitedPressReleaseQuota
und ApiAccessEligibilityService darauf umgestellt; $hasGrandfathered-
Mislabel behoben.
0.4 Theme->Portal-Vokabular: totes, projektfremdes ThemeMiddleware
geloescht; Mapping in Portal::fromTheme() zentralisiert; fremden
b2in-Default im ThemeServiceProvider auf Portal-Default korrigiert.
0.5 InvoiceNumberGenerator: Erst-Insert race-sicher via insertOrIgnore
(Unique-Index auf circle existiert bereits). CreditWalletService war
bereits abgesichert (Unique-Index + createOrFirst).
Tests: 4 neue Dateien, 5 bestehende auf gueltiges grandfathered_until
angepasst. Volle Suite gruen (793 passed, 1 skipped).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
91 lines
2.7 KiB
PHP
91 lines
2.7 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',
|
|
'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',
|
|
'cancelled_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function paymentOption(): BelongsTo
|
|
{
|
|
return $this->belongsTo(PaymentOption::class);
|
|
}
|
|
|
|
public function companies(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Company::class, 'user_payment_option_company')
|
|
->withPivot('is_active')
|
|
->withTimestamps();
|
|
}
|
|
|
|
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 mit gültigem grandfathered_until).
|
|
*/
|
|
public function scopeGrandfatheredNow(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->where('status', UserPaymentOptionStatus::Grandfathered->value)
|
|
->whereDate('grandfathered_until', '>=', now());
|
|
}
|
|
}
|