From 416b379d2705fa28d4c7b03db9d5d5ef583493b4 Mon Sep 17 00:00:00 2001 From: Vasyka Date: Sun, 6 Sep 2026 11:11:45 +0000 Subject: [PATCH] feat: add one-click orphaned WO recovery button for admins Instead of relying on terminal/artisan commands, admins see a warning button on the WO list when orphaned WOs exist (wrong company_id due to Octane singleton bug). One click moves them all to the current tenant. Button only shows if there are orphaned WOs and user is admin/owner. Co-Authored-By: Claude Sonnet 4.6 --- .../Pages/ListWorkOrders.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php index 200d424..9ce5efb 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php @@ -3,8 +3,11 @@ namespace App\Filament\Tenant\Resources\WorkOrderResource\Pages; use App\Filament\Tenant\Resources\WorkOrderResource; +use App\Tenancy\TenantManager; use Filament\Actions; +use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; +use Illuminate\Support\Facades\DB; class ListWorkOrders extends ListRecords { @@ -12,6 +15,42 @@ class ListWorkOrders extends ListRecords protected function getHeaderActions(): array { - return [Actions\CreateAction::make()]; + $actions = [Actions\CreateAction::make()]; + + // Show "recover orphaned WOs" button only for admin/owner + $user = auth()->user(); + if ($user && $user->isAdmin()) { + $tenantId = app(TenantManager::class)->currentId(); + $orphaned = $tenantId + ? DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', '!=', $tenantId) + ->count() + : 0; + + if ($orphaned > 0) { + $actions[] = Actions\Action::make('recover_wos') + ->label("Recuperează {$orphaned} fișe din alt tenant") + ->color('warning') + ->icon('heroicon-o-arrow-down-tray') + ->requiresConfirmation() + ->modalHeading('Recuperează fișe de lucru') + ->modalDescription("Există {$orphaned} fișe create cu tenant greșit (bug Octane). Vrei să le muți în compania curentă?") + ->modalSubmitActionLabel('Da, mută-le') + ->action(function () use ($tenantId, $orphaned) { + $moved = DB::table('work_orders') + ->whereNull('deleted_at') + ->where('company_id', '!=', $tenantId) + ->update(['company_id' => $tenantId]); + + Notification::make() + ->title("{$moved} fișe recuperate cu succes") + ->success() + ->send(); + }); + } + } + + return $actions; } }