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.

97 lines
3.2KB

  1. <?php
  2. namespace common\services;
  3. use common\helpers\MeanPayment;
  4. use common\models\Order;
  5. use common\models\Producer;
  6. class UserProducerService
  7. {
  8. protected CreditHistoryService $creditHistoryService;
  9. public function __construct()
  10. {
  11. $this->creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();
  12. }
  13. public function updateCredit($creditHistory)
  14. {
  15. $userProducer = Yii::$app->logic->getUserProducerContainer()->getRepository()->getOne($creditHistory->id_user, $creditHistory->id_producer);
  16. if ($userProducer) {
  17. $oldCredit = $userProducer->credit;
  18. $this->deductCredit($userProducer, $creditHistory);
  19. $this->initMeanPaymentOrder($creditHistory);
  20. $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
  21. }
  22. }
  23. public function deductCredit($userProducer, $creditHistory)
  24. {
  25. if ($this->creditHistoryService->isTypeCredit($creditHistory)) {
  26. $userProducer->credit += $creditHistory->amount;
  27. } elseif ($this->creditHistoryService->isTypeDebit($creditHistory)) {
  28. $userProducer->credit -= $creditHistory->amount;
  29. }
  30. $userProducer->save();
  31. }
  32. public function initMeanPaymentOrder($creditHistory)
  33. {
  34. // set mean payment
  35. if ($creditHistory->id_order && $creditHistory->id_order > 0) {
  36. $order = Order::searchOne(['id' => (int)$creditHistory->id_order]);
  37. if ($order) {
  38. $paymentStatus = $order->getPaymentStatus();
  39. if ($paymentStatus == Order::PAYMENT_PAID
  40. || $paymentStatus == Order::PAYMENT_SURPLUS) {
  41. $order->mean_payment = MeanPayment::CREDIT;
  42. $order->save();
  43. }
  44. }
  45. }
  46. }
  47. public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
  48. {
  49. $userRepository = Yii::$app->logic->getUserContainer()->getRepository();
  50. $producerRepository = Yii::$app->logic->getProducerContainer()->getRepository();
  51. $newCredit = $userProducer->credit;
  52. if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
  53. $user = $userRepository->getOneById($creditHistory->id_user);
  54. $producer = $producerRepository->getOneById($creditHistory->id_producer);
  55. Yii::$app->mailer->compose(
  56. [
  57. 'html' => 'creditLimitReminder-html',
  58. 'text' => 'creditLimitReminder-text'
  59. ],
  60. [
  61. 'user' => $user,
  62. 'producer' => $producer,
  63. 'credit' => $newCredit
  64. ]
  65. )
  66. ->setTo($user->email)
  67. ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
  68. ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
  69. ->send();
  70. }
  71. }
  72. public function isCreditLimitCrossed($oldCredit, $newCredit)
  73. {
  74. $creditLimitReminder = Producer::getConfig('credit_limit_reminder');
  75. return !is_null($creditLimitReminder) &&
  76. $oldCredit > $creditLimitReminder
  77. && $newCredit <= $creditLimitReminder;
  78. }
  79. }