50 lines
1.9 KiB
PHP
50 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\LDC;
|
|
|
|
use Gloudemans\Shoppingcart\CartItem;
|
|
use Gloudemans\Shoppingcart\Contracts\Calculator;
|
|
|
|
class CartCalculator implements Calculator
|
|
{
|
|
/**
|
|
* All purpose getter
|
|
*
|
|
* @param string $attribute
|
|
* @param CartItem $cartItem
|
|
* @return float|void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
public static function getAttribute(string $attribute, CartItem $cartItem)
|
|
{
|
|
// @phpcs:enable
|
|
$decimals = config('cart.format.decimals', 2);
|
|
|
|
switch ($attribute) {
|
|
case 'discount':
|
|
return round($cartItem->price * ($cartItem->getDiscountRate() / 100) * 100) / 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':
|
|
// Taxe pour un item multiplié par la quantité.
|
|
$unit_price = max($cartItem->price - $cartItem->discount, 0);
|
|
$unit_tax = round($unit_price * ($cartItem->taxRate / 100), $decimals);
|
|
$taxTotal = round($unit_tax * $cartItem->qty, $decimals);
|
|
return $taxTotal;
|
|
case 'total':
|
|
return round($cartItem->subtotal + $cartItem->taxTotal, $decimals);
|
|
default:
|
|
return;
|
|
}//end switch
|
|
}
|
|
}
|