Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

199 linhas
7.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. 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 isCreditActiveByUser(UserInterface $user, MerchantInterface $merchant = null){
  72. $userMerchant = $this->getUserMerchant($user, $merchant);
  73. return $this->isCreditActive($userMerchant);
  74. }
  75. public function isCreditActive(UserMerchantInterface $userMerchant = null){
  76. if(!$userMerchant || ($userMerchant && !$userMerchant->isCreditActive())) {
  77. return false ;
  78. }
  79. return true ;
  80. }
  81. public function isCreditSufficientToPay($userMerchant, $amount)
  82. {
  83. if($this->isCreditActive($userMerchant) && $userMerchant->getCredit() >= $amount) {
  84. return true ;
  85. }
  86. return false;
  87. }
  88. public function createCreditHistory($type, $user, $params = [])
  89. {
  90. $creditHistory = $this->initCreditHistory($type, $user, $params) ;
  91. return $this->saveCreditHistory($creditHistory) ;
  92. }
  93. public function initCreditHistory($type, $user, $params = [])
  94. {
  95. $merchant = isset($params['merchant']) ? $params['merchant'] : null ;
  96. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  97. $isCreditActive = $this->isCreditActive($userMerchant) ;
  98. if($isCreditActive) {
  99. $amount = isset($params['amount']) ? $params['amount'] : null ;
  100. $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
  101. $reference = isset($params['reference']) ? $params['reference'] : null ;
  102. $comment = isset($params['comment']) ? $params['comment'] : null ;
  103. $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
  104. $orderRefund = isset($params['orderRefund']) ? $params['orderRefund'] : null ;
  105. // credit history
  106. $classCreditHistory = $this->em->getClassMetadata(CreditHistoryInterface::class)->getName();
  107. $creditHistory = new $classCreditHistory ;
  108. $creditHistory->setType($type) ;
  109. $creditHistory->setUserMerchant($userMerchant) ;
  110. $creditHistory->setAmount($amount) ;
  111. $creditHistory->setMeanPayment($meanPayment) ;
  112. $creditHistory->setReference($reference) ;
  113. $creditHistory->setComment($comment) ;
  114. $creditHistory->setOrderPayment($orderPayment) ;
  115. $creditHistory->setOrderRefund($orderRefund) ;
  116. return $creditHistory ;
  117. }
  118. return false ;
  119. }
  120. public function saveCreditHistory($creditHistory)
  121. {
  122. if($creditHistory) {
  123. $userMerchant = $creditHistory->getUserMerchant() ;
  124. $isCreditActive = $this->isCreditActive($userMerchant) ;
  125. if($isCreditActive) {
  126. $this->em->persist($creditHistory) ;
  127. $this->em->flush() ;
  128. if($creditHistory->getType() == CreditHistory::TYPE_CREDIT) {
  129. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited() ;
  130. }
  131. elseif($creditHistory->getType() == CreditHistory::TYPE_DEBIT) {
  132. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited() ;
  133. }
  134. if($this->isCreditSufficientToPay($userMerchant,$userMerchantAmount)) {
  135. $this->updateCredit($userMerchant, $userMerchantAmount) ;
  136. return true ;
  137. }else{
  138. $this->utils->addFlash('success', 'error.credit.notEnoughCredit');
  139. return false ;
  140. }
  141. }
  142. }
  143. return false ;
  144. }
  145. public function updateCredit($userMerchant, $amount)
  146. {
  147. $userMerchant->setCredit($amount) ;
  148. $this->em->persist($userMerchant) ;
  149. $this->em->flush() ;
  150. }
  151. public function getMerchant($merchant)
  152. {
  153. if(!$merchant) {
  154. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  155. }
  156. return $merchant ;
  157. }
  158. }