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.

90 lines
3.0KB

  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\SovBundle\Model\User\UserInterface;
  6. use Lc\SovBundle\Repository\AbstractStore;
  7. class ReductionCartStore extends AbstractStore
  8. {
  9. protected ReductionCartRepositoryQuery $query;
  10. protected OrderShopStore $orderShopStore;
  11. public function __construct(
  12. ReductionCartRepositoryQuery $query,
  13. OrderShopStore $orderShopStore
  14. ) {
  15. $this->query = $query;
  16. $this->orderShopStore = $orderShopStore;
  17. }
  18. // getReductionCartByCode
  19. public function getByCode(string $code)
  20. {
  21. $query = $this->query->create();
  22. $query->filterByCode($code);
  23. $reductionCarts = $query->find();
  24. $findReductionCart = null;
  25. foreach ($reductionCarts as $reductionCart) {
  26. if ($reductionCart && in_array($code, $reductionCart->getCodes())) {
  27. $findReductionCart = $reductionCart;
  28. }
  29. }
  30. return $findReductionCart;
  31. }
  32. // getReductionCartRemainingQuantity
  33. public function getRemainingQuantity(ReductionCartInterface $reductionCart): float
  34. {
  35. return $reductionCart->getAvailableQuantity() - $this->orderShopStore->countValidOrderWithReductionCart(
  36. $reductionCart
  37. );
  38. }
  39. // getReductionCartUsedQuantityPerUser
  40. public function getUsedQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
  41. {
  42. return $this->orderShopStore->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  43. }
  44. // getReductionCartUsedQuantity
  45. public function getUsedQuantity(ReductionCartInterface $reductionCart): float
  46. {
  47. return $this->orderShopStore->countValidOrderWithReductionCart($reductionCart);
  48. }
  49. // getReductionCartRemainingQuantityPerUser
  50. public function getRemainingQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
  51. {
  52. if ($reductionCart->getAvailableQuantityPerUser()) {
  53. return $reductionCart->getAvailableQuantityPerUser(
  54. ) - $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  55. }
  56. return false;
  57. }
  58. // findAllAvailableForUser / getReductionCartsAvailableByUser
  59. public function getAvailablesByUser(UserInterface $user)
  60. {
  61. $reductionCarts = $this->query->find();
  62. $reductionCartsArray = [];
  63. foreach ($reductionCarts as $reductionCart) {
  64. if ($reductionCart->matchWithUser($user)
  65. && $reductionCart->matchWithGroupUser($user)
  66. && $this->getRemainingQuantityByUser($reductionCart, $user)
  67. && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)) {
  68. $reductionCartsArray[] = $reductionCart;
  69. }
  70. }
  71. return $reductionCartsArray;
  72. }
  73. }