Files
fotospiel-app/app/Filament/Clusters/DailyOps/Resources/TenantCheckoutHealths/TenantCheckoutHealthResource.php
Codex Agent 7262617897
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Refactor checkout health resource
2026-02-05 10:54:05 +01:00

122 lines
4.7 KiB
PHP

<?php
namespace App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths;
use App\Filament\Clusters\DailyOps\DailyOpsCluster;
use App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths\Pages\ListTenantCheckoutHealths;
use App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths\Tables\TenantCheckoutHealthTable;
use App\Models\CheckoutSession;
use App\Models\Tenant;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;
class TenantCheckoutHealthResource extends Resource
{
public const TRANSACTION_WINDOW_DAYS = 30;
public const DEFAULT_PROVIDER = CheckoutSession::PROVIDER_PAYPAL;
protected static ?string $model = Tenant::class;
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-credit-card';
protected static ?string $cluster = DailyOpsCluster::class;
protected static ?string $slug = 'checkout-health';
protected static ?int $navigationSort = 20;
public static function table(Table $table): Table
{
return TenantCheckoutHealthTable::configure($table);
}
public static function canCreate(): bool
{
return false;
}
public static function getNavigationLabel(): string
{
return __('admin.checkout_health.navigation.label');
}
public static function getNavigationGroup(): UnitEnum|string|null
{
return __('admin.nav.billing');
}
public static function getEloquentQuery(): Builder
{
$provider = static::provider();
$windowStart = now()->subDays(self::TRANSACTION_WINDOW_DAYS);
return parent::getEloquentQuery()
->with(['activeResellerPackage.package'])
->withExists('activeResellerPackage as has_active_reseller_package')
->withCount([
'purchases as checkout_transaction_count' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', false),
'purchases as checkout_transaction_count_window' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', false)
->where('purchased_at', '>=', $windowStart),
'purchases as checkout_refund_count_window' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', true)
->where('purchased_at', '>=', $windowStart),
'checkoutSessions as checkout_requires_action_count' => fn (Builder $query) => $query
->where('provider', $provider)
->where('status', CheckoutSession::STATUS_REQUIRES_CUSTOMER_ACTION),
'checkoutSessions as checkout_processing_count' => fn (Builder $query) => $query
->where('provider', $provider)
->where('status', CheckoutSession::STATUS_PROCESSING),
'checkoutSessions as checkout_expired_count' => fn (Builder $query) => $query
->where('provider', $provider)
->whereNotIn('status', [
CheckoutSession::STATUS_COMPLETED,
CheckoutSession::STATUS_CANCELLED,
])
->whereNotNull('expires_at')
->where('expires_at', '<', now()),
])
->withSum([
'purchases as checkout_transaction_total' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', false),
], 'price')
->withSum([
'purchases as checkout_transaction_total_window' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', false)
->where('purchased_at', '>=', $windowStart),
], 'price')
->withSum([
'purchases as checkout_refund_total_window' => fn (Builder $query) => $query
->where('provider', $provider)
->where('refunded', true)
->where('purchased_at', '>=', $windowStart),
], 'price')
->withMax([
'purchases as last_checkout_transaction_at' => fn (Builder $query) => $query
->where('provider', $provider),
], 'purchased_at');
}
public static function getPages(): array
{
return [
'index' => ListTenantCheckoutHealths::route('/'),
];
}
public static function provider(): string
{
return (string) config('checkout.default_provider', self::DEFAULT_PROVIDER);
}
}