items(CountrySelectResource::collection(Country::orderBy('name')->get())); } public function states(): JsonResponse { $items = State::query() ->when(request()->integer('country_id'), fn ($query, $id) => $query->where('country_id', $id)) ->orderBy('name')->get(); return $this->items(StateSelectResource::collection($items)); } public function cities(): JsonResponse { $items = City::query() ->when(request()->integer('state_id'), fn ($query, $id) => $query->where('state_id', $id)) ->when(request()->integer('country_id'), fn ($query, $id) => $query->where('country_id', $id)) ->orderBy('name')->get(); return $this->items(CitySelectResource::collection($items)); } public function units(): JsonResponse { return $this->items(UnitSelectResource::collection(Unit::orderBy('fantasy_name')->get())); } public function groups(): JsonResponse { return $this->items(GroupSelectResource::collection(Group::with('units:id')->orderBy('name')->get())); } public function products(): JsonResponse { $items = Product::query() ->when(request()->boolean('visible_franchisee'), fn ($query) => $query->where('visible_franchisee', true)) ->orderBy('name')->get(); return $this->items(ProductSelectResource::collection($items)); } public function franchiseePackages(): JsonResponse { $unitId = $this->activeUnitId(); $items = $unitId ? ClassPackageUnit::where('unit_id', $unitId)->where('visible', true)->orderBy('name')->get() : collect(); return $this->items(PackageSelectResource::collection($items)); } public function franchiseeUsers(): JsonResponse { $unitId = $this->activeUnitId(); $items = $unitId ? User::query() ->join('unit_users', fn ($join) => $join->on('unit_users.user_id', 'users.id')->where('unit_users.unit_id', $unitId)) ->leftJoin('user_types', 'user_types.id', 'unit_users.user_type_id') ->orderBy('users.name') ->get(['users.id', 'users.name', 'user_types.system_key as user_type_system_key']) : collect(); return $this->items(UserSelectResource::collection($items)); } public function franchiseeStudents(): JsonResponse { $unitId = $this->activeUnitId(); $items = $unitId ? Student::where('unit_id', $unitId)->orderBy('name')->get() : collect(); return $this->items(StudentSelectResource::collection($items)); } public function userTypes(): JsonResponse { $scope = request()->input('access_scope', auth()->user()?->access_scope); $unitId = request()->integer('unit_id') ?: $this->activeUnitId(); $isFranchisorAssignment = request()->boolean('include_system_admin'); $items = UserType::query() ->when($scope === 'unit' && $unitId, fn ($query) => $query->where('unit_id', $unitId)) ->when($scope === 'unit' && !$unitId, fn ($query) => $query->where('system_key', 'ADMIN_FRANCHISEE')) ->when($scope === 'franchisor', fn ($query) => $query->whereNull('unit_id')) ->when( $scope === 'unit' && $isFranchisorAssignment, fn ($query) => $query->where(fn ($types) => $types->where('is_system', false)->orWhere('system_key', 'ADMIN_FRANCHISEE')), fn ($query) => $query->where('is_system', false), ) ->orderBy('label')->get(); return $this->items(UserTypeSelectResource::collection($items)); } public function inhabitantClassifications(): JsonResponse { return $this->items(InhabitantClassificationSelectResource::collection( InhabitantClassification::with('municipalitySize')->orderBy('description')->get() )); } public function municipalitySizes(): JsonResponse { return $this->items(MunicipalitySizeSelectResource::collection(MunicipalitySize::orderBy('id')->get())); } public function financialPlanAccounts(): JsonResponse { $unitId = $this->activeUnitId(); $items = FinancialPlanAccount::query() ->when( $unitId, fn ($query) => $query->where(fn ($scope) => $scope->where('unit_id', $unitId)->orWhereNull('unit_id')), fn ($query) => $query->whereNull('unit_id'), ) ->orderBy('code')->get(); return $this->items(FinancialPlanAccountSelectResource::collection($items)); } private function items($items): JsonResponse { return $this->successResponse(payload: $items); } }