158 líneas
6.8KB

  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 compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
  11. {
  12. return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
  13. || ($orderProductReductionCatalog1
  14. && $orderProductReductionCatalog2
  15. && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
  16. && (string)$orderProductReductionCatalog1->getValue() == (string)$orderProductReductionCatalog2->getValue()
  17. && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate());
  18. }
  19. public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
  20. {
  21. $text = '';
  22. if ($orderProductReductionCatalog) {
  23. if ($orderProductReductionCatalog->getUnit() == 'amount') {
  24. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;€';
  25. }
  26. if ($orderProductReductionCatalog->getUnit() == 'percent') {
  27. $text .= '- ' . $orderProductReductionCatalog->getValue() . '&nbsp;%';
  28. }
  29. }
  30. return $text;
  31. }
  32. public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart)
  33. {
  34. $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
  35. $orderReductionCart = new $orderReductionCartClass->name;
  36. $orderReductionCart->setOrderShop($orderShop);
  37. $orderReductionCart->setReductionCart($reductionCart);
  38. $orderReductionCart->setTitle($reductionCart->getTitle());
  39. $orderReductionCart->setValue($reductionCart->getValue());
  40. $orderReductionCart->setUnit($reductionCart->getUnit());
  41. $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
  42. $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
  43. $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
  44. $orderReductionCart->setType($reductionCart->getType());
  45. $orderShop->addOrderReductionCart($orderReductionCart) ;
  46. if($this->isOrderShopPositiveAmount($orderShop)) {
  47. $this->em->persist($orderReductionCart);
  48. $this->em->flush();
  49. return $orderReductionCart ;
  50. }
  51. return false;
  52. }
  53. public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  54. {
  55. $user = $orderShop->getUser() ;
  56. // appartient à l'utilisateur
  57. if(!$reductionCredit->getUsers()->contains($user)) {
  58. $this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
  59. return false ;
  60. }
  61. // n'a pas été utilisé
  62. if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
  63. $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  64. return false;
  65. }
  66. return true;
  67. }
  68. public function getReductionCartRemainingQuantity($reductionCart) :float
  69. {
  70. return $reductionCart->getAvailableQuantity() - $this->orderShopRepo->countValidOrderWithReductionCart();
  71. }
  72. public function getReductionCartRemainingQuantityPerUser($reductionCart) :float
  73. {
  74. return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser();
  75. }
  76. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  77. {
  78. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  79. $orderReductionCredit = new $orderReductionCreditClass->name;
  80. $orderReductionCredit->setOrderShop($orderShop);
  81. $orderReductionCredit->setReductionCredit($reductionCredit);
  82. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  83. $orderReductionCredit->setValue($reductionCredit->getValue());
  84. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  85. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  86. $orderShop->addOrderReductionCredit($orderReductionCredit) ;
  87. if($this->isOrderShopPositiveAmount($orderShop)) {
  88. $this->em->persist($orderReductionCredit);
  89. $this->em->flush();
  90. return $orderReductionCredit;
  91. }
  92. return false ;
  93. }
  94. /*public function getReductionCreditsAvailable($order)
  95. {
  96. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  97. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  98. foreach ($reductionCredits as $reductionCredit){
  99. }
  100. }*/
  101. public function getReductionCreditsAvailableByUser($user)
  102. {
  103. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  104. $reductionCreditsArray = [] ;
  105. foreach($reductionCredits as $reductionCredit) {
  106. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  107. $reductionCreditsArray[] = $reductionCredit ;
  108. }
  109. }
  110. return $reductionCreditsArray ;
  111. }
  112. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  113. {
  114. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  115. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  116. return true ;
  117. }
  118. }
  119. return false ;
  120. }
  121. }