Skip to content

Commit

Permalink
rework totals
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 15, 2023
1 parent 0c115a3 commit 86a3885
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 72 deletions.
2 changes: 1 addition & 1 deletion resources/views/emails/customer-new-order.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

**Tax**: {{ $order->formattedTax }}

**Net. Total**: {{ $order->formattedNetTotal }}
**Subtotal**: {{ $order->formattedSubtotal }}

**Gross Total**: {{ $order->formattedTotal }}

Expand Down
18 changes: 14 additions & 4 deletions src/Interfaces/Itemable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@ public function getTotal(): float;
public function getFormattedTotal(): string;

/**
* Get the net total.
* Get the subtotal.
*/
public function getNetTotal(): float;
public function getSubtotal(): float;

/**
* Get the formatted net total.
* Get the formatted subtotal.
*/
public function getFormattedNetTotal(): string;
public function getFormattedSubtotal(): string;

/**
* Get the itemable model's fee total.
*/
public function getFeeTotal(): float;

/**
* Get the formatted fee total.
*/
public function getFormattedFeeTotal(): string;

/**
* Get the tax.
Expand Down
8 changes: 4 additions & 4 deletions src/Interfaces/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function getTotal(): float;
public function getFormattedTotal(): string;

/**
* Get the net total.
* Get the subtotal.
*/
public function getNetTotal(): float;
public function getSubtotal(): float;

/**
* Get the formatted net total.
* Get the formatted subtotal.
*/
public function getFormattedNetTotal(): string;
public function getFormattedSubtotal(): string;

/**
* Get the quantity.
Expand Down
35 changes: 22 additions & 13 deletions src/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Bazar\Models;

use Cone\Bazar\Database\Factories\ItemFactory;
use Cone\Bazar\Interfaces\Buyable;
use Cone\Bazar\Interfaces\Models\Item as Contract;
use Cone\Bazar\Traits\InteractsWithTaxes;
use Cone\Root\Traits\InteractsWithProxy;
Expand All @@ -27,7 +28,7 @@ class Item extends Model implements Contract
* @var array<string>
*/
protected $appends = [
'net_total',
'subtotal',
'total',
];

Expand Down Expand Up @@ -149,22 +150,22 @@ protected function formattedTotal(): Attribute
}

/**
* Get the net total attribute.
* Get the subtotal attribute.
*/
protected function netTotal(): Attribute
protected function subtotal(): Attribute
{
return new Attribute(
get: fn (): float => $this->getNetTotal(),
get: fn (): float => $this->getSubtotal(),
);
}

/**
* Get the formatted net total attribute.
* Get the formatted subtotal attribute.
*/
protected function formattedNetTotal(): Attribute
protected function formattedSubtotal(): Attribute
{
return new Attribute(
get: fn (): string => $this->getFormattedNetTotal(),
get: fn (): string => $this->getFormattedSubtotal(),
);
}

Expand Down Expand Up @@ -209,19 +210,19 @@ public function getFormattedTotal(): string
}

/**
* Get the net total.
* Get the subtotal.
*/
public function getNetTotal(): float
public function getSubtotal(): float
{
return $this->getPrice() * $this->getQuantity();
}

/**
* Get the formatted net total.
* Get the formatted subtotal.
*/
public function getFormattedNetTotal(): string
public function getFormattedSubtotal(): string
{
return Str::currency($this->getNetTotal(), $this->itemable->getCurrency());
return Str::currency($this->getSubtotal(), $this->itemable->getCurrency());
}

/**
Expand All @@ -232,11 +233,19 @@ public function getQuantity(): float
return $this->quantity;
}

/**
* Determine if the item is a line item.
*/
public function isLineItem(): bool
{
return $this->buyable instanceof Buyable;
}

/**
* Determine if the item is a fee.
*/
public function isFee(): bool
{
return is_null($this->buyable);
return ! $this->isLineItem();
}
}
4 changes: 2 additions & 2 deletions src/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class Order extends Model implements Contract
*/
protected $appends = [
'formatted_discount',
'formatted_net_total',
'formatted_subtotal',
'formatted_tax',
'formatted_total',
'net_total',
'subtotal',
'status_name',
'tax',
'total',
Expand Down
22 changes: 11 additions & 11 deletions src/Models/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ protected function formattedTotal(): Attribute
}

/**
* Get the net total attribute.
* Get the subtotal attribute.
*/
protected function netTotal(): Attribute
protected function subtotal(): Attribute
{
return new Attribute(
get: function (): float {
return $this->getNetTotal();
return $this->getSubtotal();
}
);
}

/**
* Get the formatted net total attribute.
* Get the formatted subtotal attribute.
*/
protected function formattedNetTotal(): Attribute
protected function formattedSubtotal(): Attribute
{
return new Attribute(
get: function (): string {
return $this->getFormattedNetTotal();
return $this->getFormattedSubtotal();
}
);
}
Expand Down Expand Up @@ -214,19 +214,19 @@ public function getFormattedTotal(): string
}

/**
* Get the shipping's net total.
* Get the shipping's subtotal.
*/
public function getNetTotal(): float
public function getSubtotal(): float
{
return $this->getPrice();
}

/**
* Get the shipping's formatted net total.
* Get the shipping's formatted subtotal.
*/
public function getFormattedNetTotal(): string
public function getFormattedSubtotal(): string
{
return Str::currency($this->getNetTotal(), $this->shippable->getCurrency());
return Str::currency($this->getSubtotal(), $this->shippable->getCurrency());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/AsCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public function address(): MorphOne
return $this->addresses()->one()->ofMany([
'default' => 'max',
'id' => 'min',
]);
])->withDefault();
}
}
88 changes: 68 additions & 20 deletions src/Traits/InteractsWithItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ protected function currency(): Attribute
* Get the line items attribute.
*/
protected function lineItems(): Attribute
{
return new Attribute(
get: function (): Collection {
return $this->items->filter->isLineItem();
}
);
}

/**
* Get the fees attribute.
*/
protected function fees(): Attribute
{
return new Attribute(
get: function (): Collection {
return $this->items->filter->isFee();
}
);
}

/**
* Get the taxables attribute.
*/
protected function taxables(): Attribute
{
return new Attribute(
get: function (): Collection {
Expand Down Expand Up @@ -111,25 +135,25 @@ protected function formattedTotal(): Attribute
}

/**
* Get the net total attribute.
* Get the subtotal attribute.
*/
protected function netTotal(): Attribute
protected function subtotal(): Attribute
{
return new Attribute(
get: function (): float {
return $this->getNetTotal();
return $this->getSubtotal();
}
);
}

/**
* Get the formatted net total attribute.
* Get the formatted subtotal attribute.
*/
protected function formattedNetTotal(): Attribute
protected function formattedSubtotal(): Attribute
{
return new Attribute(
get: function (): string {
return $this->getFormattedNetTotal();
return $this->getFormattedSubtotal();
}
);
}
Expand Down Expand Up @@ -183,9 +207,13 @@ public function getCurrency(): string
*/
public function getTotal(): float
{
$value = $this->lineItems->reduce(static function (float $value, LineItem $item): float {
return $value + $item->getTotal();
}, -$this->discount);
$value = $this->items->sum(static function (LineItem $item): float {
return $item->getTotal();
});

$value += $this->shipping->getTotal();

$value -= $this->discount;

return round($value < 0 ? 0 : $value, 2);
}
Expand All @@ -195,27 +223,47 @@ public function getTotal(): float
*/
public function getFormattedTotal(): string
{
return Str::currency($this->getNetTotal(), $this->getCurrency());
return Str::currency($this->getTotal(), $this->getCurrency());
}

/**
* Get the itemable model's total.
* Get the itemable model's subtotal.
*/
public function getSubtotal(): float
{
$value = $this->lineItems->sum(static function (LineItem $item): float {
return $item->getSubtotal();
});

return round($value < 0 ? 0 : $value, 2);
}

/**
* Get the formatted subtotal.
*/
public function getNetTotal(): float
public function getFormattedSubtotal(): string
{
$value = $this->lineItems->reduce(static function (float $value, LineItem $item): float {
return $value + $item->getNetTotal();
}, -$this->discount);
return Str::currency($this->getSubtotal(), $this->getCurrency());
}

/**
* Get the itemable model's fee total.
*/
public function getFeeTotal(): float
{
$value = $this->fees->sum(static function (LineItem $item): float {
return $item->getSubtotal();
});

return round($value < 0 ? 0 : $value, 2);
}

/**
* Get the formatted net total.
* Get the formatted fee total.
*/
public function getFormattedNetTotal(): string
public function getFormattedFeeTotal(): string
{
return Str::currency($this->getNetTotal(), $this->getCurrency());
return Str::currency($this->getFeeTotal(), $this->getCurrency());
}

/**
Expand Down Expand Up @@ -243,7 +291,7 @@ public function getFormattedTax(): string
*/
public function calculateTax(bool $update = true): float
{
return $this->lineItems->sum(static function (LineItem $item) use ($update): float {
return $this->taxables->sum(static function (LineItem $item) use ($update): float {
return $item->calculateTax($update) * $item->getQuantity();
});
}
Expand Down Expand Up @@ -296,7 +344,7 @@ public function mergeItem(Item $item): Item
public function syncItems(): void
{
$this->items->each(static function (Item $item): void {
if (! $item->isFee() && ! is_null($item->itemable)) {
if ($item->isLineItem() && ! is_null($item->itemable)) {
$data = $item->buyable->toItem($item->itemable, $item->only('properties'))->only('price');

$item->fill($data)->calculateTax();
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public function test_cart_has_total_attribute(): void
$this->assertEquals($total, $this->cart->total);
}

public function test_cart_has_net_total_attribute(): void
public function test_cart_has_subtotal_attribute(): void
{
$total = $this->cart->items->sum(function ($item) {
return $item->price * $item->quantity;
});

$total -= $this->cart->discount;

$this->assertEquals($total, $this->cart->netTotal);
$this->assertEquals($total, $this->cart->subtotal);
}

public function test_cart_can_be_locked(): void
Expand Down
Loading

0 comments on commit 86a3885

Please sign in to comment.