Feature: Order Service Provider, Order Events Observers
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Order\Support;
|
||||
|
||||
use Lunar\Models\Order;
|
||||
use Modules\Core\Order\Enums\FulfillmentStatus;
|
||||
use Modules\Core\Order\Enums\PaymentStatus;
|
||||
use Modules\Core\Shipping\Enums\TrackingStatus;
|
||||
|
||||
/**
|
||||
* Payment/fulfillment state, derived on read from transactions and
|
||||
* shipments rather than stored — mirrors the logic Lunar's own
|
||||
* ManageOrder::paymentStatus() computes as a Livewire #[Computed] method
|
||||
* (Filament-page-only, not reusable), reimplemented here as a plain,
|
||||
* queryable value any code can call via Order::macro() in
|
||||
* OrderServiceProvider.
|
||||
*/
|
||||
class OrderStatus
|
||||
{
|
||||
public static function payment(Order $order): PaymentStatus
|
||||
{
|
||||
$transactions = $order->transactions;
|
||||
|
||||
$intentTotal = $transactions
|
||||
->filter(fn ($t) => $t->type === 'intent' && $t->success)
|
||||
->sum('amount.value');
|
||||
|
||||
$captureTotal = $transactions
|
||||
->filter(fn ($t) => $t->type === 'capture' && $t->success)
|
||||
->sum('amount.value');
|
||||
|
||||
$refundTotal = $transactions
|
||||
->filter(fn ($t) => $t->type === 'refund' && $t->success)
|
||||
->sum('amount.value');
|
||||
|
||||
$total = $intentTotal ?: $captureTotal;
|
||||
|
||||
if (! $total) {
|
||||
return PaymentStatus::Offline;
|
||||
}
|
||||
|
||||
if (
|
||||
($refundTotal && $refundTotal < $total) ||
|
||||
($captureTotal && $captureTotal < $intentTotal)
|
||||
) {
|
||||
return PaymentStatus::PartialRefund;
|
||||
}
|
||||
|
||||
if ($refundTotal >= $total) {
|
||||
return PaymentStatus::Refunded;
|
||||
}
|
||||
|
||||
if ($captureTotal >= $intentTotal) {
|
||||
return PaymentStatus::Captured;
|
||||
}
|
||||
|
||||
return PaymentStatus::Uncaptured;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads shipments.shipmentInfo if already eager-loaded (the caller's
|
||||
* job — e.g. Order::with('shipments.shipmentInfo')) and picks the
|
||||
* latest checkpoint in PHP, instead of Shipment::latestShipmentInfo()'s
|
||||
* per-shipment query — calling this across a list of orders would
|
||||
* otherwise be an extra query per shipment.
|
||||
*/
|
||||
public static function fulfillment(Order $order): FulfillmentStatus
|
||||
{
|
||||
$shipments = $order->shipments->reject(fn ($shipment) => $shipment->cancelled_at !== null);
|
||||
|
||||
if ($shipments->isEmpty()) {
|
||||
return FulfillmentStatus::Unfulfilled;
|
||||
}
|
||||
|
||||
$latestStatuses = $shipments->map(function ($shipment) {
|
||||
$latest = $shipment->relationLoaded('shipmentInfo')
|
||||
? $shipment->shipmentInfo->sortByDesc('occurred_at')->first()
|
||||
: $shipment->latestShipmentInfo();
|
||||
|
||||
return $latest?->status ?? TrackingStatus::Pending;
|
||||
});
|
||||
|
||||
if ($latestStatuses->every(fn (TrackingStatus $status) => $status === TrackingStatus::Delivered)) {
|
||||
return FulfillmentStatus::Delivered;
|
||||
}
|
||||
|
||||
if ($latestStatuses->contains(fn (TrackingStatus $status) => $status === TrackingStatus::Delivered)) {
|
||||
return FulfillmentStatus::PartiallyShipped;
|
||||
}
|
||||
|
||||
return FulfillmentStatus::Shipped;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user