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:
@@ -17,38 +17,73 @@ class ListWorkOrders extends ListRecords
|
|||||||
{
|
{
|
||||||
$actions = [Actions\CreateAction::make()];
|
$actions = [Actions\CreateAction::make()];
|
||||||
|
|
||||||
// Show "recover orphaned WOs" button only for admin/owner
|
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
if ($user && $user->isAdmin()) {
|
if (! $user?->isAdmin()) {
|
||||||
$tenantId = app(TenantManager::class)->currentId();
|
return $actions;
|
||||||
$orphaned = $tenantId
|
}
|
||||||
? DB::table('work_orders')
|
|
||||||
->whereNull('deleted_at')
|
|
||||||
->where('company_id', '!=', $tenantId)
|
|
||||||
->count()
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
if ($orphaned > 0) {
|
$tenantId = app(TenantManager::class)->currentId();
|
||||||
$actions[] = Actions\Action::make('recover_wos')
|
if (! $tenantId) {
|
||||||
->label("Recuperează {$orphaned} fișe din alt tenant")
|
return $actions;
|
||||||
->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()
|
// Count ALL work orders across ALL companies (no scopes)
|
||||||
->title("{$moved} fișe recuperate cu succes")
|
$inTenant = DB::table('work_orders')->where('company_id', $tenantId)->whereNull('deleted_at')->count();
|
||||||
->success()
|
$deletedTenant = DB::table('work_orders')->where('company_id', $tenantId)->whereNotNull('deleted_at')->count();
|
||||||
->send();
|
$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;
|
return $actions;
|
||||||
|
|||||||
Reference in New Issue
Block a user