Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Jul 23, 2024
1 parent ae1386c commit c6eac70
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Services/Contracts/CouponServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function cartContainsItemsNotExcludedFromCoupon(Coupon $coupon, Cart $car
/**
* @param Coupon $coupon
* @param Cart $cart
* @return Collection<int, CartItem>
* @return Collection<int, \Treestoneit\ShoppingCart\Models\CartItem|CartItem>
*/
public function cartItemsWithoutExcludedFromCoupon(Coupon $coupon, Cart $cart): Collection;
public function cartItemIsExcludedFromCoupon(Coupon $coupon, CartItem $item): bool;
Expand Down
13 changes: 10 additions & 3 deletions src/Services/CouponService.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ public function cartContainsItemsIncludedInCoupon(Coupon $coupon, Cart $cart): b
*/
public function cartItemsIncludedInCoupon(Coupon $coupon, Cart $cart): Collection
{
return $cart->items->filter(fn (CartItem $item) => $this->cartItemIsIncludedInCoupon($coupon, $item));
/** @var Collection<int, CartItem|\EscolaLms\Vouchers\Models\CartItem> $items */
// @phpstan-ignore-next-line
$items = $cart->items->filter(fn (CartItem $item) => $this->cartItemIsIncludedInCoupon($coupon, $item));
return $items;
}

public function cartItemIsIncludedInCoupon(Coupon $coupon, CartItem $item): bool
Expand All @@ -277,11 +280,14 @@ public function cartContainsItemsNotExcludedFromCoupon(Coupon $coupon, Cart $car
/**
* @param Coupon $coupon
* @param Cart $cart
* @return Collection<int, CartItem>
* @return Collection<int, CartItem|\EscolaLms\Vouchers\Models\CartItem>
*/
public function cartItemsWithoutExcludedFromCoupon(Coupon $coupon, Cart $cart): Collection
{
return $cart->items->filter(fn (CartItem $item) => !$this->cartItemIsExcludedFromCoupon($coupon, $item));
/** @var Collection<int, CartItem|\EscolaLms\Vouchers\Models\CartItem> $result */
// @phpstan-ignore-next-line
$result = $cart->items->filter(fn (CartItem $item) => !$this->cartItemIsExcludedFromCoupon($coupon, $item));
return $result;
}

public function cartItemIsExcludedFromCoupon(Coupon $coupon, CartItem $item): bool
Expand All @@ -291,6 +297,7 @@ public function cartItemIsExcludedFromCoupon(Coupon $coupon, CartItem $item): bo

public function cartFulfilPromotionConditions(Coupon $coupon, Cart $cart): bool
{
// @phpstan-ignore-next-line
return !$coupon->exclude_promotions || $cart->items->filter(fn (CartItem $item) => $this->productOnPromotion($coupon, $item))->count() === 0;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class OrderService extends BaseOrderService implements OrderServiceContract
{
/** @return Order */
// @phpstan-ignore-next-line
public function find($id): Model
{
return Order::findOrFail($id);

Check failure on line 21 in src/Services/OrderService.php

View workflow job for this annotation

GitHub Actions / php82-laravel-latest-phpstan-postgres

Method EscolaLms\Vouchers\Services\OrderService::find() should return EscolaLms\Vouchers\Models\Order but returns Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model.
Expand All @@ -28,9 +29,11 @@ public function createOrderFromCart(BaseCart $cart, ?ClientDetailsDto $clientDet
public function createOrderFromCartManager(BaseCartManager $cartManager, ?ClientDetailsDto $clientDetailsDto = null): Order
{
if (!$cartManager instanceof CartManager) {
// @phpstan-ignore-next-line
$cartManager = new CartManager($cartManager->getModel());
}
$order = parent::createOrderFromCartManager($cartManager, $clientDetailsDto);
/** @var Order $order */
$order = $order instanceof Order ? $order : Order::find($order->getKey());
$order->coupon_id = optional($cartManager->getCoupon())->getKey();
$order->discount = $cartManager->additionalDiscount();
Expand Down
1 change: 1 addition & 0 deletions src/Strategies/CartPercentDiscountStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function calculateDiscountForItem(Cart $cart, CartItem $cartItem): int
if (app(CouponServiceContract::class)->cartItemIsExcludedFromCoupon($this->coupon, $cartItem)) {
return 0;
}
// @phpstan-ignore-next-line
return (int) round($this->coupon->amount * $cartItem->buyable->getBuyablePrice() / 100, 0);
}
}
2 changes: 1 addition & 1 deletion src/Strategies/ProductFixedDiscountStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function calculateDiscountForItem(Cart $cart, CartItem $cartItem): int
$itemValue = $cartItem->basePrice * $tax;
$discount = $itemValue < $this->coupon->amount ? $itemValue : $this->coupon->amount;
if ($discount === $itemValue) {
return $cartItem->basePrice;
return (int) $cartItem->basePrice;
}
return (int) round($discount / $tax, 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/Strategies/ProductPercentDiscountStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function calculateDiscountForItem(Cart $cart, CartItem $cartItem): int
if (!app(CouponServiceContract::class)->cartItemIsIncludedInCoupon($this->coupon, $cartItem)) {
return 0;
}
// @phpstan-ignore-next-line
return (int) round($this->coupon->amount * $cartItem->buyable->getBuyablePrice() / 100, 0);
}
}

0 comments on commit c6eac70

Please sign in to comment.