Files
autocrm/app/Filament/Tenant/Pages/Reports.php
T
Vasyka 78ff8d4b43 feat: i18n on Filament admin sidebar — 54 resources/pages translated
User screenshots showed the tenant admin panel (Filament) had sidebar
labels stuck in Romanian even when switching to Russian: 'Cereri',
'Calendar vizual', 'Atelierul meu', 'KPI mecanici', 'Fișe lucru',
'Norme-ore', 'Tehnicieni', 'Șabloane servicii', 'Depozite', 'Scaner',
'Depozit', 'VIN-căutare', 'Furnizori', 'Achiziții', 'Procentaj',
'Coeficienți preț', plus all group headers.

Root cause: every Filament Resource and Page had static properties
'protected static ?string $navigationLabel = "Fișe lucru"' — string
literals baked into class definitions. Static properties don't run
through the translation layer.

Fix in two parts:

1. New translation files with 52 label keys + 12 group keys:
   - lang/ro/nav.php — Romanian (identity)
   - lang/ru/nav.php — full Russian translations (Заказ-наряды,
     Автомобили, Клиенты, Календарь, Моя мастерская, Механики KPI,
     Настройки, etc.)
   - lang/en/nav.php — English translations (Work orders, Vehicles,
     Clients, Calendar, My workshop, Mechanic KPI, Settings, etc.)

   Keyed by the Romanian original so lookups map 1:1 —
   'nav.label.Fișe lucru' returns 'Заказ-наряды' in RU, 'Work orders'
   in EN, 'Fișe lucru' in RO.

2. Python transformer converted 54 files:
   - 33 Filament Tenant Resources
   - 15 Filament Tenant Pages
   - 4 Filament Central Resources
   - 1 Filament Central Page
   - 1 Widget

   Each 'protected static ?string $navigationLabel = "X";' became
   'public static function getNavigationLabel(): string { return
   __("nav.label.X"); }'. Same treatment for $navigationGroup.

Cleanup: 6 resources already had manually-added getNavigationLabel
methods from an earlier partial effort — those used flat JSON keys
(__("Cereri")) that never resolved. Deduped so only the nav.label.*
version remains.

Untouched (intentional):
- $modelLabel / $pluralModelLabel (used in breadcrumbs and headings —
  still hardcoded, next tier of work)
- Section titles, column headers, form field labels (medium priority)
- $navigationSort (numeric, no translation needed)
- $navigationIcon (icon reference)

Suite: 306 passed (853 assertions). Unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-07-13 20:41:55 +00:00

227 lines
7.6 KiB
PHP

<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\Client;
use App\Models\Tenant\Expense;
use App\Models\Tenant\Lead;
use App\Models\Tenant\Part;
use App\Models\Tenant\Payment;
use App\Models\Tenant\User;
use App\Models\Tenant\WorkOrder;
use App\Models\Tenant\WorkOrderPart;
use App\Models\Tenant\WorkOrderWork;
use Carbon\Carbon;
use Filament\Pages\Page;
class Reports extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
public static function getNavigationLabel(): string
{
return __('nav.label.Rapoarte');
}
public static function getNavigationGroup(): ?string
{
return __('nav.group.Analiză');
}
protected static ?int $navigationSort = 70;
protected static ?string $title = 'Rapoarte';
protected string $view = 'filament.tenant.pages.reports';
public string $period = 'this_month';
public string $tab = 'finance';
public function dateRange(): array
{
return match ($this->period) {
'today' => [Carbon::today(), Carbon::today()->endOfDay()],
'this_week' => [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()],
'this_month' => [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()],
'last_month' => [Carbon::now()->subMonthNoOverflow()->startOfMonth(), Carbon::now()->subMonthNoOverflow()->endOfMonth()],
'this_year' => [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()],
default => [Carbon::now()->subYear(), Carbon::now()],
};
}
public function periods(): array
{
return [
'today' => 'Astăzi',
'this_week' => 'Săptămâna curentă',
'this_month' => 'Luna curentă',
'last_month' => 'Luna trecută',
'this_year' => 'Anul curent',
];
}
public function tabs(): array
{
return [
'finance' => '💰 Finanțe',
'workload' => '📊 Încărcare',
'masters' => '👨‍🔧 Mecanici',
'works' => '🔧 Manopere top',
'parts' => '📦 Piese',
'clients' => '👥 Clienți',
];
}
public function setPeriod(string $period): void
{
$this->period = $period;
}
public function setTab(string $tab): void
{
$this->tab = $tab;
}
public function data(): array
{
[$start, $end] = $this->dateRange();
return match ($this->tab) {
'finance' => $this->financeReport($start, $end),
'workload' => $this->workloadReport($start, $end),
'masters' => $this->mastersReport($start, $end),
'works' => $this->popularWorksReport($start, $end),
'parts' => $this->partsReport($start, $end),
'clients' => $this->clientsReport($start, $end),
default => [],
};
}
protected function financeReport($start, $end): array
{
$income = (float) Payment::whereBetween('paid_at', [$start, $end])->sum('amount');
$expenses = (float) Expense::whereBetween('paid_at', [$start, $end])->sum('amount');
$byMethod = Payment::whereBetween('paid_at', [$start, $end])
->selectRaw('method, COUNT(*) as cnt, SUM(amount) as total')
->groupBy('method')->get();
$byCategory = Expense::whereBetween('paid_at', [$start, $end])
->selectRaw('category, COUNT(*) as cnt, SUM(amount) as total')
->groupBy('category')->orderByDesc('total')->get();
$debt = (float) WorkOrder::where('pay_status', '!=', 'paid')
->whereNotIn('status', ['cancelled'])
->get()
->sum(fn ($w) => $w->balanceDue());
return [
'income' => $income,
'expenses' => $expenses,
'profit' => $income - $expenses,
'margin_pct' => $income > 0 ? round((($income - $expenses) / $income) * 100, 1) : 0,
'by_method' => $byMethod,
'by_category' => $byCategory,
'debt' => $debt,
];
}
protected function workloadReport($start, $end): array
{
$opened = WorkOrder::whereBetween('opened_at', [$start, $end])->count();
$closed = WorkOrder::whereBetween('closed_at', [$start, $end])->count();
$byStatus = WorkOrder::selectRaw('status, COUNT(*) as cnt')
->whereBetween('opened_at', [$start, $end])
->groupBy('status')->get();
$byDay = WorkOrder::selectRaw('DATE(opened_at) as day, COUNT(*) as cnt')
->whereBetween('opened_at', [$start, $end])
->groupBy('day')->orderBy('day')->get();
return [
'opened' => $opened,
'closed' => $closed,
'by_status' => $byStatus,
'by_day' => $byDay,
];
}
protected function mastersReport($start, $end): array
{
$rows = User::where('role', 'mechanic')->get()->map(function ($u) use ($start, $end) {
$works = WorkOrderWork::where('master_id', $u->id)
->whereHas('workOrder', fn ($q) => $q->whereBetween('opened_at', [$start, $end]))
->get();
$hoursTotal = (float) $works->sum('hours');
$revenueTotal = (float) $works->sum('total');
$worksCount = $works->count();
return [
'id' => $u->id,
'name' => $u->name,
'specialization' => $u->specialization,
'hours' => $hoursTotal,
'works' => $worksCount,
'revenue' => $revenueTotal,
];
})->sortByDesc('revenue')->values();
return ['rows' => $rows];
}
protected function popularWorksReport($start, $end): array
{
$rows = WorkOrderWork::selectRaw('name, COUNT(*) as cnt, SUM(hours) as hours, SUM(total) as revenue')
->whereHas('workOrder', fn ($q) => $q->whereBetween('opened_at', [$start, $end]))
->groupBy('name')
->orderByDesc('cnt')
->limit(20)
->get();
return ['rows' => $rows];
}
protected function partsReport($start, $end): array
{
$sold = WorkOrderPart::selectRaw('name, brand, SUM(qty) as qty, SUM(total) as revenue, SUM((sell_price - buy_price) * qty) as margin')
->whereHas('workOrder', fn ($q) => $q->whereBetween('opened_at', [$start, $end]))
->where('status', 'installed')
->groupBy('name', 'brand')
->orderByDesc('revenue')
->limit(20)
->get();
$low = Part::where('is_active', true)
->whereColumn('qty', '<=', 'min_qty')
->orderBy('qty')
->get();
return ['sold' => $sold, 'low' => $low];
}
protected function clientsReport($start, $end): array
{
$top = Client::withCount(['vehicles'])
->withSum(['workOrders' => fn ($q) => $q->whereBetween('opened_at', [$start, $end])], 'total')
->orderByDesc('work_orders_sum_total')
->limit(20)
->get();
// Fallback: if relation doesn't exist on Client
if ($top->isEmpty() || ! $top->first()->relationLoaded('workOrders')) {
$top = Client::withCount('vehicles')->limit(20)->get();
}
$newCount = Client::whereBetween('created_at', [$start, $end])->count();
$bySource = Lead::selectRaw('source, COUNT(*) as cnt')
->whereBetween('created_at', [$start, $end])
->groupBy('source')
->orderByDesc('cnt')
->get();
return ['top' => $top, 'new_count' => $newCount, 'by_source' => $bySource];
}
}