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

namespace App\Controller;

use App\Entity\AchatMonnaieAConfirmerAdherent;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\Adherent;
8
use App\Entity\GlobalParameter;
Julien Jorry committed
9 10 11 12 13
use App\Entity\Payment;
use App\Entity\TransactionAdherentAdherent;
use App\Entity\TransactionAdherentPrestataire;
use App\Form\Type\AchatMonnaieAConfirmerAdherentFormType;
use App\Form\Type\AchatMonnaieAdherentFormType;
14
use App\Form\Type\AchatMonnaieAdherentRecurrentFormType;
Julien Jorry committed
15 16 17
use App\Form\Type\AdherentInfosFormType;
use App\Form\Type\TransactionAdherentAdherentFormType;
use App\Form\Type\TransactionAdherentPrestataireFormType;
18
use App\Form\Type\SetPaymentCodeFormType;
Julien Jorry committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UserAdherentController extends FluxController
{
    /**
     * @Route("/adherent/infos", name="adherent_infos")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function adherentInfosAction(Request $request)
    {
        $form = $this->createForm(AdherentInfosFormType::class, $this->getUser()->getAdherent());
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->persist($form->getData());
            $this->em->flush();
            $this->addFlash(
                'success',
                $this->translator->trans('Infos de l\'adhérent modifiées !')
            );
            $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("/adherent/transaction/prestataire/", name="transactionAdherentPrestataire")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function transactionAdherentPrestataireAction(Request $request)
    {
        $entity = new TransactionAdherentPrestataire();
        $entity->setOperateur($this->getUser());
        $entity->setExpediteur($this->getUser()->getAdherent());
        $form = $this->createForm(TransactionAdherentPrestataireFormType::class, $entity);

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

    /**
     * @Route("/adherent/transaction/adherent/", name="transactionAdherentAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function transactionAdherentAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }
        $entity = new TransactionAdherentAdherent();
        $entity->setOperateur($this->getUser());
        $entity->setExpediteur($this->getUser()->getAdherent());
        $form = $this->createForm(TransactionAdherentAdherentFormType::class, $entity);

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

    /**
     * @Route("/adherent/achat-monnaie/", name="achatMonnaieAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function achatMonnaieAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $entity = new AchatMonnaieAdherent();
        $form = $this->createForm(AchatMonnaieAdherentFormType::class, $entity);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
105
            if ($form->has('payOther') && $form->get('payOther')->isClicked()) {
Julien Jorry committed
106
                return $this->redirectToRoute('achatMonnaieAConfirmerAdherent');
107
            } elseif ($form->has('save') && $form->get('save')->isClicked()) {
108 109 110 111
                return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                    'form'  => $form,
                    'type'  => Payment::TYPE_ACHAT_MONNAIE_ADHERENT
                ]);
112
            } elseif ($form->has('saveHelloAsso') && $form->get('saveHelloAsso')->isClicked()) {
113 114 115
                $url = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::HELLOASSO_URL_EMLC_ADHERENT);

                return $this->redirect($url);
Julien Jorry committed
116 117 118 119 120 121 122 123 124
            }
        }

        return $this->render('@kohinos/flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $this->translator->trans('Achat de monnaie locale'),
        ]);
    }

125 126 127 128 129 130 131 132
    /**
     * Validations before proceeding to payment.
     * 
     * Returns error message or null if no error.
     */
    private function paiementCotisTavValidation($flux) {

        $destinataire = $flux->getDestinataire();
133 134 135
        if($reason = $this->tavCotisationsUtils->preventCotisationDuplication($destinataire)) {
            return $reason;
        }
136 137 138 139 140

        // Look for cotisation data depending on active process
        if (true == $this->getParameter('household_based_allowance')) {
            $cotisationAmount = $destinataire->getCotisationAmount();

141 142 143 144 145
            if (
                is_null($cotisationAmount) 
                || is_null($destinataire->getHouseholdAdultCount()) && !$this->getParameter('simplified_household_based_allowance')
                || is_null($destinataire->getHouseholdCount()) && $this->getParameter('simplified_household_based_allowance')
            ) {
146 147 148 149 150 151 152 153 154 155 156 157 158
                return "Opération impossible : votre profil est incomplet, informations de cotisation manquantes. Veuillez contacter un administrateur.";
            }
        } else {
            $profile = $destinataire->getProfilDeCotisation();

            if (is_null($profile)) {
                return "Opération impossible : vous n'avez pas de profil de cotisation associé. Veuillez contacter un administrateur.";
            }
        }

        return null;
    }
    
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    /**
     * @Route("/adherent/paiement-cotis-tav/", name="paiementCotisTav")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function paiementCotisTavAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $entity = new AchatMonnaieAdherent();
        $form = $this->createForm(AchatMonnaieAdherentFormType::class, $entity);
        $form->handleRequest($request);

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

176 177
            $validationError = $this->paiementCotisTavValidation($flux);
            if (!is_null($validationError)) {
178 179
                $this->addFlash(
                    'error',
180
                    $this->translator->trans($validationError)
181 182 183 184 185
                );

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

Damien Moulard committed
186
            if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
187 188 189
                $flux->setDon(null);
            }

190
            // Redirect to payment
191 192
            return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                'form'  => $form,
193
                'type'  => Payment::TYPE_PAIEMENT_COTISATION_TAV
194
            ]);
195

Damien Moulard committed
196
            /* For test purposes, comment redirection and uncomment following part to skip payment */
197 198 199 200 201
            // $this->em->persist($flux);
            // $this->operationUtils->executeOperations($flux);

            // // Apply cotisation rate, create new flux
            // $this->tavCotisationsUtils->applyTauxCotisation($flux);
202
            
203 204 205 206 207
            // $this->em->flush();
            // $this->addFlash(
            //     'success',
            //     $this->translator->trans('Cotisation payée ! [Paiement via Payzen temporairement désactivé]')
            // );
208

209
            // return $this->redirectToRoute('index');
210 211 212 213 214 215 216 217
        }

        return $this->render('@kohinos/flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $this->translator->trans('Payer sa cotisation'),
        ]);
    }

218 219 220 221 222 223 224 225 226 227 228 229 230 231
    /**
     * @Route("/adherent/paiement-reccurent-cotis-tav/", name="paiementRecurrentCotisTav")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function paiementRecurrentCotisTavAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $entity = new AchatMonnaieAdherent();
        $form = $this->createForm(AchatMonnaieAdherentRecurrentFormType::class, $entity);
        $form->handleRequest($request);

232 233 234 235 236 237 238
        if (!$form->isValid()) {
            foreach($form->getErrors(true) as $error) {
                $this->addFlash('error', $error->getMessage());
            }
            return $this->redirectToRoute('index');
        }

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        if ($form->isSubmitted() && $form->isValid()) {
            $flux = $form->getData();

            $validationError = $this->paiementCotisTavValidation($flux);
            if (!is_null($validationError)) {
                $this->addFlash(
                    'error',
                    $this->translator->trans($validationError)
                );

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

            if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
                $flux->setDon(null);
            }

            // Redirect to payment
            return $this->forward('App\Controller\PaymentController::preparePaymentAction', [
                'form'  => $form,
                'type'  => Payment::TYPE_PAIEMENT_RECURRENT_COTISATION_TAV
            ]);
        }

        return $this->render('@kohinos/flux/transaction.html.twig', [
            'form' => $form->createView(),
            'title' => $this->translator->trans('Payer sa cotisation'),
        ]);
    }

Julien Jorry committed
269 270
    /**
     * @Route("/adherent/demande/achat-monnaie/", name="achatMonnaieAConfirmerAdherent")
271
     * @IsGranted("ROLE_ADHERENT")
Julien Jorry committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
     */
    public function achatMonnaieAConfirmerAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }
        $entity = new AchatMonnaieAConfirmerAdherent();
        $form = $this->createForm(AchatMonnaieAConfirmerAdherentFormType::class, $entity);

        return $this->manageFluxForm(
            $request,
            $form,
            '@kohinos/flux/demande_achat_monnaie.html.twig',
            ['title' => $this->translator->trans("Demande d'achat de monnaie locale numérique")]
        );
    }

    /**
     * @Route("/adherent/liste/demande/achat-monnaie/", name="listachatMonnaieAConfirmerAdherent")
     * @IsGranted("ROLE_ADHERENT")
     */
    public function listachatMonnaieAConfirmerAdherentAction(Request $request)
    {
        if (empty($this->getUser()) || empty($this->getUser()->getAdherent())) {
            return $this->redirectToRoute('index');
        }

        $q = $this->em->getRepository(AchatMonnaieAConfirmerAdherent::class)->findBy(['destinataire' => $this->getUser()->getAdherent()], ['createdAt' => 'DESC']);

        return $this->render('@kohinos/flux/list_demande_achat_monnaie.html.twig', [
            'datas' => $q,
        ]);
    }
305 306 307 308 309

    /**
     * @Route("/adherent/set-payment-code", name="adherentSetPaymentCode")
     * @IsGranted("ROLE_ADHERENT")
     */
310
    public function setPaymentCodeAction(Request $request)
311 312 313 314 315 316 317 318 319
    {
        $adherent = $this->getUser()->getAdherent();
        $form = $this->createForm(SetPaymentCodeFormType::class, $adherent);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $plainCode = $data->getPaymentCode();

Félicie committed
320
            if (ctype_digit($plainCode) && strlen($plainCode) >= 4 && strlen($plainCode) <= 8) {
321 322
                $encoded = crypt($plainCode, $this->getUser()->getSalt());

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                $adherent->setPaymentCode($encoded);
    
                $this->em->flush();
                $this->addFlash(
                    'success',
                    $this->translator->trans('Code de paiement modifié !')
                );
            } else {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Le code de paiement doit être composé de 4 à 8 chiffres.')
                );
            }
        }

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