Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

223 linhas
9.6KB

  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){
  162. if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit)) {
  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. }