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.

178 lines
7.1KB

  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 isCreditActive(UserInterface $user, MerchantInterface $merchant = null){
  63. $userMerchant = $this->getUserMerchant($user, $merchant);
  64. if($userMerchant && $userMerchant->isCreditActive()) {
  65. return true;
  66. }else{
  67. return false;
  68. }
  69. }
  70. public function checkCreditActive(UserMerchantInterface $userMerchant)
  71. {
  72. if(!$userMerchant || ($userMerchant && !$userMerchant->isCreditActive())) {
  73. throw new \ErrorException("L'utilisateur n'a pas de compte prépayé activé pour ce marchand.") ;
  74. }
  75. return true ;
  76. }
  77. public function createCreditHistory($type, $user, $params = [])
  78. {
  79. $creditHistory = $this->initCreditHistory($type, $user, $params) ;
  80. return $this->saveCreditHistory($creditHistory) ;
  81. }
  82. public function initCreditHistory($type, $user, $params = [])
  83. {
  84. $merchant = isset($params['merchant']) ? $params['merchant'] : null ;
  85. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  86. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  87. if($checkCreditActive) {
  88. $amount = isset($params['amount']) ? $params['amount'] : null ;
  89. $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
  90. $reference = isset($params['reference']) ? $params['reference'] : null ;
  91. $comment = isset($params['comment']) ? $params['comment'] : null ;
  92. $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
  93. $orderRefund = isset($params['orderRefund']) ? $params['orderRefund'] : null ;
  94. // credit history
  95. $classCreditHistory = $this->em->getClassMetadata(CreditHistoryInterface::class)->getName();
  96. $creditHistory = new $classCreditHistory ;
  97. $creditHistory->setType($type) ;
  98. $creditHistory->setUserMerchant($userMerchant) ;
  99. $creditHistory->setAmount($amount) ;
  100. $creditHistory->setMeanPayment($meanPayment) ;
  101. $creditHistory->setReference($reference) ;
  102. $creditHistory->setComment($comment) ;
  103. $creditHistory->setOrderPayment($orderPayment) ;
  104. $creditHistory->setOrderRefund($orderRefund) ;
  105. return $creditHistory ;
  106. }
  107. return false ;
  108. }
  109. public function saveCreditHistory($creditHistory)
  110. {
  111. if($creditHistory) {
  112. $userMerchant = $creditHistory->getUserMerchant() ;
  113. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  114. if($checkCreditActive) {
  115. $this->em->persist($creditHistory) ;
  116. $this->em->flush() ;
  117. if($creditHistory->getType() == CreditHistory::TYPE_CREDIT) {
  118. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited() ;
  119. }
  120. elseif($creditHistory->getType() == CreditHistory::TYPE_DEBIT) {
  121. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited() ;
  122. }
  123. if(isset($userMerchantAmount)) {
  124. $this->updateCredit($userMerchant, $userMerchantAmount) ;
  125. }
  126. return true ;
  127. }
  128. }
  129. return false ;
  130. }
  131. public function updateCredit($userMerchant, $amount)
  132. {
  133. $userMerchant->setCredit($amount) ;
  134. $this->em->persist($userMerchant) ;
  135. $this->em->flush() ;
  136. }
  137. public function getMerchant($merchant)
  138. {
  139. if(!$merchant) {
  140. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  141. }
  142. return $merchant ;
  143. }
  144. }