From 5477b953a8cf99e104e71ab045862b27cd8d58d8 Mon Sep 17 00:00:00 2001 From: E98Developer Date: Thu, 16 Jul 2026 08:53:09 +0200 Subject: [PATCH] =?UTF-8?q?mod=20EV3-442=20Profitcenter=20=C3=BCtemez?= =?UTF-8?q?=C3=A9s=202.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CreateProfitCenterSupplierSchedule.php | 24 +++++++++++++++++ .../EditProfitCenterSupplierSchedule.php | 26 +++++++++++++++++++ .../ProfitCenterSupplierScheduleForm.php | 4 ++- .../Widgets/DeliveryCalendarWidget.php | 20 +++++++++++++- app/Services/DeliveryCalendarService.php | 16 +++++++----- 5 files changed, 81 insertions(+), 9 deletions(-) diff --git a/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/CreateProfitCenterSupplierSchedule.php b/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/CreateProfitCenterSupplierSchedule.php index a13ccd3..82709d0 100644 --- a/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/CreateProfitCenterSupplierSchedule.php +++ b/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/CreateProfitCenterSupplierSchedule.php @@ -3,9 +3,33 @@ namespace App\Filament\Resources\ProfitCenterSupplierSchedules\Pages; use App\Filament\Resources\ProfitCenterSupplierSchedules\ProfitCenterSupplierScheduleResource; +use App\Models\ProfitCenterSupplierSchedule; use Filament\Resources\Pages\CreateRecord; +use Illuminate\Database\Eloquent\Model; class CreateProfitCenterSupplierSchedule extends CreateRecord { protected static string $resource = ProfitCenterSupplierScheduleResource::class; + + /** + * Több profit center esetén minden egyes profit centerhez külön rekordot hoz létre. + * + * @param array $data + */ + protected function handleRecordCreation(array $data): Model + { + $profitCenterIds = (array) $data['profit_center_id']; + + $lastRecord = null; + + foreach ($profitCenterIds as $profitCenterId) { + $lastRecord = ProfitCenterSupplierSchedule::create([ + 'profit_center_id' => $profitCenterId, + 'supplier_id' => $data['supplier_id'], + 'delivery_schedule_id' => $data['delivery_schedule_id'], + ]); + } + + return $lastRecord; + } } diff --git a/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/EditProfitCenterSupplierSchedule.php b/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/EditProfitCenterSupplierSchedule.php index 141a530..65cc742 100644 --- a/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/EditProfitCenterSupplierSchedule.php +++ b/app/Filament/Resources/ProfitCenterSupplierSchedules/Pages/EditProfitCenterSupplierSchedule.php @@ -6,12 +6,38 @@ use Filament\Actions\DeleteAction; use Filament\Actions\ForceDeleteAction; use Filament\Actions\RestoreAction; +use Filament\Forms\Components\Select; use Filament\Resources\Pages\EditRecord; +use Filament\Schemas\Schema; class EditProfitCenterSupplierSchedule extends EditRecord { protected static string $resource = ProfitCenterSupplierScheduleResource::class; + public function form(Schema $schema): Schema + { + return $schema + ->components([ + Select::make('profit_center_id') + ->label('Profit Center') + ->relationship('profitCenter', 'name') + ->searchable() + ->required(), + Select::make('supplier_id') + ->label('Beszállító') + ->relationship('supplier', 'name') + ->searchable() + ->required(), + Select::make('delivery_schedule_id') + ->label('Szállítási sablon') + ->relationship('deliverySchedule', 'name') + ->searchable() + ->live() + ->afterStateUpdated(fn ($state, $livewire) => $livewire->dispatch('delivery-schedule-changed', deliveryScheduleId: $state)) + ->required(), + ]); + } + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/ProfitCenterSupplierSchedules/Schemas/ProfitCenterSupplierScheduleForm.php b/app/Filament/Resources/ProfitCenterSupplierSchedules/Schemas/ProfitCenterSupplierScheduleForm.php index 7cee796..fcaed07 100644 --- a/app/Filament/Resources/ProfitCenterSupplierSchedules/Schemas/ProfitCenterSupplierScheduleForm.php +++ b/app/Filament/Resources/ProfitCenterSupplierSchedules/Schemas/ProfitCenterSupplierScheduleForm.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources\ProfitCenterSupplierSchedules\Schemas; +use App\Models\ProfitCenter; use Filament\Forms\Components\Select; use Filament\Schemas\Schema; @@ -13,8 +14,9 @@ public static function configure(Schema $schema): Schema ->components([ Select::make('profit_center_id') ->label('Profit Center') - ->relationship('profitCenter', 'name') + ->options(ProfitCenter::orderBy('name')->pluck('name', 'id')) ->searchable() + ->multiple() ->required(), Select::make('supplier_id') ->label('Beszállító') diff --git a/app/Filament/Widgets/DeliveryCalendarWidget.php b/app/Filament/Widgets/DeliveryCalendarWidget.php index f933a81..c170cd9 100644 --- a/app/Filament/Widgets/DeliveryCalendarWidget.php +++ b/app/Filament/Widgets/DeliveryCalendarWidget.php @@ -33,6 +33,9 @@ class DeliveryCalendarWidget extends FullCalendarWidget /** Következő szállítási nap dátuma (YYYY-MM-DD formátum), kék kiemeléshez. */ public ?string $nextDeliveryDate = null; + /** Előnézeti szállítási sablon ID (edit oldalon, mentés előtti változás). */ + public ?int $previewDeliveryScheduleId = null; + /** * Inaktív (nem választható) napok listája. * @@ -74,6 +77,13 @@ public function onNextDeliveryDateChanged(?string $date): void $this->refreshRecords(); } + #[On('delivery-schedule-changed')] + public function onDeliveryScheduleChanged(?int $deliveryScheduleId): void + { + $this->previewDeliveryScheduleId = $deliveryScheduleId; + $this->refreshRecords(); + } + protected function loadRecordFromFilter(): void { if ($this->record instanceof ProfitCenterSupplierSchedule) { @@ -114,11 +124,19 @@ public function fetchEvents(array $info): array $events = []; // 1. Szállítási napok – zöld háttér + $assignment = $this->record; + if ($this->previewDeliveryScheduleId && $this->previewDeliveryScheduleId !== $this->record->delivery_schedule_id) { + $assignment = clone $this->record; + $assignment->delivery_schedule_id = $this->previewDeliveryScheduleId; + $assignment->setRelation('deliverySchedule', \App\Models\DeliverySchedule::find($this->previewDeliveryScheduleId)); + } + $deliveryDates = $service->getAvailableDeliveryDates( $this->record->profitCenter, $this->record->supplier, $start, - $end + $end, + $assignment ); foreach ($deliveryDates as $date) { diff --git a/app/Services/DeliveryCalendarService.php b/app/Services/DeliveryCalendarService.php index 72cef99..d384473 100644 --- a/app/Services/DeliveryCalendarService.php +++ b/app/Services/DeliveryCalendarService.php @@ -26,7 +26,7 @@ public function __construct( * 3. Általános override (az összes PC-re vonatkozó) * Ha egyetlen szinten sem található override, folytatódik a normál kiértékelés. */ - public function isDeliveryDay(ProfitCenter $pc, Supplier $supplier, Carbon $date): bool + public function isDeliveryDay(ProfitCenter $pc, Supplier $supplier, Carbon $date, ?ProfitCenterSupplierSchedule $assignment = null): bool { $date = $date->startOfDay(); @@ -42,10 +42,12 @@ public function isDeliveryDay(ProfitCenter $pc, Supplier $supplier, Carbon $date } // PC-hez rendelt szállítási sablon lekérése (2. és 5. prioritáshoz egyaránt szükséges) - $assignment = ProfitCenterSupplierSchedule::where('profit_center_id', $pc->id) - ->where('supplier_id', $supplier->id) - ->with('deliverySchedule') - ->first(); + if ($assignment === null) { + $assignment = ProfitCenterSupplierSchedule::where('profit_center_id', $pc->id) + ->where('supplier_id', $supplier->id) + ->with('deliverySchedule') + ->first(); + } // 2. Prioritás: DeliverySchedule-specifikus felülbírálás (a PC-hez rendelt szállítási sablon alapján) if ($assignment && $assignment->delivery_schedule_id) { @@ -91,14 +93,14 @@ public function isDeliveryDay(ProfitCenter $pc, Supplier $supplier, Carbon $date * * @return Collection */ - public function getAvailableDeliveryDates(ProfitCenter $pc, Supplier $supplier, Carbon $start, Carbon $end): Collection + public function getAvailableDeliveryDates(ProfitCenter $pc, Supplier $supplier, Carbon $start, Carbon $end, ?ProfitCenterSupplierSchedule $assignment = null): Collection { $dates = collect(); $current = $start->copy()->startOfDay(); $end = $end->copy()->startOfDay(); while ($current <= $end) { - if ($this->isDeliveryDay($pc, $supplier, $current)) { + if ($this->isDeliveryDay($pc, $supplier, $current, $assignment)) { $dates->push($current->copy()); } $current->addDay();