2026-09-14 00:03:06 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Shipping\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Routing\Controller;
|
|
|
|
|
use Modules\Core\Shipping\Contracts\CarrierFulfillmentInterface;
|
|
|
|
|
use Modules\Core\Shipping\Models\Shipment;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Streams a carrier's raw label bytes (CarrierFulfillmentInterface::
|
|
|
|
|
* printLabel() — already whatever file format the carrier's own API
|
|
|
|
|
* returns, e.g. a PDF for both ACS and Box Now today) straight to the
|
|
|
|
|
* browser. Only reachable via a short-lived signed URL (see
|
|
|
|
|
* Modules\Core\Shipping\Extensions\OrderShipmentsExtension's "Print
|
|
|
|
|
* Label" action) — the same auth model Lunar's own vendor
|
|
|
|
|
* DownloadPdfController uses for order PDFs (a valid signature IS the
|
|
|
|
|
* auth check; there is no separate staff-session check here, matching
|
|
|
|
|
* that precedent), so the link only works for the few minutes it's
|
|
|
|
|
* actually open in a browser tab.
|
|
|
|
|
*
|
|
|
|
|
* Looks the Shipment up manually from a plain {shipment} id rather than
|
|
|
|
|
* relying on implicit route-model-binding — this route is registered via
|
|
|
|
|
* loadRoutesFrom() with no middleware group (see
|
|
|
|
|
* Modules\Core\Providers\ShippingServiceProvider::boot()), so
|
|
|
|
|
* SubstituteBindings never runs and a type-hinted Shipment parameter
|
|
|
|
|
* silently resolves to an empty, non-existent model instead of 404ing.
|
|
|
|
|
*
|
|
|
|
|
* Sets label_printed_at as a side effect of a successful stream — this is
|
|
|
|
|
* the first place in the codebase that actually delivers a label's bytes
|
|
|
|
|
* to a human; the existing Modules\Core\Shipping\Filament\Pages\
|
|
|
|
|
* ManagePickupManifests "Print" action calls printLabel() too, but only
|
|
|
|
|
* to mark the timestamp, discarding the returned bytes entirely (no
|
|
|
|
|
* download route existed until this one).
|
|
|
|
|
*/
|
|
|
|
|
class DownloadShipmentLabelController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __invoke(Request $request, int $shipment)
|
|
|
|
|
{
|
|
|
|
|
if (! $request->hasValidSignature()) {
|
|
|
|
|
abort(401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$shipment = Shipment::findOrFail($shipment);
|
|
|
|
|
|
|
|
|
|
$service = app(CarrierFulfillmentInterface::class, ['carrier' => $shipment->carrier]);
|
|
|
|
|
|
2026-09-30 17:28:47 +03:00
|
|
|
// Manual carriers, typed-in vouchers and synced ones have no
|
|
|
|
|
// carrier label to print.
|
|
|
|
|
if (! $service || ! $shipment->hasCarrierLabel()) {
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-14 00:03:06 +03:00
|
|
|
$bytes = $service->printLabel($shipment);
|
|
|
|
|
|
2026-09-30 17:28:47 +03:00
|
|
|
// Printing may have just issued the voucher number (ELTA pending
|
|
|
|
|
// vouchers), so name the file after the refreshed record.
|
|
|
|
|
$name = $shipment->refresh()->tracking_reference ?? $shipment->id;
|
|
|
|
|
|
2026-09-14 00:03:06 +03:00
|
|
|
return response($bytes, 200, [
|
|
|
|
|
'Content-Type' => 'application/pdf',
|
2026-09-30 17:28:47 +03:00
|
|
|
'Content-Disposition' => 'inline; filename="shipment-'.$name.'.pdf"',
|
2026-09-14 00:03:06 +03:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|