53 lines
1.9 KiB
PHP
53 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Core\Shipping\Carriers\Manual;
|
|
|
|
use Lunar\Models\Order;
|
|
use Lunar\Shipping\Models\ShippingMethod;
|
|
use Modules\Core\Shipping\Contracts\CarrierFulfillmentInterface;
|
|
use Modules\Core\Shipping\DTOs\ShipmentRequest;
|
|
use Modules\Core\Shipping\Models\Shipment;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Shipments of a manual carrier (ManualRateDriver): recorded here only,
|
|
* with no carrier API. The carrier's name and tracking URL template are
|
|
* copied from the shipping method into the shipment's meta, so links keep
|
|
* working if the method is edited later. Tracking updates are added by
|
|
* staff from the order page.
|
|
*/
|
|
class ManualFulfillmentService implements CarrierFulfillmentInterface
|
|
{
|
|
public function createShipment(Order $order, ShipmentRequest $request): Shipment
|
|
{
|
|
$code = $order->shippingAddress?->shipping_option;
|
|
$method = $code ? ShippingMethod::where('code', $code)->first() : null;
|
|
|
|
return Shipment::create([
|
|
'order_id' => $order->id,
|
|
'carrier' => 'manual',
|
|
'source' => Shipment::SOURCE_MANUAL,
|
|
'tracking_reference' => filled($request->trackingReference) ? trim($request->trackingReference) : null,
|
|
'meta' => [
|
|
'carrier_name' => $method?->data['carrier_name'] ?? null,
|
|
'tracking_url' => $method?->data['tracking_url'] ?? null,
|
|
'package_count' => $request->packageCount,
|
|
'weight' => $request->weight,
|
|
'cod_amount' => $request->paymentMode === 'cod'
|
|
? (float) ($request->amountToCollect ?? $order->total->decimal)
|
|
: null,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function printLabel(Shipment $shipment): string
|
|
{
|
|
throw new RuntimeException('Manual carriers have no printable label.');
|
|
}
|
|
|
|
public function cancelShipment(Shipment $shipment): void
|
|
{
|
|
$shipment->update(['cancelled_at' => now()]);
|
|
}
|
|
}
|