Files
ladill-queue/app/Http/Controllers/Qms/SettingsController.php
T
isaaccladandCursor b208e59bd0
Deploy Ladill Queue / deploy (push) Successful in 3m12s
Strip healthcare packaging and Care integration from Queue.
Queue is general-purpose QMS only; Care runs its own native engine.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 10:15:32 +00:00

181 lines
7.5 KiB
PHP

<?php
namespace App\Http\Controllers\Qms;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Qms\Concerns\ScopesToAccount;
use App\Models\Branch;
use App\Services\Qms\AuditLogger;
use App\Services\Qms\Industry\IndustryPackageInstaller;
use App\Services\Qms\Industry\IndustryPackageRegistry;
use App\Services\Qms\PlanService;
use App\Services\Qms\QmsPermissions;
use App\Support\OrganizationBranding;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class SettingsController extends Controller
{
use ScopesToAccount;
public function edit(Request $request, PlanService $plans, IndustryPackageRegistry $packages): View
{
$this->authorizeAbility($request, 'settings.view');
$organization = $this->organization($request);
$member = $this->member($request);
$permissions = app(QmsPermissions::class);
$canManage = $permissions->can($member, 'settings.manage');
$branchCount = Branch::owned($this->ownerRef($request))
->where('organization_id', $organization->id)
->count();
$packageKey = data_get($organization->settings, 'industry_package.key')
?? data_get($organization->settings, 'industry')
?? 'custom';
$activePackage = $packages->get($packageKey);
$selectedIndustry = data_get($organization->settings, 'industry', $packageKey);
$selectedPackage = $packages->get($selectedIndustry) ?? $activePackage;
return view('qms.settings.edit', [
'organization' => $organization,
'canManage' => $canManage,
'branchCount' => $branchCount,
'appointmentModes' => config('qms.appointment_modes'),
'industries' => $packages->labels(),
'activePackage' => $activePackage,
'selectedPackage' => $selectedPackage,
'packageMeta' => data_get($organization->settings, 'industry_package'),
'needsPackageReinstall' => IndustryPackageInstaller::needsReinstall($organization),
'hasPaidPlan' => $plans->hasPaidPlan($organization),
'isPro' => $plans->isPro($organization),
'isEnterprise' => $plans->isEnterprise($organization),
'hasBranchesFeature' => $plans->hasFeature($organization, 'branches'),
'hasTeamFeature' => $plans->hasFeature($organization, 'team'),
'canViewBranches' => $permissions->can($member, 'admin.branches.view'),
'canViewTeam' => $permissions->can($member, 'admin.members.view'),
'proPriceMinor' => $plans->proPricePerBranchMinor(),
'activeBranchCount' => $plans->activeBranchCount($organization),
]);
}
public function update(Request $request): RedirectResponse
{
$this->authorizeAbility($request, 'settings.manage');
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'timezone' => ['required', 'string', 'timezone:all'],
'appointment_mode' => ['required', 'string', 'in:'.implode(',', array_keys(config('qms.appointment_modes')))],
'industry' => ['nullable', 'string', 'max:64'],
'notifications_enabled' => ['boolean'],
'frontdesk_integration_enabled' => ['nullable', 'boolean'],
'logo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp,svg', 'max:2048'],
'remove_logo' => ['nullable', 'boolean'],
]);
$registry = app(IndustryPackageRegistry::class);
$settings = $organization->settings ?? [];
$industry = isset($validated['industry']) && $validated['industry'] !== ''
? $registry->resolveKey($validated['industry'])
: ($settings['industry'] ?? null);
$settings['appointment_mode'] = $validated['appointment_mode'];
$previousIndustry = $settings['industry'] ?? null;
$installedPackage = data_get($settings, 'industry_package.key');
if ($industry !== null) {
$settings['industry'] = $industry;
}
$settings['notifications_enabled'] = $request->boolean('notifications_enabled', true);
$integrations = $settings['integrations'] ?? [];
if ($request->has('frontdesk_integration_enabled')) {
$integrations['frontdesk_enabled'] = $request->boolean('frontdesk_integration_enabled');
}
$settings['integrations'] = $integrations;
$industryChanged = $industry !== null
&& $industry !== $previousIndustry;
$outOfSyncWithPackage = $industry !== null
&& is_string($installedPackage)
&& $installedPackage !== ''
&& $industry !== $installedPackage;
if ($industryChanged || $outOfSyncWithPackage) {
$settings['industry_package_needs_reinstall'] = true;
}
$logoPath = $organization->logo_path;
if ($request->boolean('remove_logo')) {
OrganizationBranding::deleteStoredLogo($organization);
$logoPath = null;
}
if ($request->hasFile('logo')) {
$logoPath = OrganizationBranding::storeLogo($organization, $request->file('logo'));
}
$organization->update([
'name' => $validated['name'],
'timezone' => $validated['timezone'],
'logo_path' => $logoPath,
'settings' => $settings,
]);
AuditLogger::record($owner, 'organization.updated', $organization->id, $owner, \App\Models\Organization::class, $organization->id);
$packageNote = '';
if ($industryChanged || $outOfSyncWithPackage) {
$label = $registry->get($industry)?->label() ?? $industry;
$packageNote = " Industry changed to {$label} — reinstall the industry package to replace queues and service points.";
}
$redirect = back()->with('success', 'Settings saved.'.$packageNote);
if ($industryChanged || $outOfSyncWithPackage) {
$redirect->with(
'warning',
'Reinstall the industry package to apply the new template. This will clear existing queues, tickets, and service points.'
);
}
return $redirect;
}
public function reinstallPackage(Request $request, IndustryPackageInstaller $installer): RedirectResponse
{
$this->authorizeAbility($request, 'settings.manage');
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$branch = Branch::query()
->where('organization_id', $organization->id)
->where('is_active', true)
->orderBy('id')
->first();
if (! $branch) {
return back()->with('error', 'Add a branch before reinstalling the industry package.');
}
$key = data_get($organization->settings, 'industry', 'custom');
$result = $installer->install($organization, $branch, $key, [
'actor_ref' => $owner,
'include_optional' => true,
'replace' => true,
]);
$message = ($result['replaced'] ?? false)
? 'Industry package reinstalled. Previous queues, tickets, and service points were cleared.'
: 'Industry package synced.';
if (($result['skipped_reason'] ?? null) === 'plan_queue_limit') {
$message .= ' Some stages were skipped because of your plan queue limit — upgrade to Pro for the full template.';
}
return back()->with('success', $message);
}
}