%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emtnaeewxm/www/src/EEM/DepenseBundle/Controller/
Upload File :
Create Path :
Current File : /home/emtnaeewxm/www/src/EEM/DepenseBundle/Controller/DepenseController.php

<?php

namespace EEM\DepenseBundle\Controller;

use EEM\DepenseBundle\Entity\Depense;
use EEM\FonctionnaliteBundle\Service\TresorieService;
use EEM\VenteBundle\Controller\TresorieController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

/**
 * Depense controller.
 *
 * @Route("/admin/depense")
 */
class DepenseController extends Controller
{
    /**
     * Lists all depense entities.
     *
     * @Route("/", name="depense_index")
     * @Method("GET")
     * @Security("has_role('ROLE_USER')")
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $paginator = $this->get('knp_paginator');
        $depenses = $paginator->paginate(
            $em->getRepository('EEMDepenseBundle:Depense')->findby([],['createdAt'=>'desc']), /* query NOT result */
            $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
        );
        return $this->render('@EEMDepense/depense/index.html.twig', array(
            'depenses' => $depenses,
        ));
    }

    /**
     * Creates a new depense entity.
     *
     * @Route("/new", name="depense_new")
     * @Method({"GET", "POST"})
     * @Security("has_role('ROLE_USER')")
     */
    public function newAction(Request $request)
    {
        $depense = new Depense();
        $form = $this->createForm('EEM\DepenseBundle\Form\DepenseType', $depense);
        $form->handleRequest($request);

        $em = $this->getDoctrine()->getManager();
        $tresorieService = new TresorieService($em);
        $tresorieService->controleExisteTresorie();

        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($depense);
            $em->flush();
            $tresorieService->mettreAJourTresorie(-$depense->getMontant());
            $this->addFlash('success', 'Nouvelle depense ajoutée avec succès.');
            return $this->redirectToRoute('depense_show', array('id' => $depense->getId()));
        }

        return $this->render('@EEMDepense/depense/new.html.twig', array(
            'depense' => $depense,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a depense entity.
     *
     * @Route("/{id}", name="depense_show")
     * @Method("GET")
     * @Security("has_role('ROLE_USER')")
     */
    public function showAction(Depense $depense)
    {
        $deleteForm = $this->createDeleteForm($depense);

        return $this->render('@EEMDepense/depense/show.html.twig', array(
            'depense' => $depense,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing depense entity.
     *
     * @Route("/{id}/edit", name="depense_edit")
     * @Method({"GET", "POST"})
     * @Security("has_role('ROLE_USER')")
     */
    public function editAction(Request $request, Depense $depense)
    {
        $deleteForm = $this->createDeleteForm($depense);
        $editForm = $this->createForm('EEM\DepenseBundle\Form\DepenseType', $depense);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();
            $old_montant = $request->request->all()['old_depense'];

            $em = $this->getDoctrine()->getManager();
            $tresorieService = new TresorieService($em);
            $tresorieService->controleExisteTresorie();
            $tresorieService->mettreAJourTresorie(-($depense->getMontant() - $old_montant));

            $this->addFlash('success', 'Depense modifiée avec succès.');
            return $this->redirectToRoute('depense_edit', array('id' => $depense->getId()));
        }

        return $this->render('@EEMDepense/depense/edit.html.twig', array(
            'depense' => $depense,
            'form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a depense entity.
     *
     * @Route("/{id}/delete", name="depense_delete")
     * @Method("DELETE")
     * @Security("has_role('ROLE_USER')")
     */
    public function deleteAction(Request $request, Depense $depense)
    {
        $form = $this->createDeleteForm($depense);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $montant_depense=$depense->getMontant();
            $em = $this->getDoctrine()->getManager();
            $em->remove($depense);
            $em->flush();
            $this->addFlash('success', 'Depense supprimée avec succès.');

            $tresorieService = new TresorieService($em);
            $tresorieService->controleExisteTresorie();
            $tresorieService->mettreAJourTresorie($montant_depense);
        }

        return $this->redirectToRoute('depense_index');
    }

    /**
     * Creates a form to delete a depense entity.
     *
     * @param Depense $depense The depense entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Depense $depense)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('depense_delete', array('id' => $depense->getId())))
            ->setMethod('DELETE')
            ->getForm();
    }
}

Zerion Mini Shell 1.0