src/Controller/OperacjeWydatkiController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Operacje;
  4. use App\Entity\Rodzaje;
  5. use App\Entity\Rozchody;
  6. use App\Form\OperacjaType;
  7. use App\Repository\RozchodyRepository;
  8. use App\Repository\OperacjeRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class OperacjeWydatkiController extends AbstractController
  15. {
  16.     private $entityManager;
  17.     private $rozchodyRepository;
  18.     private $operacjeRepository;
  19.     public function __construct(
  20.         EntityManagerInterface $entityManager
  21.         RozchodyRepository $rozchodyRepository,
  22.         OperacjeRepository $operacjeRepository
  23.     ) {
  24.         $this->entityManager $entityManager;
  25.         $this->rozchodyRepository $rozchodyRepository;
  26.         $this->operacjeRepository $operacjeRepository;
  27.     }
  28.     /**
  29.      * @Route("/operacje/wydatki", name="operacje_wydatki")
  30.      */
  31.     public function index(Request $request): Response
  32.     {
  33.         // Pobierz aktualny rok i miesiąc
  34.         $rok $request->query->get('rok'date('Y'));
  35.         $miesiac $request->query->get('miesiac'date('n'));
  36.         
  37.         // Pobierz dostępne lata
  38.         $dostepneLata $this->rozchodyRepository->getDostepneLata();
  39.         
  40.         // Pobierz rozchody z operacjami dla wybranego okresu
  41.         $rozchodyZOperacjami $this->rozchodyRepository->getRozchodyZOperacjami($rok$miesiac);
  42.         
  43.         // Tworzymy nowy obiekt Operacje dla formularza
  44.         $operacja = new Operacje();
  45.         $form $this->createForm(OperacjaType::class, $operacja, [
  46.             'typ_operacji' => 'wydatek'
  47.         ]);
  48.         
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             $this->entityManager->persist($operacja);
  52.             $this->entityManager->flush();
  53.             $this->addFlash('success''Operacja wydatku została dodana pomyślnie.');
  54.             return $this->redirectToRoute('operacje_wydatki', [
  55.                 'rok' => $rok,
  56.                 'miesiac' => $miesiac
  57.             ]);
  58.         }
  59.         
  60.         return $this->render('operacje/wydatki.html.twig', [
  61.             'rozchodyZOperacjami' => $rozchodyZOperacjami,
  62.             'wybranyRok' => $rok,
  63.             'wybranyMiesiac' => $miesiac,
  64.             'dostepneLata' => $dostepneLata,
  65.             'form' => $form->createView(),
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/operacje/wydatki/new", name="operacje_wydatki_new", methods={"GET", "POST"})
  70.      */
  71.     public function new(Request $request): Response
  72.     {
  73.         $operacja = new Operacje();
  74.         $rodzajRozchodu $this->entityManager->getRepository(Rodzaje::class)->findOneBy(['nazwa' => 'rozchody']);
  75.         $operacja->setRodzaj($rodzajRozchodu);
  76.         $operacja->setDataOperacji(new \DateTime());
  77.         $form $this->createForm(OperacjaType::class, $operacja, [
  78.             'typ_operacji' => 'rozchod'
  79.         ]);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             if ($this->getUser()) {
  83.                 $planKto = (string) (method_exists($this->getUser(), 'getId') ? $this->getUser()->getUsername() : $this->getUser()->getUserIdentifier());
  84.                 $operacja->setKto($planKto);
  85.             }
  86.             $this->entityManager->persist($operacja);
  87.             $this->entityManager->flush();
  88.             $this->addFlash('success''Dodano nowy wydatek.');
  89.             return $this->redirectToRoute('operacje_wydatki');
  90.         }
  91.         return $this->render('operacje/new.html.twig', [
  92.             'form' => $form->createView(),
  93.             'tytul' => 'Nowy Wydatek',
  94.             'tytul_submit' => 'Dodaj wydatek',
  95.             'route_path' => 'operacje_wydatki',
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/operacje/wydatki/edit/{id}", name="operacje_wydatki_edit", methods={"GET", "POST"})
  100.      */
  101.     public function edit(Request $request$id): Response
  102.     {
  103.         $operacja $this->entityManager->getRepository(Operacje::class)->find($id);
  104.         if (!$operacja) {
  105.             throw $this->createNotFoundException('Nie znaleziono operacji o podanym ID');
  106.         }
  107.         $form $this->createForm(OperacjaType::class, $operacja, [
  108.             'typ_operacji' => 'rozchod',
  109.             'action' => $this->generateUrl('operacje_wydatki_edit', ['id' => $id])
  110.         ]);
  111.         $form->handleRequest($request);
  112.         if ($form->isSubmitted() && $form->isValid()) {
  113.             $this->entityManager->flush();
  114.             $this->addFlash('success''Wydatek został zaktualizowany.');
  115.             return $this->redirectToRoute('operacje_wydatki');
  116.         }
  117.         return $this->render('operacje/new.html.twig', [
  118.             'form' => $form->createView(),
  119.             'tytul' => 'Zmień Wydatek',
  120.             'tytul_submit' => 'Zapisz zmiany',
  121.             'operacja' => $operacja,
  122.             'route_path' => 'operacje_wydatki',
  123.         ]);
  124.     }
  125.     /**
  126.      * @Route("/operacje/wydatki/delete/{id}", name="operacje_wydatki_delete", methods={"POST"})
  127.      */
  128.     public function delete(Request $request$id): Response
  129.     {
  130.         $operacja $this->entityManager->getRepository(Operacje::class)->find($id);
  131.         if (!$operacja) {
  132.             throw $this->createNotFoundException('Nie znaleziono operacji o podanym ID');
  133.         }
  134.         if ($this->isCsrfTokenValid('delete'.$id$request->request->get('_token'))) {
  135.             $this->entityManager->remove($operacja);
  136.             $this->entityManager->flush();
  137.             $this->addFlash('success''Wydatek został usunięty.');
  138.         }
  139.         return $this->redirectToRoute('operacje_wydatki');
  140.     }
  141. }