Add diagnostic button to WO list for admins to inspect DB state

Shows total WO count broken down by tenant/other-company/deleted across
all records without global scopes, helping debug missing WO visibility.
Also adds a recover action when orphaned WOs are found in wrong company.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-06 11:20:23 +00:00
parent 416b379d27
commit 0efe2e0bff
@@ -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 = "<table style='width:100%;border-collapse:collapse;font-size:13px'>";
$html .= "<tr style='background:#f3f4f6'><th style='padding:4px 8px;text-align:left'>ID</th><th>Nr.</th><th>company_id</th><th>tenant?</th><th>șters?</th></tr>";
foreach ($rows as $r) {
$isTenant = $r->company_id === $tenantId;
$isDeleted = $r->deleted_at !== null;
$bg = ! $isTenant ? '#fef3c7' : ($isDeleted ? '#fee2e2' : 'white');
$flag = ! $isTenant ? '⚠ ALT TENANT' : ($isDeleted ? '🗑 ȘTERS' : '✓');
$html .= "<tr style='background:{$bg}'><td style='padding:3px 8px'>{$r->id}</td><td>#{$r->number}</td><td>{$r->company_id}</td><td>" . ($isTenant ? 'DA' : 'NU') . "</td><td>{$flag}</td></tr>";
}
$html .= "</table>";
$html .= "<p style='margin-top:12px;font-size:12px;color:#6b7280'>tenant_id curent: {$tenantId} | total în DB: {$total}</p>";
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;