AdherentAdminController.php 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace App\Controller;

use App\Utils\CustomEntityManager;
use App\Utils\TAVCotisationUtils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Security;
14 15
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Adherent;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

class AdherentAdminController extends CRUDController
{
    protected $em;
    protected $security;
    protected $tavCotisationUtils;

    public function __construct(CustomEntityManager $em, Security $security, TAVCotisationUtils $tavCotisationUtils)
    {
        $this->em = $em;
        $this->security = $security;
        $this->tavCotisationUtils = $tavCotisationUtils;
    }

    /**
     * Prélèvement d'un adhérent pour ramener son solde sous son plafond.
     *
     * @param Request $request
     * @param Uuid    $id      Id du prestataire
35
     * @IsGranted({"ROLE_SUPER_ADMIN", "ROLE_ADMIN_SIEGE", "ROLE_TRESORIER"})
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
     * @return Response
     */
    public function withdrawDownToTheCeilingAction(Request $request, $id): Response
    {
        $adherent = $this->admin->getSubject();

        if (!$adherent) {
            throw new NotFoundHttpException(sprintf('Impossible de trouver l\'adhérent avec l\'id: %s', $id));
        }

        $amountDiff = $this->tavCotisationUtils->withdrawDownToTheCeiling($adherent);

        $this->em->flush();

        $this->addFlash(
            'sonata_flash_success',
52
            'Prélèvement de ' . -$amountDiff . ' MonA' . ' effectué.'
53 54 55 56 57 58
        );

        return new RedirectResponse(
            $this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()])
        );
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    /**
     * Recalculate household allocation in case of switching between household allocation methods.
     *
     * @param Request $request
     * @IsGranted({"ROLE_SUPER_ADMIN", "ROLE_ADMIN_SIEGE", "ROLE_TRESORIER"})
     * @return Response
     */
    public function recalculateHouseholdAllocationsAction(Request $request): Response
    {

        $qb = $this->em->getRepository(Adherent::class)->createQueryBuilder('a');

        if ($this->getParameter('simplified_household_based_allowance')) {
            $adherents = $qb->where($qb->expr()->isNotNull("a.householdCount"))
                ->andWhere($qb->expr()->isNotNull("a.cotisationAmount"))
                ->getQuery()
                ->getResult();
            ;
    
            foreach ($adherents as $adherent)  {
                $this->tavCotisationUtils->calculateAllowanceAccordingToHouseholdSimplified($adherent);
                $this->em->persist($adherent);
            }
        } else if ($this->getParameter('household_based_allowance')) {
            $adherents = $qb->where($qb->expr()->isNotNull("a.householdAdultCount"))
                ->andWhere($qb->expr()->isNotNull("a.cotisationAmount"))
                ->getQuery()
                ->getResult();
            ;

            foreach ($adherents as $adherent)  {
                $this->tavCotisationUtils->calculateAllowanceAccordingToHousehold($adherent);
                $this->em->persist($adherent);
            }
        }

        $this->em->flush();

        $this->addFlash(
            'sonata_flash_success',
            'Allocation recalculées avec succès'
        );

        return new RedirectResponse(
            $this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()])
        );
    }
107
}