<?php
namespace App\Controller;
use App\Entity\Operacje;
use App\Entity\Rodzaje;
use App\Entity\Rozchody;
use App\Form\OperacjaType;
use App\Repository\RozchodyRepository;
use App\Repository\OperacjeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class OperacjeWydatkiController extends AbstractController
{
private $entityManager;
private $rozchodyRepository;
private $operacjeRepository;
public function __construct(
EntityManagerInterface $entityManager,
RozchodyRepository $rozchodyRepository,
OperacjeRepository $operacjeRepository
) {
$this->entityManager = $entityManager;
$this->rozchodyRepository = $rozchodyRepository;
$this->operacjeRepository = $operacjeRepository;
}
/**
* @Route("/operacje/wydatki", name="operacje_wydatki")
*/
public function index(Request $request): Response
{
// Pobierz aktualny rok i miesiąc
$rok = $request->query->get('rok', date('Y'));
$miesiac = $request->query->get('miesiac', date('n'));
// Pobierz dostępne lata
$dostepneLata = $this->rozchodyRepository->getDostepneLata();
// Pobierz rozchody z operacjami dla wybranego okresu
$rozchodyZOperacjami = $this->rozchodyRepository->getRozchodyZOperacjami($rok, $miesiac);
// Tworzymy nowy obiekt Operacje dla formularza
$operacja = new Operacje();
$form = $this->createForm(OperacjaType::class, $operacja, [
'typ_operacji' => 'wydatek'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($operacja);
$this->entityManager->flush();
$this->addFlash('success', 'Operacja wydatku została dodana pomyślnie.');
return $this->redirectToRoute('operacje_wydatki', [
'rok' => $rok,
'miesiac' => $miesiac
]);
}
return $this->render('operacje/wydatki.html.twig', [
'rozchodyZOperacjami' => $rozchodyZOperacjami,
'wybranyRok' => $rok,
'wybranyMiesiac' => $miesiac,
'dostepneLata' => $dostepneLata,
'form' => $form->createView(),
]);
}
/**
* @Route("/operacje/wydatki/new", name="operacje_wydatki_new", methods={"GET", "POST"})
*/
public function new(Request $request): Response
{
$operacja = new Operacje();
$rodzajRozchodu = $this->entityManager->getRepository(Rodzaje::class)->findOneBy(['nazwa' => 'rozchody']);
$operacja->setRodzaj($rodzajRozchodu);
$operacja->setDataOperacji(new \DateTime());
$form = $this->createForm(OperacjaType::class, $operacja, [
'typ_operacji' => 'rozchod'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($this->getUser()) {
$planKto = (string) (method_exists($this->getUser(), 'getId') ? $this->getUser()->getUsername() : $this->getUser()->getUserIdentifier());
$operacja->setKto($planKto);
}
$this->entityManager->persist($operacja);
$this->entityManager->flush();
$this->addFlash('success', 'Dodano nowy wydatek.');
return $this->redirectToRoute('operacje_wydatki');
}
return $this->render('operacje/new.html.twig', [
'form' => $form->createView(),
'tytul' => 'Nowy Wydatek',
'tytul_submit' => 'Dodaj wydatek',
'route_path' => 'operacje_wydatki',
]);
}
/**
* @Route("/operacje/wydatki/edit/{id}", name="operacje_wydatki_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, $id): Response
{
$operacja = $this->entityManager->getRepository(Operacje::class)->find($id);
if (!$operacja) {
throw $this->createNotFoundException('Nie znaleziono operacji o podanym ID');
}
$form = $this->createForm(OperacjaType::class, $operacja, [
'typ_operacji' => 'rozchod',
'action' => $this->generateUrl('operacje_wydatki_edit', ['id' => $id])
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->flush();
$this->addFlash('success', 'Wydatek został zaktualizowany.');
return $this->redirectToRoute('operacje_wydatki');
}
return $this->render('operacje/new.html.twig', [
'form' => $form->createView(),
'tytul' => 'Zmień Wydatek',
'tytul_submit' => 'Zapisz zmiany',
'operacja' => $operacja,
'route_path' => 'operacje_wydatki',
]);
}
/**
* @Route("/operacje/wydatki/delete/{id}", name="operacje_wydatki_delete", methods={"POST"})
*/
public function delete(Request $request, $id): Response
{
$operacja = $this->entityManager->getRepository(Operacje::class)->find($id);
if (!$operacja) {
throw $this->createNotFoundException('Nie znaleziono operacji o podanym ID');
}
if ($this->isCsrfTokenValid('delete'.$id, $request->request->get('_token'))) {
$this->entityManager->remove($operacja);
$this->entityManager->flush();
$this->addFlash('success', 'Wydatek został usunięty.');
}
return $this->redirectToRoute('operacje_wydatki');
}
}