|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /**
- Copyright Guillaume Bourgeois (2018)
-
- contact@souke.fr
-
- Ce logiciel est un programme informatique servant à aider les producteurs
- à distribuer leur production en circuits courts.
-
- Ce logiciel est régi par la licence CeCILL soumise au droit français et
- respectant les principes de diffusion des logiciels libres. Vous pouvez
- utiliser, modifier et/ou redistribuer ce programme sous les conditions
- de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
- sur le site "http://www.cecill.info".
-
- En contrepartie de l'accessibilité au code source et des droits de copie,
- de modification et de redistribution accordés par cette licence, il n'est
- offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
- seule une responsabilité restreinte pèse sur l'auteur du programme, le
- titulaire des droits patrimoniaux et les concédants successifs.
-
- A cet égard l'attention de l'utilisateur est attirée sur les risques
- associés au chargement, à l'utilisation, à la modification et/ou au
- développement et à la reproduction du logiciel par l'utilisateur étant
- donné sa spécificité de logiciel libre, qui peut le rendre complexe à
- manipuler et qui le réserve donc à des développeurs et des professionnels
- avertis possédant des connaissances informatiques approfondies. Les
- utilisateurs sont donc invités à charger et tester l'adéquation du
- logiciel à leurs besoins dans des conditions permettant d'assurer la
- sécurité de leurs systèmes et ou de leurs données et, plus généralement,
- à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
-
- Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
- pris connaissance de la licence CeCILL, et que vous en avez accepté les
- termes.
- */
-
- namespace domain\User\UserProducer;
-
- use common\helpers\MeanPayment;
- use domain\Communication\Email\Email;
- use domain\Order\Order\Order;
- use domain\Order\Order\OrderRepository;
- use domain\Order\Order\OrderSolver;
- use domain\Payment\Payment;
- use domain\Payment\PaymentSolver;
- use domain\Producer\Producer\Producer;
- use domain\Producer\Producer\ProducerRepository;
- use domain\Producer\Producer\ProducerSolver;
- use domain\User\User\User;
- use domain\User\User\UserRepository;
- use domain\_\AbstractBuilder;
-
- class UserProducerBuilder extends AbstractBuilder
- {
- protected PaymentSolver $paymentSolver;
- protected UserProducerRepository $userProducerRepository;
- protected OrderRepository $orderRepository;
- protected ProducerRepository $producerRepository;
- protected OrderSolver $orderSolver;
- protected UserRepository $userRepository;
- protected ProducerSolver $producerSolver;
-
- public function loadDependencies(): void
- {
- $this->paymentSolver = $this->loadService(PaymentSolver::class);
- $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
- $this->orderRepository = $this->loadService(OrderRepository::class);
- $this->producerRepository = $this->loadService(ProducerRepository::class);
- $this->orderSolver = $this->loadService(OrderSolver::class);
- $this->userRepository = $this->loadService(UserRepository::class);
- $this->producerSolver = $this->loadService(ProducerSolver::class);
- }
-
- public function instanciateUserProducer(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true)
- {
- $userProducer = new UserProducer();
-
- $userProducer->populateUser($user);
- $userProducer->populateproducer($producer);
- $userProducer->setCredit(0);
- $userProducer->setActive(true);
- $userProducer->setBookmark($bookmark);
- $userProducer->setNewsletter($newsletter);
- $userProducer->setNewsletterOrderTaking($newsletter);
-
- return $userProducer;
- }
-
- public function createUserProducer(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true): UserProducer
- {
- $userProducer = $this->instanciateUserProducer($user, $producer, $bookmark, $newsletter);
- $this->create($userProducer);
- return $userProducer;
- }
-
- public function createUserProducerIfNotExist(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true): UserProducer
- {
- return $this->userProducerRepository->findOneUserProducer($user)
- ?? $this->createUserProducer($user, $producer, $bookmark, $newsletter);
- }
-
- public function updateCredit(Payment $payment): void
- {
- if($payment->user && (
- $payment->getType() == Payment::TYPE_DEBIT
- || $payment->getType() == Payment::TYPE_CREDIT
- || ($payment->getType() == Payment::TYPE_PAYMENT && $this->paymentSolver->isMeanPaymentCredit($payment))
- || ($payment->getType() == Payment::TYPE_REFUND && $this->paymentSolver->isMeanPaymentCredit($payment))
- )) {
-
- $userProducer = $this->userProducerRepository->findOneUserProducer($payment->user);
-
- if ($userProducer) {
- $oldCredit = $userProducer->getCredit();
-
- $this->deductCredit($userProducer, $payment);
- //$this->initMeanPaymentOrder($payment);
- $this->sendCreditLimitReminder($userProducer, $payment, $oldCredit);
- }
- }
- }
-
- public function deductCredit(UserProducer $userProducer, Payment $payment)
- {
- if ($this->paymentSolver->isTypeCredit($payment)) {
- $userProducer->setCredit($userProducer->getCredit() + $payment->getAmount());
- } elseif ($this->paymentSolver->isTypeDebit($payment)) {
- $userProducer->setCredit($userProducer->getCredit() - $payment->getAmount());
- }
-
- $this->update($userProducer);
- }
-
- public function initMeanPaymentOrder(Payment $payment)
- {
- if ($payment->id_order && $payment->id_order > 0) {
- $order = $this->orderRepository->findOneOrderById((int) $payment->id_order);
-
- if ($order) {
- $paymentStatus = $this->orderSolver->getPaymentStatus($order);
- if ($paymentStatus == Order::PAYMENT_PAID
- || $paymentStatus == Order::PAYMENT_SURPLUS) {
-
- $order->mean_payment = MeanPayment::CREDIT;
-
- $this->update($order);
- }
- }
- }
- }
-
- public function sendCreditLimitReminder($userProducer, $payment, $oldCredit)
- {
- $newCredit = $userProducer->credit;
-
- if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
-
- $user = $this->userRepository->findOneUserById($payment->id_user);
- $producer = $this->producerRepository->findOneProducerById($payment->id_producer);
-
- if($user && $user->email && strlen($user->email) > 0) {
- \Yii::$app->mailerService->getMailer()->compose(
- [
- 'html' => '@common/mail/creditLimitReminder-html',
- 'text' => '@common/mail/creditLimitReminder-text'
- ],
- [
- 'user' => $user,
- 'producer' => $producer,
- 'credit' => $newCredit
- ]
- )
- ->setTo($user->email)
- ->setFrom(['contact@souke.fr' => 'Souke'])
- ->setSubject('[Souke] Seuil limite de cagnotte dépassé')
- ->send();
- }
- }
- }
-
- public function isCreditLimitCrossed($oldCredit, $newCredit)
- {
- $creditLimitReminder = $this->producerSolver->getConfig('credit_limit_reminder');
-
- return !is_null($creditLimitReminder) &&
- $oldCredit > $creditLimitReminder
- && $newCredit <= $creditLimitReminder;
- }
-
- public function updateActive(User $user, Producer $producer, bool $active): bool
- {
- $userProducer = $this->createUserProducerIfNotExist(
- $user,
- $producer
- );
-
- $userProducer->active = $active;
-
- return $this->update($userProducer);
- }
-
- public function updateBookmark(UserProducer $userProducer, bool $bookmark)
- {
- $userProducer->bookmark = $bookmark;
- return $this->update($userProducer);
- }
-
- public function addProducerBookmark(User $user)
- {
- $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
- return $this->updateBookmark($userProducer, true);
- }
-
- public function removeProducerBookmark(User $user)
- {
- $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
- return $this->updateBookmark($userProducer, false);
- }
-
- public function unlinkUserProducer(UserProducer $userProducer)
- {
- $userProducer->active = 0;
- $userProducer->bookmark = 0;
- return $this->update($userProducer);
- }
-
- public function updateUserProducerNewsletterByType(UserProducer $userProducer, bool $newsletter, string $type)
- {
- if($type == Email::TYPE_NEWSLETTER) {
- return $this->updateUserProducerNewsletter($userProducer, $newsletter);
- }
- elseif($type == Email::TYPE_ORDER_TAKING) {
- return $this->updateUserProducerNewsletterOrderTaking($userProducer, $newsletter);
- }
-
- return false;
- }
-
- public function updateUserProducerNewsletter(UserProducer $userProducer, bool $newsletter)
- {
- $userProducer->newsletter = $newsletter;
- return $this->update($userProducer);
- }
-
- public function updateUserProducerNewsletterOrderTaking(UserProducer $userProducer, bool $newsletterOrderTaking)
- {
- $userProducer->newsletter_order_taking = $newsletterOrderTaking;
- return $this->update($userProducer);
- }
- }
|