Files
core/src/Shipping/Http/Controllers/DownloadShipmentLabelsController.php
T

49 lines
1.6 KiB
PHP

<?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\Contracts\SupportsBatchLabels;
use Modules\Core\Shipping\Models\Shipment;
/**
* The Pending Vouchers screen's "Print selected": several shipments' labels
* as one PDF. Same signed-URL auth as DownloadShipmentLabelController —
* the ids are part of the signed query string, so they can't be changed.
* All shipments must be one carrier's (the screen's tabs are per carrier)
* and that carrier must implement SupportsBatchLabels.
*/
class DownloadShipmentLabelsController extends Controller
{
public function __invoke(Request $request)
{
if (! $request->hasValidSignature()) {
abort(401);
}
$ids = array_filter(array_map('intval', explode(',', (string) $request->query('ids'))));
$shipments = Shipment::whereIn('id', $ids)->orderBy('id')->get()
->filter(fn (Shipment $shipment) => $shipment->hasCarrierLabel());
$carriers = $shipments->pluck('carrier')->unique();
if ($shipments->isEmpty() || $carriers->count() !== 1) {
abort(404);
}
$service = app(CarrierFulfillmentInterface::class, ['carrier' => $carriers->first()]);
if (! $service instanceof SupportsBatchLabels) {
abort(404);
}
return response($service->printLabels($shipments), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="labels-'.now()->format('Ymd-His').'.pdf"',
]);
}
}