1f471cc1ea
MechanicKpi getPeriodLabel() was hardcoded to ->locale('ro') which
forced Romanian month names ('iulie 2026') regardless of the app
locale. Same on PDF appointments day-label. Both now switch to
app()->getLocale() so RU shows «июль 2026», EN 'July 2026'.
SetLocale middleware already calls Carbon::setLocale on every
request, so translatedFormat() elsewhere already respects the app
locale — no other files needed patching.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
99 lines
3.0 KiB
PHP
99 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Tenant\Pages;
|
|
|
|
use App\Models\Tenant\User;
|
|
use App\Models\Tenant\WorkOrderWork;
|
|
use Carbon\Carbon;
|
|
use Filament\Pages\Page;
|
|
|
|
/**
|
|
* Aggregate KPI dashboard per mechanic over a period: tasks done, norm vs
|
|
* actual hours, efficiency %, revenue from manopere. Period defaults to
|
|
* current month.
|
|
*/
|
|
class MechanicKpi extends Page
|
|
{
|
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
|
|
|
|
public static function getNavigationLabel(): string
|
|
{
|
|
return __('nav.label.KPI mecanici');
|
|
}
|
|
|
|
public static function getNavigationGroup(): ?string
|
|
{
|
|
return __('nav.group.Service');
|
|
}
|
|
|
|
protected static ?int $navigationSort = 28;
|
|
|
|
protected static ?string $title = null;
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return __('KPI mecanici');
|
|
}
|
|
|
|
protected string $view = 'filament.tenant.pages.mechanic-kpi';
|
|
|
|
public string $period = '';
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->period = now()->format('Y-m');
|
|
}
|
|
|
|
public function shiftMonth(int $delta): void
|
|
{
|
|
$this->period = Carbon::parse($this->period . '-01')->addMonths($delta)->format('Y-m');
|
|
}
|
|
|
|
public function getRows(): array
|
|
{
|
|
[$y, $m] = explode('-', $this->period);
|
|
|
|
$rows = WorkOrderWork::query()
|
|
->with('workOrder:id,master_id')
|
|
->where('mechanic_status', 'done')
|
|
->whereYear('mechanic_done_at', $y)
|
|
->whereMonth('mechanic_done_at', $m)
|
|
->get()
|
|
->groupBy(fn ($w) => $w->workOrder?->master_id ?: 0);
|
|
|
|
$masters = User::whereIn('id', $rows->keys()->all())->get(['id', 'name'])->keyBy('id');
|
|
|
|
$out = [];
|
|
foreach ($rows as $masterId => $works) {
|
|
if (! $masterId) continue;
|
|
$totalNorm = (float) $works->sum('hours');
|
|
$totalActual = (float) $works->sum('actual_hours');
|
|
$efficiencyPct = $totalNorm > 0 ? round(100 * $totalActual / $totalNorm) : null;
|
|
$cls = match (true) {
|
|
$efficiencyPct === null => 'gray',
|
|
$efficiencyPct <= 100 => 'green',
|
|
$efficiencyPct <= 130 => 'amber',
|
|
default => 'red',
|
|
};
|
|
$out[] = [
|
|
'master_id' => $masterId,
|
|
'master_name' => $masters[$masterId]?->name ?? 'Mecanic #' . $masterId,
|
|
'tasks_done' => $works->count(),
|
|
'norm_hours' => round($totalNorm, 2),
|
|
'actual_hours' => round($totalActual, 2),
|
|
'efficiency_pct' => $efficiencyPct,
|
|
'efficiency_class' => $cls,
|
|
'revenue' => round((float) $works->sum('total'), 2),
|
|
];
|
|
}
|
|
|
|
usort($out, fn ($a, $b) => $b['revenue'] <=> $a['revenue']);
|
|
return $out;
|
|
}
|
|
|
|
public function getPeriodLabel(): string
|
|
{
|
|
return Carbon::parse($this->period . '-01')->locale(app()->getLocale())->isoFormat('MMMM YYYY');
|
|
}
|
|
}
|