<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\EventSubscriber;
use App\Entity\Security\Notification;
use App\Entity\Training\Invoice;
use App\Event\InvoiceNotificationEvent;
use App\Repository\Security\UserRepository;
use App\Service\Mailer\Emailer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
/**
* Notifies post's author about new invoices.
*
*/
class InvoiceNotificationSubscriber implements EventSubscriberInterface {
private $translator;
private $urlGenerator;
private $sender;
private $userRepository;
private $entityManager;
private $emailer;
private $twig;
public function __construct(
UrlGeneratorInterface $urlGenerator,
EntityManagerInterface $entityManager,
TranslatorInterface $translator,
UserRepository $userRepository,
string $sender,
Emailer $emailer,
Environment $twig) {
$this->urlGenerator = $urlGenerator;
$this->translator = $translator;
$this->sender = $sender;
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
$this->emailer = $emailer;
$this->twig = $twig;
}
public static function getSubscribedEvents(): array {
return [
InvoiceNotificationEvent::CREATE => [['onInvoiceCreated', 1]],
InvoiceNotificationEvent::VALIDATE => [['onInvoiceValidated', 1]],
InvoiceNotificationEvent::PAY => [['onInvoicePayed', 1]],
// InvoiceNotificationEvent::ARCHIVE => 'onInvoiceArchived',
InvoiceNotificationEvent::ARCHIVE => [['onInvoiceArchived', 1]],
InvoiceNotificationEvent::DELETE => [['onInvoiceDeleted', 1]],
InvoiceNotificationEvent::GENERATE => [['onInvoicesGenerated', 1]],
];
}
public function onInvoiceCreated(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = $event->getInvoice();
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_view',
['slug' => $invoice->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.invoice_created');
$body = $this->translator->trans('notification.invoice_created.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
$manager = null;
$pmanger = $invoice->getManager();
if ($pmanger != null) {
$manager = $pmanger->getAccount();
}
if ($manager !== null) {
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($manager);
$this->entityManager->persist($notification);
$this->entityManager->flush();
$this->emailer->sendMail ($this->sender, $manager->getEmail(), $subject, $body);
// $this->emailer->sendMail($this->sender, $this->sender, $subject, $body);
}
}
public function onInvoicesGenerated(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = null;
$invoices = $event->getInvoices();
$tenancyName = 'no_tenancy';
if ($invoices[0]->getTenancy() !== null) {
$tenancyName = $invoices[0]->getTenancy()->getName();
}
$subject = 'PROIMMO - Génération factures - ' . $tenancyName . '(' . $invoices->count() . ' factures)';
$mName = '';
$to = Emailer::$defaultTo;
if ($invoices[0]->getManager() !== null and $invoices[0]->getManager()->getEmail() !== null) {
$to = $invoices[0]->getManager()->getEmail();
$mName = $invoices[0]->getManager()->getName();
}
$description = 'Bonjour ' . $mName . ',' . PHP_EOL . PHP_EOL . ' Ci-dessous les factures générées de l\'application PROIMMO.' . PHP_EOL;
$amount = 0;
foreach ($invoices as $invoice) {
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_view',
['slug' => $invoice->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL);
$ntitle = $this->translator->trans('notification.invoice_created');
$nbody = $this->translator->trans('notification.invoice_created.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
$manager = null;
$pmanger = $invoice->getManager();
if ($pmanger != null) {
$manager = $pmanger->getAccount();
}
if ($manager !== null) {
$notification = new Notification();
$notification->setTitle($ntitle);
$notification->setMessage($nbody);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($manager);
$this->entityManager->persist($notification);
}
$description .= ' ' . $invoice->getName() . ' --- ' . $this->urlGenerator->generate('pr_jit_invoice_view', ['slug' => $invoice->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL) . PHP_EOL;
$amount += $invoice->getAmt();
}
$this->entityManager->flush();
$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';
$htmlContents = $this->twig->render('jit/invoice/_tpl_not_generate.html.twig', [
'dest_name' => $mName,
'invoices' => $invoices,
]);
$this->emailer->sendMail(Emailer::$defaultFrom, $to, $subject, $description, $htmlContents);
}
public function onInvoiceValidated(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = $event->getInvoice();
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_view',
['slug' => $invoice->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.invoice_validated');
$body = $this->translator->trans('notification.invoice_validated.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
// $email = null;
$manager = null;
$pmanger = $invoice->getDest();
if ($pmanger != null) {
$manager = $pmanger->getAccount();
}
if ($manager !== null) {
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($manager);
$this->entityManager->persist($notification);
// if ($email === null) {
// $email = (new Email())
// ->from($this->sender)
// ->to($manager->getEmail())
// ->subject($subject)
// ->html($body)
// ;
// } else {
// $email->addTo($manager->getEmail());
// }
$to = Emailer::$defaultTo;
if ($pmanger->getEmail()) {
$to = $pmanger->getEmail();
$mName = $pmanger->getName();
}
$htmlContents = $this->twig->render('jit/invoice/_tpl_not_validate.html.twig', [
'dest_name' => $mName,
'invoice' => $invoice,
]);
$this->emailer->sendMail(Emailer::$defaultFrom, $to, $subject, $body, $htmlContents);
}
$this->entityManager->flush();
// if ($email !== null) {
// $this->mailer->send($email);
// }
}
public function onInvoicePayed(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = $event->getInvoice();
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_view',
['slug' => $invoice->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.invoice_payed');
$body = $this->translator->trans('notification.invoice_payed.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
// $notification = new Notification();
// $notification->setTitle($subject);
// $notification->setMessage($body);
// $notification->setType(Notification::TYPE_INVOICE);
// $notification->setUrl($linkToInvoice);
// $notification->setUser($invoice->getAuthor());
//
// $this->entityManager->persist($notification);
// $email = null;
$pmanger = $invoice->getManager();
if ($pmanger !== null) {
$manager = $pmanger->getAccount();
if ($manager !== null) {
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($manager);
$this->entityManager->persist($notification);
}
$to = Emailer::$defaultTo;
if ($pmanger->getEmail()) {
$to = $pmanger->getEmail();
$mName = $pmanger->getName();
}
$htmlContents = $this->twig->render('jit/invoice/_tpl_not_payed.html.twig', [
'dest_name' => $mName,
'invoice' => $invoice,
]);
$this->emailer->sendMail(Emailer::$defaultFrom, $to, $subject, $body, $htmlContents);
}
$powner = $invoice->getDest();
if ($powner !== null) {
$owner = $powner->getAccount();
if ($owner !== null) {
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($owner);
$this->entityManager->persist($notification);
}
$to = Emailer::$defaultTo;
if ($powner->getEmail()) {
$to = $powner->getEmail();
$mName = $powner->getName();
}
$htmlContents = $this->twig->render('jit/invoice/_tpl_not_pay.html.twig', [
'dest_name' => $mName,
'invoice' => $invoice,
]);
$this->emailer->sendMail(Emailer::$defaultFrom, $to, $subject, $body, $htmlContents);
}
$this->entityManager->flush();
// $this->mailer->send($email);
}
public function onInvoiceArchived(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = $event->getInvoice();
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_view',
['slug' => $invoice->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.invoice_archived');
$body = $this->translator->trans('notification.invoice_archived.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($invoice->getAuthor());
$this->entityManager->persist($notification);
$this->entityManager->flush();
$email = (new Email())
->from($this->sender)
->to($invoice->getAuthor()->getEmail())
->subject($subject)
->html($body)
;
// $this->mailer->send($email);
}
public function onInvoiceDeleted(InvoiceNotificationEvent $event): void {
/** @var Invoice $invoice */
$invoice = $event->getInvoice();
$linkToInvoice = $this->urlGenerator->generate('pr_jit_invoice_index',
[],
UrlGeneratorInterface::ABSOLUTE_URL);
$subject = $this->translator->trans('notification.invoice_deleted');
$body = $this->translator->trans('notification.invoice_deleted.description', [
'%title%' => $invoice->getName(),
'%link%' => $linkToInvoice,
]);
$email = null;
$manager = null;
$pmanger = $invoice->getManager();
if ($pmanger != null) {
$manager = $pmanger->getAccount();
}
if ($manager !== null) {
$notification = new Notification();
$notification->setTitle($subject);
$notification->setMessage($body);
$notification->setType(Notification::TYPE_INVOICE);
$notification->setUrl($linkToInvoice);
$notification->setUser($manager);
$this->entityManager->persist($notification);
if ($email === null) {
$email = (new Email())
->from($this->sender)
->to($manager->getEmail())
->subject($subject)
->html($body)
;
} else {
$email->addTo($manager->getEmail());
}
}
$this->entityManager->flush();
// $this->mailer->send($email);
}
}