src/EventListener/InvoiceBatchSubscriber.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\InvoiceBatch;
  4. use App\Event\InvoiceBatchEvent;
  5. use App\Model\InvoiceBatchFactory;
  6. use App\Model\InvoiceFactory;
  7. use App\Model\InvoiceProcessor;
  8. use App\Service\SupplierConnectionService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class InvoiceBatchSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var InvoiceFactory
  16.      */
  17.     private $invoiceFactory;
  18.     private $invoiceBatchFactory;
  19.     private $invoiceProcessor;
  20.     /** @var EventDispatcherInterface $eventDispatcher */
  21.     private $eventDispatcher;
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $entityManager;
  26.     /**
  27.      * @var SupplierConnectionService
  28.      */
  29.     private $supplierConnectionService;
  30.     public function __construct(
  31.         InvoiceFactory $invoiceFactory,
  32.         InvoiceBatchFactory $invoiceBatchFactory,
  33.         EntityManagerInterface $entityManager,
  34.         EventDispatcherInterface $eventDispatcher,
  35.         InvoiceProcessor $invoiceProcessor,
  36.         SupplierConnectionService $supplierConnectionService
  37.     ) {
  38.         $this->invoiceFactory $invoiceFactory;
  39.         $this->invoiceBatchFactory $invoiceBatchFactory;
  40.         $this->entityManager $entityManager;
  41.         $this->eventDispatcher $eventDispatcher;
  42.         $this->invoiceProcessor $invoiceProcessor;
  43.         $this->supplierConnectionService $supplierConnectionService;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             InvoiceBatchEvent::STATUS_REFRESH_EVENT => [ 'onInvoiceBatchStatusRefresh' ],
  49.             InvoiceBatchEvent::PROCESS_EVENT => [ 'onInvoiceBatchProcess' ]
  50.         ];
  51.     }
  52.     public function onInvoiceBatchStatusRefresh(InvoiceBatchEvent $event)
  53.     {
  54.         $invoiceBatchEntity $event->getInvoiceBatch();
  55.         $cptPending 0;
  56.         $cptProcessing 0;
  57.         $cptProcessed 0;
  58.         $invoiceEntities $invoiceBatchEntity->getInvoices();
  59.         if ($invoiceEntities) {
  60.             foreach ($invoiceEntities as $invoiceEntity) {
  61.                 $invoice $this->invoiceFactory->createModel($invoiceEntity);
  62.                 if ($invoice->isPending()) {
  63.                     $cptPending++;
  64.                 } elseif ($invoice->isProcessing()) {
  65.                     $cptProcessing++;
  66.                 } else {
  67.                     $cptProcessed++;
  68.                 }
  69.             }
  70.             if ($cptPending $cptProcessing == 0) {
  71.                 $newStatus InvoiceBatch::STATUS_PROCESSED;
  72.             } elseif ($cptPending === $invoiceBatchEntity->getInvoices()->count()) {
  73.                 $newStatus InvoiceBatch::STATUS_PENDING;
  74.             } else {
  75.                 $newStatus InvoiceBatch::STATUS_PROCESSING;
  76.             }
  77.             $invoiceBatchEntity->setStatus($newStatus);
  78.             $this->entityManager->flush();
  79.             $invoiceBatch $this->invoiceBatchFactory->createModel($invoiceBatchEntity);
  80.             if ($invoiceBatch->isProcessed()) {
  81.                 $invoiceBatchEvent = new InvoiceBatchEvent($invoiceBatchEntity);
  82.                 $this->eventDispatcher->dispatch(
  83.                     $invoiceBatchEvent,
  84.                     InvoiceBatchEvent::PROCESS_EVENT
  85.                 );
  86.             }
  87.         }
  88.     }
  89.     public function onInvoiceBatchProcess(InvoiceBatchEvent $event)
  90.     {
  91.         $invoiceBatchEntity $event->getInvoiceBatch();
  92.         foreach ($invoiceBatchEntity->getInvoices() as $invoiceEntity) {
  93.             $invoice $this->invoiceFactory->createModel($invoiceEntity);
  94.             // On ne prend pas en compte les rejetées
  95.             if ($invoice->isProcessed()) {
  96.                 $this->invoiceProcessor->setInvoice($invoiceEntity);
  97.                 $supplierCustomer $this->invoiceProcessor->getSupplierCustomer();
  98.                 $supplierCustomerNoTP $this->supplierConnectionService->getSupplierCustomerNoTP();
  99.                 if (is_null($supplierCustomer->getId())) {
  100.                     $this->entityManager->persist($supplierCustomer);
  101.                 }
  102.                 if (! is_null($supplierCustomerNoTP)) {
  103.                     $this->entityManager->persist($supplierCustomerNoTP);
  104.                 }
  105.                 $this->entityManager->flush();
  106.             }
  107.             // Enable the products
  108.             if ($products $invoiceEntity->getProducts()) {
  109.                 foreach ($products as $product) {
  110.                     $product->setEnabled(true);
  111.                     $this->entityManager->flush();
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }