diff --git a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php index dcf4a83..9478e0f 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource/Pages/ListWorkOrders.php @@ -22,115 +22,102 @@ 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 - $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') - ->groupBy('companies.id', 'companies.name', 'companies.slug') - ->get(); + ->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'); + } - $html = "

Companii & fișe

"; - $html .= ""; - $html .= ""; - foreach ($companies as $c) { - $isCurrent = $c->id === $tenantId; - $bg = $isCurrent ? '#dcfce7' : 'white'; - $html .= ""; + // Companies summary + $companies = DB::table('companies') + ->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(); + + $total = DB::table('work_orders')->count(); + + $html = "

tenant_id curent: {$tenantId} | total WO în DB: {$total}

"; + + $html .= "

Companii

"; + $html .= "
IDSlugNumeActiveȘterseCurent?
{$c->id}{$c->slug}{$c->name}{$c->active_count}{$c->deleted_count}" . ($isCurrent ? '✓ DA' : '') . "
"; + $html .= ""; + foreach ($companies as $c) { + $isCurrent = (int) $c->id === (int) $tenantId; + $bg = $isCurrent ? '#dcfce7' : 'white'; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + } + $html .= "
IDSlugNumeActiveȘterse
{$c->id}{$c->slug}" . ($isCurrent ? ' ✓' : '') . "{$c->name}{$c->active_count}{$c->deleted_count}
"; + + // All WOs + $rows = DB::table('work_orders') + ->select('id', 'number', 'company_id', 'status', 'deleted_at') + ->orderBy('id') + ->get(); + + $html .= "

Fișe ({$total})

"; + $html .= ""; + $html .= ""; + 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 .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + $html .= ""; + } + $html .= "
IDNr.company_idtenant?status
{$r->id}#{$r->number}{$r->company_id}" . ($isTenant ? 'DA' : 'NU') . "{$flag}
"; + } catch (\Throwable $e) { + $html = "

Eroare: " . htmlspecialchars($e->getMessage()) . "

"; } - $html .= ""; - - // All WOs - $rows = DB::table('work_orders') - ->select('id', 'number', 'company_id', 'status', 'deleted_at') - ->orderBy('id') - ->get(); - - $html .= "

Toate fișele ({$total})

"; - $html .= ""; - $html .= ""; - 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 .= ""; - } - $html .= "
IDNr.company_idtenant?șters?
{$r->id}#{$r->number}{$r->company_id}" . ($isTenant ? 'DA' : 'NU') . "{$flag}
"; - $html .= "

tenant_id curent: {$tenantId} | total în DB: {$total}

"; 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") - ->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) { - $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(); - }); - } + ->modalCancelActionLabel('Închide') + ->modalFooterActions([ + Actions\Action::make('restore_deleted') + ->label('Restaurează fișele șterse din tenant curent') + ->color('success') + ->requiresConfirmation() + ->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")->success()->send(); + }), + Actions\Action::make('close_diag') + ->label('Închide') + ->color('gray') + ->cancelParentActions(), + ]); return $actions; }