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.

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