UserComptoirController.php 11.9 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7
<?php

namespace App\Controller;

use App\Entity\Adherent;
use App\Entity\ChangeAdherentComptoir;
use App\Entity\ChangePrestataireComptoir;
8
use App\Entity\Comptoir;
9
use App\Entity\DonAdherent;
Julien Jorry committed
10 11 12 13 14 15 16 17
use App\Entity\RetraitComptoirAdherent;
use App\Entity\RetraitComptoirPrestataire;
use App\Entity\VenteComptoirAdherent;
use App\Entity\VenteComptoirPrestataire;
use App\Entity\VenteEmlcComptoirAdherent;
use App\Entity\VenteEmlcComptoirPrestataire;
use App\Form\Type\ChangeAdherentComptoirFormType;
use App\Form\Type\ChangePrestataireComptoirFormType;
18
use App\Form\Type\ComptoirEncaisserDonAdherentFormType;
19
use App\Form\Type\ComptoirInfosFormType;
Julien Jorry committed
20 21 22 23 24 25
use App\Form\Type\RetraitComptoirAdherentFormType;
use App\Form\Type\RetraitComptoirPrestataireFormType;
use App\Form\Type\VenteComptoirAdherentFormType;
use App\Form\Type\VenteComptoirPrestataireFormType;
use App\Form\Type\VenteEmlcAdherentFormType;
use App\Form\Type\VenteEmlcPrestataireFormType;
26
use App\Form\Type\EncaisserCotisationAdherentFormType;
Julien Jorry committed
27 28 29 30 31 32 33 34 35 36 37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UserComptoirController extends FluxController
{
    /**
     * @Route("/user/comptoir/infos", name="comptoir_infos")
     * @IsGranted("ROLE_COMPTOIR")
     */
38
    public function comptoirInfosAction(Request $request)
Julien Jorry committed
39
    {
40 41 42
        $comptoir = $this->session->get('_comptoirgere');
        $comptoir = $this->em->getRepository(Comptoir::class)->findOneById($comptoir->getId());
        $form = $this->createForm(ComptoirInfosFormType::class, $comptoir);
Julien Jorry committed
43 44 45 46
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
47
            $this->em->persist($form->getData());
Julien Jorry committed
48 49 50
            $this->em->flush();
            $this->addFlash(
                'success',
51
                $this->translator->trans('Informations du comptoir bien modifiées !')
Julien Jorry committed
52
            );
53
            $this->session->set('_comptoirgere', $comptoir);
Julien Jorry committed
54 55 56 57 58 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            $referer = $request->headers->get('referer');
            if ($referer && !$request->isXmlHttpRequest()) {
                return $this->redirect($referer);
            } elseif (!$request->isXmlHttpRequest()) {
                return new Response('', Response::HTTP_BAD_REQUEST);
            }
        }

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

    /**
     * @Route("/user/comptoir/vente/adherent/", name="venteComptoirAdherent")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function venteComptoirAdherentAction(Request $request)
    {
        $entity = new VenteComptoirAdherent();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(VenteComptoirAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/vente/prestataire/", name="venteComptoirPrestataire")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function venteComptoirPrestataireAction(Request $request)
    {
        $entity = new VenteComptoirPrestataire();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(VenteComptoirPrestataireFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/retrait/adherent/", name="retraitComptoirAdherent")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function retraitComptoirAdherentAction(Request $request)
    {
        $entity = new RetraitComptoirAdherent();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(RetraitComptoirAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/retrait/prestataire/", name="retraitComptoirPrestataire")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function retraitComptoirPrestataireAction(Request $request)
    {
        $entity = new RetraitComptoirPrestataire();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(RetraitComptoirPrestataireFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/change/prestataire/", name="changePrestataireComptoir")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function changePrestataireComptoirAction(Request $request)
    {
        $entity = new ChangePrestataireComptoir();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(ChangePrestataireComptoirFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/change/adherent/", name="changeAdherentComptoir")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function changeAdherentComptoirAction(Request $request)
    {
        $entity = new ChangeAdherentComptoir();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(ChangeAdherentComptoirFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/vente/emlc/adherent/", name="venteEmlcAdherent")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function venteEmlcAdherentAction(Request $request)
    {
        $entity = new VenteEmlcComptoirAdherent();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(VenteEmlcAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }

    /**
     * @Route("/user/comptoir/vente/emlc/prestataire/", name="venteEmlcPrestataire")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function venteEmlcPrestataireAction(Request $request)
    {
        $entity = new VenteEmlcComptoirPrestataire();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(VenteEmlcPrestataireFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form
        );
    }
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    /**
     * @Route("/user/comptoir/encaisser/cotisation/adherent/", name="comptoirEncaisserCotisation")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function comptoirEncaisserCotisationAction(Request $request)
    {
        $entity = new VenteEmlcComptoirAdherent();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(EncaisserCotisationAdherentFormType::class, $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $flux = $form->getData();

207
            // Look for existing cotisation
208
            if ($reason = $this->tavCotisationUtils->preventCotisationDuplication($flux->getDestinataire())) {
209 210
                $this->addFlash(
                    'error',
211
                    $this->translator->trans($reason)
212 213 214 215 216
                );

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

217 218
            $destinataire = $flux->getDestinataire();

219 220 221 222 223
            $allocationMethod = $this->tavCotisationUtils->getAppropriateAllocationMethod($destinataire);

            // Some code duplication between household_based_allowance && simplified_household_based_allowance, for better readability
            if ($allocationMethod == 'simplified_household_based_allowance') {
                /* Process: simplified allowance based on household */
224 225 226
                $cotisationAmount = $destinataire->getCotisationAmount();

                // Verifications
227
                if (is_null($cotisationAmount) || is_null($destinataire->getHouseholdCount()) ) {
228 229 230 231 232 233 234 235
                    $this->addFlash(
                        'error',
                        $this->translator->trans("Opération impossible : le profil de l'habitant.e est incomplet, veuillez le compléter dans l'interface d'administration.")
                    );

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

236 237 238 239 240 241 242 243 244
                $flux->setMontant($cotisationAmount);
                $this->em->persist($flux);
                $this->operationUtils->executeOperations($flux);

                // Create new flux based on difference with cotisation amount
                $this->tavCotisationUtils->applyHouseholdAllowance($flux);
            } else if ($allocationMethod == 'household_based_allowance') {
                /* Process: allowance based on household */
                $cotisationAmount = $destinataire->getCotisationAmount();
245

246 247 248 249 250 251 252 253 254 255 256 257
                // Verifications
                if (is_null($cotisationAmount) || is_null($destinataire->getHouseholdAdultCount()) ) {
                    $this->addFlash(
                        'error',
                        $this->translator->trans("Opération impossible : le profil de l'habitant.e est incomplet, veuillez le compléter dans l'interface d'administration.")
                    );

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

                if (is_null($destinataire->getAllocationAmount())) {
                    $this->tavCotisationUtils->calculateAllowanceAccordingToHousehold($destinataire);
258 259 260 261 262 263 264
                    $this->em->persist($destinataire);
                }

                $flux->setMontant($cotisationAmount);
                $this->em->persist($flux);
                $this->operationUtils->executeOperations($flux);

265 266
                // Create new flux based on difference with cotisation amount
                $this->tavCotisationUtils->applyHouseholdAllowance($flux);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
            } else {
                /* Process: allowance based on cotisation profile with cotisation rate */
                $profile = $destinataire->getProfilDeCotisation();

                if (is_null($profile)) {
                    $this->addFlash(
                        'error',
                        $this->translator->trans('Opération impossible : l\'habitant.e n\'a pas de profil de cotisation associé.')
                    );

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

                $cotisationAmount = $profile->getMontant();  // Amount in € paid by the user
                $flux->setMontant($cotisationAmount);
                
                $this->em->persist($flux);
                $this->operationUtils->executeOperations($flux);

                // Apply cotisation rate, create new flux
287
                $this->tavCotisationUtils->applyTauxCotisation($flux);
288 289 290 291 292 293 294 295 296 297 298 299
            }

            $this->em->flush();
            
            $this->addFlash(
                'success',
                $this->translator->trans('Opération réussie !')
            );
        }

        return $this->redirectToRoute('index');
    }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

    /**
     * @Route("/user/comptoir/encaisser/don/adherent/", name="comptoirEncaisserDonAdherent")
     * @IsGranted("ROLE_COMPTOIR")
     */
    public function comptoirEncaisserDonAdherentAction(Request $request)
    {
        $entity = new DonAdherent();
        $entity->setOperateur($this->getUser());
        $form = $this->createForm(ComptoirEncaisserDonAdherentFormType::class, $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if($form->isValid()) {
                $flux = $form->getData();
                $this->em->persist($flux);
                $this->operationUtils->executeOperations($flux);
                $this->em->flush();

                $this->addFlash(
                    'success',
                    $this->translator->trans('Opération réussie !')
                );
            } else {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Erreur : le don n\'a pas été enregistré.')
                );
            }
        }

        return $this->redirectToRoute('index');
    }
Julien Jorry committed
333
}