您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

144 行
5.0KB

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