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.

125 lines
4.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Reduction;
  3. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  4. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  5. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditModel;
  6. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  7. use Lc\SovBundle\Model\User\UserInterface;
  8. use Lc\SovBundle\Repository\AbstractStore;
  9. class ReductionCreditStore extends AbstractStore
  10. {
  11. protected ReductionCreditRepositoryQuery $query;
  12. protected OrderShopStore $orderShopStore;
  13. public function __construct(
  14. ReductionCreditRepositoryQuery $query,
  15. OrderShopStore $orderShopStore
  16. ) {
  17. $this->query = $query;
  18. $this->orderShopStore = $orderShopStore;
  19. }
  20. // isReductionCreditAllowAddToOrder
  21. public function isAllowAddToOrder(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  22. {
  23. $user = $orderShop->getUser();
  24. // appartient à l'utilisateur
  25. if (!$reductionCredit->getUsers()->contains($user)) {
  26. // @TODO : gérer les addFlash dans le controleur
  27. //$this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
  28. return false;
  29. }
  30. // n'a pas été utilisé
  31. if ($reductionCredit->getType() == ReductionCredit::TYPE_CREDIT) {
  32. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
  33. //$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  34. return false;
  35. }
  36. } else {
  37. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit) > 0) {
  38. //$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. // findReductionCreditsByUser
  45. public function getByTypeAndUser(string $type = ReductionCreditModel::TYPE_CREDIT, UserInterface $user)
  46. {
  47. $query = $this->query->create();
  48. $query
  49. ->filterStatusOnline()
  50. ->filterByType($type)
  51. ->filterByUser($user);
  52. return $query->find();
  53. }
  54. public function getReductionCreditsAvailableByUser(UserInterface $user)
  55. {
  56. $reductionCredits = $this->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
  57. $reductionCreditsArray = [];
  58. foreach ($reductionCredits as $reductionCredit) {
  59. if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  60. $reductionCreditsArray[] = $reductionCredit;
  61. }
  62. }
  63. return $reductionCreditsArray;
  64. }
  65. public function getReductionGiftsAvailableByUser($user)
  66. {
  67. $reductionGifts = $this->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
  68. $reductionGiftsArray = [];
  69. foreach ($reductionGifts as $reductionGift) {
  70. if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
  71. $reductionGiftsArray[] = $reductionGift;
  72. }
  73. }
  74. return $reductionGiftsArray;
  75. }
  76. public function isReductionGiftUsed(ReductionCreditInterface $reductionGift)
  77. {
  78. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. public function isReductionCreditUsed(ReductionCreditInterface $reductionCredit, UserInterface $user = null)
  85. {
  86. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. public function isReductionCreditAddedToOrder(
  93. OrderShopInterface $orderShop,
  94. ReductionCreditInterface $reductionCredit
  95. ) {
  96. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  97. if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. }