CreditUtils.php 7.7KB

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