您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OrderUtilsReductionTrait.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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($reductionCart);
  92. }
  93. public function getReductionCartUsedQuantityPerUser($reductionCart, $user) :float
  94. {
  95. return $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  96. }
  97. public function getReductionCartUsedQuantity($reductionCart) :float
  98. {
  99. return $this->orderShopRepo->countValidOrderWithReductionCart($reductionCart);
  100. }
  101. public function getReductionCartRemainingQuantityPerUser($reductionCart, $user) :float
  102. {
  103. if ($reductionCart->getAvailableQuantityPerUser()) {
  104. return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  105. }
  106. return false;
  107. }
  108. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  109. {
  110. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  111. $orderReductionCredit = new $orderReductionCreditClass->name;
  112. $orderReductionCredit->setOrderShop($orderShop);
  113. $orderReductionCredit->setReductionCredit($reductionCredit);
  114. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  115. $orderReductionCredit->setValue($reductionCredit->getValue());
  116. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  117. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  118. $orderReductionCredit->setType($reductionCredit->getType());
  119. $orderShop->addOrderReductionCredit($orderReductionCredit) ;
  120. if($this->isOrderShopPositiveAmount($orderShop)) {
  121. $this->em->persist($orderReductionCredit);
  122. $this->em->flush();
  123. return $orderReductionCredit;
  124. }
  125. return false ;
  126. }
  127. public function getReductionCartsAvailableByUser($user)
  128. {
  129. $reductionCartRepository = $this->em->getRepository(ReductionCartInterface::class) ;
  130. return $reductionCartRepository->findAllAvailableForUser($user);
  131. }
  132. public function getReductionCreditsAvailableByUser($user)
  133. {
  134. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  135. $reductionCreditsArray = [] ;
  136. foreach($reductionCredits as $reductionCredit) {
  137. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  138. $reductionCreditsArray[] = $reductionCredit ;
  139. }
  140. }
  141. return $reductionCreditsArray ;
  142. }
  143. public function getReductionGiftsAvailableByUser($user)
  144. {
  145. $reductionGifts = $this->reductionCreditRepo->findReductionGiftToUseByUser($user) ;
  146. $reductionGiftsArray = [] ;
  147. foreach($reductionGifts as $reductionGift) {
  148. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
  149. $reductionGiftsArray[] = $reductionGift ;
  150. }
  151. }
  152. return $reductionGiftsArray ;
  153. }
  154. public function isReductionGiftUsed($reductionGift){
  155. if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
  156. return true;
  157. }else{
  158. return false;
  159. }
  160. }
  161. public function isReductionCreditUsed($reductionCredit, $user = false){
  162. if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  163. return true;
  164. }else{
  165. return false;
  166. }
  167. }
  168. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  169. {
  170. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  171. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  172. return true ;
  173. }
  174. }
  175. return false ;
  176. }
  177. }