src/Controller/App/ExpenseController.php line 28

  1. <?php
  2. namespace App\Controller\App;
  3. use DateTime;
  4. use App\Entity\Expense;
  5. use App\Form\ExpenseType;
  6. use App\Entity\ExpenseCategory;
  7. use App\Repository\ExpenseRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. #[Route('/app/expense')]
  17. #[IsGranted('IS_AUTHENTICATED_FULLY')]
  18. class ExpenseController extends AbstractController
  19. {
  20.     public function __construct(private TranslatorInterface $translator)
  21.     {
  22.     }
  23.     #[Route('/'name'app_expense_index'methods: ['GET''POST'])]
  24.     public function index(Request $requestExpenseRepository $expenseRepositoryEntityManagerInterface $manager): Response
  25.     {
  26.         if ($request->isXmlHttpRequest()) {
  27.             if ($this->isCsrfTokenValid('expense'$_POST['expense']['_token'])) {
  28.                 $expense = new Expense();
  29.                 $form $this->createForm(ExpenseType::class, $expense);
  30.                 $expenseCategory $manager->getRepository(ExpenseCategory::class)->find($_POST['expense']['type']);
  31.                 $expense
  32.                     ->setType($expenseCategory)
  33.                     ->setName($_POST['expense']['name'])
  34.                     ->setReference($_POST['expense']['reference'])
  35.                     ->setAmount($_POST['expense']['amount'])
  36.                     ->setDescription($_POST['expense']['description'])
  37.                     ->setDate(new DateTime($_POST['expense']['date']));
  38.                 $expenseRepository->add($expensetrue);
  39.                 return new JsonResponse(['status' => 'ok''data' => $expense->getId(), 'msg' => $this->translator->trans('Dépense crée!')]);
  40.             } else {
  41.                 return new JsonResponse(['status' => 'no''data' => null'msg' => $this->translator->trans('token invalid !')]);
  42.             }
  43.         }
  44.         $expense = new Expense();
  45.         $form $this->createForm(ExpenseType::class, $expense);
  46.         return $this->render('app/expense/index.html.twig', [
  47.             'expenses' => $expenseRepository->findAll(),
  48.             'form' => $form->createView()
  49.         ]);
  50.     }
  51.     #[Route('/new'name'app_expense_new'methods: ['GET''POST'])]
  52.     public function new(Request $requestExpenseRepository $expenseRepository): Response
  53.     {
  54.         return $this->redirectToRoute('app_expense_index', [], Response::HTTP_SEE_OTHER);
  55.         // $expense = new Expense();
  56.         // $form = $this->createForm(ExpenseType::class, $expense);
  57.         // $form->handleRequest($request);
  58.         // if ($form->isSubmitted() && $form->isValid()) {
  59.         //     $expenseRepository->add($expense, true);
  60.         //     $this->addFlash('success', $this->translator->trans('Succès !!'));
  61.         //     return $this->redirectToRoute('app_expense_index', [], Response::HTTP_SEE_OTHER);
  62.         // }
  63.         // return $this->renderForm('app/expense/new.html.twig', [
  64.         //     'expense' => $expense,
  65.         //     'form' => $form,
  66.         // ]);
  67.     }
  68.     #[Route('/{id}'name'app_expense_show'methods: ['GET'])]
  69.     public function show(Expense $expense): Response
  70.     {
  71.         return $this->redirectToRoute('app_expense_index', [], Response::HTTP_SEE_OTHER);
  72.         // return $this->render('app/expense/show.html.twig', [
  73.         //     'expense' => $expense,
  74.         // ]);
  75.     }
  76.     #[Route('/{id}/edit'name'app_expense_edit'methods: ['GET''POST'])]
  77.     public function edit(Request $requestExpense $expenseExpenseRepository $expenseRepository): Response
  78.     {
  79.         $form $this->createForm(ExpenseType::class, $expense);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             $expenseRepository->add($expensetrue);
  83.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  84.             return $this->redirectToRoute('app_expense_index', [], Response::HTTP_SEE_OTHER);
  85.         }
  86.         return $this->renderForm('app/expense/edit.html.twig', [
  87.             'expense' => $expense,
  88.             'form' => $form,
  89.         ]);
  90.     }
  91.     #[Route('/{id}'name'app_expense_delete'methods: ['POST'])]
  92.     public function delete(Request $requestExpense $expenseExpenseRepository $expenseRepository): Response
  93.     {
  94.         if ($this->isCsrfTokenValid('delete' $expense->getId(), $request->request->get('_token'))) {
  95.             try {
  96.                 $expenseRepository->remove($expensetrue);
  97.                 $this->addFlash('success'$this->translator->trans('Succès !!'));
  98.             } catch (\Exception $e) {
  99.                 $errorMessage $e->getMessage();
  100.                 $result explode(':'$errorMessage);
  101.                 $this->addFlash('error'$result[0]);
  102.             }
  103.         }
  104.         return $this->redirectToRoute('app_expense_index', [], Response::HTTP_SEE_OTHER);
  105.     }
  106. }