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
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* Thin wrapper over Coolify v4 REST API. Used to add tenant subdomains
* to the AutoCRM application's FQDN list when a new Company is created.
*
* Configure via env:
* COOLIFY_API_URL=http://65.21.20.141:8000
* COOLIFY_API_TOKEN=<token>
* COOLIFY_APP_UUID=<autocrm-app-uuid>
*/
class CoolifyClient
{
public function __construct(
protected ?string $base = null,
protected ?string $token = null,
) {
$this->base = rtrim($base ?? (string) env('COOLIFY_API_URL'), '/');
$this->token = $token ?? (string) env('COOLIFY_API_TOKEN');
}
public function isConfigured(): bool
{
return $this->base !== '' && $this->token !== '';
}
protected function http()
{
return Http::withToken($this->token)
->withHeaders(['Accept' => 'application/json'])
->withOptions(['verify' => false])
->timeout(15);
}
public function getApp(string $uuid): ?array
{
if (! $this->isConfigured()) return null;
$r = $this->http()->get($this->base . '/api/v1/applications/' . $uuid);
return $r->ok() ? $r->json() : null;
}
/**
* Add a new domain to the application's FQDN list (idempotent).
* Returns true if successful or already present.
*/
public function addDomain(string $appUuid, string $url): bool
{
if (! $this->isConfigured()) {
Log::warning('CoolifyClient not configured; skipping addDomain', ['url' => $url]);
return false;
}
$app = $this->getApp($appUuid);
if (! $app) {
Log::error('CoolifyClient: cannot fetch app', ['uuid' => $appUuid]);
return false;
}
$current = (string) ($app['fqdn'] ?? '');
$domains = array_filter(array_map('trim', explode(',', $current)));
if (in_array($url, $domains, true)) {
return true;
}
$domains[] = $url;
$newFqdn = implode(',', $domains);
$r = $this->http()->patch(
$this->base . '/api/v1/applications/' . $appUuid,
['domains' => $newFqdn]
);
if (! $r->successful()) {
Log::error('CoolifyClient addDomain failed', [
'status' => $r->status(),
'body' => $r->body(),
]);
return false;
}
return true;
}
/**
* Trigger a redeploy on the app (after FQDN change Coolify needs redeploy
* to update Traefik labels).
*/
public function deploy(string $appUuid, bool $force = true): bool
{
if (! $this->isConfigured()) return false;
$r = $this->http()->post($this->base . '/api/v1/deploy', [
'uuid' => $appUuid,
'force' => $force,
]);
if (! $r->successful()) {
// try query string variant (Coolify's GET /deploy?uuid=...)
$r = $this->http()->post($this->base . '/api/v1/deploy?uuid=' . $appUuid . '&force=' . ($force ? 'true' : 'false'));
}
return $r->successful();
}
}