You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 satır
6.8KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\CreditHistoryInterface;
  5. use Lc\ShopBundle\Context\MerchantInterface;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\UserInterface;
  8. use Lc\ShopBundle\Context\UserMerchantInterface;
  9. use Lc\ShopBundle\Model\CreditHistory;
  10. class CreditUtils
  11. {
  12. protected $em ;
  13. protected $merchantUtils ;
  14. protected $userMerchantRepository ;
  15. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
  16. {
  17. $this->em = $em ;
  18. $this->merchantUtils = $merchantUtils ;
  19. $this->userMerchantRepository = $this->em->getRepository($this->em->getClassMetadata(UserMerchantInterface::class)->getName()) ;
  20. }
  21. public function getUserMerchant(UserInterface $user, MerchantInterface $merchant = null)
  22. {
  23. $merchant = $this->getMerchant($merchant) ;
  24. $userMerchant = $this->userMerchantRepository->findOneBy([
  25. 'user' => $user,
  26. 'merchant' => $merchant
  27. ]) ;
  28. return $userMerchant ;
  29. }
  30. public function createUserMerchant(UserInterface $user, MerchantInterface $merchant = null)
  31. {
  32. $merchant = $this->getMerchant($merchant) ;
  33. $classUserMerchant = $this->em->getClassMetadata(UserMerchantInterface::class)->getName();
  34. $userMerchant = new $classUserMerchant ;
  35. $userMerchant->setUser($user) ;
  36. $userMerchant->setMerchant($merchant) ;
  37. $userMerchant->setCredit(0) ;
  38. $userMerchant->setCreditActive(true) ;
  39. $this->em->persist($userMerchant) ;
  40. $this->em->flush() ;
  41. return $userMerchant ;
  42. }
  43. public function updateCreditActive($user, $merchant = null, $creditActive = true)
  44. {
  45. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  46. if(!$userMerchant) {
  47. $userMerchant = $this->createUserMerchant($user, $merchant) ;
  48. }
  49. $userMerchant->setCreditActive($creditActive) ;
  50. $this->em->persist($userMerchant) ;
  51. $this->em->flush() ;
  52. return $userMerchant ;
  53. }
  54. public function activeCredit($user, $merchant = null)
  55. {
  56. return $this->updateCreditActive($user, $merchant, true) ;
  57. }
  58. public function unactiveCredit($user, $merchant = null)
  59. {
  60. return $this->updateCreditActive($user, $merchant, false) ;
  61. }
  62. public function checkCreditActive(UserMerchantInterface $userMerchant)
  63. {
  64. if(!$userMerchant || ($userMerchant && !$userMerchant->isCreditActive())) {
  65. throw new \ErrorException("L'utilisateur n'a pas de compte prépayé activé pour ce marchand.") ;
  66. }
  67. return true ;
  68. }
  69. public function createCreditHistory($type, $user, $params = [])
  70. {
  71. $creditHistory = $this->initCreditHistory($type, $user, $params) ;
  72. return $this->saveCreditHistory($creditHistory) ;
  73. }
  74. public function initCreditHistory($type, $user, $params = [])
  75. {
  76. $merchant = isset($params['merchant']) ? $params['merchant'] : null ;
  77. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  78. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  79. if($checkCreditActive) {
  80. $amount = isset($params['amount']) ? $params['amount'] : null ;
  81. $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
  82. $reference = isset($params['reference']) ? $params['reference'] : null ;
  83. $comment = isset($params['comment']) ? $params['comment'] : null ;
  84. $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
  85. $orderRefund = isset($params['orderRefund']) ? $params['orderRefund'] : null ;
  86. // credit history
  87. $classCreditHistory = $this->em->getClassMetadata(CreditHistoryInterface::class)->getName();
  88. $creditHistory = new $classCreditHistory ;
  89. $creditHistory->setType($type) ;
  90. $creditHistory->setUserMerchant($userMerchant) ;
  91. $creditHistory->setAmount($amount) ;
  92. $creditHistory->setMeanPayment($meanPayment) ;
  93. $creditHistory->setReference($reference) ;
  94. $creditHistory->setComment($comment) ;
  95. $creditHistory->setOrderPayment($orderPayment) ;
  96. $creditHistory->setOrderRefund($orderRefund) ;
  97. return $creditHistory ;
  98. }
  99. return false ;
  100. }
  101. public function saveCreditHistory($creditHistory)
  102. {
  103. if($creditHistory) {
  104. $userMerchant = $creditHistory->getUserMerchant() ;
  105. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  106. if($checkCreditActive) {
  107. $this->em->persist($creditHistory) ;
  108. $this->em->flush() ;
  109. if($creditHistory->getType() == CreditHistory::TYPE_CREDIT) {
  110. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited() ;
  111. }
  112. elseif($creditHistory->getType() == CreditHistory::TYPE_DEBIT) {
  113. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited() ;
  114. }
  115. if(isset($userMerchantAmount)) {
  116. $this->updateCredit($userMerchant, $userMerchantAmount) ;
  117. }
  118. return true ;
  119. }
  120. }
  121. return false ;
  122. }
  123. public function updateCredit($userMerchant, $amount)
  124. {
  125. $userMerchant->setCredit($amount) ;
  126. $this->em->persist($userMerchant) ;
  127. $this->em->flush() ;
  128. }
  129. public function getMerchant($merchant)
  130. {
  131. if(!$merchant) {
  132. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  133. }
  134. return $merchant ;
  135. }
  136. }