src/Controller/CustomsCrudActionsController.php line 38

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Entity\InvoiceDetail;
  9. use App\Entity\Invoice;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use App\Form\InvoiceDetailType;
  12. use Symfony\Component\Form\FormEvent;
  13. use Symfony\Component\Form\FormEvents;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\Extension\Core\Type\EntityType;
  16. use App\Entity\Services;
  17. class CustomsCrudActionsController extends AbstractController
  18. {
  19.     private $entityManager;
  20.     public function __construct(EntityManagerInterface $entityManager)
  21.     {
  22.         $this->entityManager $entityManager;
  23.     }
  24.     #[Route('/customs/crud/actions'name'app_customs_crud_actions')]
  25.     public function index(): Response
  26.     {
  27.         return $this->render('customs_crud_actions/index.html.twig', [
  28.             'controller_name' => 'CustomsCrudActionsController',
  29.         ]);
  30.     }
  31.     #[Route('/custom/crud/delete/detail/{invoice_id}/{detail_id}'name'app_customs_crud_delete_detail')]
  32.     public function detailDelete($invoice_id,$detail_idEntityManagerInterface $entityManagerRequest $request): Response
  33.     {
  34.         $repository $this->entityManager->getRepository(InvoiceDetail::class);
  35.         $detail $repository->findOneBy(['id'=>$detail_id]);
  36.         $entityManager->remove($detail);
  37.         $entityManager->flush();
  38.         $referer $request->headers->get('referer');
  39.         return new RedirectResponse($referer);
  40.     }
  41.     #[Route('/custom/crud/create/detail/{invoice_id}'name'app_customs_crud_create_detail')]
  42.     public function detailCreate($invoice_idEntityManagerInterface $entityManagerRequest $request): Response
  43.     {
  44.         $invoiceDetail = new InvoiceDetail();
  45.         $form $this->createForm(InvoiceDetailType::class, $invoiceDetail);
  46.         
  47.         // Add event listener to filter services based on selected category
  48.         $form->get('category')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  49.             $form $event->getForm();
  50.             $category $form->getData();
  51.             $form->getParent()->add('service'EntityType::class, [
  52.                 'class' => Services::class,
  53.                 'label' => 'Services',
  54.                 'placeholder' => '-- Select a service --',
  55.                 'choice_label' => 'name',
  56.                 'query_builder' => function ($repo) use ($category) {
  57.                     return $repo->createQueryBuilder('s')
  58.                         ->andWhere('s.category = :category')
  59.                         ->setParameter('category'$category)
  60.                         ->orderBy('s.name''ASC');
  61.                 }
  62.             ]);
  63.         });
  64.         
  65.         $form->handleRequest($request);
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             // persist and flush data here
  68.     
  69.             return $this->redirectToRoute('invoice_detail_index');
  70.         }
  71.         
  72.     
  73.         return $this->render('/customs_crud_actions/detail_edit.html.twig', [
  74.             'form' => $form->createView(),
  75.         ]);
  76.     }
  77.     #[Route('/custom/crud/edit/detail/{detail_id}'name'app_customs_crud_edit_detail')]
  78.     public function detailEdit($detail_id ,Request $requestEntityManagerInterface $entityManager): Response
  79.     {
  80.         $invoiceDetail $repository->findOneBy(['id'=>$detail_id]);
  81.         $form $this->createForm(InvoiceDetailType::class, $invoiceDetail);
  82.         $form->handleRequest($request);
  83.         if ($form->isSubmitted() && $form->isValid()) {
  84.             $entityManager->flush();
  85.     
  86.             $this->addFlash('success''Invoice detail updated successfully.');
  87.             return $this->redirectToRoute('app_customs_crud_edit_detail', ['detail_id' => $invoiceDetail->getId()]);
  88.         }
  89.     
  90.         return $this->render('/customs_crud_actions/detail_edit.html.twig', [
  91.             'form' => $form->createView(),
  92.             'invoiceDetail' => $invoiceDetail,
  93.         ]);
  94.     }
  95. }