authorizeAbility($request, 'admin.branches.view'); $branches = Branch::owned($this->ownerRef($request)) ->where('organization_id', $this->organization($request)->id) ->orderBy('name') ->get(); return response()->json(['data' => $branches]); } public function store(Request $request): JsonResponse { $this->authorizeAbility($request, 'admin.branches.manage'); $organization = $this->organization($request); $owner = $this->ownerRef($request); $currentCount = Branch::owned($owner)->where('organization_id', $organization->id)->count(); abort_unless(app(PlanService::class)->canAddBranch($organization, $currentCount), 422, 'Branch limit reached for current plan.'); $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'code' => ['nullable', 'string', 'max:50'], 'address' => ['nullable', 'string', 'max:500'], 'phone' => ['nullable', 'string', 'max:50'], ]); $branch = Branch::create([ 'owner_ref' => $owner, 'organization_id' => $organization->id, ...$validated, 'is_active' => true, ]); AuditLogger::record($owner, 'branch.created', $organization->id, $owner, Branch::class, $branch->id); return response()->json(['data' => $branch], 201); } }