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.

177 satır
6.9KB

  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, $merchant = null, $creditActive = true)
  50. {
  51. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  52. if(!$userMerchant) {
  53. $userMerchant = $this->createUserMerchant($user, $merchant) ;
  54. }
  55. $userMerchant->setCreditActive($creditActive) ;
  56. $this->em->persist($userMerchant) ;
  57. $this->em->flush() ;
  58. return $userMerchant ;
  59. }
  60. public function activeCredit($user, $merchant = null)
  61. {
  62. return $this->updateCreditActive($user, $merchant, true) ;
  63. }
  64. public function unactiveCredit($user, $merchant = null)
  65. {
  66. return $this->updateCreditActive($user, $merchant, false) ;
  67. }
  68. public function checkCreditActive($userMerchant)
  69. {
  70. if(!$userMerchant || ($userMerchant && !$userMerchant->isCreditActive())) {
  71. return false ;
  72. }
  73. return true ;
  74. }
  75. public function createCreditHistory($type, $user, $params = [])
  76. {
  77. $creditHistory = $this->initCreditHistory($type, $user, $params) ;
  78. return $this->saveCreditHistory($creditHistory) ;
  79. }
  80. public function initCreditHistory($type, $user, $params = [])
  81. {
  82. $merchant = isset($params['merchant']) ? $params['merchant'] : null ;
  83. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  84. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  85. if($checkCreditActive) {
  86. $amount = isset($params['amount']) ? $params['amount'] : null ;
  87. $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
  88. $reference = isset($params['reference']) ? $params['reference'] : null ;
  89. $comment = isset($params['comment']) ? $params['comment'] : null ;
  90. $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
  91. $orderRefund = isset($params['orderRefund']) ? $params['orderRefund'] : null ;
  92. // credit history
  93. $classCreditHistory = $this->em->getClassMetadata(CreditHistoryInterface::class)->getName();
  94. $creditHistory = new $classCreditHistory ;
  95. $creditHistory->setType($type) ;
  96. $creditHistory->setUserMerchant($userMerchant) ;
  97. $creditHistory->setAmount($amount) ;
  98. $creditHistory->setMeanPayment($meanPayment) ;
  99. $creditHistory->setReference($reference) ;
  100. $creditHistory->setComment($comment) ;
  101. $creditHistory->setOrderPayment($orderPayment) ;
  102. $creditHistory->setOrderRefund($orderRefund) ;
  103. return $creditHistory ;
  104. }
  105. return false ;
  106. }
  107. public function saveCreditHistory($creditHistory)
  108. {
  109. if($creditHistory) {
  110. $userMerchant = $creditHistory->getUserMerchant() ;
  111. $checkCreditActive = $this->checkCreditActive($userMerchant) ;
  112. if($checkCreditActive) {
  113. $this->em->persist($creditHistory) ;
  114. $this->em->flush() ;
  115. if($creditHistory->getType() == CreditHistory::TYPE_CREDIT) {
  116. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited() ;
  117. }
  118. elseif($creditHistory->getType() == CreditHistory::TYPE_DEBIT) {
  119. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited() ;
  120. }
  121. if(isset($userMerchantAmount)) {
  122. $this->updateCredit($userMerchant, $userMerchantAmount) ;
  123. }
  124. return true ;
  125. }
  126. }
  127. return false ;
  128. }
  129. public function updateCredit($userMerchant, $amount)
  130. {
  131. $userMerchant->setCredit($amount) ;
  132. $this->em->persist($userMerchant) ;
  133. $this->em->flush() ;
  134. }
  135. public function getMerchant($merchant)
  136. {
  137. if(!$merchant) {
  138. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  139. }
  140. return $merchant ;
  141. }
  142. }