Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UserProducerBuilder.php 9.6KB

1 год назад
2 месяцев назад
1 год назад
1 год назад
5 месяцев назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
5 месяцев назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
7 месяцев назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
1 год назад
5 месяцев назад
5 месяцев назад
1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. Copyright Guillaume Bourgeois (2018)
  4. contact@souke.fr
  5. Ce logiciel est un programme informatique servant à aider les producteurs
  6. à distribuer leur production en circuits courts.
  7. Ce logiciel est régi par la licence CeCILL soumise au droit français et
  8. respectant les principes de diffusion des logiciels libres. Vous pouvez
  9. utiliser, modifier et/ou redistribuer ce programme sous les conditions
  10. de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  11. sur le site "http://www.cecill.info".
  12. En contrepartie de l'accessibilité au code source et des droits de copie,
  13. de modification et de redistribution accordés par cette licence, il n'est
  14. offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  15. seule une responsabilité restreinte pèse sur l'auteur du programme, le
  16. titulaire des droits patrimoniaux et les concédants successifs.
  17. A cet égard l'attention de l'utilisateur est attirée sur les risques
  18. associés au chargement, à l'utilisation, à la modification et/ou au
  19. développement et à la reproduction du logiciel par l'utilisateur étant
  20. donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  21. manipuler et qui le réserve donc à des développeurs et des professionnels
  22. avertis possédant des connaissances informatiques approfondies. Les
  23. utilisateurs sont donc invités à charger et tester l'adéquation du
  24. logiciel à leurs besoins dans des conditions permettant d'assurer la
  25. sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  26. à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  27. Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  28. pris connaissance de la licence CeCILL, et que vous en avez accepté les
  29. termes.
  30. */
  31. namespace domain\User\UserProducer;
  32. use common\helpers\MeanPayment;
  33. use domain\Communication\Email\Email;
  34. use domain\Order\Order\Order;
  35. use domain\Order\Order\OrderRepository;
  36. use domain\Order\Order\OrderSolver;
  37. use domain\Payment\Payment;
  38. use domain\Payment\PaymentSolver;
  39. use domain\Producer\Producer\Producer;
  40. use domain\Producer\Producer\ProducerRepository;
  41. use domain\Producer\Producer\ProducerSolver;
  42. use domain\User\User\User;
  43. use domain\User\User\UserRepository;
  44. use domain\_\AbstractBuilder;
  45. class UserProducerBuilder extends AbstractBuilder
  46. {
  47. protected PaymentSolver $paymentSolver;
  48. protected UserProducerRepository $userProducerRepository;
  49. protected OrderRepository $orderRepository;
  50. protected ProducerRepository $producerRepository;
  51. protected OrderSolver $orderSolver;
  52. protected UserRepository $userRepository;
  53. protected ProducerSolver $producerSolver;
  54. public function loadDependencies(): void
  55. {
  56. $this->paymentSolver = $this->loadService(PaymentSolver::class);
  57. $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
  58. $this->orderRepository = $this->loadService(OrderRepository::class);
  59. $this->producerRepository = $this->loadService(ProducerRepository::class);
  60. $this->orderSolver = $this->loadService(OrderSolver::class);
  61. $this->userRepository = $this->loadService(UserRepository::class);
  62. $this->producerSolver = $this->loadService(ProducerSolver::class);
  63. }
  64. public function instanciateUserProducer(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true)
  65. {
  66. $userProducer = new UserProducer();
  67. $userProducer->populateUser($user);
  68. $userProducer->populateproducer($producer);
  69. $userProducer->setCredit(0);
  70. $userProducer->setActive(true);
  71. $userProducer->setBookmark($bookmark);
  72. $userProducer->setNewsletter($newsletter);
  73. $userProducer->setNewsletterOrderTaking($newsletter);
  74. return $userProducer;
  75. }
  76. public function createUserProducer(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true): UserProducer
  77. {
  78. $userProducer = $this->instanciateUserProducer($user, $producer, $bookmark, $newsletter);
  79. $this->create($userProducer);
  80. return $userProducer;
  81. }
  82. public function createUserProducerIfNotExist(User $user, Producer $producer, bool $bookmark = true, bool $newsletter = true): UserProducer
  83. {
  84. return $this->userProducerRepository->findOneUserProducer($user)
  85. ?? $this->createUserProducer($user, $producer, $bookmark, $newsletter);
  86. }
  87. public function updateCredit(Payment $payment): void
  88. {
  89. if($payment->user && (
  90. $payment->getType() == Payment::TYPE_DEBIT
  91. || $payment->getType() == Payment::TYPE_CREDIT
  92. || ($payment->getType() == Payment::TYPE_PAYMENT && $this->paymentSolver->isMeanPaymentCredit($payment))
  93. || ($payment->getType() == Payment::TYPE_REFUND && $this->paymentSolver->isMeanPaymentCredit($payment))
  94. )) {
  95. $userProducer = $this->userProducerRepository->findOneUserProducer($payment->user);
  96. if ($userProducer) {
  97. $oldCredit = $userProducer->getCredit();
  98. $this->deductCredit($userProducer, $payment);
  99. //$this->initMeanPaymentOrder($payment);
  100. $this->sendCreditLimitReminder($userProducer, $payment, $oldCredit);
  101. }
  102. }
  103. }
  104. public function deductCredit(UserProducer $userProducer, Payment $payment)
  105. {
  106. if ($this->paymentSolver->isTypeCredit($payment)) {
  107. $userProducer->setCredit($userProducer->getCredit() + $payment->getAmount());
  108. } elseif ($this->paymentSolver->isTypeDebit($payment)) {
  109. $userProducer->setCredit($userProducer->getCredit() - $payment->getAmount());
  110. }
  111. $this->update($userProducer);
  112. }
  113. public function initMeanPaymentOrder(Payment $payment)
  114. {
  115. if ($payment->id_order && $payment->id_order > 0) {
  116. $order = $this->orderRepository->findOneOrderById((int) $payment->id_order);
  117. if ($order) {
  118. $paymentStatus = $this->orderSolver->getPaymentStatus($order);
  119. if ($paymentStatus == Order::PAYMENT_PAID
  120. || $paymentStatus == Order::PAYMENT_SURPLUS) {
  121. $order->mean_payment = MeanPayment::CREDIT;
  122. $this->update($order);
  123. }
  124. }
  125. }
  126. }
  127. public function sendCreditLimitReminder($userProducer, $payment, $oldCredit)
  128. {
  129. $newCredit = $userProducer->credit;
  130. if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) {
  131. $user = $this->userRepository->findOneUserById($payment->id_user);
  132. $producer = $this->producerRepository->findOneProducerById($payment->id_producer);
  133. if($user && $user->email && strlen($user->email) > 0) {
  134. \Yii::$app->mailerService->getMailer()->compose(
  135. [
  136. 'html' => '@common/mail/creditLimitReminder-html',
  137. 'text' => '@common/mail/creditLimitReminder-text'
  138. ],
  139. [
  140. 'user' => $user,
  141. 'producer' => $producer,
  142. 'credit' => $newCredit
  143. ]
  144. )
  145. ->setTo($user->email)
  146. ->setFrom(['contact@souke.fr' => 'Souke'])
  147. ->setSubject('[Souke] Seuil limite de cagnotte dépassé')
  148. ->send();
  149. }
  150. }
  151. }
  152. public function isCreditLimitCrossed($oldCredit, $newCredit)
  153. {
  154. $creditLimitReminder = $this->producerSolver->getConfig('credit_limit_reminder');
  155. return !is_null($creditLimitReminder) &&
  156. $oldCredit > $creditLimitReminder
  157. && $newCredit <= $creditLimitReminder;
  158. }
  159. public function updateActive(User $user, Producer $producer, bool $active): bool
  160. {
  161. $userProducer = $this->createUserProducerIfNotExist(
  162. $user,
  163. $producer
  164. );
  165. $userProducer->active = $active;
  166. return $this->update($userProducer);
  167. }
  168. public function updateBookmark(UserProducer $userProducer, bool $bookmark)
  169. {
  170. $userProducer->bookmark = $bookmark;
  171. return $this->update($userProducer);
  172. }
  173. public function addProducerBookmark(User $user)
  174. {
  175. $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
  176. return $this->updateBookmark($userProducer, true);
  177. }
  178. public function removeProducerBookmark(User $user)
  179. {
  180. $userProducer = $this->createUserProducerIfNotExist($user, $this->getProducerContext());
  181. return $this->updateBookmark($userProducer, false);
  182. }
  183. public function unlinkUserProducer(UserProducer $userProducer)
  184. {
  185. $userProducer->active = 0;
  186. $userProducer->bookmark = 0;
  187. return $this->update($userProducer);
  188. }
  189. public function updateUserProducerNewsletterByType(UserProducer $userProducer, bool $newsletter, string $type)
  190. {
  191. if($type == Email::TYPE_NEWSLETTER) {
  192. return $this->updateUserProducerNewsletter($userProducer, $newsletter);
  193. }
  194. elseif($type == Email::TYPE_ORDER_TAKING) {
  195. return $this->updateUserProducerNewsletterOrderTaking($userProducer, $newsletter);
  196. }
  197. return false;
  198. }
  199. public function updateUserProducerNewsletter(UserProducer $userProducer, bool $newsletter)
  200. {
  201. $userProducer->newsletter = $newsletter;
  202. return $this->update($userProducer);
  203. }
  204. public function updateUserProducerNewsletterOrderTaking(UserProducer $userProducer, bool $newsletterOrderTaking)
  205. {
  206. $userProducer->newsletter_order_taking = $newsletterOrderTaking;
  207. return $this->update($userProducer);
  208. }
  209. }