2026-09-14 00:03:06 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Shipping\Filament\Resources;
|
|
|
|
|
|
|
|
|
|
use Filament\Actions\Action;
|
|
|
|
|
use Filament\Actions\BulkAction;
|
|
|
|
|
use Filament\Notifications\Notification;
|
|
|
|
|
use Filament\Resources\Resource;
|
|
|
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
|
use Filament\Tables\Table;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2026-09-30 17:28:47 +03:00
|
|
|
use Illuminate\Support\Facades\URL;
|
2026-09-14 00:03:06 +03:00
|
|
|
use Modules\Core\Shipping\Contracts\CarrierFulfillmentInterface;
|
2026-09-30 17:28:47 +03:00
|
|
|
use Modules\Core\Shipping\Contracts\SupportsBatchLabels;
|
2026-09-14 00:03:06 +03:00
|
|
|
use Modules\Core\Shipping\Contracts\SupportsManifestBatching;
|
|
|
|
|
use Modules\Core\Shipping\Filament\Resources\ShipmentResource\Pages\ListShipments;
|
|
|
|
|
use Modules\Core\Shipping\Models\Shipment;
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replaces the standalone Modules\Core\Shipping\Filament\Pages\
|
|
|
|
|
* ManagePickupManifests page — a bare Page has no access to Filament's
|
|
|
|
|
* resource-level pill-tab UI (Filament\Resources\Concerns\HasTabs is
|
|
|
|
|
* scoped to ListRecords), so carrier-by-carrier separation
|
2026-09-30 17:28:47 +03:00
|
|
|
* (ListShipments::getTabs()) needed a real Resource to attach to.
|
2026-09-14 00:03:06 +03:00
|
|
|
*
|
2026-09-30 17:28:47 +03:00
|
|
|
* Shows only shipments created through a carrier's API that are NOT yet on
|
|
|
|
|
* an issued manifest — see Modules\Core\Shipping\Filament\Resources\
|
|
|
|
|
* ManifestResource for shipments that already are. Manual carriers, manual
|
|
|
|
|
* vouchers and carrier-reported (synced) vouchers have no label to print
|
|
|
|
|
* or manifest to join, so they never show here.
|
|
|
|
|
*
|
|
|
|
|
* Print opens the label through the same signed URL as the order page; for
|
|
|
|
|
* carriers that issue the voucher on print (ELTA), that first open is what
|
|
|
|
|
* gives the shipment its voucher number.
|
2026-09-14 00:03:06 +03:00
|
|
|
*/
|
|
|
|
|
class ShipmentResource extends Resource
|
|
|
|
|
{
|
|
|
|
|
protected static ?string $model = Shipment::class;
|
|
|
|
|
|
|
|
|
|
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-truck';
|
|
|
|
|
|
|
|
|
|
protected static string | \UnitEnum | null $navigationGroup = 'Sales';
|
|
|
|
|
|
|
|
|
|
protected static ?string $navigationLabel = 'Pending Vouchers';
|
|
|
|
|
|
|
|
|
|
protected static ?string $modelLabel = 'Shipment';
|
|
|
|
|
|
|
|
|
|
protected static ?int $navigationSort = 100;
|
|
|
|
|
|
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
|
|
|
{
|
|
|
|
|
return parent::getEloquentQuery()
|
2026-09-30 17:28:47 +03:00
|
|
|
->where('source', Shipment::SOURCE_CREATED)
|
|
|
|
|
->whereNotNull('order_id')
|
2026-09-14 00:03:06 +03:00
|
|
|
->whereNull('manifest_id')
|
|
|
|
|
->whereNull('cancelled_at');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function table(Table $table): Table
|
|
|
|
|
{
|
|
|
|
|
return $table
|
2026-09-30 17:28:47 +03:00
|
|
|
->defaultSort('created_at', 'desc')
|
2026-09-14 00:03:06 +03:00
|
|
|
->columns([
|
2026-09-30 17:28:47 +03:00
|
|
|
TextColumn::make('carrier')
|
|
|
|
|
->badge()
|
|
|
|
|
->formatStateUsing(fn (Shipment $record) => $record->carrierLabel()),
|
|
|
|
|
TextColumn::make('tracking_reference')
|
|
|
|
|
->label('Tracking #')
|
|
|
|
|
->placeholder('Not issued yet')
|
|
|
|
|
->copyable(),
|
2026-09-14 00:03:06 +03:00
|
|
|
TextColumn::make('order.reference')->label('Order'),
|
2026-09-30 17:28:47 +03:00
|
|
|
TextColumn::make('created_at')->label('Created')->dateTime(),
|
2026-09-14 00:03:06 +03:00
|
|
|
TextColumn::make('label_printed_at')->label('Printed')->dateTime()->placeholder('Not printed'),
|
|
|
|
|
])
|
|
|
|
|
->recordActions([
|
|
|
|
|
Action::make('print')
|
|
|
|
|
->label('Print')
|
|
|
|
|
->icon('heroicon-o-printer')
|
2026-09-30 17:28:47 +03:00
|
|
|
->url(fn (Shipment $record) => self::labelUrl($record), shouldOpenInNewTab: true),
|
|
|
|
|
Action::make('cancel')
|
|
|
|
|
->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) => self::cancelShipment($record)),
|
2026-09-14 00:03:06 +03:00
|
|
|
])
|
|
|
|
|
->toolbarActions([
|
|
|
|
|
BulkAction::make('print_selected')
|
|
|
|
|
->label('Print selected')
|
|
|
|
|
->icon('heroicon-o-printer')
|
2026-09-30 17:28:47 +03:00
|
|
|
->deselectRecordsAfterCompletion()
|
|
|
|
|
->action(fn (Collection $records, $livewire) => self::printSelected($records, $livewire)),
|
2026-09-14 00:03:06 +03:00
|
|
|
BulkAction::make('issue_manifest')
|
|
|
|
|
->label('Issue Manifest')
|
|
|
|
|
->icon('heroicon-o-check-circle')
|
2026-09-30 17:28:47 +03:00
|
|
|
->visible(fn ($livewire) => filled($livewire->activeTab ?? null)
|
|
|
|
|
&& self::fulfillmentService($livewire->activeTab) instanceof SupportsManifestBatching)
|
2026-09-14 00:03:06 +03:00
|
|
|
->action(fn (Collection $records) => self::issueManifest($records)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 17:28:47 +03:00
|
|
|
public static function labelUrl(Shipment $shipment): string
|
2026-09-14 00:03:06 +03:00
|
|
|
{
|
2026-09-30 17:28:47 +03:00
|
|
|
return URL::temporarySignedRoute('shipments.label', now()->addMinutes(5), ['shipment' => $shipment->id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* One combined PDF for carriers that support it (opened in a new tab,
|
|
|
|
|
* with a link in the notification in case the browser blocks the
|
|
|
|
|
* pop-up); anything else has to be printed row by row.
|
|
|
|
|
*/
|
|
|
|
|
public static function printSelected(Collection $shipments, $livewire): void
|
|
|
|
|
{
|
|
|
|
|
$carriers = $shipments->pluck('carrier')->unique();
|
2026-09-14 00:03:06 +03:00
|
|
|
|
2026-09-30 17:28:47 +03:00
|
|
|
if ($carriers->count() !== 1) {
|
2026-09-14 00:03:06 +03:00
|
|
|
Notification::make()
|
2026-09-30 17:28:47 +03:00
|
|
|
->title('Select shipments of a single carrier to print them together.')
|
|
|
|
|
->warning()
|
2026-09-14 00:03:06 +03:00
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 17:28:47 +03:00
|
|
|
$shipment = $shipments->first();
|
|
|
|
|
|
|
|
|
|
if (! self::fulfillmentService($shipment->carrier) instanceof SupportsBatchLabels) {
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title("{$shipment->carrierLabel()} labels can't be combined — print them individually.")
|
|
|
|
|
->warning()
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$url = URL::temporarySignedRoute('shipments.labels', now()->addMinutes(5), [
|
|
|
|
|
'ids' => $shipments->pluck('id')->sort()->implode(','),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$livewire->js('window.open('.json_encode($url).', "_blank")');
|
|
|
|
|
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title('Printing '.$shipments->count().' labels.')
|
|
|
|
|
->success()
|
|
|
|
|
->actions([
|
|
|
|
|
Action::make('open')->label('Open PDF')->url($url, shouldOpenInNewTab: true),
|
|
|
|
|
])
|
|
|
|
|
->send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function cancelShipment(Shipment $shipment): void
|
|
|
|
|
{
|
|
|
|
|
if ($shipment->cancelsLocallyOnly()) {
|
|
|
|
|
$shipment->update(['cancelled_at' => now()]);
|
|
|
|
|
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title('Shipment marked as cancelled. Void the voucher with the courier as well.')
|
|
|
|
|
->success()
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$service = self::fulfillmentService($shipment->carrier);
|
|
|
|
|
|
2026-09-14 00:03:06 +03:00
|
|
|
try {
|
2026-09-30 17:28:47 +03:00
|
|
|
if (! $service) {
|
|
|
|
|
throw new \RuntimeException("No fulfillment integration configured for {$shipment->carrierLabel()}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$service->cancelShipment($shipment);
|
2026-09-14 00:03:06 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
|
|
|
|
|
Notification::make()
|
2026-09-30 17:28:47 +03:00
|
|
|
->title('Failed to cancel shipment: '.$e->getMessage())
|
2026-09-14 00:03:06 +03:00
|
|
|
->danger()
|
|
|
|
|
->send();
|
2026-09-30 17:28:47 +03:00
|
|
|
|
|
|
|
|
return;
|
2026-09-14 00:03:06 +03:00
|
|
|
}
|
2026-09-30 17:28:47 +03:00
|
|
|
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title('Shipment cancelled.')
|
|
|
|
|
->success()
|
|
|
|
|
->send();
|
2026-09-14 00:03:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function issueManifest(Collection $shipments): void
|
|
|
|
|
{
|
|
|
|
|
$shipments->groupBy('carrier')->each(function (Collection $group, string $carrier) {
|
|
|
|
|
$service = self::fulfillmentService($carrier);
|
|
|
|
|
|
|
|
|
|
if (! $service instanceof SupportsManifestBatching) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$result = $service->issueManifest($group);
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title("Failed to issue manifest for {$carrier}: {$e->getMessage()}")
|
|
|
|
|
->danger()
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $result->success) {
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title("Manifest blocked for {$carrier}: {$result->reason}")
|
|
|
|
|
->danger()
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notification::make()
|
|
|
|
|
->title("Manifest issued for {$carrier}: {$result->reference}")
|
|
|
|
|
->success()
|
|
|
|
|
->send();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function fulfillmentService(string $carrier): ?CarrierFulfillmentInterface
|
|
|
|
|
{
|
|
|
|
|
return app(CarrierFulfillmentInterface::class, ['carrier' => $carrier]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getPages(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'index' => ListShipments::route('/'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|