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.

UserProducerBuilder.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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->saveCreate($userProducer);
  44. return $userProducer;
  45. }
  46. public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
  47. {
  48. return $this->userProducerRepository->findOneUserProducer($user, $producer)
  49. ?? $this->createUserProducer($user, $producer, $bookmark);
  50. }
  51. public function updateCredit(CreditHistory $creditHistory): void
  52. {
  53. $userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user, $creditHistory->producer);
  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->saveUpdate($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->saveUpdate($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. \Yii::$app->mailer->compose(
  93. [
  94. 'html' => 'creditLimitReminder-html',
  95. 'text' => 'creditLimitReminder-text'
  96. ],
  97. [
  98. 'user' => $user,
  99. 'producer' => $producer,
  100. 'credit' => $newCredit
  101. ]
  102. )
  103. ->setTo($user->email)
  104. ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
  105. ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
  106. ->send();
  107. }
  108. }
  109. public function isCreditLimitCrossed($oldCredit, $newCredit)
  110. {
  111. $creditLimitReminder = $this->producerRepository->getConfig('credit_limit_reminder');
  112. return !is_null($creditLimitReminder) &&
  113. $oldCredit > $creditLimitReminder
  114. && $newCredit <= $creditLimitReminder;
  115. }
  116. public function updateActive(User $user, Producer $producer, bool $active): void
  117. {
  118. $userProducer = $this->createUserProducerIfNotExist(
  119. $user,
  120. $producer
  121. );
  122. $userProducer->active = $active;
  123. $this->saveUpdate($userProducer);
  124. }
  125. }