8d82af2f54
═══ Faza 3.5: Marketing ═══
Schema: msg_templates, marketing_channels, calls
Modele cu logică:
- MessageTemplate::render($context) — substituie {key} tokens
- MarketingChannel: roi/conversion_rate/cost_per_lead computed attrs
- Call: duration_formatted helper
Resources Filament (group Marketing):
- MessageTemplateResource: 5 canale (telegram/whatsapp/viber/sms/email)
- MarketingChannelResource: budget vs revenue cu ROI live calculat
- CallResource: in/out/missed cu filtre azi/missed
═══ Faza 3.6: Analytics ═══
Custom Filament Page Reports cu 6 rapoarte tab-uite:
- Finanțe: încasări/cheltuieli/profit/datorii + breakdown pe metodă/categorie
- Încărcare: fișe deschise/închise + breakdown pe status
- Mecanici: ore lucrate, manopere, venit per mecanic
- Manopere top: cele mai frecvente cu nr/ore/venit
- Piese: top vândute + low-stock
- Clienți: noi în perioadă + lead-uri pe sursă
Selector perioadă: azi / săptămâna / luna / luna trecută / anul
═══ Faza 4: Central provisioning ═══
- CoolifyClient service (Coolify v4 REST API wrapper)
- CompanyProvisioner: creează Company + admin user + roles + adaugă
subdomeniul în Coolify FQDN + trigger redeploy automat
- CreateCompany page override → folosește provisioner, returnează
notificare cu credentialele admin
- Form CompanyResource extins cu admin_name/email/password (vizibil doar create)
- Action 'Suspendă' / 'Activează' pe table cu confirmation
Env vars necesare în Coolify pentru provisioning auto:
COOLIFY_API_URL=http://65.21.20.141:8000
COOLIFY_API_TOKEN=<token>
COOLIFY_APP_UUID=g13hlrpd5g44zxl5af3ktio2
═══ Faza 5: PWA + branding ═══
- Route /manifest.json dinamic per tenant (nume, theme color, icons)
- Route /sw.js — service worker minimal (cache shell + static)
- TenantPanelProvider renderHook HEAD_END — link manifest + theme-color
+ apple-mobile-web-app meta
- TenantPanelProvider renderHook BODY_END — registrare service worker
Seed extins:
- 5 template-uri mesaje (programare/auto-gata/reminder/ITP/felicitare)
- 5 canale marketing (Google Ads/FB/IG/Telegram/Recomandări)
- 2 apeluri demo
Total Filament tenant routes: 81.
221 lines
7.5 KiB
PHP
221 lines
7.5 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';
|
|
|
|
protected static ?string $navigationLabel = 'Rapoarte';
|
|
|
|
protected static string|\UnitEnum|null $navigationGroup = '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];
|
|
}
|
|
}
|