mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-21 06:44:59 +00:00
Refactor gross price / default into calculators,
Remove gross_price from config replaced with calculator
This commit is contained in:
34
src/Calculation/DefaultCalculator.php
Normal file
34
src/Calculation/DefaultCalculator.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Calculation;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
|
||||
class DefaultCalculator {
|
||||
static function getAttribute(string $attribute, CartItem $cartItem) {
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
switch ($attribute) {
|
||||
case 'discount':
|
||||
return $cartItem->price * ($cartItem->getDiscountRate() / 100);
|
||||
case 'tax':
|
||||
return round($cartItem->priceTarget * ($cartItem->taxRate / 100), $decimals);
|
||||
case 'priceTax':
|
||||
return round($cartItem->priceTarget + $cartItem->tax, $decimals);
|
||||
case 'discountTotal':
|
||||
return round($cartItem->discount * $cartItem->qty, $decimals);
|
||||
case 'priceTotal':
|
||||
return round($cartItem->price * $cartItem->qty, $decimals);
|
||||
case 'subtotal':
|
||||
return max(round($cartItem->priceTotal - $cartItem->discountTotal, $decimals), 0);
|
||||
case 'priceTarget':
|
||||
return round(($cartItem->priceTotal - $cartItem->discountTotal) / $cartItem->qty, $decimals);
|
||||
case 'taxTotal':
|
||||
return round($cartItem->subtotal * ($cartItem->taxRate / 100), $decimals);
|
||||
case 'total':
|
||||
return round($cartItem->subtotal + $cartItem->taxTotal, $decimals);
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/Calculation/GrossPrice.php
Normal file
36
src/Calculation/GrossPrice.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Calculation;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
|
||||
class GrossPrice {
|
||||
static function getAttribute(string $attribute, CartItem $cartItem) {
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
switch ($attribute) {
|
||||
case 'priceNet':
|
||||
return round($cartItem->price / (1 + ($cartItem->taxRate / 100)), $decimals);
|
||||
case 'discount':
|
||||
return $cartItem->priceNet * ($cartItem->getDiscountRate() / 100);
|
||||
case 'tax':
|
||||
return round($cartItem->priceTarget * ($cartItem->taxRate / 100), $decimals);
|
||||
case 'priceTax':
|
||||
return round($cartItem->priceTarget + $cartItem->tax, $decimals);
|
||||
case 'discountTotal':
|
||||
return round($cartItem->discount * $cartItem->qty, $decimals);
|
||||
case 'priceTotal':
|
||||
return round($cartItem->priceNet * $cartItem->qty, $decimals);
|
||||
case 'subtotal':
|
||||
return max(round($cartItem->priceTotal - $cartItem->discountTotal, $decimals), 0);
|
||||
case 'priceTarget':
|
||||
return round(($cartItem->priceTotal - $cartItem->discountTotal) / $cartItem->qty, $decimals);
|
||||
case 'taxTotal':
|
||||
return round($cartItem->subtotal * ($cartItem->taxRate / 100), $decimals);
|
||||
case 'total':
|
||||
return round($cartItem->subtotal + $cartItem->taxTotal, $decimals);
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user