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.

94 lines
2.8KB

  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. public function __construct(
  13. ReductionCreditRepositoryQuery $query
  14. ) {
  15. $this->query = $query;
  16. }
  17. // findReductionCreditsByUser
  18. public function getByTypeAndUser(string $type = ReductionCreditModel::TYPE_CREDIT, UserInterface $user)
  19. {
  20. $query = $this->query->create();
  21. $query
  22. ->filterStatusOnline()
  23. ->filterByType($type)
  24. ->filterByUser($user);
  25. return $query->find();
  26. }
  27. public function getReductionCreditsAvailableByUser(UserInterface $user)
  28. {
  29. $reductionCredits = $this->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
  30. $reductionCreditsArray = [];
  31. foreach ($reductionCredits as $reductionCredit) {
  32. if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  33. $reductionCreditsArray[] = $reductionCredit;
  34. }
  35. }
  36. return $reductionCreditsArray;
  37. }
  38. public function getReductionGiftsAvailableByUser($user)
  39. {
  40. $reductionGifts = $this->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
  41. $reductionGiftsArray = [];
  42. foreach ($reductionGifts as $reductionGift) {
  43. if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
  44. $reductionGiftsArray[] = $reductionGift;
  45. }
  46. }
  47. return $reductionGiftsArray;
  48. }
  49. public function isReductionGiftUsed(ReductionCreditInterface $reductionGift)
  50. {
  51. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. public function isReductionCreditUsed(ReductionCreditInterface $reductionCredit, UserInterface $user = null)
  58. {
  59. if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. public function isReductionCreditAddedToOrder(
  66. OrderShopInterface $orderShop,
  67. ReductionCreditInterface $reductionCredit
  68. ) {
  69. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  70. if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. }