No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

OrderUtilsReductionTrait.php 10KB

hace 4 años
hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {
  62. $this->em->persist($orderReductionCart);
  63. $this->em->flush();
  64. return $orderReductionCart ;
  65. }
  66. else {
  67. $orderShop->removeOrderReductionCart($orderReductionCart) ;
  68. return false;
  69. }
  70. }
  71. public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  72. {
  73. $user = $orderShop->getUser() ;
  74. // appartient à l'utilisateur
  75. if(!$reductionCredit->getUsers()->contains($user)) {
  76. $this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
  77. return false ;
  78. }
  79. // n'a pas été utilisé
  80. if($reductionCredit->getType()== ReductionCredit::TYPE_CREDIT){
  81. if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
  82. $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  83. return false;
  84. }
  85. }else{
  86. if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit) > 0) {
  87. $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. public function getReductionCartRemainingQuantity($reductionCart) :float
  94. {
  95. return $reductionCart->getAvailableQuantity() - $this->orderShopRepo->countValidOrderWithReductionCart($reductionCart);
  96. }
  97. public function getReductionCartUsedQuantityPerUser($reductionCart, $user) :float
  98. {
  99. return $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  100. }
  101. public function getReductionCartUsedQuantity($reductionCart) :float
  102. {
  103. return $this->orderShopRepo->countValidOrderWithReductionCart($reductionCart);
  104. }
  105. public function getReductionCartRemainingQuantityPerUser($reductionCart, $user) :float
  106. {
  107. if ($reductionCart->getAvailableQuantityPerUser()) {
  108. return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
  109. }
  110. return false;
  111. }
  112. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  113. {
  114. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  115. $orderReductionCredit = new $orderReductionCreditClass->name;
  116. $orderReductionCredit->setOrderShop($orderShop);
  117. $orderReductionCredit->setReductionCredit($reductionCredit);
  118. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  119. $orderReductionCredit->setValue($reductionCredit->getValue());
  120. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  121. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  122. $orderReductionCredit->setType($reductionCredit->getType());
  123. $orderShop->addOrderReductionCredit($orderReductionCredit) ;
  124. if($this->isOrderShopPositiveAmount($orderShop)
  125. && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {
  126. $this->em->persist($orderReductionCredit);
  127. $this->em->flush();
  128. return $orderReductionCredit;
  129. }
  130. else {
  131. $orderShop->removeOrderReductionCredit($orderReductionCredit) ;
  132. return false;
  133. }
  134. }
  135. public function getReductionCartsAvailableByUser($user)
  136. {
  137. $reductionCartRepository = $this->em->getRepository(ReductionCartInterface::class) ;
  138. return $reductionCartRepository->findAllAvailableForUser($user);
  139. }
  140. public function getReductionCreditsAvailableByUser($user)
  141. {
  142. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  143. $reductionCreditsArray = [] ;
  144. foreach($reductionCredits as $reductionCredit) {
  145. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  146. $reductionCreditsArray[] = $reductionCredit ;
  147. }
  148. }
  149. return $reductionCreditsArray ;
  150. }
  151. public function getReductionGiftsAvailableByUser($user)
  152. {
  153. $reductionGifts = $this->reductionCreditRepo->findReductionGiftToUseByUser($user) ;
  154. $reductionGiftsArray = [] ;
  155. foreach($reductionGifts as $reductionGift) {
  156. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
  157. $reductionGiftsArray[] = $reductionGift ;
  158. }
  159. }
  160. return $reductionGiftsArray ;
  161. }
  162. public function isReductionGiftUsed($reductionGift){
  163. if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
  164. return true;
  165. }else{
  166. return false;
  167. }
  168. }
  169. public function isReductionCreditUsed($reductionCredit, $user = false){
  170. if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  171. return true;
  172. }else{
  173. return false;
  174. }
  175. }
  176. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  177. {
  178. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  179. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  180. return true ;
  181. }
  182. }
  183. return false ;
  184. }
  185. }