attributes->get('supervisor') ?? $request->user(); $rules = EnsureSupervisorScope::pricingRulesQuery($supervisor) ->when($request->filled('establishment_id'), fn ($q) => $q->where('establishment_id', $request->integer('establishment_id'))) ->orderBy('priority') ->paginate($request->integer('per_page', 50)); return $this->success([ 'pricing_rules' => PricingRuleResource::collection($rules), 'meta' => [ 'current_page' => $rules->currentPage(), 'last_page' => $rules->lastPage(), 'per_page' => $rules->perPage(), 'total' => $rules->total(), ], ]); } public function store(StorePricingRequest $request): JsonResponse { /** @var Supervisor $supervisor */ $supervisor = $request->attributes->get('supervisor') ?? $request->user(); $establishment = Establishment::query()->findOrFail($request->integer('establishment_id')); if ($establishment->organization_id !== $supervisor->organization_id) { return $this->error('Établissement hors périmètre.', 403); } if ($supervisor->establishment_id !== null && $supervisor->establishment_id !== $establishment->id) { return $this->error('Établissement hors périmètre.', 403); } $rule = PricingRule::query()->create([ 'establishment_id' => $establishment->id, 'machine_id' => $request->input('machine_id'), 'machine_type' => $request->input('machine_type'), 'day_type' => $request->string('day_type')->toString(), 'slot_start' => $request->string('slot_start')->toString(), 'slot_end' => $request->string('slot_end')->toString(), 'price' => $request->input('price'), 'label' => $request->input('label'), 'requires_app' => $request->boolean('requires_app'), 'priority' => $request->integer('priority', 100), 'is_active' => $request->boolean('is_active', true), ]); return $this->created([ 'pricing_rule' => new PricingRuleResource($rule->load(['establishment', 'machine'])), ], 'Règle tarifaire créée.'); } public function update(UpdatePricingRequest $request, int $id): JsonResponse { /** @var Supervisor $supervisor */ $supervisor = $request->attributes->get('supervisor') ?? $request->user(); $rule = EnsureSupervisorScope::pricingRulesQuery($supervisor) ->whereKey($id) ->firstOrFail(); $rule->update($request->validated()); return $this->success([ 'pricing_rule' => new PricingRuleResource($rule->fresh(['establishment', 'machine'])), ], 'Règle tarifaire mise à jour.'); } }