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:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Central\Plan;
|
||||
use App\Models\Tenant\User;
|
||||
use App\Tenancy\TenantManager;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
/**
|
||||
* Bootstraps a brand new tenant: creates the Company row, seeds default
|
||||
* roles + admin user, and (if Coolify is configured) adds the new
|
||||
* subdomain to the AutoCRM application's FQDN list and triggers redeploy.
|
||||
*
|
||||
* Returns plain credentials so the central admin can copy/email them.
|
||||
*/
|
||||
class CompanyProvisioner
|
||||
{
|
||||
public function __construct(
|
||||
protected TenantManager $tenants,
|
||||
protected PermissionRegistrar $permissions,
|
||||
protected CoolifyClient $coolify,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{company: Company, admin_email: string, admin_password: string, deploy_triggered: bool}
|
||||
*/
|
||||
public function provision(array $data): array
|
||||
{
|
||||
$defaults = [
|
||||
'plan_id' => Plan::where('slug', 'free')->value('id'),
|
||||
'status' => 'trial',
|
||||
'trial_ends_at' => now()->addDays(14),
|
||||
'settings' => [
|
||||
'currency' => 'MDL',
|
||||
'language' => 'ro',
|
||||
'theme_color' => '#3B82F6',
|
||||
'labor_rate' => 400,
|
||||
],
|
||||
];
|
||||
|
||||
return DB::transaction(function () use ($data, $defaults) {
|
||||
$company = Company::create(array_merge($defaults, [
|
||||
'slug' => $data['slug'],
|
||||
'name' => $data['name'],
|
||||
'display_name' => $data['display_name'] ?? $data['name'],
|
||||
'city' => $data['city'] ?? null,
|
||||
'phone' => $data['phone'] ?? null,
|
||||
'email' => $data['email'] ?? null,
|
||||
'contact_name' => $data['contact_name'] ?? null,
|
||||
]));
|
||||
|
||||
// Activate tenant context to seed roles + user with company_id auto-fill.
|
||||
$this->tenants->setCurrent($company);
|
||||
$this->permissions->setPermissionsTeamId($company->id);
|
||||
|
||||
// Default roles per tenant.
|
||||
foreach (['admin', 'manager', 'receptionist', 'mechanic', 'parts_manager', 'accountant', 'marketer'] as $r) {
|
||||
Role::findOrCreate($r, 'web');
|
||||
}
|
||||
|
||||
// Admin user.
|
||||
$adminEmail = $data['admin_email'] ?? "admin@{$company->slug}.local";
|
||||
$plainPassword = $data['admin_password'] ?? Str::password(10, true, true, false);
|
||||
$admin = User::create([
|
||||
'company_id' => $company->id,
|
||||
'name' => $data['admin_name'] ?? 'Administrator',
|
||||
'email' => $adminEmail,
|
||||
'password' => Hash::make($plainPassword),
|
||||
'role' => 'admin',
|
||||
'status' => 'active',
|
||||
'locale' => 'ro',
|
||||
'email_verified_at' => now(),
|
||||
]);
|
||||
$admin->syncRoles(['admin']);
|
||||
|
||||
$this->tenants->clear();
|
||||
|
||||
// Add subdomain to Coolify FQDN list + trigger redeploy.
|
||||
$deployTriggered = false;
|
||||
if ($this->coolify->isConfigured() && env('COOLIFY_APP_UUID')) {
|
||||
$appUuid = (string) env('COOLIFY_APP_UUID');
|
||||
$url = $company->url('');
|
||||
$url = rtrim($url, '/') . ':8000'; // internal port suffix Coolify expects
|
||||
if ($this->coolify->addDomain($appUuid, $url)) {
|
||||
$deployTriggered = $this->coolify->deploy($appUuid, true);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'company' => $company->fresh(),
|
||||
'admin_email' => $adminEmail,
|
||||
'admin_password' => $plainPassword,
|
||||
'deploy_triggered' => $deployTriggered,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function suspend(Company $company): void
|
||||
{
|
||||
$company->update(['status' => 'suspended']);
|
||||
}
|
||||
|
||||
public function reactivate(Company $company): void
|
||||
{
|
||||
$company->update(['status' => 'active']);
|
||||
}
|
||||
|
||||
public function archive(Company $company): void
|
||||
{
|
||||
$company->update(['status' => 'archived']);
|
||||
$company->delete(); // soft-delete
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user