diff --git a/Changelog.md b/Changelog.md index 8d7a1fbf..3dc427e5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ - Added the unidirectional links feature - Added the possibility to retrieve the link items directly using `linkItems()` method as `Get::the($type)->linkItems()->of($model)` - Added the `link_items` helper (shortcut to Get::the()->linkItems() +- Added the `taxes_total`, `shipping_total` and `total` attribute getters to the Foundation `Order` model - Changed the offline payment gateway's icon from a circle to a plug+x ## 4.0.1 diff --git a/src/Foundation/Models/Order.php b/src/Foundation/Models/Order.php index ed1d5997..4cb8231b 100644 --- a/src/Foundation/Models/Order.php +++ b/src/Foundation/Models/Order.php @@ -15,10 +15,12 @@ namespace Vanilo\Foundation\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Konekt\Customer\Models\CustomerProxy; use Vanilo\Adjustments\Contracts\Adjustable; +use Vanilo\Adjustments\Models\AdjustmentType; use Vanilo\Adjustments\Support\HasAdjustmentsViaRelation; use Vanilo\Adjustments\Support\RecalculatesAdjustments; use Vanilo\Channel\Contracts\Channel; @@ -43,7 +45,9 @@ * @property null|PaymentMethod $paymentMethod * @property null|int $customer_id * @property null|string $payable_remote_id - * @property null|\Vanilo\Contracts\Customer $customer + * @property-read float taxes_total + * @property-read float shipping_total + * @property-read null|\Vanilo\Contracts\Customer $customer * @property-read Collection|Payment[] $payments * @property-read Collection|ShipmentContract[] $shipments */ @@ -164,7 +168,7 @@ public function scopeWithCurrentPayment(Builder $query) ->whereColumn('payable_id', 'orders.id') ->where('payable_type', $this->getPayableType()) ->orderByDesc('id') - ->take(1) + ->take(1), ])->with('currentPayment'); } @@ -172,4 +176,23 @@ public function payments() { return $this->morphMany(PaymentProxy::modelClass(), 'payable'); } + + protected function taxesTotal(): Attribute + { + return Attribute::make( + get: fn (mixed $value) => $this->adjustments()->byType(AdjustmentType::TAX())->total(true), + ); + } + + protected function shippingTotal(): Attribute + { + return Attribute::make( + get: fn (mixed $value) => $this->adjustments()->byType(AdjustmentType::SHIPPING())->total(true), + ); + } + + protected function getTotalAttribute(): float + { + return $this->total(); + } }