Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

198 lines
7.4KB

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