Refactor WO diagnostic: move all DB queries inside modal, add try/catch

Queries now run only when modal is opened — no risk of page-load
exception hiding the button. Button is always visible for admin/owner.
Adds restore action inside modal footer.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-06 11:44:12 +00:00
parent 24bdc36913
commit 9be4aab7e5
@@ -22,43 +22,46 @@ class ListWorkOrders extends ListRecords
return $actions;
}
$tenantId = app(TenantManager::class)->currentId()
?? DB::table('companies')->where('slug', request()->getHost() === 'localhost' ? 'psauto' : explode('.', request()->getHost())[0])->value('id');
if (! $tenantId) {
return $actions;
}
// 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)")
->label('Diagnostic DB')
->color('gray')
->icon('heroicon-o-information-circle')
->modalHeading('Diagnostic fișe de lucru')
->modalContent(function () use ($tenantId, $total) {
// All companies with WO counts
->modalContent(function () {
try {
$tenantId = app(TenantManager::class)->currentId();
if (! $tenantId) {
$host = request()->getHost();
$slug = explode('.', $host)[0];
$tenantId = DB::table('companies')->where('slug', $slug)->value('id');
}
// Companies summary
$companies = DB::table('companies')
->select('companies.id', 'companies.name', 'companies.slug')
->selectRaw('COUNT(wo.id) as active_count')
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NOT NULL THEN 1 ELSE 0 END) as deleted_count')
->leftJoin('work_orders as wo', 'companies.id', '=', 'wo.company_id')
->select('companies.id', 'companies.name', 'companies.slug')
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NULL AND wo.id IS NOT NULL THEN 1 ELSE 0 END) as active_count')
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NOT NULL THEN 1 ELSE 0 END) as deleted_count')
->groupBy('companies.id', 'companies.name', 'companies.slug')
->get();
$html = "<h4 style='margin:0 0 8px;font-size:13px;font-weight:600'>Companii &amp; fișe</h4>";
$total = DB::table('work_orders')->count();
$html = "<p style='font-size:12px;color:#6b7280;margin-bottom:8px'>tenant_id curent: {$tenantId} | total WO în DB: {$total}</p>";
$html .= "<h4 style='margin:0 0 6px;font-size:13px;font-weight:600'>Companii</h4>";
$html .= "<table style='width:100%;border-collapse:collapse;font-size:13px;margin-bottom:16px'>";
$html .= "<tr style='background:#f3f4f6'><th style='padding:4px 8px;text-align:left'>ID</th><th>Slug</th><th>Nume</th><th>Active</th><th>Șterse</th><th>Curent?</th></tr>";
$html .= "<tr style='background:#f3f4f6'><th style='padding:4px 8px;text-align:left'>ID</th><th>Slug</th><th>Nume</th><th>Active</th><th>Șterse</th></tr>";
foreach ($companies as $c) {
$isCurrent = $c->id === $tenantId;
$isCurrent = (int) $c->id === (int) $tenantId;
$bg = $isCurrent ? '#dcfce7' : 'white';
$html .= "<tr style='background:{$bg}'><td style='padding:3px 8px'>{$c->id}</td><td><b>{$c->slug}</b></td><td>{$c->name}</td><td>{$c->active_count}</td><td>{$c->deleted_count}</td><td>" . ($isCurrent ? '✓ DA' : '') . "</td></tr>";
$html .= "<tr style='background:{$bg}'>";
$html .= "<td style='padding:3px 8px'>{$c->id}</td>";
$html .= "<td><b>{$c->slug}</b>" . ($isCurrent ? ' ✓' : '') . "</td>";
$html .= "<td>{$c->name}</td>";
$html .= "<td>{$c->active_count}</td>";
$html .= "<td>{$c->deleted_count}</td>";
$html .= "</tr>";
}
$html .= "</table>";
@@ -68,69 +71,53 @@ class ListWorkOrders extends ListRecords
->orderBy('id')
->get();
$html .= "<h4 style='margin:0 0 8px;font-size:13px;font-weight:600'>Toate fișele ({$total})</h4>";
$html .= "<h4 style='margin:0 0 6px;font-size:13px;font-weight:600'>Fișe ({$total})</h4>";
$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>";
$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>status</th></tr>";
foreach ($rows as $r) {
$isTenant = $r->company_id === $tenantId;
$isTenant = (int) $r->company_id === (int) $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>";
$flag = ! $isTenant ? '⚠ ALT TENANT' : ($isDeleted ? 'STERS' : '✓');
$html .= "<tr style='background:{$bg}'>";
$html .= "<td style='padding:3px 8px'>{$r->id}</td>";
$html .= "<td>#{$r->number}</td>";
$html .= "<td>{$r->company_id}</td>";
$html .= "<td>" . ($isTenant ? 'DA' : 'NU') . "</td>";
$html .= "<td>{$flag}</td>";
$html .= "</tr>";
}
$html .= "</table>";
$html .= "<p style='margin-top:12px;font-size:12px;color:#6b7280'>tenant_id curent: {$tenantId} | total în DB: {$total}</p>";
} catch (\Throwable $e) {
$html = "<p style='color:red'>Eroare: " . htmlspecialchars($e->getMessage()) . "</p>";
}
return new \Illuminate\Support\HtmlString($html);
})
->modalSubmitAction(false)
->modalCancelActionLabel('Închide');
// Restore button — only if there are soft-deleted WOs in this tenant
if ($deletedTenant > 0) {
$actions[] = Actions\Action::make('restore_deleted_wos')
->label("Restaurează {$deletedTenant} fișe șterse")
->modalCancelActionLabel('Închide')
->modalFooterActions([
Actions\Action::make('restore_deleted')
->label('Restaurează fișele șterse din tenant curent')
->color('success')
->icon('heroicon-o-arrow-uturn-left')
->requiresConfirmation()
->modalHeading('Restaurează fișe șterse')
->modalDescription("Există {$deletedTenant} fișe soft-șterse în această companie. Vrei să le restaurezi?")
->modalSubmitActionLabel('Da, restaurează-le')
->action(function () use ($tenantId) {
->action(function () {
$tenantId = app(TenantManager::class)->currentId();
if (! $tenantId) {
$slug = explode('.', request()->getHost())[0];
$tenantId = DB::table('companies')->where('slug', $slug)->value('id');
}
$restored = DB::table('work_orders')
->where('company_id', $tenantId)
->whereNotNull('deleted_at')
->update(['deleted_at' => null]);
Notification::make()
->title("{$restored} fișe restaurate cu succes")
->success()
->send();
});
}
// 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();
});
}
Notification::make()->title("{$restored} fișe restaurate")->success()->send();
}),
Actions\Action::make('close_diag')
->label('Închide')
->color('gray')
->cancelParentActions(),
]);
return $actions;
}