Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

116 linhas
4.1KB

  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. class UserProducerBuilder extends BaseService implements BuilderInterface
  11. {
  12. protected CreditHistorySolver $creditHistorySolver;
  13. protected UserProducerRepository $userProducerRepository;
  14. public function __construct()
  15. {
  16. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  17. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  18. }
  19. public function instanciate(int $idUser, int $idProducer, int $bookmark = 1)
  20. {
  21. $userProducer = new UserProducer();
  22. $userProducer->setIdUser($idUser);
  23. $userProducer->setIdProducer($idProducer);
  24. $userProducer->setCredit(0);
  25. $userProducer->setActive(1);
  26. $userProducer->setBookmark($bookmark);
  27. return $userProducer;
  28. }
  29. public function updateCredit(CreditHistory $creditHistory): void
  30. {
  31. $userProducer = $this->userProducerRepository->getOne($creditHistory->getIdUser(), $creditHistory->getIdProducer());
  32. if ($userProducer) {
  33. $oldCredit = $userProducer->getCredit();
  34. $this->deductCredit($userProducer, $creditHistory);
  35. $this->initMeanPaymentOrder($creditHistory);
  36. $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
  37. }
  38. }
  39. public function deductCredit(UserProducer $userProducer, CreditHistory $creditHistory)
  40. {
  41. if ($this->creditHistorySolver->isTypeCredit($creditHistory)) {
  42. $userProducer->setCredit($userProducer->getCredit() + $creditHistory->getAmount());
  43. } elseif ($this->creditHistorySolver->isTypeDebit($creditHistory)) {
  44. $userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
  45. }
  46. $userProducer->save();
  47. }
  48. public function initMeanPaymentOrder($creditHistory)
  49. {
  50. // set mean payment
  51. if ($creditHistory->id_order && $creditHistory->id_order > 0) {
  52. $order = Order::searchOne(['id' => (int)$creditHistory->id_order]);
  53. if ($order) {
  54. $paymentStatus = $order->getPaymentStatus();
  55. if ($paymentStatus == Order::PAYMENT_PAID
  56. || $paymentStatus == Order::PAYMENT_SURPLUS) {
  57. $order->mean_payment = MeanPayment::CREDIT;
  58. $order->save();
  59. }
  60. }
  61. }
  62. }
  63. public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
  64. {
  65. $userRepository = \Yii::$app->logic->getUserContainer()->getRepository();
  66. $producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
  67. $newCredit = $userProducer->credit;
  68. if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
  69. $user = $userRepository->getOneById($creditHistory->id_user);
  70. $producer = $producerRepository->getOneById($creditHistory->id_producer);
  71. \Yii::$app->mailer->compose(
  72. [
  73. 'html' => 'creditLimitReminder-html',
  74. 'text' => 'creditLimitReminder-text'
  75. ],
  76. [
  77. 'user' => $user,
  78. 'producer' => $producer,
  79. 'credit' => $newCredit
  80. ]
  81. )
  82. ->setTo($user->email)
  83. ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
  84. ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
  85. ->send();
  86. }
  87. }
  88. public function isCreditLimitCrossed($oldCredit, $newCredit)
  89. {
  90. $creditLimitReminder = Producer::getConfig('credit_limit_reminder');
  91. return !is_null($creditLimitReminder) &&
  92. $oldCredit > $creditLimitReminder
  93. && $newCredit <= $creditLimitReminder;
  94. }
  95. }