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.

179 rindas
6.6KB

  1. <?php
  2. namespace common\logic\User\UserProducer\Service;
  3. use common\helpers\MeanPayment;
  4. use common\logic\AbstractBuilder;
  5. use common\logic\Order\Order\Model\Order;
  6. use common\logic\Order\Order\Repository\OrderRepository;
  7. use common\logic\Order\Order\Service\OrderSolver;
  8. use common\logic\Producer\Producer\Model\Producer;
  9. use common\logic\Producer\Producer\Repository\ProducerRepository;
  10. use common\logic\User\CreditHistory\Model\CreditHistory;
  11. use common\logic\User\CreditHistory\Service\CreditHistorySolver;
  12. use common\logic\User\User\Model\User;
  13. use common\logic\User\UserProducer\Model\UserProducer;
  14. use common\logic\User\UserProducer\Repository\UserProducerRepository;
  15. class UserProducerBuilder extends AbstractBuilder
  16. {
  17. protected CreditHistorySolver $creditHistorySolver;
  18. protected UserProducerRepository $userProducerRepository;
  19. protected OrderRepository $orderRepository;
  20. protected ProducerRepository $producerRepository;
  21. protected OrderSolver $orderSolver;
  22. public function loadDependencies(): void
  23. {
  24. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  25. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  26. $this->orderRepository = $this->loadService(OrderRepository::class);
  27. $this->producerRepository = $this->loadService(ProducerRepository::class);
  28. $this->orderSolver = $this->loadService(OrderSolver::class);
  29. }
  30. public function instanciateUserProducer(User $user, Producer $producer, int $bookmark = 1)
  31. {
  32. $userProducer = new UserProducer();
  33. $userProducer->populateUser($user);
  34. $userProducer->populateproducer($producer);
  35. $userProducer->setCredit(0);
  36. $userProducer->setActive(1);
  37. $userProducer->setBookmark($bookmark);
  38. return $userProducer;
  39. }
  40. public function createUserProducer(User $user, Producer $producer, int $bookmark = 1): UserProducer
  41. {
  42. $userProducer = $this->instanciateUserProducer($user, $producer, $bookmark);
  43. $this->create($userProducer);
  44. return $userProducer;
  45. }
  46. public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
  47. {
  48. return $this->userProducerRepository->findOneUserProducer($user)
  49. ?? $this->createUserProducer($user, $producer, $bookmark);
  50. }
  51. public function updateCredit(CreditHistory $creditHistory): void
  52. {
  53. $userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user);
  54. if ($userProducer) {
  55. $oldCredit = $userProducer->getCredit();
  56. $this->deductCredit($userProducer, $creditHistory);
  57. $this->initMeanPaymentOrder($creditHistory);
  58. $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
  59. }
  60. }
  61. public function deductCredit(UserProducer $userProducer, CreditHistory $creditHistory)
  62. {
  63. if ($this->creditHistorySolver->isTypeCredit($creditHistory)) {
  64. $userProducer->setCredit($userProducer->getCredit() + $creditHistory->getAmount());
  65. } elseif ($this->creditHistorySolver->isTypeDebit($creditHistory)) {
  66. $userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
  67. }
  68. $this->update($userProducer);
  69. }
  70. public function initMeanPaymentOrder($creditHistory)
  71. {
  72. if ($creditHistory->id_order && $creditHistory->id_order > 0) {
  73. $order = $this->orderRepository->findOneOrderById((int) $creditHistory->id_order);
  74. if ($order) {
  75. $paymentStatus = $this->orderSolver->getPaymentStatus($order);
  76. if ($paymentStatus == Order::PAYMENT_PAID
  77. || $paymentStatus == Order::PAYMENT_SURPLUS) {
  78. $order->mean_payment = MeanPayment::CREDIT;
  79. $this->update($order);
  80. }
  81. }
  82. }
  83. }
  84. public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
  85. {
  86. $userRepository = \Yii::$app->logic->getUserContainer()->getRepository();
  87. $producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
  88. $newCredit = $userProducer->credit;
  89. if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
  90. $user = $userRepository->findOneUserById($creditHistory->id_user);
  91. $producer = $producerRepository->findOneProducerById($creditHistory->id_producer);
  92. if($user && $user->email && strlen($user->email) > 0) {
  93. \Yii::$app->mailer->compose(
  94. [
  95. 'html' => 'creditLimitReminder-html',
  96. 'text' => 'creditLimitReminder-text'
  97. ],
  98. [
  99. 'user' => $user,
  100. 'producer' => $producer,
  101. 'credit' => $newCredit
  102. ]
  103. )
  104. ->setTo($user->email)
  105. ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
  106. ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
  107. ->send();
  108. }
  109. }
  110. }
  111. public function isCreditLimitCrossed($oldCredit, $newCredit)
  112. {
  113. $creditLimitReminder = $this->producerRepository->getConfig('credit_limit_reminder');
  114. return !is_null($creditLimitReminder) &&
  115. $oldCredit > $creditLimitReminder
  116. && $newCredit <= $creditLimitReminder;
  117. }
  118. public function updateActive(User $user, Producer $producer, bool $active): bool
  119. {
  120. $userProducer = $this->createUserProducerIfNotExist(
  121. $user,
  122. $producer
  123. );
  124. $userProducer->active = $active;
  125. return $this->update($userProducer);
  126. }
  127. public function updateBookmark(UserProducer $userProducer, bool $bookmark)
  128. {
  129. $userProducer->bookmark = $bookmark;
  130. return $this->update($userProducer);
  131. }
  132. public function addProducerBookmark(User $user)
  133. {
  134. $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
  135. return $this->updateBookmark($userProducer, true);
  136. }
  137. public function removeProducerBookmark(User $user)
  138. {
  139. $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
  140. return $this->updateBookmark($userProducer, false);
  141. }
  142. public function unlinkUserProducer(UserProducer $userProducer)
  143. {
  144. $userProducer->active = 0;
  145. $userProducer->bookmark = 0;
  146. return $this->update($userProducer);
  147. }
  148. }