-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartFixedDiscountStrategy.php
45 lines (36 loc) · 1.41 KB
/
CartFixedDiscountStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace EscolaLms\Vouchers\Strategies;
use EscolaLms\Vouchers\Models\Cart;
use EscolaLms\Vouchers\Models\CartItem;
use EscolaLms\Vouchers\Services\Contracts\CouponServiceContract;
use EscolaLms\Vouchers\Strategies\Abstracts\DiscountStrategy;
use EscolaLms\Vouchers\Strategies\Contracts\DiscountStrategyContract;
class CartFixedDiscountStrategy extends DiscountStrategy implements DiscountStrategyContract
{
public function calculateAdditionalDiscount(Cart $cart): int
{
return 0;
}
public function calculateDiscountForItem(Cart $cart, CartItem $cartItem): int
{
if (app(CouponServiceContract::class)->cartItemIsExcludedFromCoupon($this->coupon, $cartItem)) {
return 0;
}
$totalAmount = 0;
$maxAmount = $this->coupon->amount;
foreach ($cart->items as $item) {
if (!app(CouponServiceContract::class)->cartItemIsExcludedFromCoupon($this->coupon, $item)) {
$tax = (1 + $item->tax_rate / 100);
$totalAmount += $item->basePrice * $tax;
}
}
$maxAmount = min($totalAmount, $maxAmount);
if ($totalAmount > 0) {
$tax = (1 + $cartItem->tax_rate / 100);
$itemValue = $cartItem->basePrice * $tax;
$discount = round($itemValue / $totalAmount * $maxAmount, 0);
return (int) round($discount / $tax, 0);
}
return 0;
}
}