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.

216 rindas
7.9KB

  1. <?php
  2. namespace domain\User\UserProducer;
  3. use common\helpers\MeanPayment;
  4. use domain\Communication\Email\Email;
  5. use domain\Order\Order\Order;
  6. use domain\Order\Order\OrderRepository;
  7. use domain\Order\Order\OrderSolver;
  8. use domain\Payment\Payment;
  9. use domain\Payment\PaymentSolver;
  10. use domain\Producer\Producer\Producer;
  11. use domain\Producer\Producer\ProducerRepository;
  12. use domain\Producer\Producer\ProducerSolver;
  13. use domain\User\User\User;
  14. use domain\User\User\UserRepository;
  15. use domain\_\AbstractBuilder;
  16. class UserProducerBuilder extends AbstractBuilder
  17. {
  18. protected PaymentSolver $paymentSolver;
  19. protected UserProducerRepository $userProducerRepository;
  20. protected OrderRepository $orderRepository;
  21. protected ProducerRepository $producerRepository;
  22. protected OrderSolver $orderSolver;
  23. protected UserRepository $userRepository;
  24. protected ProducerSolver $producerSolver;
  25. public function loadDependencies(): void
  26. {
  27. $this->paymentSolver = $this->loadService(PaymentSolver::class);
  28. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  29. $this->orderRepository = $this->loadService(OrderRepository::class);
  30. $this->producerRepository = $this->loadService(ProducerRepository::class);
  31. $this->orderSolver = $this->loadService(OrderSolver::class);
  32. $this->userRepository = $this->loadService(UserRepository::class);
  33. $this->producerSolver = $this->loadService(ProducerSolver::class);
  34. }
  35. public function instanciateUserProducer(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true)
  36. {
  37. $userProducer = new UserProducer();
  38. $userProducer->populateUser($user);
  39. $userProducer->populateproducer($producer);
  40. $userProducer->setCredit(0);
  41. $userProducer->setActive(true);
  42. $userProducer->setBookmark($bookmark);
  43. $userProducer->setNewsletter($newsletter);
  44. $userProducer->setNewsletterOrderTaking($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->mailerService->getMailer()->compose(
  106. [
  107. 'html' => '@common/mail/creditLimitReminder-html',
  108. 'text' => '@common/mail/creditLimitReminder-text'
  109. ],
  110. [
  111. 'user' => $user,
  112. 'producer' => $producer,
  113. 'credit' => $newCredit
  114. ]
  115. )
  116. ->setTo($user->email)
  117. ->setFrom(['contact@souke.fr' => 'Souke'])
  118. ->setSubject('[Souke] Seuil limite de cagnotte 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 updateUserProducerNewsletterByType(UserProducer $userProducer, bool $newsletter, string $type)
  161. {
  162. if($type == Email::TYPE_NEWSLETTER) {
  163. return $this->updateUserProducerNewsletter($userProducer, $newsletter);
  164. }
  165. elseif($type == Email::TYPE_ORDER_TAKING) {
  166. return $this->updateUserProducerNewsletterOrderTaking($userProducer, $newsletter);
  167. }
  168. return false;
  169. }
  170. public function updateUserProducerNewsletter(UserProducer $userProducer, bool $newsletter)
  171. {
  172. $userProducer->newsletter = $newsletter;
  173. return $this->update($userProducer);
  174. }
  175. public function updateUserProducerNewsletterOrderTaking(UserProducer $userProducer, bool $newsletterOrderTaking)
  176. {
  177. $userProducer->newsletter_order_taking = $newsletterOrderTaking;
  178. return $this->update($userProducer);
  179. }
  180. }