|
@@ -0,0 +1,165 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Http\Controllers;
|
|
|
|
|
+
|
|
|
|
|
+use App\Http\Controllers\Concerns\ResolvesActiveUnit;
|
|
|
|
|
+use App\Http\Resources\Select\CitySelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\CountrySelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\GroupSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\FinancialPlanAccountSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\InhabitantClassificationSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\MunicipalitySizeSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\PackageSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\ProductSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\StateSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\StudentSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\UnitSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\UserSelectResource;
|
|
|
|
|
+use App\Http\Resources\Select\UserTypeSelectResource;
|
|
|
|
|
+use App\Models\City;
|
|
|
|
|
+use App\Models\ClassPackageUnit;
|
|
|
|
|
+use App\Models\Country;
|
|
|
|
|
+use App\Models\Group;
|
|
|
|
|
+use App\Models\FinancialPlanAccount;
|
|
|
|
|
+use App\Models\InhabitantClassification;
|
|
|
|
|
+use App\Models\MunicipalitySize;
|
|
|
|
|
+use App\Models\Product;
|
|
|
|
|
+use App\Models\State;
|
|
|
|
|
+use App\Models\Student;
|
|
|
|
|
+use App\Models\Unit;
|
|
|
|
|
+use App\Models\User;
|
|
|
|
|
+use App\Models\UserType;
|
|
|
|
|
+use Illuminate\Http\JsonResponse;
|
|
|
|
|
+
|
|
|
|
|
+class SelectController extends Controller
|
|
|
|
|
+{
|
|
|
|
|
+ use ResolvesActiveUnit;
|
|
|
|
|
+
|
|
|
|
|
+ public function countries(): JsonResponse
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->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);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|