Faza 3.5+3.6+4+5: Marketing, Reports, Provisioning, PWA

═══ 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.
This commit is contained in:
2026-05-07 04:55:33 +00:00
parent f0f9fdd555
commit 8d82af2f54
26 changed files with 1512 additions and 1 deletions
+220
View File
@@ -0,0 +1,220 @@
<?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];
}
}
@@ -0,0 +1,102 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\CallResource\Pages;
use App\Models\Tenant\Call;
use App\Models\Tenant\Client;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class CallResource extends Resource
{
protected static ?string $model = Call::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-phone';
protected static ?string $navigationLabel = 'Apeluri';
protected static string|\UnitEnum|null $navigationGroup = 'Marketing';
protected static ?string $modelLabel = 'apel';
protected static ?string $pluralModelLabel = 'apeluri';
protected static ?int $navigationSort = 62;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Apel')
->columns(2)
->schema([
Forms\Components\DateTimePicker::make('called_at')->label('Data & ora')->default(now())->required(),
Forms\Components\Select::make('direction')
->options(Call::DIRECTIONS)
->default('incoming')
->required(),
Forms\Components\TextInput::make('phone')->label('Telefon')->tel()->required()->maxLength(40),
Forms\Components\Select::make('status')
->options(Call::STATUSES)
->default('answered')
->required(),
Forms\Components\TextInput::make('duration_sec')->label('Durată (sec)')->numeric()->default(0),
Forms\Components\Select::make('client_id')
->label('Client')
->options(fn () => Client::pluck('name', 'id'))
->searchable(),
]),
Forms\Components\Textarea::make('notes')->label('Notițe')->columnSpanFull()->rows(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('called_at')->label('Data')->dateTime('d.m.Y H:i')->sortable(),
Tables\Columns\TextColumn::make('direction')
->formatStateUsing(fn ($s) => Call::DIRECTIONS[$s] ?? $s)
->badge()
->colors([
'success' => ['incoming'],
'info' => ['outgoing'],
'danger' => ['missed'],
]),
Tables\Columns\TextColumn::make('phone')->copyable()->searchable(),
Tables\Columns\TextColumn::make('client.name')->label('Client')->placeholder('—'),
Tables\Columns\TextColumn::make('duration_formatted')->label('Durată')->state(fn (Call $r) => $r->duration_formatted),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => Call::STATUSES[$s] ?? $s)
->badge(),
])
->filters([
Tables\Filters\SelectFilter::make('direction')->options(Call::DIRECTIONS),
Tables\Filters\Filter::make('today')
->label('Astăzi')
->query(fn ($q) => $q->whereDate('called_at', today())),
Tables\Filters\Filter::make('missed')
->label('Pierdute')
->query(fn ($q) => $q->where('direction', 'missed')->orWhere('status', 'missed')),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('called_at', 'desc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListCalls::route('/'),
'create' => Pages\CreateCall::route('/create'),
'edit' => Pages\EditCall::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\CallResource\Pages;
use App\Filament\Tenant\Resources\CallResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateCall extends CreateRecord
{
protected static string $resource = CallResource::class;
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\CallResource\Pages;
use App\Filament\Tenant\Resources\CallResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditCall extends EditRecord
{
protected static string $resource = CallResource::class;
protected function getHeaderActions(): array { return [Actions\DeleteAction::make()]; }
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\CallResource\Pages;
use App\Filament\Tenant\Resources\CallResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListCalls extends ListRecords
{
protected static string $resource = CallResource::class;
protected function getHeaderActions(): array { return [Actions\CreateAction::make()]; }
}
@@ -0,0 +1,98 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\MarketingChannelResource\Pages;
use App\Models\Tenant\MarketingChannel;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class MarketingChannelResource extends Resource
{
protected static ?string $model = MarketingChannel::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-megaphone';
protected static ?string $navigationLabel = 'Canale marketing';
protected static string|\UnitEnum|null $navigationGroup = 'Marketing';
protected static ?string $modelLabel = 'canal';
protected static ?string $pluralModelLabel = 'canale marketing';
protected static ?int $navigationSort = 61;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Identificare')
->columns(3)
->schema([
Forms\Components\TextInput::make('name')->label('Nume canal')->required()->maxLength(120),
Forms\Components\TextInput::make('icon')->label('Iconiță (emoji)')->maxLength(8)->placeholder('🔍 / 📘 / 📸'),
Forms\Components\ColorPicker::make('color'),
]),
Schemas\Components\Section::make('Buget & rezultate (luna curentă)')
->columns(3)
->schema([
Forms\Components\TextInput::make('budget_monthly')->label('Buget')->numeric()->default(0),
Forms\Components\TextInput::make('spent_monthly')->label('Cheltuit')->numeric()->default(0),
Forms\Components\TextInput::make('revenue')->label('Venit generat')->numeric()->default(0),
Forms\Components\TextInput::make('leads_count')->label('Lead-uri')->numeric()->default(0),
Forms\Components\TextInput::make('converted_count')->label('Convertite')->numeric()->default(0),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\ColorColumn::make('color')->label(''),
Tables\Columns\TextColumn::make('icon')->label('')->width(40),
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('budget_monthly')->label('Buget')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('spent_monthly')->label('Cheltuit')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('leads_count')->label('Lead-uri')->alignRight(),
Tables\Columns\TextColumn::make('converted_count')->label('Convertite')->alignRight(),
Tables\Columns\TextColumn::make('revenue')->label('Venit')->money('MDL')->alignRight()->color('success'),
Tables\Columns\TextColumn::make('roi')
->label('ROI')
->state(fn (MarketingChannel $r) => $r->roi)
->formatStateUsing(fn ($s) => $s . '%')
->color(fn ($s) => $s >= 0 ? 'success' : 'danger')
->alignRight(),
Tables\Columns\TextColumn::make('cost_per_lead')
->label('Cost/lead')
->state(fn (MarketingChannel $r) => $r->cost_per_lead)
->money('MDL')
->alignRight(),
Tables\Columns\IconColumn::make('is_active')->boolean(),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('revenue', 'desc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListMarketingChannels::route('/'),
'create' => Pages\CreateMarketingChannel::route('/create'),
'edit' => Pages\EditMarketingChannel::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MarketingChannelResource\Pages;
use App\Filament\Tenant\Resources\MarketingChannelResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateMarketingChannel extends CreateRecord
{
protected static string $resource = MarketingChannelResource::class;
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MarketingChannelResource\Pages;
use App\Filament\Tenant\Resources\MarketingChannelResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditMarketingChannel extends EditRecord
{
protected static string $resource = MarketingChannelResource::class;
protected function getHeaderActions(): array { return [Actions\DeleteAction::make()]; }
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MarketingChannelResource\Pages;
use App\Filament\Tenant\Resources\MarketingChannelResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListMarketingChannels extends ListRecords
{
protected static string $resource = MarketingChannelResource::class;
protected function getHeaderActions(): array { return [Actions\CreateAction::make()]; }
}
@@ -0,0 +1,86 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\MessageTemplateResource\Pages;
use App\Models\Tenant\MessageTemplate;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class MessageTemplateResource extends Resource
{
protected static ?string $model = MessageTemplate::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-chat-bubble-left-right';
protected static ?string $navigationLabel = 'Template-uri mesaje';
protected static string|\UnitEnum|null $navigationGroup = 'Marketing';
protected static ?string $modelLabel = 'template';
protected static ?string $pluralModelLabel = 'template-uri';
protected static ?int $navigationSort = 60;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Identificare')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label('Nume template')->required()->maxLength(120),
Forms\Components\Select::make('channel')
->options(MessageTemplate::CHANNELS)
->default('telegram')
->required(),
Forms\Components\TextInput::make('subject')->label('Subiect (email)')->maxLength(160)->columnSpanFull(),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
Schemas\Components\Section::make('Conținut')
->columns(1)
->schema([
Forms\Components\Textarea::make('body')
->label('Mesaj')
->required()
->rows(6)
->helperText('Variabile disponibile: {name}, {car}, {date}, {time}, {amount}, {service}, {mileage}'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('channel')
->formatStateUsing(fn ($s) => MessageTemplate::CHANNELS[$s] ?? $s)
->badge(),
Tables\Columns\TextColumn::make('body')->label('Preview')->limit(60),
Tables\Columns\IconColumn::make('is_active')->boolean(),
])
->filters([
Tables\Filters\SelectFilter::make('channel')->options(MessageTemplate::CHANNELS),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('channel');
}
public static function getPages(): array
{
return [
'index' => Pages\ListMessageTemplates::route('/'),
'create' => Pages\CreateMessageTemplate::route('/create'),
'edit' => Pages\EditMessageTemplate::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MessageTemplateResource\Pages;
use App\Filament\Tenant\Resources\MessageTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateMessageTemplate extends CreateRecord
{
protected static string $resource = MessageTemplateResource::class;
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MessageTemplateResource\Pages;
use App\Filament\Tenant\Resources\MessageTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditMessageTemplate extends EditRecord
{
protected static string $resource = MessageTemplateResource::class;
protected function getHeaderActions(): array { return [Actions\DeleteAction::make()]; }
}
@@ -0,0 +1,14 @@
<?php
namespace App\Filament\Tenant\Resources\MessageTemplateResource\Pages;
use App\Filament\Tenant\Resources\MessageTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListMessageTemplates extends ListRecords
{
protected static string $resource = MessageTemplateResource::class;
protected function getHeaderActions(): array { return [Actions\CreateAction::make()]; }
}