Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

OrderUtilsReductionTrait.php 8.8KB

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