You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Reduction;
  3. use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
  4. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  5. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  6. use Lc\SovBundle\Model\User\UserInterface;
  7. use Lc\SovBundle\Repository\AbstractStore;
  8. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  9. class ReductionCartStore extends AbstractStore
  10. {
  11. protected ReductionCartRepositoryQuery $query;
  12. protected OrderShopStore $orderShopStore;
  13. protected PriceSolver $priceSolver;
  14. protected FlashBagInterface $flashBag;
  15. public function __construct(
  16. ReductionCartRepositoryQuery $query,
  17. OrderShopStore $orderShopStore,
  18. PriceSolver $priceSolver,
  19. FlashBagInterface $flashBag
  20. ) {
  21. $this->query = $query;
  22. $this->orderShopStore = $orderShopStore;
  23. $this->priceSolver = $priceSolver;
  24. $this->flashBag = $flashBag;
  25. }
  26. // getReductionCartByCode
  27. public function getByCode(string $code)
  28. {
  29. $query = $this->query->create();
  30. $query->filterByCode($code);
  31. $reductionCarts = $query->find();
  32. $findReductionCart = null;
  33. foreach ($reductionCarts as $reductionCart) {
  34. if ($reductionCart && in_array($code, $reductionCart->getCodes())) {
  35. $findReductionCart = $reductionCart;
  36. }
  37. }
  38. return $findReductionCart;
  39. }
  40. // getReductionCartRemainingQuantity
  41. public function getRemainingQuantity(ReductionCartInterface $reductionCart): float
  42. {
  43. return $reductionCart->getAvailableQuantity() - $this->orderShopStore->countValidWithReductionCart(
  44. $reductionCart
  45. );
  46. }
  47. // getReductionCartUsedQuantityPerUser
  48. public function getUsedQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
  49. {
  50. return $this->orderShopStore->countValidWithReductionCartByUser($reductionCart, $user);
  51. }
  52. // getReductionCartUsedQuantity
  53. public function getUsedQuantity(ReductionCartInterface $reductionCart): float
  54. {
  55. return $this->orderShopStore->countValidWithReductionCart($reductionCart);
  56. }
  57. // getReductionCartRemainingQuantityPerUser
  58. public function getRemainingQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
  59. {
  60. if ($reductionCart->getAvailableQuantityPerUser()) {
  61. return $reductionCart->getAvailableQuantityPerUser(
  62. ) - $this->orderShopStore->countValidWithReductionCartByUser($reductionCart, $user);
  63. }
  64. return false;
  65. }
  66. // findAllAvailableForUser / getReductionCartsAvailableByUser
  67. public function getAvailableByUser(UserInterface $user)
  68. {
  69. $reductionCarts = $this->query->find();
  70. $reductionCartsArray = [];
  71. foreach ($reductionCarts as $reductionCart) {
  72. if ($reductionCart->matchWithUser($user)
  73. && $reductionCart->matchWithGroupUser($user)
  74. && $this->getRemainingQuantityByUser($reductionCart, $user)
  75. && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)) {
  76. $reductionCartsArray[] = $reductionCart;
  77. }
  78. }
  79. return $reductionCartsArray;
  80. }
  81. }