src/EventSubscriber/Expediting/DocumentationKitGenerator.php line 42

  1. <?php
  2. namespace App\EventSubscriber\Expediting;
  3. use App\Doctrine\Type\Expediting\TransitType;
  4. use App\Dto\Expediting\DocumentationKitDatasheet;
  5. use App\Dto\Expediting\DocumentationKitDatasheetContainerInput;
  6. use App\Dto\Expediting\DocumentationKitDatasheetExpeditableInput;
  7. use App\Dto\Expediting\DocumentationKitDatasheetExpeditablePackagingInput;
  8. use App\Dto\Expediting\DocumentationKitDatasheetFreightInput;
  9. use App\Dto\Expediting\DocumentationKitDatasheetPackageInput;
  10. use App\Entity\Expediting\Document;
  11. use App\Entity\Expediting\DocumentationKit;
  12. use App\Entity\Expediting\Item;
  13. use App\Entity\Expediting\Part;
  14. use App\Entity\Expediting\Shipment;
  15. use App\Event\Expediting\DocumentationKitRequestEvent;
  16. use App\Event\Expediting\ShipmentEventInterface;
  17. use App\EventSubscriber\TasksCancelerTrait;
  18. use App\Repository\Scheduler\TaskRepository;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Symfony\Bundle\SecurityBundle\Security;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. readonly class DocumentationKitGenerator implements EventSubscriberInterface
  23. {
  24.     use TasksCancelerTrait;
  25.     public function __construct(
  26.         private EntityManagerInterface $entityManager,
  27.         private TaskRepository         $taskRepository,
  28.         private Security               $security,
  29.     ) {}
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             DocumentationKitRequestEvent::IDENTIFIER => ['onDocumentationKitRequest'0],
  34.         ];
  35.     }
  36.     public function onDocumentationKitRequest(ShipmentEventInterface $event): void
  37.     {
  38.         $shipment $event->getShipment();
  39.         $documentationKit = (new DocumentationKit())
  40.             ->setData($this->getKitDatasheet($shipment))
  41.             ->setTransitType(TransitType::EXPORT)
  42.             ->setCreatedBy($this->security->getUser());
  43.         $shipment->addDocumentationKit($documentationKit);
  44.         $this->entityManager->persist($shipment);
  45.         $this->entityManager->flush();
  46.     }
  47.     private function getKitDatasheet(Shipment $shipment): DocumentationKitDatasheet
  48.     {
  49.         $datasheet = new DocumentationKitDatasheet();
  50.         if ($shipment->getDocumentationKit()?->getData() instanceof DocumentationKitDatasheet) {
  51.             $previousKit $shipment->getDocumentationKit()->getData();
  52.             $datasheet->shipper $previousKit->shipper;
  53.             $datasheet->shipperAddress $previousKit->shipperAddress;
  54.             $datasheet->buyer $previousKit->buyer;
  55.             $datasheet->buyerAddress $previousKit->buyerAddress;
  56.             $datasheet->consignee $previousKit->consignee;
  57.             $datasheet->consigneeAddress $previousKit->consigneeAddress;
  58.             $datasheet->notifyParty $previousKit->notifyParty;
  59.             $datasheet->notifyPartyAddress $previousKit->notifyPartyAddress;
  60.             $datasheet->formMNumber $previousKit->formMNumber;
  61.             $datasheet->baNumber $previousKit->baNumber;
  62.         } else {
  63.             $datasheet->shipper 'Acme Corp';
  64.             $datasheet->shipperAddress '123 Acme Way';
  65.             $datasheet->buyer 'Buyer Inc';
  66.             $datasheet->buyerAddress '456 Buyer Blvd';
  67.             $datasheet->consignee 'Consignee Ltd';
  68.             $datasheet->consigneeAddress '789 Consignee Street';
  69.             $datasheet->notifyParty 'Notify LLC';
  70.             $datasheet->notifyPartyAddress '321 Notify Circle';
  71.             $datasheet->formMNumber 'FM00123';
  72.             $datasheet->baNumber 'BA00123';
  73.         }
  74.         $datasheet->freights $this->getFreights($shipment);
  75.         $datasheet->containers $this->getContainers($shipment);
  76.         $datasheet->packages $this->getPackages($shipment);
  77.         $datasheet->items $this->getItems($shipment);
  78.         return $datasheet;
  79.     }
  80.     /** @return DocumentationKitDatasheetFreightInput[] */
  81.     private function getFreights(Shipment $shipment): array
  82.     {
  83.         $freights = [];
  84.         foreach ($shipment->getWorkOrders() as $workOrder) {
  85.             foreach ($workOrder->getFreights() as $freight) {
  86.                 $itemsIds = [];
  87.                 foreach ($freight->getItems() as $item) {
  88.                     $itemsIds []= $item->getId();
  89.                 }
  90.                 $freights []= new DocumentationKitDatasheetFreightInput(
  91.                     id$freight->getId(),
  92.                     workOrderReference$workOrder->getReference(),
  93.                     orderNumber$freight->getOrder()->getNumber(),
  94.                     supplier$freight->getOrder()->getSupplier()->getName(),
  95.                     invoicesNumbers$freight->getInvoicesNumbers(),
  96.                     itemsIds$itemsIds,
  97.                 );
  98.             }
  99.         }
  100.         return $freights;
  101.     }
  102.     /** @return DocumentationKitDatasheetContainerInput[] */
  103.     private function getContainers(Shipment $shipment): array
  104.     {
  105.         $containers = [];
  106.         foreach ($shipment->getContainers() as $container) {
  107.             $containers []= new DocumentationKitDatasheetContainerInput(
  108.                 id$container->getId(),
  109.                 sealNumber$container->getsealNumber(),
  110.                 identificationNumber$container->getidentificationNumber(),
  111.                 containerType$container->getcontainerType(),
  112.                 containerSize$container->getcontainerSize(),
  113.             );
  114.         }
  115.         return $containers;
  116.     }
  117.     /** @return DocumentationKitDatasheetPackageInput[] */
  118.     private function getPackages(Shipment $shipment): array
  119.     {
  120.         $packages = [];
  121.         foreach ($shipment->getPackages() as $package) {
  122.             $containerId null;
  123.             foreach ($package->getContainers() as $container) {
  124.                 if ($container->getShipment() === $shipment) {
  125.                     $containerId $container->getId();
  126.                     break;
  127.                 }
  128.             }
  129.             $ordersNumbers = [];
  130.             $workOrdersReferences = [];
  131.             foreach ($package->getItems() as $item) {
  132.                 if ($item instanceof Item) {
  133.                     $ordersNumbers []= $item->getOrder()->getNumber();
  134.                     $workOrdersReferences []= $item->getFreight()->getWorkOrder()->getReference();
  135.                 } elseif ($item instanceof Part) {
  136.                     $ordersNumbers []= $item->getItem()->getOrder()->getNumber();
  137.                     $ordersNumbers []= $item->getItem()->getFreight()->getWorkOrder()->getReference();
  138.                 } elseif ($item instanceof Document) {
  139.                     // TODO : Add documents !
  140.                 }
  141.             }
  142.             $packages []= new DocumentationKitDatasheetPackageInput(
  143.                 id$package->getId(),
  144.                 containerId$containerId,
  145.                 workOrdersReferences$workOrdersReferences,
  146.                 ordersNumbers$ordersNumbers,
  147.                 number$package->getPackageNumber(),
  148.                 type$package->getCasingType(),
  149.                 material$package->getCasingMaterial(),
  150.                 width$package->getWidth(),
  151.                 length$package->getLength(),
  152.                 height$package->getHeight(),
  153.                 weight$package->getWeight(),
  154.             );
  155.         }
  156.         return $packages;
  157.     }
  158.     /** @return DocumentationKitDatasheetExpeditableInput[] */
  159.     private function getItems(Shipment $shipment): array
  160.     {
  161.         $map = [];
  162.         $packagesIds = [];
  163.         foreach ($shipment->getPackages() as $package) {
  164.             $packagesIds []= $package->getId();
  165.             foreach ($package->getItems() as $item) {
  166.                 if ($item instanceof Part) {
  167.                     $map[$item->getId()] = $item->getItem();
  168.                 } elseif ($item instanceof Item) {
  169.                     $map[$item->getId()] = $item;
  170.                 } else {
  171.                     // TODO : handle Document
  172.                 }
  173.             }
  174.         }
  175.         $items = [];
  176.         foreach ($map as $item) {
  177.             $packagings = [];
  178.             foreach ($item->getParts() as $part) {
  179.                 foreach ($part->getPackages() as $package) {
  180.                     if (!in_array($package->getId(), $packagesIds)) {
  181.                         continue;
  182.                     }
  183.                     $packagings []= new DocumentationKitDatasheetExpeditablePackagingInput(
  184.                         id$package->getId(),
  185.                         quantity0,
  186.                         isParttrue,
  187.                     );
  188.                 }
  189.             }
  190.             foreach ($item->getPackages() as $package) {
  191.                 if (!in_array($package->getId(), $packagesIds)) {
  192.                     continue;
  193.                 }
  194.                 $packagings []= new DocumentationKitDatasheetExpeditablePackagingInput(
  195.                     id$package->getId(),
  196.                     quantity$item->getQuantity(),
  197.                     isPartfalse,
  198.                 );
  199.             }
  200.             $items []= new DocumentationKitDatasheetExpeditableInput(
  201.                 id$item->getId(),
  202.                 workOrderReference$item->getFreight()->getWorkOrder()->getReference(),
  203.                 orderNumber$item->getOrder()->getNumber(),
  204.                 number$item->getLineItem()->getNumber(),
  205.                 materialNumber$item->getLineItem()->getNumber(),
  206.                 description$item->getLineItem()->getCustomerDescription() ?? $item->getLineItem()->getSupplierDescription(),
  207.                 quantity$item->getQuantity(),
  208.                 unitPrice$item->getLineItem()->getValue() / $item->getLineItem()->getQuantity(),
  209.                 countryOfOrigin$item->getLineItem()->getCountryOfOrigin(),
  210.                 hsCode$item->getLineItem()->getHsCode(),
  211.                 asset$item->getLineItem()->getAsset()?->getName(),
  212.                 isDangerous$item->isDangerous(),
  213.                 packagings$packagings,
  214.             );
  215.         }
  216.         return $items;
  217.     }
  218. }