src/Controller/CustomsCrudActionsController.php line 42
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\InvoiceDetail;
use App\Entity\Invoice;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\Form\InvoiceDetailType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\Type\EntityType;
use App\Entity\Services;
class CustomsCrudActionsController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
#[Route('/customs/crud/actions', name: 'app_customs_crud_actions')]
public function index(): Response
{
return $this->render('customs_crud_actions/index.html.twig', [
'controller_name' => 'CustomsCrudActionsController',
]);
}
#[Route('/custom/crud/delete/detail/{invoice_id}/{detail_id}', name: 'app_customs_crud_delete_detail')]
public function detailDelete($invoice_id,$detail_id, EntityManagerInterface $entityManager, Request $request): Response
{
$repository = $this->entityManager->getRepository(InvoiceDetail::class);
$detail = $repository->findOneBy(['id'=>$detail_id]);
$entityManager->remove($detail);
$entityManager->flush();
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
#[Route('/custom/crud/create/detail/{invoice_id}', name: 'app_customs_crud_create_detail')]
public function detailCreate($invoice_id, EntityManagerInterface $entityManager, Request $request): Response
{
$invoiceDetail = new InvoiceDetail();
$form = $this->createForm(InvoiceDetailType::class, $invoiceDetail);
// Add event listener to filter services based on selected category
$form->get('category')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$category = $form->getData();
$form->getParent()->add('service', EntityType::class, [
'class' => Services::class,
'label' => 'Services',
'placeholder' => '-- Select a service --',
'choice_label' => 'name',
'query_builder' => function ($repo) use ($category) {
return $repo->createQueryBuilder('s')
->andWhere('s.category = :category')
->setParameter('category', $category)
->orderBy('s.name', 'ASC');
}
]);
});
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// persist and flush data here
return $this->redirectToRoute('invoice_detail_index');
}
return $this->render('/customs_crud_actions/detail_edit.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/custom/crud/edit/detail/{detail_id}', name: 'app_customs_crud_edit_detail')]
public function detailEdit($detail_id ,Request $request, EntityManagerInterface $entityManager): Response
{
$invoiceDetail = $repository->findOneBy(['id'=>$detail_id]);
$form = $this->createForm(InvoiceDetailType::class, $invoiceDetail);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
$this->addFlash('success', 'Invoice detail updated successfully.');
return $this->redirectToRoute('app_customs_crud_edit_detail', ['detail_id' => $invoiceDetail->getId()]);
}
return $this->render('/customs_crud_actions/detail_edit.html.twig', [
'form' => $form->createView(),
'invoiceDetail' => $invoiceDetail,
]);
}
}