|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Reduction;
-
- use App\Entity\Order\OrderShop;
- use App\Entity\Reduction\ReductionCart;
- use App\Resolver\Price\PriceResolver;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Repository\AbstractStore;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
-
- class ReductionCartStore extends AbstractStore
- {
- protected ReductionCartRepositoryQuery $query;
- protected OrderShopStore $orderShopStore;
- protected PriceResolver $priceResolver;
- protected FlashBagInterface $flashBag;
-
- public function __construct(
- ReductionCartRepositoryQuery $query,
- OrderShopStore $orderShopStore,
- PriceResolver $priceResolver,
- FlashBagInterface $flashBag
- ) {
- $this->query = $query;
- $this->orderShopStore = $orderShopStore;
- $this->priceResolver = $priceResolver;
- $this->flashBag = $flashBag;
- }
-
- // getReductionCartByCode
- public function getByCode(string $code)
- {
- $query = $this->query->create();
-
- $query->filterByCode($code);
- $reductionCarts = $query->find();
-
- $findReductionCart = null;
- foreach ($reductionCarts as $reductionCart) {
- if ($reductionCart && in_array($code, $reductionCart->getCodes())) {
- $findReductionCart = $reductionCart;
- }
- }
- return $findReductionCart;
- }
-
- // getReductionCartRemainingQuantity
- public function getRemainingQuantity(ReductionCartInterface $reductionCart): float
- {
- return $reductionCart->getAvailableQuantity() - $this->orderShopStore->countValidOrderWithReductionCart(
- $reductionCart
- );
- }
-
- // getReductionCartUsedQuantityPerUser
- public function getUsedQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
- {
- return $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart, $user);
- }
-
- // getReductionCartUsedQuantity
- public function getUsedQuantity(ReductionCartInterface $reductionCart): float
- {
- return $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart);
- }
-
- // getReductionCartRemainingQuantityPerUser
- public function getRemainingQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
- {
- if ($reductionCart->getAvailableQuantityPerUser()) {
- return $reductionCart->getAvailableQuantityPerUser(
- ) - $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart, $user);
- }
-
- return false;
- }
-
- // findAllAvailableForUser / getReductionCartsAvailableByUser
- public function getAvailablesByUser(UserInterface $user)
- {
- $reductionCarts = $this->query->find();
-
- $reductionCartsArray = [];
- foreach ($reductionCarts as $reductionCart) {
- if ($reductionCart->matchWithUser($user)
- && $reductionCart->matchWithGroupUser($user)
- && $this->getRemainingQuantityByUser($reductionCart, $user)
- && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)) {
- $reductionCartsArray[] = $reductionCart;
- }
- }
-
- return $reductionCartsArray;
- }
-
- }
|