Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CreditUtils.php 3.8KB

pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class CreditUtils
  10. {
  11. protected $em ;
  12. protected $merchantUtils ;
  13. protected $userMerchantRepository ;
  14. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
  15. {
  16. $this->em = $em ;
  17. $this->merchantUtils = $merchantUtils ;
  18. $this->userMerchantRepository = $this->em->getRepository($this->em->getClassMetadata(UserMerchantInterface::class)->getName()) ;
  19. }
  20. public function getUserMerchant(UserInterface $user, MerchantInterface $merchant = null)
  21. {
  22. $merchant = $this->getMerchant($merchant) ;
  23. $userMerchant = $this->userMerchantRepository->findOneBy([
  24. 'user' => $user,
  25. 'merchant' => $merchant
  26. ]) ;
  27. return $userMerchant ;
  28. }
  29. public function createUserMerchant(UserInterface $user, MerchantInterface $merchant = null)
  30. {
  31. $merchant = $this->getMerchant($merchant) ;
  32. $classUserMerchant = $this->em->getClassMetadata(UserMerchantInterface::class)->getName();
  33. $userMerchant = new $classUserMerchant ;
  34. $userMerchant->setUser($user) ;
  35. $userMerchant->setMerchant($merchant) ;
  36. $userMerchant->setCredit(0) ;
  37. $userMerchant->setCreditActive(true) ;
  38. $this->em->persist($userMerchant) ;
  39. $this->em->flush() ;
  40. return $userMerchant ;
  41. }
  42. public function updateCreditActive($user, $merchant = null, $creditActive = true)
  43. {
  44. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  45. if(!$userMerchant) {
  46. $userMerchant = $this->createUserMerchant($user, $merchant) ;
  47. }
  48. $userMerchant->setCreditActive($creditActive) ;
  49. $this->em->persist($userMerchant) ;
  50. $this->em->flush() ;
  51. return $userMerchant ;
  52. }
  53. public function activeCredit($user, $merchant = null)
  54. {
  55. return $this->updateCreditActive($user, $merchant, true) ;
  56. }
  57. public function unactiveCredit($user, $merchant = null)
  58. {
  59. return $this->updateCreditActive($user, $merchant, false) ;
  60. }
  61. public function createCreditHistory($user, $amount, $meanPayment, $comment = null, $merchant = null)
  62. {
  63. $userMerchant = $this->getUserMerchant($user, $merchant) ;
  64. if($userMerchant && $userMerchant->isCreditActive()) {
  65. $classCreditHistory = $this->em->getClassMetadata(CreditHistoryInterface::class)->getName();
  66. $creditHistory = new $classCreditHistory ;
  67. }
  68. return $creditHistory ;
  69. }
  70. public function addCredit($user, $amount, $merchant = null)
  71. {
  72. $merchant = $this->getMerchant($merchant) ;
  73. }
  74. public function removeCredit($user, $amount, $merchant = null)
  75. {
  76. $merchant = $this->getMerchant($merchant) ;
  77. }
  78. public function setCredit($user, $amount, $merchant = null)
  79. {
  80. }
  81. public function getMerchant($merchant)
  82. {
  83. if(!$merchant) {
  84. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  85. }
  86. return $merchant ;
  87. }
  88. }