diff --git a/src/Fields/Items.php b/src/Fields/Items.php index 41d40502..b0ba853c 100644 --- a/src/Fields/Items.php +++ b/src/Fields/Items.php @@ -21,6 +21,7 @@ class Items extends MorphMany protected array $with = [ 'buyable', 'checkoutable', + 'taxes', ]; /** @@ -64,12 +65,11 @@ public function fields(Request $request): array return (new Currency($value, $model->checkoutable->currency))->format(); }), - Number::make(__('TAX'), 'tax') - ->min(0) - ->required() - ->format(static function (Request $request, Model $model, ?float $value): string { - return (new Currency($value, $model->checkoutable->currency))->format(); - }), + Number::make(__('TAX'), function (Request $request, Model $model): float { + return $model->getTax(); + })->format(static function (Request $request, Model $model, float $value): string { + return (new Currency($value, $model->checkoutable->currency))->format(); + }), Number::make(__('Quantity'), 'quantity') ->required() diff --git a/src/Interfaces/Taxable.php b/src/Interfaces/Taxable.php index bea33d6b..8cb95555 100644 --- a/src/Interfaces/Taxable.php +++ b/src/Interfaces/Taxable.php @@ -19,11 +19,21 @@ public function getTaxBase(): float; /** * Get the tax total. */ - public function getTaxTotal(): float; + public function getTax(): float; /** * Get the formatted tax. */ + public function getFormattedTax(): string; + + /** + * Get the tax total total. + */ + public function getTaxTotal(): float; + + /** + * Get the formatted tax total. + */ public function getFormattedTaxTotal(): string; /** diff --git a/src/Models/Item.php b/src/Models/Item.php index 648f3a67..36f284cf 100644 --- a/src/Models/Item.php +++ b/src/Models/Item.php @@ -212,7 +212,7 @@ public function getFormattedPrice(): string */ public function getTotal(): float { - return ($this->getPrice() + $this->getTaxTotal()) * $this->getQuantity(); + return ($this->getPrice() + $this->getTax()) * $this->getQuantity(); } /** @@ -247,6 +247,14 @@ public function getTaxBase(): float return $this->price; } + /** + * Get the formatted tax. + */ + public function getFormattedTax(): string + { + return (new Currency($this->getTax(), $this->checkoutable->getCurrency()))->format(); + } + /** * Get the formatted tax. */ diff --git a/src/Models/Shipping.php b/src/Models/Shipping.php index 648a03de..97ae7b1e 100644 --- a/src/Models/Shipping.php +++ b/src/Models/Shipping.php @@ -253,6 +253,14 @@ public function getFormattedSubtotal(): string return (new Currency($this->getSubtotal(), $this->shippable->getCurrency()))->format(); } + /** + * Get the formatted tax total. + */ + public function getFormattedTax(): string + { + return (new Currency($this->getTax(), $this->shippable->getCurrency()))->format(); + } + /** * Get the formatted tax total. */ diff --git a/src/Traits/InteractsWithTaxes.php b/src/Traits/InteractsWithTaxes.php index 3d66d05a..67cb2241 100644 --- a/src/Traits/InteractsWithTaxes.php +++ b/src/Traits/InteractsWithTaxes.php @@ -34,6 +34,38 @@ public function taxes(): MorphToMany ->withTimestamps(); } + /** + * Get the tax total. + * + * @return \Illuminate\Database\Eloquent\Casts\Attribute + */ + protected function tax(): Attribute + { + return new Attribute( + get: fn (): float => $this->getTax() + ); + } + + /** + * Get the formatted tax attribute. + * + * @return \Illuminate\Database\Eloquent\Casts\Attribute + */ + protected function formattedTax(): Attribute + { + return new Attribute( + get: fn (): string => $this->getFormattedTax() + ); + } + + /** + * Get the tax total. + */ + public function getTax(): float + { + return $this->taxes->sum('tax.value'); + } + /** * Get the tax total. *