Skip to content

Commit

Permalink
Added the taxes_total, shipping_total and total attribute gette…
Browse files Browse the repository at this point in the history
…rs to the Foundation `Order` model
  • Loading branch information
fulopattila122 committed Jun 2, 2024
1 parent 4c09dc3 commit 559e1b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions src/Foundation/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -164,12 +168,31 @@ public function scopeWithCurrentPayment(Builder $query)
->whereColumn('payable_id', 'orders.id')
->where('payable_type', $this->getPayableType())
->orderByDesc('id')
->take(1)
->take(1),
])->with('currentPayment');
}

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();
}
}

0 comments on commit 559e1b3

Please sign in to comment.