Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

186 lines
6.9KB

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