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:
@@ -22,115 +22,102 @@ class ListWorkOrders extends ListRecords
|
|||||||
return $actions;
|
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')
|
$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')
|
->color('gray')
|
||||||
->icon('heroicon-o-information-circle')
|
->icon('heroicon-o-information-circle')
|
||||||
->modalHeading('Diagnostic fișe de lucru')
|
->modalHeading('Diagnostic fișe de lucru')
|
||||||
->modalContent(function () use ($tenantId, $total) {
|
->modalContent(function () {
|
||||||
// All companies with WO counts
|
try {
|
||||||
$companies = DB::table('companies')
|
$tenantId = app(TenantManager::class)->currentId();
|
||||||
->select('companies.id', 'companies.name', 'companies.slug')
|
if (! $tenantId) {
|
||||||
->selectRaw('COUNT(wo.id) as active_count')
|
$host = request()->getHost();
|
||||||
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NOT NULL THEN 1 ELSE 0 END) as deleted_count')
|
$slug = explode('.', $host)[0];
|
||||||
->leftJoin('work_orders as wo', 'companies.id', '=', 'wo.company_id')
|
$tenantId = DB::table('companies')->where('slug', $slug)->value('id');
|
||||||
->groupBy('companies.id', 'companies.name', 'companies.slug')
|
}
|
||||||
->get();
|
|
||||||
|
|
||||||
$html = "<h4 style='margin:0 0 8px;font-size:13px;font-weight:600'>Companii & fișe</h4>";
|
// Companies summary
|
||||||
$html .= "<table style='width:100%;border-collapse:collapse;font-size:13px;margin-bottom:16px'>";
|
$companies = DB::table('companies')
|
||||||
$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>";
|
->leftJoin('work_orders as wo', 'companies.id', '=', 'wo.company_id')
|
||||||
foreach ($companies as $c) {
|
->select('companies.id', 'companies.name', 'companies.slug')
|
||||||
$isCurrent = $c->id === $tenantId;
|
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NULL AND wo.id IS NOT NULL THEN 1 ELSE 0 END) as active_count')
|
||||||
$bg = $isCurrent ? '#dcfce7' : 'white';
|
->selectRaw('SUM(CASE WHEN wo.deleted_at IS NOT NULL THEN 1 ELSE 0 END) as deleted_count')
|
||||||
$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>";
|
->groupBy('companies.id', 'companies.name', 'companies.slug')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$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></tr>";
|
||||||
|
foreach ($companies as $c) {
|
||||||
|
$isCurrent = (int) $c->id === (int) $tenantId;
|
||||||
|
$bg = $isCurrent ? '#dcfce7' : 'white';
|
||||||
|
$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>";
|
||||||
|
|
||||||
|
// All WOs
|
||||||
|
$rows = DB::table('work_orders')
|
||||||
|
->select('id', 'number', 'company_id', 'status', 'deleted_at')
|
||||||
|
->orderBy('id')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$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>status</th></tr>";
|
||||||
|
foreach ($rows as $r) {
|
||||||
|
$isTenant = (int) $r->company_id === (int) $tenantId;
|
||||||
|
$isDeleted = $r->deleted_at !== null;
|
||||||
|
$bg = ! $isTenant ? '#fef3c7' : ($isDeleted ? '#fee2e2' : 'white');
|
||||||
|
$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>";
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$html = "<p style='color:red'>Eroare: " . htmlspecialchars($e->getMessage()) . "</p>";
|
||||||
}
|
}
|
||||||
$html .= "</table>";
|
|
||||||
|
|
||||||
// All WOs
|
|
||||||
$rows = DB::table('work_orders')
|
|
||||||
->select('id', 'number', 'company_id', 'status', 'deleted_at')
|
|
||||||
->orderBy('id')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$html .= "<h4 style='margin:0 0 8px;font-size:13px;font-weight:600'>Toate fișele ({$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>";
|
|
||||||
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);
|
return new \Illuminate\Support\HtmlString($html);
|
||||||
})
|
})
|
||||||
->modalSubmitAction(false)
|
->modalSubmitAction(false)
|
||||||
->modalCancelActionLabel('Închide');
|
->modalCancelActionLabel('Închide')
|
||||||
|
->modalFooterActions([
|
||||||
// Restore button — only if there are soft-deleted WOs in this tenant
|
Actions\Action::make('restore_deleted')
|
||||||
if ($deletedTenant > 0) {
|
->label('Restaurează fișele șterse din tenant curent')
|
||||||
$actions[] = Actions\Action::make('restore_deleted_wos')
|
->color('success')
|
||||||
->label("Restaurează {$deletedTenant} fișe șterse")
|
->requiresConfirmation()
|
||||||
->color('success')
|
->action(function () {
|
||||||
->icon('heroicon-o-arrow-uturn-left')
|
$tenantId = app(TenantManager::class)->currentId();
|
||||||
->requiresConfirmation()
|
if (! $tenantId) {
|
||||||
->modalHeading('Restaurează fișe șterse')
|
$slug = explode('.', request()->getHost())[0];
|
||||||
->modalDescription("Există {$deletedTenant} fișe soft-șterse în această companie. Vrei să le restaurezi?")
|
$tenantId = DB::table('companies')->where('slug', $slug)->value('id');
|
||||||
->modalSubmitActionLabel('Da, restaurează-le')
|
}
|
||||||
->action(function () use ($tenantId) {
|
$restored = DB::table('work_orders')
|
||||||
$restored = DB::table('work_orders')
|
->where('company_id', $tenantId)
|
||||||
->where('company_id', $tenantId)
|
->whereNotNull('deleted_at')
|
||||||
->whereNotNull('deleted_at')
|
->update(['deleted_at' => null]);
|
||||||
->update(['deleted_at' => null]);
|
Notification::make()->title("{$restored} fișe restaurate")->success()->send();
|
||||||
|
}),
|
||||||
Notification::make()
|
Actions\Action::make('close_diag')
|
||||||
->title("{$restored} fișe restaurate cu succes")
|
->label('Închide')
|
||||||
->success()
|
->color('gray')
|
||||||
->send();
|
->cancelParentActions(),
|
||||||
});
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
return $actions;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user