|
- <?php
-
- namespace common\logic\User\UserProducer;
-
- use common\helpers\MeanPayment;
- use common\logic\BaseService;
- use common\logic\BuilderInterface;
- use common\logic\Order\Order\Order;
- use common\logic\Producer\Producer\Producer;
- use common\logic\User\CreditHistory\CreditHistory;
- use common\logic\User\CreditHistory\CreditHistorySolver;
- use common\logic\User\User\User;
-
- class UserProducerBuilder extends BaseService implements BuilderInterface
- {
- protected CreditHistorySolver $creditHistorySolver;
- protected UserProducerRepository $userProducerRepository;
-
- public function __construct()
- {
- $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
- $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
- }
-
- public function instanciate(User $user, Producer $producer, int $bookmark = 1)
- {
- $userProducer = new UserProducer();
-
- $userProducer->populateUser($user);
- $userProducer->populateproducer($producer);
- $userProducer->setCredit(0);
- $userProducer->setActive(1);
- $userProducer->setBookmark($bookmark);
-
- return $userProducer;
- }
-
- public function create(User $user, Producer $producer, int $bookmark = 1): UserProducer
- {
- $userProducer = $this->instanciate($user, $producer, $bookmark);
- $userProducer->save();
- return $userProducer;
- }
-
- public function createIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
- {
- $userProducer = $this->userProducerRepository->getOne($user, $producer);
- if (!$userProducer) {
- $userProducer = $this->create($user, $producer, $bookmark);
- }
-
- return $userProducer;
- }
-
- public function updateCredit(CreditHistory $creditHistory): void
- {
- $userProducer = $this->userProducerRepository->getOne($creditHistory->getIdUser(), $creditHistory->getIdProducer());
-
- if ($userProducer) {
- $oldCredit = $userProducer->getCredit();
-
- $this->deductCredit($userProducer, $creditHistory);
- $this->initMeanPaymentOrder($creditHistory);
- $this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit);
- }
- }
-
- public function deductCredit(UserProducer $userProducer, CreditHistory $creditHistory)
- {
- if ($this->creditHistorySolver->isTypeCredit($creditHistory)) {
- $userProducer->setCredit($userProducer->getCredit() + $creditHistory->getAmount());
- } elseif ($this->creditHistorySolver->isTypeDebit($creditHistory)) {
- $userProducer->setCredit($userProducer->getCredit() - $creditHistory->getAmount());
- }
-
- $userProducer->save();
- }
-
- public function initMeanPaymentOrder($creditHistory)
- {
- // set mean payment
- if ($creditHistory->id_order && $creditHistory->id_order > 0) {
- $order = Order::searchOne(['id' => (int)$creditHistory->id_order]);
-
- if ($order) {
- $paymentStatus = $order->getPaymentStatus();
- if ($paymentStatus == Order::PAYMENT_PAID
- || $paymentStatus == Order::PAYMENT_SURPLUS) {
-
- $order->mean_payment = MeanPayment::CREDIT;
- $order->save();
- }
- }
- }
- }
-
- public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit)
- {
- $userRepository = \Yii::$app->logic->getUserContainer()->getRepository();
- $producerRepository = \Yii::$app->logic->getProducerContainer()->getRepository();
- $newCredit = $userProducer->credit;
-
- if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
-
- $user = $userRepository->getOneById($creditHistory->id_user);
- $producer = $producerRepository->getOneById($creditHistory->id_producer);
-
- \Yii::$app->mailer->compose(
- [
- 'html' => 'creditLimitReminder-html',
- 'text' => 'creditLimitReminder-text'
- ],
- [
- 'user' => $user,
- 'producer' => $producer,
- 'credit' => $newCredit
- ]
- )
- ->setTo($user->email)
- ->setFrom(['contact@opendistrib.net' => 'Opendistrib'])
- ->setSubject('[Opendistrib] Seuil limite de crédit dépassé')
- ->send();
- }
- }
-
- public function isCreditLimitCrossed($oldCredit, $newCredit)
- {
- $creditLimitReminder = Producer::getConfig('credit_limit_reminder');
-
- return !is_null($creditLimitReminder) &&
- $oldCredit > $creditLimitReminder
- && $newCredit <= $creditLimitReminder;
- }
-
- public function updateActive(User $user, Producer $producer, bool $active): void
- {
- $userProducer = $this->getUserProducerContainer()->getRepository()->getOne(
- $user,
- $producer
- );
- $userProducer->active = $active;
- $userProducer->save();
- }
- }
|