Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 26, 2024
1 parent dfd2c56 commit 6cb8ebf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Fields/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Items extends MorphMany
protected array $with = [
'buyable',
'checkoutable',
'taxes',
];

/**
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion src/Interfaces/Taxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Traits/InteractsWithTaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ public function taxes(): MorphToMany
->withTimestamps();
}

/**
* Get the tax total.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
*/
protected function tax(): Attribute
{
return new Attribute(
get: fn (): float => $this->getTax()
);
}

/**
* Get the formatted tax attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
*/
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.
*
Expand Down

0 comments on commit 6cb8ebf

Please sign in to comment.