src/EventSubscriber/InvoiceNotificationSubscriber.php line 324

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Entity\Security\Notification;
  12. use App\Entity\Training\Invoice;
  13. use App\Event\InvoiceNotificationEvent;
  14. use App\Repository\Security\UserRepository;
  15. use App\Service\Mailer\Emailer;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Mime\Email;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Twig\Environment;
  22. /**
  23.  * Notifies post's author about new invoices.
  24.  *
  25.  */
  26. class InvoiceNotificationSubscriber implements EventSubscriberInterface {
  27.     private $translator;
  28.     private $urlGenerator;
  29.     private $sender;
  30.     private $userRepository;
  31.     private $entityManager;
  32.     private $emailer;
  33.     private $twig;
  34.     public function __construct(
  35.             UrlGeneratorInterface $urlGenerator,
  36.             EntityManagerInterface $entityManager,
  37.             TranslatorInterface $translator,
  38.             UserRepository $userRepository,
  39.             string $sender,
  40.             Emailer $emailer,
  41.             Environment $twig) {
  42.         $this->urlGenerator $urlGenerator;
  43.         $this->translator $translator;
  44.         $this->sender $sender;
  45.         $this->userRepository $userRepository;
  46.         $this->entityManager $entityManager;
  47.         $this->emailer $emailer;
  48.         $this->twig $twig;
  49.     }
  50.     public static function getSubscribedEvents(): array {
  51.         return [
  52.             InvoiceNotificationEvent::CREATE => [['onInvoiceCreated'1]],
  53.             InvoiceNotificationEvent::VALIDATE => [['onInvoiceValidated'1]],
  54.             InvoiceNotificationEvent::PAY => [['onInvoicePayed'1]],
  55. //            InvoiceNotificationEvent::ARCHIVE => 'onInvoiceArchived',
  56.             InvoiceNotificationEvent::ARCHIVE => [['onInvoiceArchived'1]],
  57.             InvoiceNotificationEvent::DELETE => [['onInvoiceDeleted'1]],
  58.             InvoiceNotificationEvent::GENERATE => [['onInvoicesGenerated'1]],
  59.         ];
  60.     }
  61.     public function onInvoiceCreated(InvoiceNotificationEvent $event): void {
  62.         /** @var Invoice $invoice */
  63.         $invoice $event->getInvoice();
  64.         $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_view',
  65.                 ['slug' => $invoice->getSlug()],
  66.                 UrlGeneratorInterface::ABSOLUTE_URL);
  67.         $subject $this->translator->trans('notification.invoice_created');
  68.         $body $this->translator->trans('notification.invoice_created.description', [
  69.             '%title%' => $invoice->getName(),
  70.             '%link%' => $linkToInvoice,
  71.         ]);
  72.         $manager null;
  73.         $pmanger $invoice->getManager();
  74.         if ($pmanger != null) {
  75.             $manager $pmanger->getAccount();
  76.         }
  77.         if ($manager !== null) {
  78.             $notification = new Notification();
  79.             $notification->setTitle($subject);
  80.             $notification->setMessage($body);
  81.             $notification->setType(Notification::TYPE_INVOICE);
  82.             $notification->setUrl($linkToInvoice);
  83.             $notification->setUser($manager);
  84.             $this->entityManager->persist($notification);
  85.             $this->entityManager->flush();
  86.             $this->emailer->sendMail ($this->sender$manager->getEmail(), $subject$body);
  87. //            $this->emailer->sendMail($this->sender, $this->sender, $subject, $body);
  88.         }
  89.     }
  90.     public function onInvoicesGenerated(InvoiceNotificationEvent $event): void {
  91.         /** @var Invoice $invoice */
  92.         $invoice null;
  93.         $invoices $event->getInvoices();
  94.         $tenancyName 'no_tenancy';
  95.         if ($invoices[0]->getTenancy() !== null) {
  96.             $tenancyName $invoices[0]->getTenancy()->getName();
  97.         }
  98.         $subject 'PROIMMO - Génération factures - ' $tenancyName '(' $invoices->count() . ' factures)';
  99.         $mName '';
  100.         $to Emailer::$defaultTo;
  101.         if ($invoices[0]->getManager() !== null and $invoices[0]->getManager()->getEmail() !== null) {
  102.             $to $invoices[0]->getManager()->getEmail();
  103.             $mName $invoices[0]->getManager()->getName();
  104.         }
  105.         $description 'Bonjour ' $mName ',' PHP_EOL PHP_EOL ' Ci-dessous les factures générées de l\'application PROIMMO.' PHP_EOL;
  106.         $amount 0;
  107.         foreach ($invoices as $invoice) {
  108.             $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_view',
  109.                     ['slug' => $invoice->getSlug()],
  110.                     UrlGeneratorInterface::ABSOLUTE_URL);
  111.             $ntitle $this->translator->trans('notification.invoice_created');
  112.             $nbody $this->translator->trans('notification.invoice_created.description', [
  113.                 '%title%' => $invoice->getName(),
  114.                 '%link%' => $linkToInvoice,
  115.             ]);
  116.             $manager null;
  117.             $pmanger $invoice->getManager();
  118.             if ($pmanger != null) {
  119.                 $manager $pmanger->getAccount();
  120.             }
  121.             if ($manager !== null) {
  122.                 $notification = new Notification();
  123.                 $notification->setTitle($ntitle);
  124.                 $notification->setMessage($nbody);
  125.                 $notification->setType(Notification::TYPE_INVOICE);
  126.                 $notification->setUrl($linkToInvoice);
  127.                 $notification->setUser($manager);
  128.                 $this->entityManager->persist($notification);
  129.             }
  130.             $description .= '    ' $invoice->getName() . ' --- ' $this->urlGenerator->generate('pr_jit_invoice_view', ['slug' => $invoice->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL) . PHP_EOL;
  131.             $amount += $invoice->getAmt();
  132.         }
  133.         $this->entityManager->flush();
  134.         $description .= PHP_EOL PHP_EOL 'Merci de valider ces factures afin qu\'elles soient envoyées aux locataires.' PHP_EOL PHP_EOL 'Cordiallement. It\'s me';
  135.         $htmlContents $this->twig->render('jit/invoice/_tpl_not_generate.html.twig', [
  136.             'dest_name' => $mName,
  137.             'invoices' => $invoices,
  138.         ]);
  139.         $this->emailer->sendMail(Emailer::$defaultFrom$to$subject$description$htmlContents);
  140.     }
  141.     public function onInvoiceValidated(InvoiceNotificationEvent $event): void {
  142.         /** @var Invoice $invoice */
  143.         $invoice $event->getInvoice();
  144.         $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_view',
  145.                 ['slug' => $invoice->getSlug()],
  146.                 UrlGeneratorInterface::ABSOLUTE_URL);
  147.         $subject $this->translator->trans('notification.invoice_validated');
  148.         $body $this->translator->trans('notification.invoice_validated.description', [
  149.             '%title%' => $invoice->getName(),
  150.             '%link%' => $linkToInvoice,
  151.         ]);
  152. //        $email = null;
  153.         $manager null;
  154.         $pmanger $invoice->getDest();
  155.         if ($pmanger != null) {
  156.             $manager $pmanger->getAccount();
  157.         }
  158.         if ($manager !== null) {
  159.             $notification = new Notification();
  160.             $notification->setTitle($subject);
  161.             $notification->setMessage($body);
  162.             $notification->setType(Notification::TYPE_INVOICE);
  163.             $notification->setUrl($linkToInvoice);
  164.             $notification->setUser($manager);
  165.             $this->entityManager->persist($notification);
  166. //            if ($email === null) {
  167. //                $email = (new Email())
  168. //                        ->from($this->sender)
  169. //                        ->to($manager->getEmail())
  170. //                        ->subject($subject)
  171. //                        ->html($body)
  172. //                ;
  173. //            } else {
  174. //                $email->addTo($manager->getEmail());
  175. //            }
  176.             $to Emailer::$defaultTo;
  177.             if ($pmanger->getEmail()) {
  178.                 $to $pmanger->getEmail();
  179.                 $mName $pmanger->getName();
  180.             }
  181.             $htmlContents $this->twig->render('jit/invoice/_tpl_not_validate.html.twig', [
  182.                 'dest_name' => $mName,
  183.                 'invoice' => $invoice,
  184.             ]);
  185.             $this->emailer->sendMail(Emailer::$defaultFrom$to$subject$body$htmlContents);
  186.         }
  187.         $this->entityManager->flush();
  188. //        if ($email !== null) {
  189. //            $this->mailer->send($email);
  190. //        }
  191.     }
  192.     public function onInvoicePayed(InvoiceNotificationEvent $event): void {
  193.         /** @var Invoice $invoice */
  194.         $invoice $event->getInvoice();
  195.         $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_view',
  196.                 ['slug' => $invoice->getSlug()],
  197.                 UrlGeneratorInterface::ABSOLUTE_URL);
  198.         $subject $this->translator->trans('notification.invoice_payed');
  199.         $body $this->translator->trans('notification.invoice_payed.description', [
  200.             '%title%' => $invoice->getName(),
  201.             '%link%' => $linkToInvoice,
  202.         ]);
  203. //        $notification = new Notification();
  204. //        $notification->setTitle($subject);
  205. //        $notification->setMessage($body);
  206. //        $notification->setType(Notification::TYPE_INVOICE);
  207. //        $notification->setUrl($linkToInvoice);
  208. //        $notification->setUser($invoice->getAuthor());
  209. //
  210. //        $this->entityManager->persist($notification);
  211. //        $email = null;
  212.         $pmanger $invoice->getManager();
  213.         if ($pmanger !== null) {
  214.             $manager $pmanger->getAccount();
  215.             if ($manager !== null) {
  216.                 $notification = new Notification();
  217.                 $notification->setTitle($subject);
  218.                 $notification->setMessage($body);
  219.                 $notification->setType(Notification::TYPE_INVOICE);
  220.                 $notification->setUrl($linkToInvoice);
  221.                 $notification->setUser($manager);
  222.                 $this->entityManager->persist($notification);
  223.             }
  224.             $to Emailer::$defaultTo;
  225.             if ($pmanger->getEmail()) {
  226.                 $to $pmanger->getEmail();
  227.                 $mName $pmanger->getName();
  228.             }
  229.             $htmlContents $this->twig->render('jit/invoice/_tpl_not_payed.html.twig', [
  230.                 'dest_name' => $mName,
  231.                 'invoice' => $invoice,
  232.             ]);
  233.             $this->emailer->sendMail(Emailer::$defaultFrom$to$subject$body$htmlContents);
  234.         }
  235.         
  236.         $powner $invoice->getDest();
  237.         if ($powner !== null) {
  238.             $owner $powner->getAccount();
  239.             if ($owner !== null) {
  240.                 $notification = new Notification();
  241.                 $notification->setTitle($subject);
  242.                 $notification->setMessage($body);
  243.                 $notification->setType(Notification::TYPE_INVOICE);
  244.                 $notification->setUrl($linkToInvoice);
  245.                 $notification->setUser($owner);
  246.                 $this->entityManager->persist($notification);
  247.             }
  248.             $to Emailer::$defaultTo;
  249.             if ($powner->getEmail()) {
  250.                 $to $powner->getEmail();
  251.                 $mName $powner->getName();
  252.             }
  253.             $htmlContents $this->twig->render('jit/invoice/_tpl_not_pay.html.twig', [
  254.                 'dest_name' => $mName,
  255.                 'invoice' => $invoice,
  256.             ]);
  257.             $this->emailer->sendMail(Emailer::$defaultFrom$to$subject$body$htmlContents);
  258.         }
  259.         $this->entityManager->flush();
  260. //        $this->mailer->send($email);
  261.     }
  262.     public function onInvoiceArchived(InvoiceNotificationEvent $event): void {
  263.         /** @var Invoice $invoice */
  264.         $invoice $event->getInvoice();
  265.         $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_view',
  266.                 ['slug' => $invoice->getSlug()],
  267.                 UrlGeneratorInterface::ABSOLUTE_URL);
  268.         $subject $this->translator->trans('notification.invoice_archived');
  269.         $body $this->translator->trans('notification.invoice_archived.description', [
  270.             '%title%' => $invoice->getName(),
  271.             '%link%' => $linkToInvoice,
  272.         ]);
  273.         $notification = new Notification();
  274.         $notification->setTitle($subject);
  275.         $notification->setMessage($body);
  276.         $notification->setType(Notification::TYPE_INVOICE);
  277.         $notification->setUrl($linkToInvoice);
  278.         $notification->setUser($invoice->getAuthor());
  279.         $this->entityManager->persist($notification);
  280.         $this->entityManager->flush();
  281.         $email = (new Email())
  282.                 ->from($this->sender)
  283.                 ->to($invoice->getAuthor()->getEmail())
  284.                 ->subject($subject)
  285.                 ->html($body)
  286.         ;
  287. //        $this->mailer->send($email);
  288.     }
  289.     public function onInvoiceDeleted(InvoiceNotificationEvent $event): void {
  290.         /** @var Invoice $invoice */
  291.         $invoice $event->getInvoice();
  292.         $linkToInvoice $this->urlGenerator->generate('pr_jit_invoice_index',
  293.                 [],
  294.                 UrlGeneratorInterface::ABSOLUTE_URL);
  295.         $subject $this->translator->trans('notification.invoice_deleted');
  296.         $body $this->translator->trans('notification.invoice_deleted.description', [
  297.             '%title%' => $invoice->getName(),
  298.             '%link%' => $linkToInvoice,
  299.         ]);
  300.         $email null;
  301.         $manager null;
  302.         $pmanger $invoice->getManager();
  303.         if ($pmanger != null) {
  304.             $manager $pmanger->getAccount();
  305.         }
  306.         if ($manager !== null) {
  307.             $notification = new Notification();
  308.             $notification->setTitle($subject);
  309.             $notification->setMessage($body);
  310.             $notification->setType(Notification::TYPE_INVOICE);
  311.             $notification->setUrl($linkToInvoice);
  312.             $notification->setUser($manager);
  313.             $this->entityManager->persist($notification);
  314.             if ($email === null) {
  315.                 $email = (new Email())
  316.                         ->from($this->sender)
  317.                         ->to($manager->getEmail())
  318.                         ->subject($subject)
  319.                         ->html($body)
  320.                 ;
  321.             } else {
  322.                 $email->addTo($manager->getEmail());
  323.             }
  324.         }
  325.         $this->entityManager->flush();
  326. //        $this->mailer->send($email);
  327.     }
  328. }