src/EventSubscriber/Finance/Bill/TransportBillDrafter.php line 27
<?php
namespace App\EventSubscriber\Finance\Bill;
use App\Doctrine\Type\Expediting\ShipmentType;
use App\Doctrine\Type\Finance\BillType;
use App\Doctrine\Type\Finance\PayableStatus;
use App\Event\Expediting\ShipmentStatusUpdateEvent;
use DateTime;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
readonly class TransportBillDrafter implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
) {}
public static function getSubscribedEvents(): array
{
return [
ShipmentStatusUpdateEvent::IDENTIFIER => ['process', -100],
];
}
public function process(ShipmentStatusUpdateEvent $event): void
{
$segment = $event->getShippingSegment();
$shippingPlan = $segment->getShippingPlan();
$workOrder = $shippingPlan->getWorkOrder();
if (null === $workOrder || !in_array(ShipmentType::PICKUP, $segment->getTypes())) {
return;
}
$quotation = null;
$quotations = $shippingPlan->getQuotations();
foreach ($quotations as $entity) {
if (PayableStatus::ACCEPTED !== $entity->getStatus()) {
continue;
}
$quotation = $entity;
}
if (null === $quotation) {
return;
}
try {
$this->entityManager->getConnection()->executeStatement(
'UPDATE finance_bill SET type = :type WHERE payable_to = :agentId AND shipping_plan_id = :planId',
[
'type' => BillType::BILL->value,
'planId' => $shippingPlan->getId(),
'agentId' => $quotation->getPayableTo()->getId(),
],
);
$quotation->setDueOn(new DateTime());
$quotation->setStatus(PayableStatus::QUOTED);
$this->entityManager->persist($quotation);
$this->entityManager->flush();
$this->entityManager->clear();
} catch (Exception $e) {
dd($e->$e->getMessage());
}
}
}