src/EventSubscriber/Finance/Bill/TransportBillDrafter.php line 27

  1. <?php
  2. namespace App\EventSubscriber\Finance\Bill;
  3. use App\Doctrine\Type\Expediting\ShipmentType;
  4. use App\Doctrine\Type\Finance\BillType;
  5. use App\Doctrine\Type\Finance\PayableStatus;
  6. use App\Event\Expediting\ShipmentStatusUpdateEvent;
  7. use DateTime;
  8. use Doctrine\DBAL\Exception;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. readonly class TransportBillDrafter implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private EntityManagerInterface $entityManager,
  15.     ) {}
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             ShipmentStatusUpdateEvent::IDENTIFIER => ['process', -100],
  20.         ];
  21.     }
  22.     public function process(ShipmentStatusUpdateEvent $event): void
  23.     {
  24.         $segment $event->getShippingSegment();
  25.         $shippingPlan $segment->getShippingPlan();
  26.         $workOrder $shippingPlan->getWorkOrder();
  27.         if (null === $workOrder || !in_array(ShipmentType::PICKUP$segment->getTypes())) {
  28.             return;
  29.         }
  30.         $quotation null;
  31.         $quotations $shippingPlan->getQuotations();
  32.         foreach ($quotations as $entity) {
  33.             if (PayableStatus::ACCEPTED !== $entity->getStatus()) {
  34.                 continue;
  35.             }
  36.             $quotation $entity;
  37.         }
  38.         if (null === $quotation) {
  39.             return;
  40.         }
  41.         try {
  42.             $this->entityManager->getConnection()->executeStatement(
  43.                 'UPDATE finance_bill SET type = :type WHERE payable_to = :agentId AND shipping_plan_id = :planId',
  44.                 [
  45.                     'type' => BillType::BILL->value,
  46.                     'planId' => $shippingPlan->getId(),
  47.                     'agentId' => $quotation->getPayableTo()->getId(),
  48.                 ],
  49.             );
  50.             $quotation->setDueOn(new DateTime());
  51.             $quotation->setStatus(PayableStatus::QUOTED);
  52.             $this->entityManager->persist($quotation);
  53.             $this->entityManager->flush();
  54.             $this->entityManager->clear();
  55.         } catch (Exception $e) {
  56.             dd($e->$e->getMessage());
  57.         }
  58.     }
  59. }