shipmentsSection()]); return $schema; } private function shipmentsSection(): Section { return Section::make('shipments') ->heading('Shipments') ->compact() ->collapsed(fn ($record) => $record->shipments->isEmpty()) ->collapsible(fn ($record) => $record->shipments->isNotEmpty()) ->schema([ RepeatableEntry::make('shipments') ->hiddenLabel() ->placeholder('No shipments have been created for this order yet.') ->contained(true) ->schema([ TextEntry::make('tracking_reference') ->label(fn (Shipment $record) => $record->carrierLabel()) ->inlineLabel() ->placeholder('Voucher not issued yet') ->copyable(), TextEntry::make('status') ->label('Status') ->inlineLabel() ->state(fn (Shipment $record) => $this->statusLabel($record)) ->badge() ->color(fn (Shipment $record) => $this->statusColor($record)) ->helperText(fn (Shipment $record) => $this->helperText($record)) ->suffixActions([ Action::make('print_label') ->label('Print Label') ->icon('heroicon-o-printer') ->url(fn (Shipment $record) => URL::temporarySignedRoute( 'shipments.label', now()->addMinutes(5), ['shipment' => $record->id], ), shouldOpenInNewTab: true) ->visible(fn (Shipment $record) => $record->hasCarrierLabel()), Action::make('open_carrier_tracking') ->label('Carrier tracking') ->icon('heroicon-o-arrow-top-right-on-square') ->url(fn (Shipment $record) => $record->trackingUrl(), shouldOpenInNewTab: true) ->visible(fn (Shipment $record) => filled($record->trackingUrl())), Action::make('edit_tracking_reference') ->label('Edit number') ->icon('heroicon-o-pencil-square') ->fillForm(fn (Shipment $record) => ['tracking_reference' => $record->tracking_reference]) ->schema(fn (Shipment $record) => [ TextInput::make('tracking_reference') ->label('Voucher / tracking number') ->required($record->source === Shipment::SOURCE_MANUAL_VOUCHER) ->unique(Shipment::class, 'tracking_reference', ignorable: $record), ]) ->action(fn (Shipment $record, array $data) => $record->update([ 'tracking_reference' => filled($data['tracking_reference']) ? trim($data['tracking_reference']) : null, ])) ->visible(fn (Shipment $record) => in_array($record->source, [Shipment::SOURCE_MANUAL, Shipment::SOURCE_MANUAL_VOUCHER], true) && ! $record->isCancelled()), Action::make('add_tracking_update') ->label('Add tracking update') ->icon('heroicon-o-plus-circle') ->schema([ Select::make('status') ->label('Status') ->options(collect(TrackingStatus::cases()) ->reject(fn (TrackingStatus $status) => in_array($status, [TrackingStatus::Unknown, TrackingStatus::Pending], true)) ->mapWithKeys(fn (TrackingStatus $status) => [$status->value => (string) str($status->value)->replace('_', ' ')->title()])) ->native(false) ->required(), DateTimePicker::make('occurred_at') ->label('When') ->seconds(false) ->default(now()) ->required(), TextInput::make('location')->label('Location'), TextInput::make('message') ->label('Message') ->helperText('Shown to the customer on their order page.'), ]) ->action(fn (Shipment $record, array $data) => $this->addTrackingUpdate($record, $data)) ->visible(fn (Shipment $record) => in_array($record->source, [Shipment::SOURCE_MANUAL, Shipment::SOURCE_MANUAL_VOUCHER], true) && ! $record->isCancelled()), Action::make('cancel_shipment') ->label('Cancel') ->icon('heroicon-o-x-circle') ->color('danger') ->requiresConfirmation() ->modalDescription(fn (Shipment $record) => $record->cancelsLocallyOnly() ? 'Marks this shipment as cancelled here only. Void the voucher with the courier as well.' : 'Cancels this shipment with the carrier. This cannot be undone.') ->action(fn (Shipment $record) => ShipmentResource::cancelShipment($record)) ->visible(fn (Shipment $record) => ! $record->cancelled_at), ]), RepeatableEntry::make('shipmentInfo') ->label('Tracking history') ->state(fn (Shipment $record) => $this->orderedCheckpoints($record)) ->hidden(fn (Shipment $record) => $record->shipmentInfo->isEmpty()) ->schema([ TextEntry::make('status') ->label(fn (ShipmentInfo $record) => $record->occurred_at->inDisplayTimezone()->format('Y-m-d H:i')) ->inlineLabel() ->state(fn (ShipmentInfo $record) => (string) str($record->status->value)->replace('_', ' ')->title()) // Our status as the label, the carrier's own wording underneath. ->helperText(fn (ShipmentInfo $record) => collect([ $record->carrier_status !== 'manual' ? $record->carrier_status : null, $record->location, ])->filter()->unique()->implode(' · ') ?: null), ]), ]), ]); } /** * Oldest first — a delivery journey (Collected → In Transit → * Delivered) reads naturally top-to-bottom in that order, unlike * statusLabel()/statusColor() above which only ever need the single * latest checkpoint and so use latestShipmentInfo() directly instead. * * @return Collection */ private function orderedCheckpoints(Shipment $record): Collection { return $record->shipmentInfo->sortBy('occurred_at')->values(); } /** * A manual carrier's checkpoint, entered by staff. Dispatches the same * event as carrier polling, so the order's status and emails follow it * exactly like an integrated carrier's. */ private function addTrackingUpdate(Shipment $record, array $data): void { $info = ShipmentInfo::create([ 'shipment_id' => $record->id, 'status' => TrackingStatus::from($data['status']), 'carrier_status' => 'manual', 'message' => $data['message'] ?? null, 'location' => $data['location'] ?? null, 'occurred_at' => $data['occurred_at'], ]); ShipmentStatusUpdatedByCarrier::dispatch($info); Notification::make() ->title('Tracking update added.') ->success() ->send(); } private function helperText(Shipment $record): string { $parts = ['Created '.$record->created_at->inDisplayTimezone()->format('Y-m-d H:i')]; if ($parcel = $record->parcelDescription()) { $parts[] = $parcel; } if (BoxNowShipping::isDriver($record->carrier) && $locker = $record->meta['locker_id'] ?? null) { $parts[] = 'Locker '.$locker; } if ($cod = $record->meta['cod_amount'] ?? null) { $parts[] = 'Cash on delivery: €'.number_format((float) $cod, 2); } $services = collect($record->meta['services'] ?? []) ->map(fn (string $value) => ExtraService::tryFrom($value)?->label()) ->filter(); if ($services->isNotEmpty()) { $parts[] = $services->implode(', '); } if ($record->source === Shipment::SOURCE_MANUAL_VOUCHER) { $parts[] = 'Manual voucher'; } // Carrier-reported extras (ELTA so far): its routing note, the // return voucher issued with special handling, who received the // parcel and when, and the COD cash paid back to us. if ($routing = $record->meta['routing'] ?? null) { $parts[] = $routing; } if ($returnVoucher = $record->meta['return_vg'] ?? null) { $parts[] = 'Return voucher '.$returnVoucher; } if ($deliveredTo = $record->meta['delivered_to'] ?? null) { $parts[] = trim('Received by '.$deliveredTo.' '.($record->meta['delivered_at'] ?? '')); } if ($remitted = $record->meta['cod_remitted'] ?? null) { $parts[] = trim('COD paid back: €'.number_format((float) $remitted, 2).' '.($record->meta['cod_remittance'] ?? '')); } return implode(' · ', $parts); } private function statusLabel(Shipment $record): string { if ($record->cancelled_at) { return 'Cancelled'; } $latest = $record->latestShipmentInfo(); return $latest ? (string) str($latest->status->value)->replace('_', ' ')->title() : 'Pending'; } private function statusColor(Shipment $record): string { if ($record->cancelled_at) { return 'danger'; } return match ($record->latestShipmentInfo()?->status?->value) { 'delivered' => 'success', 'failed', 'returned' => 'danger', 'in_transit', 'out_for_delivery', 'collected_from_sender' => 'warning', default => 'gray', }; } }