src/EventSubscriber/InvoiceSubscriber.php line 156

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Event;
  4. use App\Entity\Invoice;
  5. use App\Event\InvoiceEvent;
  6. use App\Repository\UserRepository;
  7. use App\Service\Constants\LogEvents;
  8. use App\Service\Invoice\InvoiceService;
  9. use App\Service\Invoice\Outgoing\OutgoingInvoiceService;
  10. use App\Service\Log\QuotationLogService;
  11. use App\Service\Subscription\SubscriptionService;
  12. use App\Service\UserLog;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Workflow\Registry;
  17. class InvoiceSubscriber implements EventSubscriberInterface
  18. {
  19.     protected Security $security;
  20.     protected UserRepository $userRepo;
  21.     protected SubscriptionService $subscriptionService;
  22.     private EntityManagerInterface $em;
  23.     private QuotationLogService $logService;
  24.     private UserLog $userLog;
  25.     private InvoiceService $invoiceService;
  26.     private OutgoingInvoiceService $outgoingInvoiceService;
  27.     private Registry $workflows;
  28.     public function __construct(
  29.         SubscriptionService    $subscriptionService,
  30.         Security               $security,
  31.         EntityManagerInterface $em,
  32.         UserRepository         $userRepo,
  33.         QuotationLogService    $logService,
  34.         InvoiceService         $invoiceService,
  35.         UserLog                $userLog,
  36.         Registry               $workflows,
  37.         OutgoingInvoiceService $outgoingInvoiceService
  38.     )
  39.     {
  40.         $this->security $security;
  41.         $this->subscriptionService $subscriptionService;
  42.         $this->userRepo $userRepo;
  43.         $this->em $em;
  44.         $this->logService $logService;
  45.         $this->userLog $userLog;
  46.         $this->invoiceService $invoiceService;
  47.         $this->workflows $workflows;
  48.         $this->outgoingInvoiceService $outgoingInvoiceService;
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             InvoiceEvent::CREATED => [
  54.                 ['logInvoiceCreated'],
  55.                 ['updateTotals'],
  56.                 ['updateCustomerJsonData'],
  57.                 ['updateInvoiceJsonData'],
  58.                 ['invoiceCreatedNotify']
  59.             ],
  60.             InvoiceEvent::UPDATED => [
  61.                 ['logInvoiceUpdated'],
  62.                 ['updateTotals'],
  63.                 ['updateInvoiceJsonData']
  64.             ],
  65.             InvoiceEvent::UPDATED_IS_SENT => [
  66.                 ['logIsSentChange'],
  67.                 ['updateQuotationStatus'],
  68.             ],
  69.             InvoiceEvent::CREATED_INVOICE_COPY => [
  70.                 ['invoiceCreatedNotify']
  71.             ],
  72.             InvoiceEvent::UPDATED_IS_CANCELED => [
  73.                 ['logInvoiceCanceled'],
  74.             ]
  75.         ];
  76.     }
  77.     public function updateQuotationStatus(InvoiceEvent $event)
  78.     {
  79.         if ($quotation $event->getInvoice()->getQuotation()) {
  80.             $workflow $this->workflows->get($quotation);
  81.             if ($workflow->can($quotation'to_waiting_payment')) {
  82.                 $workflow->apply($quotation'to_waiting_payment');
  83.                 $this->em->flush();
  84.             }
  85.         }
  86.     }
  87.     public function logIsSentChange(InvoiceEvent $event)
  88.     {
  89.         foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
  90.             $this->logService->logAction(
  91.                 $quotation,
  92.                 LogEvents::INVOICE_UPDATED_IS_SENT,
  93.                 $event->getInvoice()->getInvoiceNumber(). ' was set as sent to client.'
  94.             );
  95.         }
  96.         $this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED_IS_SENT$event->getInvoice()->getInvoiceNumber());
  97.     }
  98.     public function logInvoiceCanceled(InvoiceEvent $event)
  99.     {
  100.         foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
  101.             $this->logService->logAction(
  102.                 $quotation,
  103.                 LogEvents::INVOICE_UPDATED_IS_CANCELED,
  104.                 $event->getInvoice()->getInvoiceNumber()
  105.             );
  106.         }
  107.         $this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED_IS_CANCELED,
  108.             $event->getInvoice()->getInvoiceNumber()
  109.         );
  110.     }
  111.     public function logInvoiceCreated(InvoiceEvent $event)
  112.     {
  113.         $this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_CREATED,
  114.             $event->getInvoice()->getInvoiceNumber()
  115.         );
  116.         foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
  117.             $this->logService->logAction(
  118.                 $quotation,
  119.                 LogEvents::INVOICE_CREATED,
  120.                 $event->getInvoice()->getInvoiceNumber()
  121.             );
  122.         }
  123.     }
  124.     public function logInvoiceUpdated(InvoiceEvent $event)
  125.     {
  126.         $this->userLog->logEntityAction($event->getInvoice(), LogEvents::INVOICE_UPDATED,
  127.             $event->getInvoice()->getInvoiceNumber()
  128.         );
  129.         foreach($event->getInvoice()->getAllInvoiceQuotations() as $quotation) {
  130.             $this->logService->logAction(
  131.                 $quotation,
  132.                 LogEvents::INVOICE_UPDATED,
  133.                 $event->getInvoice()->getInvoiceNumber()
  134.             );
  135.         }
  136.     }
  137.     public function updateTotals(InvoiceEvent $event)
  138.     {
  139.         $this->invoiceService->updateInvoiceTotalSumFromRows($event->getInvoice());
  140.     }
  141.     public function updateInvoiceJsonData(InvoiceEvent $event)
  142.     {
  143.         $invoice $event->getInvoice();
  144.         if ($invoice->getInvoiceDirection() == Invoice::INVOICE_DIRECTION_OUTGOING) {
  145.             $this->outgoingInvoiceService->updateInvoiceJsonData($invoice);
  146.         }
  147.     }
  148.     public function updateCustomerJsonData(InvoiceEvent $event)
  149.     {
  150.         $invoice $event->getInvoice();
  151.         if ($invoice->getInvoiceDirection() == Invoice::INVOICE_DIRECTION_OUTGOING) {
  152.             $this->outgoingInvoiceService->updateCustomerJsonData($invoice);
  153.         }
  154.     }
  155.     public function invoiceCreatedNotify(InvoiceEvent $event)
  156.     {
  157.         $this->subscriptionService->proceedEvent(
  158.             Event::INVOICE_CREATED_NOTIFICATION,
  159.             null,
  160.             [
  161.                 'uuid' => $event->getInvoice()->getUuid(),
  162.                 'invoiceNumber' => $event->getInvoice()->getInvoiceNumber(),
  163.                 'direction' => $event->getInvoice()->getInvoiceDirection()
  164.             ]
  165.         );
  166.     }
  167. }