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) => $this->carrierLabel($record)) ->inlineLabel() ->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->cancelled_at), Action::make('cancel_shipment') ->label('Cancel') ->icon('heroicon-o-x-circle') ->color('danger') ->requiresConfirmation() ->modalDescription('Cancels this shipment with the carrier. This cannot be undone.') ->action(fn (Shipment $record) => $this->cancel($record)) ->visible(fn (Shipment $record) => ! $record->cancelled_at), ]), ]), ]); } private function carrierLabel(Shipment $record): string { return match ($record->carrier) { 'acs' => 'ACS', 'box-now' => 'Box Now', default => (string) str($record->carrier)->title(), }; } private function helperText(Shipment $record): string { $parts = ['Created '.$record->created_at->format('Y-m-d H:i')]; if ($record->carrier === 'box-now' && $locker = $record->meta['locker_id'] ?? null) { $parts[] = 'Locker '.$locker; } 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', }; } private function cancel(Shipment $record): void { $service = app(CarrierFulfillmentInterface::class, ['carrier' => $record->carrier]); try { $service->cancelShipment($record); } catch (Throwable $e) { report($e); Notification::make() ->title('Failed to cancel shipment: '.$e->getMessage()) ->color('danger') ->send(); return; } Notification::make() ->title('Shipment cancelled.') ->color('success') ->send(); } }