src/Controller/ComponentInventoryController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ComponentInventoryItem;
  4. use App\Form\Type\ComponentInventory\BatchSetQuantityType;
  5. use App\Form\Type\ComponentInventory\BatchDecreaseQuantityType;
  6. use App\Form\Type\ComponentInventory\BatchIncreaseQuantityType;
  7. use App\Form\Type\ComponentInventory\DecreaseQuantityType;
  8. use App\Form\Type\ComponentInventory\IncreaseQuantityType;
  9. use App\Form\Type\ComponentInventory\ItemCreateType;
  10. use App\Form\Type\ComponentInventory\ItemUpdateType;
  11. use App\Form\Type\ComponentInventory\LogFilterType;
  12. use App\Model\ComponentInventory\BatchSetQuantity;
  13. use App\Model\ComponentInventory\BatchDecreaseQuantity;
  14. use App\Model\ComponentInventory\BatchIncreaseQuantity;
  15. use App\Model\ComponentInventory\DecreaseQuantity;
  16. use App\Model\ComponentInventory\IncreaseQuantity;
  17. use App\Model\ComponentInventory\ItemCreate;
  18. use App\Model\ComponentInventory\ItemUpdate;
  19. use App\Model\DateRange;
  20. use App\Model\ModalResponse;
  21. use App\Repository\ComponentInventoryItemRepository;
  22. use App\Repository\ComponentInventoryLogEntryRepository;
  23. use App\Service\ComponentInventoryService;
  24. use Exception;
  25. use Symfony\Component\HttpFoundation\File\UploadedFile;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. #[Route('/component-inventory')]
  30. class ComponentInventoryController extends AbstractController
  31. {
  32.     const BATCH_DECREASE_DATA_KEY 'batchDecreaseData';
  33.     const BATCH_INCREASE_DATA_KEY 'batchIncreaseData';
  34.     const BATCH_SET_DATA_KEY 'batchSetData';
  35.     const HIDE_ZERO_QUANTITY_KEY 'hideZeroQuantity';
  36.     protected ComponentInventoryItemRepository $componentInventoryItemRepository;
  37.     protected ComponentInventoryLogEntryRepository $componentInventoryLogEntryRepository;
  38.     protected ComponentInventoryService $componentInventoryService;
  39.     public function __construct(
  40.         ComponentInventoryItemRepository $componentInventoryItemRepository,
  41.         ComponentInventoryLogEntryRepository $componentInventoryLogEntryRepository,
  42.         ComponentInventoryService $componentInventoryService
  43.     ) {
  44.         $this->componentInventoryItemRepository $componentInventoryItemRepository;
  45.         $this->componentInventoryLogEntryRepository $componentInventoryLogEntryRepository;
  46.         $this->componentInventoryService $componentInventoryService;
  47.     }
  48.     /**
  49.      * @Route(
  50.      *     "/{item}/delete",
  51.      *     name="app.component_inventory.delete",
  52.      *     requirements={"item" = "\d+"},
  53.      *     methods={"GET"}
  54.      * )
  55.      *
  56.      * @param ComponentInventoryItem $item
  57.      *
  58.      * @return Response
  59.      */
  60.     public function deleteAction(ComponentInventoryItem $item): Response
  61.     {
  62.         $this->assertAdmin();
  63.         try {
  64.             $this->componentInventoryItemRepository->delete($item);
  65.             $this->flashSuccess('ui.success.delete', [], 'ComponentInventoryItem');
  66.         } catch (Exception $e) {
  67.             $this->flashError('ui.error.delete', [], 'ComponentInventoryItem');
  68.             if ($this->getCurrentUser()->isSuperAdmin()) {
  69.                 $this->flashError($e->getMessage());
  70.             }
  71.         }
  72.         return $this->redirectResponse('app.component_inventory.index');
  73.     }
  74.     /**
  75.      * @Route(
  76.      *     "/excel",
  77.      *     name="app.component_inventory.excel"
  78.      * )
  79.      *
  80.      * @return Response
  81.      */
  82.     public function excelAction(): Response
  83.     {
  84.         try {
  85.             return $this->componentInventoryService->getInventoryExcelFile()->getResponse();
  86.         } catch (Exception $e) {
  87.             $this->flashError($e->getMessage());
  88.             $this->flashError($e->getPrevious()->getMessage());
  89.             return $this->redirectResponse('app.component_inventory.index');
  90.         }
  91.     }
  92.     /**
  93.      * @Route(
  94.      *     "/",
  95.      *     name="app.component_inventory.index"
  96.      * )
  97.      *
  98.      * @param Request $request
  99.      * @return Response
  100.      */
  101.     public function indexAction(Request $request): Response
  102.     {
  103.         return $this->render(
  104.             'ComponentInventory/index.html.twig',
  105.             [
  106.                 'items' => $this->componentInventoryItemRepository->findAll(),
  107.                 'hideZeroQuantity' => $request->getSession()->get(self::HIDE_ZERO_QUANTITY_KEYtrue),
  108.             ]
  109.         );
  110.     }
  111.     /**
  112.      * @Route(
  113.      *     "/toggle-hide-zero-quantity",
  114.      *     name="app.component_inventory.index.toggle_hide_zero_quantity"
  115.      * )
  116.      *
  117.      * @param Request $request
  118.      * @return Response
  119.      */
  120.     public function indexToggleHideZeroQuantityAction(Request $request): Response
  121.     {
  122.         $request->getSession()->set(
  123.             self::HIDE_ZERO_QUANTITY_KEY,
  124.             !$request->getSession()->get(self::HIDE_ZERO_QUANTITY_KEYtrue)
  125.         );
  126.         return $this->redirectResponse('app.component_inventory.index');
  127.     }
  128.     /**
  129.      * @Route(
  130.      *     "/{item}/log",
  131.      *     name="app.component_inventory.item_log",
  132.      *     requirements={"item" = "\d+"}
  133.      * )
  134.      *
  135.      * @param ComponentInventoryItem $item
  136.      * @return Response
  137.      */
  138.     public function itemLogAction(ComponentInventoryItem $item): Response
  139.     {
  140.         return $this->render(
  141.             'ComponentInventory/itemLog.html.twig',
  142.             [
  143.                 'item' => $item,
  144.             ]
  145.         );
  146.     }
  147.     /**
  148.      * @Route(
  149.      *     "/log",
  150.      *     name="app.component_inventory.log"
  151.      * )
  152.      *
  153.      * @param Request $request
  154.      *
  155.      * @return Response
  156.      */
  157.     public function logAction(Request $request): Response
  158.     {
  159.         $form $this->createForm(
  160.             LogFilterType::class,
  161.             null,
  162.             [
  163.                 'action' => $this->generateUrl('app.component_inventory.log'),
  164.             ]
  165.         );
  166.         $period null;
  167.         $showZeroQuantity false;
  168.         $form->handleRequest($request);
  169.         if ($form->isSubmitted()) {
  170.             if ($form->isValid()) {
  171.                 /* @var DateRange $period */
  172.                 $period $form->get('period')->getData();
  173.                 $showZeroQuantity $form->get('showZeroQuantity')->getData();
  174.                 try {
  175.                     if ($form->get('excel')->isClicked()) {
  176.                         return $this->componentInventoryService->getEventLogExcelFile(
  177.                             $period$showZeroQuantity
  178.                         )->getResponse();
  179.                     }
  180.                 } catch (Exception $e) {
  181.                     $this->flashError($e->getMessage());
  182.                     if (null !== $previous $e->getPrevious()) {
  183.                         $this->flashError($previous->getMessage());
  184.                     }
  185.                 }
  186.             }
  187.         }
  188.         return $this->render(
  189.             'ComponentInventory/log.html.twig',
  190.             [
  191.                 'logEntries' => $this->componentInventoryLogEntryRepository->findByCreated($period$showZeroQuantity),
  192.                 'form' => $form->createView(),
  193.             ]
  194.         );
  195.     }
  196.     /**
  197.      * @Route(
  198.      *     "/log/excel",
  199.      *     name="app.component_inventory.log.excel"
  200.      * )
  201.      *
  202.      * @return Response
  203.      */
  204.     public function logExcelAction(): Response
  205.     {
  206.         try {
  207.             return $this->componentInventoryService->getEventLogExcelFile()->getResponse();
  208.         } catch (Exception $e) {
  209.             $this->flashError($e->getMessage());
  210.             $this->flashError($e->getPrevious()->getMessage());
  211.             return $this->redirectResponse('app.component_inventory.index');
  212.         }
  213.     }
  214.     /**
  215.      * @Route(
  216.      *     "/batch-decrease-quantity",
  217.      *     name="app.component_inventory.batch_decrease_quantity.modal"
  218.      * )
  219.      *
  220.      * @param Request $request
  221.      *
  222.      * @return ModalResponse
  223.      */
  224.     public function modalBatchDecreaseQuantityAction(Request $request): ModalResponse
  225.     {
  226.         $response = new ModalResponse();
  227.         $response->setSubmit(true);
  228.         $response->setTitle(
  229.             $this->trans('ui.batchDecreaseQuantity', [], 'ComponentInventoryItem')
  230.         );
  231.         $form $this->createForm(
  232.             BatchDecreaseQuantityType::class,
  233.             null,
  234.             [
  235.                 'action' => $this->generateUrl(
  236.                     'app.component_inventory.batch_decrease_quantity.modal'
  237.                 )
  238.             ]
  239.         );
  240.         $form->handleRequest($request);
  241.         if ($form->isSubmitted()) {
  242.             if ($form->isValid()) {
  243.                 try {
  244.                     /* @var BatchDecreaseQuantity $data */
  245.                     $data $form->getData();
  246.                     $import $request->getSession()->get(self::BATCH_DECREASE_DATA_KEY);
  247.                     foreach ($import['items'] as $item) {
  248.                         if (null !== $obj $this->componentInventoryItemRepository->findByCode($item['code'])) {
  249.                             $data->updateItem($obj$item['quantity'], $import['file']);
  250.                             $this->componentInventoryItemRepository->save($obj);
  251.                         }
  252.                     }
  253.                     $response->setRedirect(
  254.                         $this->generateUrl('app.component_inventory.index')
  255.                     );
  256.                 } catch (Exception $e) {
  257.                     $response->setError($e->getMessage());
  258.                 }
  259.             }
  260.         }
  261.         $response->setBody(
  262.             $this->renderView(
  263.                 'ComponentInventory/batchDecreaseQuantityForm.html.twig',
  264.                 [
  265.                     'form' => $form->createView(),
  266.                 ]
  267.             )
  268.         );
  269.         return $response;
  270.     }
  271.     /**
  272.      * @Route(
  273.      *     "/batch-decrease-quantity/preview",
  274.      *     name="app.component_inventory.batch_decrease_quantity.preview"
  275.      * )
  276.      *
  277.      * @param Request $request
  278.      *
  279.      * @return Response
  280.      */
  281.     public function modalBatchDecreaseQuantityPreviewAction(Request $request): Response
  282.     {
  283.         /* @var UploadedFile $file */
  284.         $file $request->files->get('excel');
  285.         try {
  286.             $fileName $file->getClientOriginalName();
  287.             $items $this->componentInventoryService->parseBatchDecreaseExcel($file);
  288.             $hasErrors false;
  289.             foreach ($items as $item) {
  290.                 if (null !== $item['error']) {
  291.                     $hasErrors true;
  292.                     break;
  293.                 }
  294.             }
  295.             $request->getSession()->remove(self::BATCH_DECREASE_DATA_KEY);
  296.             if (!$hasErrors) {
  297.                 $data = [
  298.                     'items' => $items,
  299.                     'file' => $fileName,
  300.                 ];
  301.                 $request->getSession()->set(self::BATCH_DECREASE_DATA_KEY$data);
  302.             }
  303.             return $this->render(
  304.                 'ComponentInventory/batchDecreaseQuantityPreview.html.twig',
  305.                 [
  306.                     'items' => $items,
  307.                     'hasErrors' => $hasErrors,
  308.                 ]
  309.             );
  310.         } catch (Exception $e) {
  311.             return $this->render(
  312.                 'ComponentInventory/excelError.html.twig',
  313.                 [
  314.                     'error' => $e->getMessage(),
  315.                 ],
  316.                 new Response(''400)
  317.             );
  318.         }
  319.     }
  320.     /**
  321.      * @Route(
  322.      *     "/batch-increase-quantity",
  323.      *     name="app.component_inventory.batch_increase_quantity.modal"
  324.      * )
  325.      *
  326.      * @param Request $request
  327.      *
  328.      * @return ModalResponse
  329.      */
  330.     public function modalBatchIncreaseQuantityAction(Request $request): ModalResponse
  331.     {
  332.         $response = new ModalResponse();
  333.         $response->setSubmit(true);
  334.         $response->setTitle(
  335.             $this->trans('ui.batchIncreaseQuantity', [], 'ComponentInventoryItem')
  336.         );
  337.         $form $this->createForm(
  338.             BatchIncreaseQuantityType::class,
  339.             null,
  340.             [
  341.                 'action' => $this->generateUrl(
  342.                     'app.component_inventory.batch_increase_quantity.modal'
  343.                 )
  344.             ]
  345.         );
  346.         $form->handleRequest($request);
  347.         if ($form->isSubmitted()) {
  348.             if ($form->isValid()) {
  349.                 try {
  350.                     /* @var BatchIncreaseQuantity $data */
  351.                     $data $form->getData();
  352.                     $import $request->getSession()->get(self::BATCH_INCREASE_DATA_KEY);
  353.                     foreach ($import['items'] as $item) {
  354.                         if (null !== $obj $this->componentInventoryItemRepository->findByCode($item['code'])) {
  355.                             $data->updateItem($obj$item['description'], $item['quantity'], $item['unitPrice'], $import['file']);
  356.                         } else {
  357.                             $obj = (new ItemCreate(
  358.                                 $item['code'],
  359.                                 $item['type'],
  360.                                 $item['quantity'],
  361.                                 $item['unitPrice'],
  362.                                 $item['description'],
  363.                                 $data->getLogEntryNote(),
  364.                                 $data->getPurchaseOrderNumber(),
  365.                                 $import['file']
  366.                             ))->toItem();
  367.                         }
  368.                         $this->componentInventoryItemRepository->save($obj);
  369.                     }
  370.                     $response->setRedirect(
  371.                         $this->generateUrl('app.component_inventory.index')
  372.                     );
  373.                 } catch (Exception $e) {
  374.                     $response->setError($e->getMessage());
  375.                 }
  376.             }
  377.         }
  378.         $response->setBody(
  379.             $this->renderView(
  380.                 'ComponentInventory/batchIncreaseQuantityForm.html.twig',
  381.                 [
  382.                     'form' => $form->createView(),
  383.                 ]
  384.             )
  385.         );
  386.         return $response;
  387.     }
  388.     /**
  389.      * @Route(
  390.      *     "/batch-increase-quantity/preview",
  391.      *     name="app.component_inventory.batch_increase_quantity.preview"
  392.      * )
  393.      *
  394.      * @param Request $request
  395.      *
  396.      * @return Response
  397.      */
  398.     public function modalBatchIncreaseQuantityPreviewAction(Request $request): Response
  399.     {
  400.         /* @var UploadedFile $file */
  401.         $file $request->files->get('excel');
  402.         try {
  403.             $fileName $file->getClientOriginalName();
  404.             $items $this->componentInventoryService->parseBatchIncreaseExcel($file);
  405.             $hasErrors false;
  406.             foreach ($items as $item) {
  407.                 if (null !== $item['error']) {
  408.                     $hasErrors true;
  409.                     break;
  410.                 }
  411.             }
  412.             $request->getSession()->remove(self::BATCH_INCREASE_DATA_KEY);
  413.             if (!$hasErrors) {
  414.                 $data = [
  415.                     'items' => $items,
  416.                     'file' => $fileName,
  417.                 ];
  418.                 $request->getSession()->set(self::BATCH_INCREASE_DATA_KEY$data);
  419.             }
  420.             return $this->render(
  421.                 'ComponentInventory/batchIncreaseQuantityPreview.html.twig',
  422.                 [
  423.                     'items' => $items,
  424.                     'hasErrors' => $hasErrors,
  425.                 ]
  426.             );
  427.         } catch (Exception $e) {
  428.             return $this->render(
  429.                 'ComponentInventory/excelError.html.twig',
  430.                 [
  431.                     'error' => $e->getMessage(),
  432.                 ],
  433.                 new Response(''400)
  434.             );
  435.         }
  436.     }
  437.     /**
  438.      * @Route(
  439.      *     "/batch-set-quantity",
  440.      *     name="app.component_inventory.batch_set_quantity.modal"
  441.      * )
  442.      *
  443.      * @param Request $request
  444.      *
  445.      * @return ModalResponse
  446.      */
  447.     public function modalBatchSetQuantityAction(Request $request): ModalResponse
  448.     {
  449.         $response = new ModalResponse();
  450.         $response->setSubmit(true);
  451.         $response->setTitle(
  452.             $this->trans('ui.batchSetQuantity', [], 'ComponentInventoryItem')
  453.         );
  454.         $form $this->createForm(
  455.             BatchSetQuantityType::class,
  456.             null,
  457.             [
  458.                 'action' => $this->generateUrl(
  459.                     'app.component_inventory.batch_set_quantity.modal'
  460.                 )
  461.             ]
  462.         );
  463.         $form->handleRequest($request);
  464.         if ($form->isSubmitted()) {
  465.             if ($form->isValid()) {
  466.                 try {
  467.                     /* @var BatchSetQuantity $data */
  468.                     $data $form->getData();
  469.                     $import $request->getSession()->get(self::BATCH_SET_DATA_KEY);
  470.                     $codes = [];
  471.                     foreach ($import['items'] as $item) {
  472.                         $codes[] = $item['code'];
  473.                         if (null !== $obj $this->componentInventoryItemRepository->findByCode($item['code'])) {
  474.                             if (null !== $item['description']) {
  475.                                 $obj->setDescription($item['description']);
  476.                             }
  477.                             $data->updateItem($obj$item['quantity'], $import['file']);
  478.                             $this->componentInventoryItemRepository->save($obj);
  479.                         }
  480.                     }
  481.                     // Set quantity zero to all codes that are not listed in the batch
  482.                     foreach ($this->componentInventoryItemRepository->findByMissingCodes($codes) as $obj) {
  483.                         $data->updateItem($obj0$import['file']);
  484.                         $this->componentInventoryItemRepository->save($obj);
  485.                     }
  486.                     $response->setRedirect(
  487.                         $this->generateUrl('app.component_inventory.index')
  488.                     );
  489.                 } catch (Exception $e) {
  490.                     $response->setError($e->getMessage());
  491.                 }
  492.             }
  493.         }
  494.         $response->setBody(
  495.             $this->renderView(
  496.                 'ComponentInventory/batchSetQuantityForm.html.twig',
  497.                 [
  498.                     'form' => $form->createView(),
  499.                 ]
  500.             )
  501.         );
  502.         return $response;
  503.     }
  504.     /**
  505.      * @Route(
  506.      *     "/batch-set-quantity/preview",
  507.      *     name="app.component_inventory.batch_set_quantity.preview"
  508.      * )
  509.      *
  510.      * @param Request $request
  511.      *
  512.      * @return Response
  513.      */
  514.     public function modalBatchSetQuantityPreviewAction(Request $request): Response
  515.     {
  516.         /* @var UploadedFile $file */
  517.         $file $request->files->get('excel');
  518.         try {
  519.             $fileName $file->getClientOriginalName();
  520.             $items $this->componentInventoryService->parseBatchSetExcel($file);
  521.             $hasErrors false;
  522.             foreach ($items as $item) {
  523.                 if (null !== $item['error']) {
  524.                     $hasErrors true;
  525.                     break;
  526.                 }
  527.             }
  528.             $request->getSession()->remove(self::BATCH_SET_DATA_KEY);
  529.             if (!$hasErrors) {
  530.                 $data = [
  531.                     'items' => $items,
  532.                     'file' => $fileName,
  533.                 ];
  534.                 $request->getSession()->set(self::BATCH_SET_DATA_KEY$data);
  535.             }
  536.             return $this->render(
  537.                 'ComponentInventory/batchSetQuantityPreview.html.twig',
  538.                 [
  539.                     'items' => $items,
  540.                     'hasErrors' => $hasErrors,
  541.                 ]
  542.             );
  543.         } catch (Exception $e) {
  544.             return $this->render(
  545.                 'ComponentInventory/excelError.html.twig',
  546.                 [
  547.                     'error' => $e->getMessage(),
  548.                 ],
  549.                 new Response(''400)
  550.             );
  551.         }
  552.     }
  553.     /**
  554.      * @Route(
  555.      *     "/{item}/decrease-quantity",
  556.      *     name="app.component_inventory.decrease_quantity.modal",
  557.      *     requirements={"item" = "\d+"}
  558.      * )
  559.      *
  560.      * @param Request $request
  561.      * @param ComponentInventoryItem $item
  562.      *
  563.      * @return ModalResponse
  564.      */
  565.     public function modalDecreaseQuantityAction(Request $requestComponentInventoryItem $item): ModalResponse
  566.     {
  567.         $response = new ModalResponse();
  568.         $response->setSubmit(true);
  569.         $response->setTitle(
  570.             $this->trans('ui.decreaseQuantity', [], 'ComponentInventoryItem')
  571.         );
  572.         $response->setLabel($item->getCode());
  573.         $form $this->createForm(
  574.             DecreaseQuantityType::class,
  575.             null,
  576.             [
  577.                 'action' => $this->generateUrl(
  578.                     'app.component_inventory.decrease_quantity.modal',
  579.                     [
  580.                         'item' => $item->getId(),
  581.                     ]
  582.                 )
  583.             ]
  584.         );
  585.         $form->handleRequest($request);
  586.         if ($form->isSubmitted()) {
  587.             if ($form->isValid()) {
  588.                 try {
  589.                     /* @var DecreaseQuantity $data */
  590.                     $data $form->getData();
  591.                     $data->updateItem($item);
  592.                     $this->componentInventoryItemRepository->save($item);
  593.                     $response->setRedirect(
  594.                         $this->generateUrl('app.component_inventory.index')
  595.                     );
  596.                 } catch (Exception $e) {
  597.                     $response->setError($e->getMessage());
  598.                 }
  599.             }
  600.         }
  601.         $response->setBody(
  602.             $this->renderView(
  603.                 'ComponentInventory/decreaseQuantityForm.html.twig',
  604.                 [
  605.                     'form' => $form->createView(),
  606.                 ]
  607.             )
  608.         );
  609.         return $response;
  610.     }
  611.     /**
  612.      * @Route(
  613.      *     "/{item}/increase-quantity",
  614.      *     name="app.component_inventory.increase_quantity.modal",
  615.      *     requirements={"item" = "\d+"}
  616.      * )
  617.      *
  618.      * @param Request $request
  619.      * @param ComponentInventoryItem $item
  620.      *
  621.      * @return ModalResponse
  622.      */
  623.     public function modalIncreaseQuantityAction(Request $requestComponentInventoryItem $item): ModalResponse
  624.     {
  625.         $response = new ModalResponse();
  626.         $response->setSubmit(true);
  627.         $response->setTitle(
  628.             $this->trans('ui.increaseQuantity', [], 'ComponentInventoryItem')
  629.         );
  630.         $response->setLabel($item->getCode());
  631.         $form $this->createForm(
  632.             IncreaseQuantityType::class,
  633.             null,
  634.             [
  635.                 'action' => $this->generateUrl(
  636.                     'app.component_inventory.increase_quantity.modal',
  637.                     [
  638.                         'item' => $item->getId(),
  639.                     ]
  640.                 )
  641.             ]
  642.         );
  643.         $form->handleRequest($request);
  644.         if ($form->isSubmitted()) {
  645.             if ($form->isValid()) {
  646.                 try {
  647.                     /* @var IncreaseQuantity $data */
  648.                     $data $form->getData();
  649.                     $data->updateItem($item);
  650.                     $this->componentInventoryItemRepository->save($item);
  651.                     $response->setRedirect(
  652.                         $this->generateUrl('app.component_inventory.index')
  653.                     );
  654.                 } catch (Exception $e) {
  655.                     $response->setError($e->getMessage());
  656.                 }
  657.             }
  658.         }
  659.         $response->setBody(
  660.             $this->renderView(
  661.                 'ComponentInventory/increaseQuantityForm.html.twig',
  662.                 [
  663.                     'form' => $form->createView(),
  664.                 ]
  665.             )
  666.         );
  667.         return $response;
  668.     }
  669.     /**
  670.      * @Route(
  671.      *     "/create",
  672.      *     name="app.component_inventory.create.modal"
  673.      * )
  674.      *
  675.      * @param Request $request
  676.      *
  677.      * @return ModalResponse
  678.      */
  679.     public function modalCreateAction(Request $request): ModalResponse
  680.     {
  681.         $response = new ModalResponse();
  682.         $response->setSubmit(true);
  683.         $response->setTitle(
  684.             $this->trans('ui.create', [], 'ComponentInventoryItem')
  685.         );
  686.         $form $this->createForm(
  687.             ItemCreateType::class,
  688.             null,
  689.             [
  690.                 'action' => $this->generateUrl(
  691.                     'app.component_inventory.create.modal'
  692.                 )
  693.             ]
  694.         );
  695.         $form->handleRequest($request);
  696.         if ($form->isSubmitted()) {
  697.             if ($form->isValid()) {
  698.                 try {
  699.                     /* @var ItemCreate $data */
  700.                     $data $form->getData();
  701.                     $this->componentInventoryItemRepository->save($data->toItem());
  702.                     $response->setRedirect(
  703.                         $this->generateUrl('app.component_inventory.index')
  704.                     );
  705.                 } catch (Exception $e) {
  706.                     $response->setError($e->getMessage());
  707.                 }
  708.             }
  709.         }
  710.         $response->setBody(
  711.             $this->renderView(
  712.                 'ComponentInventory/form.html.twig',
  713.                 [
  714.                     'form' => $form->createView(),
  715.                 ]
  716.             )
  717.         );
  718.         return $response;
  719.     }
  720.     /**
  721.      * @Route(
  722.      *     "/{item}/update",
  723.      *     name="app.component_inventory.update.modal",
  724.      *     requirements={"item" = "\d+"}
  725.      * )
  726.      *
  727.      * @param Request $request
  728.      * @param ComponentInventoryItem $item
  729.      *
  730.      * @return ModalResponse
  731.      */
  732.     public function modalUpdateAction(Request $requestComponentInventoryItem $item): ModalResponse
  733.     {
  734.         $response = new ModalResponse();
  735.         $response->setSubmit(true);
  736.         $response->setTitle(
  737.             $this->trans('ui.update', [], 'ComponentInventoryItem')
  738.         );
  739.         $response->setLabel($item->getCode());
  740.         $form $this->createForm(
  741.             ItemUpdateType::class,
  742.             ItemUpdate::fromItem($item),
  743.             [
  744.                 'action' => $this->generateUrl(
  745.                     'app.component_inventory.update.modal',
  746.                     [
  747.                         'item' => $item->getId(),
  748.                     ]
  749.                 )
  750.             ]
  751.         );
  752.         $form->handleRequest($request);
  753.         if ($form->isSubmitted()) {
  754.             if ($form->isValid()) {
  755.                 try {
  756.                     /* @var ItemUpdate $data */
  757.                     $data $form->getData();
  758.                     $data->updateItem($item);
  759.                     $this->componentInventoryItemRepository->save($item);
  760.                     $response->setRedirect(
  761.                         $this->generateUrl('app.component_inventory.index')
  762.                     );
  763.                 } catch (Exception $e) {
  764.                     $response->setError($e->getMessage());
  765.                 }
  766.             }
  767.         }
  768.         $response->setBody(
  769.             $this->renderView(
  770.                 'ComponentInventory/updateForm.html.twig',
  771.                 [
  772.                     'form' => $form->createView(),
  773.                 ]
  774.             )
  775.         );
  776.         return $response;
  777.     }
  778. }