<?php
namespace App\EventSubscriber;
use App\Entity\Event;
use App\Entity\Invoice;
use App\Event\InvoiceEvent;
use App\Repository\UserRepository;
use App\Service\Constants\LogEvents;
use App\Service\Invoice\InvoiceService;
use App\Service\Invoice\Outgoing\OutgoingInvoiceService;
use App\Service\Log\QuotationLogService;
use App\Service\Subscription\SubscriptionService;
use App\Service\UserLog;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Registry;
class InvoiceSubscriber implements EventSubscriberInterface
{
protected Security $security;
protected UserRepository $userRepo;
protected SubscriptionService $subscriptionService;
private EntityManagerInterface $em;
private QuotationLogService $logService;
private UserLog $userLog;
private InvoiceService $invoiceService;
private OutgoingInvoiceService $outgoingInvoiceService;
private Registry $workflows;
public function __construct(
SubscriptionService $subscriptionService,
Security $security,
EntityManagerInterface $em,
UserRepository $userRepo,
QuotationLogService $logService,
InvoiceService $invoiceService,
UserLog $userLog,
Registry $workflows,
OutgoingInvoiceService $outgoingInvoiceService
)
{
$this->security = $security;
$this->subscriptionService = $subscriptionService;
$this->userRepo = $userRepo;
$this->em = $em;
$this->logService = $logService;
$this->userLog = $userLog;
$this->invoiceService = $invoiceService;
$this->workflows = $workflows;
$this->outgoingInvoiceService = $outgoingInvoiceService;
}
public static function getSubscribedEvents(): array
{
return [
InvoiceEvent::CREATED => [
['logInvoiceCreated'],
['updateTotals'],
['updateCustomerJsonData'],
['updateInvoiceJsonData'],
['invoiceCreatedNotify']
],
InvoiceEvent::UPDATED => [
['logInvoiceUpdated'],
['updateTotals'],
['updateInvoiceJsonData']
],
InvoiceEvent::UPDATED_IS_SENT => [
['logIsSentChange'],
['updateQuotationStatus'],
],
InvoiceEvent::CREATED_INVOICE_COPY => [
['invoiceCreatedNotify']
],
InvoiceEvent::UPDATED_IS_CANCELED => [
['logInvoiceCanceled'],
]
];
}
public function updateQuotationStatus(InvoiceEvent $event)
{
if ($quotation = $event->getInvoice()->getQuotation()) {
$workflow = $this->workflows->get($quotation);
if ($workflow->can($quotation, 'to_waiting_payment')) {
$workflow->apply($quotation, 'to_waiting_payment');
$this->em->flush();
}
}
}
public function logIsSentChange(InvoiceEvent $event)
{
foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
$this->logService->logAction(
$quotation,
LogEvents::INVOICE_UPDATED_IS_SENT,
$event->getInvoice()->getInvoiceNumber(). ' was set as sent to client.'
);
}
$this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED_IS_SENT, $event->getInvoice()->getInvoiceNumber());
}
public function logInvoiceCanceled(InvoiceEvent $event)
{
foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
$this->logService->logAction(
$quotation,
LogEvents::INVOICE_UPDATED_IS_CANCELED,
$event->getInvoice()->getInvoiceNumber()
);
}
$this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED_IS_CANCELED,
$event->getInvoice()->getInvoiceNumber()
);
}
public function logInvoiceCreated(InvoiceEvent $event)
{
$this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_CREATED,
$event->getInvoice()->getInvoiceNumber()
);
foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
$this->logService->logAction(
$quotation,
LogEvents::INVOICE_CREATED,
$event->getInvoice()->getInvoiceNumber()
);
}
}
public function logInvoiceUpdated(InvoiceEvent $event)
{
$this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED,
$event->getInvoice()->getInvoiceNumber()
);
foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
$this->logService->logAction(
$quotation,
LogEvents::INVOICE_UPDATED,
$event->getInvoice()->getInvoiceNumber()
);
}
}
public function updateTotals(InvoiceEvent $event)
{
$this->invoiceService->updateInvoiceTotalSumFromRows($event->getInvoice());
}
public function updateInvoiceJsonData(InvoiceEvent $event)
{
$invoice = $event->getInvoice();
if ($invoice->getInvoiceDirection() == Invoice::INVOICE_DIRECTION_OUTGOING) {
$this->outgoingInvoiceService->updateInvoiceJsonData($invoice);
}
}
public function updateCustomerJsonData(InvoiceEvent $event)
{
$invoice = $event->getInvoice();
if ($invoice->getInvoiceDirection() == Invoice::INVOICE_DIRECTION_OUTGOING) {
$this->outgoingInvoiceService->updateCustomerJsonData($invoice);
}
}
public function invoiceCreatedNotify(InvoiceEvent $event)
{
$this->subscriptionService->proceedEvent(
Event::INVOICE_CREATED_NOTIFICATION,
null,
[
'uuid' => $event->getInvoice()->getUuid(),
'invoiceNumber' => $event->getInvoice()->getInvoiceNumber(),
'direction' => $event->getInvoice()->getInvoiceDirection()
]
);
}
}