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); return view('qms.settings.edit', [ 'organization' => $organization, 'canManage' => $canManage, 'branchCount' => $branchCount, 'appointmentModes' => config('qms.appointment_modes'), 'industries' => $packages->labels(), 'activePackage' => $activePackage, 'packageMeta' => data_get($organization->settings, 'industry_package'), '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', 'timezone'], 'appointment_mode' => ['required', 'string', 'in:'.implode(',', array_keys(config('qms.appointment_modes')))], 'industry' => ['nullable', 'string', 'in:'.implode(',', array_keys(config('industry_packages.packages', [])))], 'notifications_enabled' => ['boolean'], 'care_integration_enabled' => ['nullable', 'boolean'], 'frontdesk_integration_enabled' => ['nullable', 'boolean'], 'logo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp,svg', 'max:2048'], 'remove_logo' => ['nullable', 'boolean'], ]); $settings = $organization->settings ?? []; $settings['appointment_mode'] = $validated['appointment_mode']; $previousIndustry = $settings['industry'] ?? null; $settings['industry'] = $validated['industry'] ?? $settings['industry'] ?? null; $settings['notifications_enabled'] = $request->boolean('notifications_enabled', true); $integrations = $settings['integrations'] ?? []; if ($request->has('care_integration_enabled')) { $integrations['care_enabled'] = $request->boolean('care_integration_enabled'); } if ($request->has('frontdesk_integration_enabled')) { $integrations['frontdesk_enabled'] = $request->boolean('frontdesk_integration_enabled'); } $settings['integrations'] = $integrations; $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); $industryChanged = $settings['industry'] && $settings['industry'] !== $previousIndustry; if ($industryChanged) { $branch = Branch::query() ->where('organization_id', $organization->id) ->where('is_active', true) ->orderBy('id') ->first(); if ($branch) { app(IndustryPackageInstaller::class)->install($organization->fresh(), $branch, $settings['industry'], [ 'actor_ref' => $owner, ]); } } return back()->with('success', $industryChanged ? 'Settings saved. Industry package re-provisioned.' : 'Settings saved.'); } 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, ]); $message = '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.'; } elseif (($result['skipped_reason'] ?? null) === 'stages_managed_by_integration') { $message .= ' Stages are managed by Ladill Care; terminology and announcements were updated.'; } return back()->with('success', $message); } }