diff --git a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php index 9ce5efb..c3cd982 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php @@ -17,38 +17,73 @@ class ListWorkOrders extends ListRecords { $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 (! $user?->isAdmin()) { + return $actions; + } - 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]); + $tenantId = app(TenantManager::class)->currentId(); + if (! $tenantId) { + return $actions; + } - Notification::make() - ->title("{$moved} fișe recuperate cu succes") - ->success() - ->send(); - }); - } + // Count ALL work orders across ALL companies (no scopes) + $inTenant = DB::table('work_orders')->where('company_id', $tenantId)->whereNull('deleted_at')->count(); + $deletedTenant = DB::table('work_orders')->where('company_id', $tenantId)->whereNotNull('deleted_at')->count(); + $otherActive = DB::table('work_orders')->where('company_id', '!=', $tenantId)->whereNull('deleted_at')->count(); + $otherDeleted = DB::table('work_orders')->where('company_id', '!=', $tenantId)->whereNotNull('deleted_at')->count(); + $total = DB::table('work_orders')->count(); + + // Debug info action — always visible for admin + $actions[] = Actions\Action::make('wo_debug') + ->label("DB: {$total} total ({$inTenant} tenant, {$deletedTenant} șterse, {$otherActive} alt-tenant, {$otherDeleted} alt-tenant șterse)") + ->color('gray') + ->icon('heroicon-o-information-circle') + ->modalHeading('Diagnostic fișe de lucru') + ->modalContent(function () use ($tenantId, $inTenant, $deletedTenant, $otherActive, $otherDeleted, $total) { + $rows = DB::table('work_orders') + ->select('id', 'number', 'company_id', 'master_id', 'status', 'deleted_at') + ->orderBy('id') + ->get(); + + $html = "
| ID | Nr. | company_id | tenant? | șters? |
|---|---|---|---|---|
| {$r->id} | #{$r->number} | {$r->company_id} | " . ($isTenant ? 'DA' : 'NU') . " | {$flag} |
tenant_id curent: {$tenantId} | total în DB: {$total}
"; + + return new \Illuminate\Support\HtmlString($html); + }) + ->modalSubmitAction(false) + ->modalCancelActionLabel('Închide'); + + // Recover button — only if there are orphaned active WOs + if ($otherActive > 0) { + $actions[] = Actions\Action::make('recover_wos') + ->label("Recuperează {$otherActive} fișe din alt tenant") + ->color('warning') + ->icon('heroicon-o-arrow-down-tray') + ->requiresConfirmation() + ->modalHeading('Recuperează fișe de lucru') + ->modalDescription("Există {$otherActive} fișe cu company_id greșit. Vrei să le muți în compania curentă?") + ->modalSubmitActionLabel('Da, mută-le') + ->action(function () use ($tenantId) { + $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;