149 lines
6.3KB

  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. $this->em->persist($orderReductionCart);
  46. $this->em->flush();
  47. return $orderReductionCart;
  48. }
  49. public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  50. {
  51. $user = $orderShop->getUser() ;
  52. // appartient à l'utilisateur
  53. if(!$reductionCredit->getUsers()->contains($user)) {
  54. $this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
  55. return false ;
  56. }
  57. // n'a pas été utilisé
  58. if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
  59. $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
  60. return false;
  61. }
  62. return true;
  63. }
  64. public function getReductionCartRemainingQuantity($reductionCart) :float
  65. {
  66. return $reductionCart->getAvailableQuantity() - $this->orderShopRepo->countValidOrderWithReductionCart();
  67. }
  68. public function getReductionCartRemainingQuantityPerUser($reductionCart) :float
  69. {
  70. return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser();
  71. }
  72. public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
  73. {
  74. $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
  75. $orderReductionCredit = new $orderReductionCreditClass->name;
  76. $orderReductionCredit->setOrderShop($orderShop);
  77. $orderReductionCredit->setReductionCredit($reductionCredit);
  78. $orderReductionCredit->setTitle($reductionCredit->getTitle());
  79. $orderReductionCredit->setValue($reductionCredit->getValue());
  80. $orderReductionCredit->setUnit($reductionCredit->getUnit());
  81. $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
  82. $this->em->persist($orderReductionCredit);
  83. $this->em->flush();
  84. return $orderReductionCredit;
  85. }
  86. /*public function getReductionCreditsAvailable($order)
  87. {
  88. $reductionCreditRepo = $this->em->getRepository(ReductionCreditInterface::class);
  89. $reductionCredits = $reductionCreditRepo->getReductionCreditByUser($order->getUser());
  90. foreach ($reductionCredits as $reductionCredit){
  91. }
  92. }*/
  93. public function getReductionCreditsAvailableByUser($user)
  94. {
  95. $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
  96. $reductionCreditsArray = [] ;
  97. foreach($reductionCredits as $reductionCredit) {
  98. if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
  99. $reductionCreditsArray[] = $reductionCredit ;
  100. }
  101. }
  102. return $reductionCreditsArray ;
  103. }
  104. public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
  105. {
  106. foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  107. if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  108. return true ;
  109. }
  110. }
  111. return false ;
  112. }
  113. }