Update partner packages, copy, and demo switcher
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-15 17:33:36 +01:00
parent 2f93271d94
commit ad829ae509
50 changed files with 1335 additions and 411 deletions

View File

@@ -19,6 +19,7 @@ class Package extends Model
'name_translations',
'slug',
'type',
'included_package_slug',
'price',
'max_photos',
'max_guests',

View File

@@ -100,7 +100,14 @@ class Tenant extends Model
public function activeResellerPackage(): HasOne
{
return $this->hasOne(TenantPackage::class)->where('active', true);
return $this->hasOne(TenantPackage::class)
->where('active', true)
->where(function ($query) {
$query->whereNull('expires_at')->orWhere('expires_at', '>', now());
})
->whereHas('package', fn ($query) => $query->withTrashed()->where('type', 'reseller'))
->orderBy('purchased_at')
->orderBy('id');
}
public function notificationLogs(): HasMany
@@ -151,6 +158,13 @@ class Tenant extends Model
return false;
}
public function hasEventAllowanceFor(?string $includedPackageSlug): bool
{
$package = $this->getActiveResellerPackageFor($includedPackageSlug);
return $package !== null && $package->canCreateEvent();
}
public function consumeEventAllowance(int $amount = 1, string $reason = 'event.create', ?string $note = null): bool
{
$package = $this->getActiveResellerPackage();
@@ -183,13 +197,68 @@ class Tenant extends Model
return false;
}
public function consumeEventAllowanceFor(?string $includedPackageSlug, int $amount = 1, string $reason = 'event.create', ?string $note = null): bool
{
$package = $this->getActiveResellerPackageFor($includedPackageSlug);
if ($package && $package->canCreateEvent()) {
$previousUsed = (int) $package->used_events;
$package->increment('used_events', $amount);
$package->refresh();
app(\App\Services\Packages\TenantUsageTracker::class)->recordEventUsage(
$package,
$previousUsed,
$amount
);
Log::info('Tenant package usage recorded', [
'tenant_id' => $this->id,
'tenant_package_id' => $package->id,
'used_events' => $package->used_events,
'amount' => $amount,
]);
return true;
}
Log::warning('Event allowance missing for tenant', [
'tenant_id' => $this->id,
'reason' => $reason,
'included_package_slug' => $includedPackageSlug,
]);
return false;
}
public function getActiveResellerPackage(): ?TenantPackage
{
return $this->activeResellerPackage()
->whereHas('package', fn ($query) => $query->withTrashed()->where('type', 'reseller'))
return $this->activeResellerPackage()->with('package')->first();
}
public function getActiveResellerPackageFor(?string $includedPackageSlug): ?TenantPackage
{
$query = $this->tenantPackages()
->with('package')
->where('active', true)
->orderByDesc('expires_at')
->first();
->where(function ($query) {
$query->whereNull('expires_at')->orWhere('expires_at', '>', now());
})
->whereHas('package', fn ($query) => $query->withTrashed()->where('type', 'reseller'))
->orderBy('purchased_at')
->orderBy('id');
if (is_string($includedPackageSlug) && $includedPackageSlug !== '') {
$query->whereHas('package', function ($query) use ($includedPackageSlug) {
$query->where('included_package_slug', $includedPackageSlug);
if ($includedPackageSlug === 'standard') {
$query->orWhereNull('included_package_slug');
}
});
}
return $query->first();
}
public function activeSubscription(): Attribute

View File

@@ -66,18 +66,30 @@ class TenantPackage extends Model
return false;
}
$maxEvents = $this->package->max_events_per_year ?? 0;
$maxEvents = $this->package->max_events_per_year;
if ($maxEvents === null) {
return true;
}
$maxEvents = max(0, (int) $maxEvents);
return $this->used_events < $maxEvents;
}
public function getRemainingEventsAttribute(): int
public function getRemainingEventsAttribute(): ?int
{
if (! $this->package->isReseller()) {
return 0;
}
$max = $this->package->max_events_per_year ?? 0;
$max = $this->package->max_events_per_year;
if ($max === null) {
return null;
}
$max = max(0, (int) $max);
return max(0, $max - $this->used_events);
}
@@ -94,9 +106,7 @@ class TenantPackage extends Model
$package = $tenantPackage->package;
if ($package && $package->isReseller()) {
if (! $tenantPackage->expires_at) {
$tenantPackage->expires_at = now()->addYear();
}
// Reseller packages represent prepaid Event-Kontingente and should not expire by default.
} elseif (! $tenantPackage->expires_at) {
$tenantPackage->expires_at = now()->addYear();
}