You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
7.1KB

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