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.

149 lines
5.4KB

  1. <?php
  2. namespace common\logic\User\UserProducer;
  3. use common\helpers\MeanPayment;
  4. use common\logic\AbstractBuilder;
  5. use common\logic\AbstractService;
  6. use common\logic\BuilderInterface;
  7. use common\logic\Order\Order\Order;
  8. use common\logic\Order\Order\OrderRepository;
  9. use common\logic\Producer\Producer\Producer;
  10. use common\logic\Producer\Producer\ProducerRepository;
  11. use common\logic\User\CreditHistory\CreditHistory;
  12. use common\logic\User\CreditHistory\CreditHistorySolver;
  13. use common\logic\User\User\User;
  14. class UserProducerBuilder extends AbstractBuilder implements BuilderInterface
  15. {
  16. protected CreditHistorySolver $creditHistorySolver;
  17. protected UserProducerRepository $userProducerRepository;
  18. protected OrderRepository $orderRepository;
  19. protected ProducerRepository $producerRepository;
  20. public function __construct()
  21. {
  22. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  23. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  24. $this->orderRepository = $this->loadService(OrderRepository::class);
  25. $this->producerRepository = $this->loadService(ProducerRepository::class);
  26. }
  27. public function instanciateUserProducer(User $user, Producer $producer, int $bookmark = 1)
  28. {
  29. $userProducer = new UserProducer();
  30. $userProducer->populateUser($user);
  31. $userProducer->populateproducer($producer);
  32. $userProducer->setCredit(0);
  33. $userProducer->setActive(1);
  34. $userProducer->setBookmark($bookmark);
  35. return $userProducer;
  36. }
  37. public function createUserProducer(User $user, Producer $producer, int $bookmark = 1): UserProducer
  38. {
  39. $userProducer = $this->instanciateUserProducer($user, $producer, $bookmark);
  40. $this->saveCreate($userProducer);
  41. return $userProducer;
  42. }
  43. public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
  44. {
  45. return $this->userProducerRepository->findOneUserProducer($user, $producer)
  46. ?? $this->createUserProducer($user, $producer, $bookmark);
  47. }
  48. public function updateCredit(CreditHistory $creditHistory): void
  49. {
  50. $userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user, $creditHistory->producer);
  51. if ($userProducer) {
  52. $oldCredit = $userProducer->getCredit();
  53. $this->deductCredit($userProducer, $creditHistory);
  54. $this->initMeanPaymentOrder($creditHistory);
  55. $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
  56. }
  57. }
  58. public function deductCredit(UserProducer $userProducer, CreditHistory $creditHistory)
  59. {
  60. if ($this->creditHistorySolver->isTypeCredit($creditHistory)) {
  61. $userProducer->setCredit($userProducer->getCredit() + $creditHistory->getAmount());
  62. } elseif ($this->creditHistorySolver->isTypeDebit($creditHistory)) {
  63. $userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
  64. }
  65. $this->saveUpdate($userProducer);
  66. }
  67. public function initMeanPaymentOrder($creditHistory)
  68. {
  69. if ($creditHistory->id_order && $creditHistory->id_order > 0) {
  70. $order = $this->orderRepository->findOneOrderById((int) $creditHistory->id_order);
  71. if ($order) {
  72. $paymentStatus = $order->getPaymentStatus();
  73. if ($paymentStatus == Order::PAYMENT_PAID
  74. || $paymentStatus == Order::PAYMENT_SURPLUS) {
  75. $order->mean_payment = MeanPayment::CREDIT;
  76. $this->saveUpdate($order);
  77. }
  78. }
  79. }
  80. }
  81. public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
  82. {
  83. $userRepository = \Yii::$app->logic->getUserContainer()->getRepository();
  84. $producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
  85. $newCredit = $userProducer->credit;
  86. if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
  87. $user = $userRepository->getOneById($creditHistory->id_user);
  88. $producer = $producerRepository->getOneById($creditHistory->id_producer);
  89. \Yii::$app->mailer->compose(
  90. [
  91. 'html' => 'creditLimitReminder-html',
  92. 'text' => 'creditLimitReminder-text'
  93. ],
  94. [
  95. 'user' => $user,
  96. 'producer' => $producer,
  97. 'credit' => $newCredit
  98. ]
  99. )
  100. ->setTo($user->email)
  101. ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
  102. ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
  103. ->send();
  104. }
  105. }
  106. public function isCreditLimitCrossed($oldCredit, $newCredit)
  107. {
  108. $creditLimitReminder = $this->producerRepository->getConfig('credit_limit_reminder');
  109. return !is_null($creditLimitReminder) &&
  110. $oldCredit > $creditLimitReminder
  111. && $newCredit <= $creditLimitReminder;
  112. }
  113. public function updateActive(User $user, Producer $producer, bool $active): void
  114. {
  115. $userProducer = $this->createUserProducerIfNotExist(
  116. $user,
  117. $producer
  118. );
  119. $userProducer->active = $active;
  120. $this->saveUpdate($userProducer);
  121. }
  122. }