Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

187 lines
8.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  4. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  5. use Lc\ShopBundle\Context\OrderShopInterface;
  6. use Lc\ShopBundle\Context\ReductionCartInterface;
  7. use Lc\ShopBundle\Context\ReductionCreditInterface;
  8. trait OrderUtilsReductionTrait
  9. {
  10. public function getReductionCartByCode($code){
  11. $reductionCartRepository = $this->em->getRepository(ReductionCartInterface::class);
  12. $reductionCarts = $reductionCartRepository->findByCode($code);
  13. $findReductionCart = null;
  14. foreach ($reductionCarts as $reductionCart) {
  15. if ($reductionCart && in_array($code, $reductionCart->getCodes())) {
  16. $findReductionCart = $reductionCart;
  17. }
  18. }
  19. return $findReductionCart;
  20. }
  21. public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  22. {
  23. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  24. || ($orderProductReductionCatalog1
  25. && $orderProductReductionCatalog2
  26. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  27. && (string)$orderProductReductionCatalog1->getValue() == (string)$orderProductReductionCatalog2->getValue()
  28. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate());
  29. }
  30. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  31. {
  32. $text = '';
  33. if ($orderProductReductionCatalog) {
  34. if ($orderProductReductionCatalog->getUnit() == 'amount') {
  35. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;€';
  36. }
  37. if ($orderProductReductionCatalog->getUnit() == 'percent') {
  38. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;%';
  39. }
  40. }
  41. return $text;
  42. }
  43. public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart, $code = null)
  44. {
  45. $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
  46. $orderReductionCart = new $orderReductionCartClass->name;
  47. $orderReductionCart->setOrderShop($orderShop);
  48. $orderReductionCart->setReductionCart($reductionCart);
  49. $orderReductionCart->setTitle($reductionCart->getTitle());
  50. $orderReductionCart->setValue($reductionCart->getValue());
  51. $orderReductionCart->setUnit($reductionCart->getUnit());
  52. $orderReductionCart->setCodeUsed($code);
  53. $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
  54. $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
  55. $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
  56. $orderReductionCart->setType($reductionCart->getType());
  57. $orderShop->addOrderReductionCart($orderReductionCart) ;
  58. if($this->isOrderShopPositiveAmount($orderShop)) {
  59. $this->em->persist($orderReductionCart);
  60. $this->em->flush();
  61. return $orderReductionCart ;
  62. }
  63. return false;
  64. }
  65. public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  66. {
  67. $user = $orderShop->getUser() ;
  68. // appartient à l'utilisateur
  69. if(!$reductionCredit->getUsers()->contains($user)) {
  70. $this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
  71. return false ;
  72. }
  73. // n'a pas été utilisé
  74. if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
  75. $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  76. return false;
  77. }
  78. return true;
  79. }
  80. public function getReductionCartRemainingQuantity($reductionCart) :float
  81. {
  82. return $reductionCart->getAvailableQuantity() - $this->orderShopRepo->countValidOrderWithReductionCart();
  83. }
  84. public function getReductionCartRemainingQuantityPerUser($reductionCart) :float
  85. {
  86. return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser();
  87. }
  88. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  89. {
  90. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  91. $orderReductionCredit = new $orderReductionCreditClass->name;
  92. $orderReductionCredit->setOrderShop($orderShop);
  93. $orderReductionCredit->setReductionCredit($reductionCredit);
  94. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  95. $orderReductionCredit->setValue($reductionCredit->getValue());
  96. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  97. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  98. $orderReductionCredit->setType($reductionCredit->getType());
  99. $orderShop->addOrderReductionCredit($orderReductionCredit) ;
  100. if($this->isOrderShopPositiveAmount($orderShop)) {
  101. $this->em->persist($orderReductionCredit);
  102. $this->em->flush();
  103. return $orderReductionCredit;
  104. }
  105. return false ;
  106. }
  107. /*public function getReductionCreditsAvailable($order)
  108. {
  109. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  110. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  111. foreach ($reductionCredits as $reductionCredit){
  112. }
  113. }*/
  114. public function getReductionCreditsAvailableByUser($user)
  115. {
  116. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  117. $reductionCreditsArray = [] ;
  118. foreach($reductionCredits as $reductionCredit) {
  119. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  120. $reductionCreditsArray[] = $reductionCredit ;
  121. }
  122. }
  123. return $reductionCreditsArray ;
  124. }
  125. public function getReductionGiftsAvailableByUser($user)
  126. {
  127. $reductionGifts = $this->reductionCreditRepo->findReductionGiftToUseByUser($user) ;
  128. $reductionGiftsArray = [] ;
  129. foreach($reductionGifts as $reductionGift) {
  130. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift, $user)) {
  131. $reductionGiftsArray[] = $reductionGift ;
  132. }
  133. }
  134. return $reductionGiftsArray ;
  135. }
  136. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  137. {
  138. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  139. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  140. return true ;
  141. }
  142. }
  143. return false ;
  144. }
  145. }