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.

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