selectedDate = $now->format('Y-m-d'); $this->selectedTime = $now->format('H:i'); $first = ProfitCenterSupplierSchedule::first(); if ($first) { $this->supplierId = $first->supplier_id; $this->profitCenterId = $first->profit_center_id; } } public function getSupplier(): ?Supplier { if (!$this->supplierId) { return null; } return Supplier::find($this->supplierId); } public function getNextDeliveryDate(): ?string { if (! $this->supplierId || ! $this->profitCenterId || ! $this->selectedDate || ! $this->selectedTime) { return null; } $dt = \Illuminate\Support\Carbon::parse("{$this->selectedDate} {$this->selectedTime}"); $result = app(NextDeliveryDateCalculatorService::class)->calculate( supplierId: $this->supplierId, profitCenterId: $this->profitCenterId, orderDateTime: $dt, ); return $result?->translatedFormat('Y. F j., l'); } public function getNextDeliveryDateRaw(): ?string { if (! $this->supplierId || ! $this->profitCenterId || ! $this->selectedDate || ! $this->selectedTime) { return null; } $dt = \Illuminate\Support\Carbon::parse("{$this->selectedDate} {$this->selectedTime}"); $result = app(NextDeliveryDateCalculatorService::class)->calculate( supplierId: $this->supplierId, profitCenterId: $this->profitCenterId, orderDateTime: $dt, ); return $result?->format('Y-m-d'); } public function getSuppliers(): array { return Supplier::orderBy('name')->pluck('name', 'id')->toArray(); } public function getProfitCenters(): array { if (!$this->supplierId) { return ProfitCenter::orderBy('name')->pluck('name', 'id')->toArray(); } $pcIds = ProfitCenterSupplierSchedule::where('supplier_id', $this->supplierId) ->pluck('profit_center_id'); return ProfitCenter::whereIn('id', $pcIds)->orderBy('name')->pluck('name', 'id')->toArray(); } public function updatedSupplierId(): void { $this->profitCenterId = null; $first = ProfitCenterSupplierSchedule::where('supplier_id', $this->supplierId)->first(); if ($first) { $this->profitCenterId = $first->profit_center_id; } $this->dispatch('calendar-filter-changed', supplierId: $this->supplierId, profitCenterId: $this->profitCenterId); $this->dispatchNextDeliveryDateChanged(); } public function updatedProfitCenterId(): void { $this->dispatch('calendar-filter-changed', supplierId: $this->supplierId, profitCenterId: $this->profitCenterId); $this->dispatchNextDeliveryDateChanged(); } public function updatedSelectedDate(): void { $this->dispatchNextDeliveryDateChanged(); } public function updatedSelectedTime(): void { $this->dispatchNextDeliveryDateChanged(); } protected function dispatchNextDeliveryDateChanged(): void { $this->dispatch('next-delivery-date-changed', date: $this->getNextDeliveryDateRaw()); } }