Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

191 Zeilen
7.4KB

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