3911012c65
Broad sweep across the whole codebase:
- Blade views (39 files, 315 wraps): tag-text and title/placeholder/alt
attributes wrapped with {{ __() }}. Excludes scripts, styles, @php,
@verbatim, {{ }}, {!! !!}, comments to avoid touching interpolations.
- PHP (54 files, 179 wraps): array 'key' => 'RO value' patterns and
list items with diacritics wrapped with __(). Reverted __() inside
const arrays (PHP disallows non-constant expressions).
- Added 269 new keys to lang/{ru,en}.json (identity fallback for
unknowns → 161 human RU + 163 EN translations added for the most
common enums, stages, roles, statuses, payment methods, vehicle
categories, warehouse, portal, form actions.
Missing translations fall back to RO so the UI never breaks. All 306
tests pass; view cache compiles cleanly.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
232 lines
7.7 KiB
PHP
232 lines
7.7 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 = null;
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return __('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];
|
|
}
|
|
}
|