authorize('viewAny', Tenant::class); $tenants = Tenant::query() ->withCount('users') ->with('installations:id,tenant_id,deployment_type,domain,status,last_seen_at') ->with(['licenses' => fn ($query) => $query->latest('created_at')]) ->orderBy('name') ->paginate(15) ->withQueryString(); return Inertia::render('administration/tenants/index', ['tenants' => $tenants]); } public function create(): Response { $this->authorize('create', Tenant::class); return Inertia::render('administration/tenants/create', ['plans' => config('billing.plans')]); } public function store(StoreTenantRequest $request, TenantProvisioningService $service): RedirectResponse { $tenant = $service->createWithOwner($request->validated() + ['activate_license' => true, 'issued_by' => $request->user()->id]); return to_route('administration.tenants.edit', $tenant) ->with('success', 'Tenant dan Tenant Owner berhasil dibuat.'); } public function edit(Tenant $tenant): Response { $this->authorize('update', $tenant); $users = User::query() ->withoutGlobalScope('tenant') ->where('tenant_id', $tenant->id) ->orderBy('name') ->get(['uuid', 'name', 'email', 'is_active', 'created_at']); return Inertia::render('administration/tenants/edit', [ 'tenant' => $tenant, 'users' => $users, 'installation' => $tenant->installations()->first(['id', 'deployment_type', 'domain', 'status', 'last_seen_at']), 'license' => $tenant->licenses()->latest('created_at')->first(), 'plans' => config('billing.plans'), ]); } public function update(UpdateTenantRequest $request, Tenant $tenant): RedirectResponse { $this->authorize('update', $tenant); $data = $request->validated(); $tenant->update(collect($data)->only(['name', 'slug', 'is_active'])->all()); if (isset($data['deployment_type'])) { $tenant->installations()->first()?->update([ 'deployment_type' => $data['deployment_type'], 'domain' => $data['deployment_domain'] ?? null, ]); } $currentLicense = $tenant->licenses()->latest('created_at')->first(); if (! empty($data['plan_code']) && $currentLicense?->plan !== $data['plan_code']) { $installation = $tenant->installations()->firstOrFail(); $tenant->licenses()->whereIn('status', ['active', 'grace'])->update(['status' => 'revoked']); app(LicenseEntitlementService::class)->provisionInitial($tenant, $installation, $data['plan_code'], true, $request->user()->id); } return back()->with('success', 'Tenant berhasil diperbarui.'); } public function destroy(Tenant $tenant): RedirectResponse { $this->authorize('delete', $tenant); abort_if($tenant->users()->exists(), 422, 'Tenant yang masih memiliki user tidak dapat dihapus. Nonaktifkan tenant sebagai gantinya.'); $tenant->delete(); return to_route('administration.tenants.index')->with('success', 'Tenant berhasil dihapus.'); } }