Commit 33b04f78 by Damien Moulard

apply taux cotisation at payment by adherent

parent 965b172b
...@@ -22,6 +22,7 @@ use App\Flux\FluxInterface; ...@@ -22,6 +22,7 @@ use App\Flux\FluxInterface;
use App\Security\LoginAuthenticator; use App\Security\LoginAuthenticator;
use App\Utils\CustomEntityManager; use App\Utils\CustomEntityManager;
use App\Utils\OperationUtils; use App\Utils\OperationUtils;
use App\Utils\TAVCotisationUtils;
use FOS\UserBundle\Model\UserManagerInterface; use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface; use FOS\UserBundle\Util\TokenGeneratorInterface;
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer; use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
...@@ -107,6 +108,7 @@ class FluxController extends AbstractController ...@@ -107,6 +108,7 @@ class FluxController extends AbstractController
Payum $payum, Payum $payum,
Environment $templating, Environment $templating,
OperationUtils $operationUtils, OperationUtils $operationUtils,
TAVCotisationUtils $tavCotisationsUtils,
TokenGeneratorInterface $tokenGenerator, TokenGeneratorInterface $tokenGenerator,
ValidatorInterface $validator, ValidatorInterface $validator,
CsrfTokenManagerInterface $tokenManager, CsrfTokenManagerInterface $tokenManager,
...@@ -127,6 +129,7 @@ class FluxController extends AbstractController ...@@ -127,6 +129,7 @@ class FluxController extends AbstractController
$this->validator = $validator; $this->validator = $validator;
$this->tokenManager = $tokenManager; $this->tokenManager = $tokenManager;
$this->router = $router; $this->router = $router;
$this->tavCotisationsUtils = $tavCotisationsUtils;
} }
protected function manageFluxForm(Request $request, Form $form, $template = '@kohinos/flux/transaction.html.twig', $params = []) protected function manageFluxForm(Request $request, Form $form, $template = '@kohinos/flux/transaction.html.twig', $params = [])
......
...@@ -136,7 +136,7 @@ class UserAdherentController extends FluxController ...@@ -136,7 +136,7 @@ class UserAdherentController extends FluxController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
// TODO: set CB payment when functionality validated // TODO: set CB payment when the functionality is validated
$flux = $form->getData(); $flux = $form->getData();
if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) { if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
...@@ -145,10 +145,14 @@ class UserAdherentController extends FluxController ...@@ -145,10 +145,14 @@ class UserAdherentController extends FluxController
$this->em->persist($flux); $this->em->persist($flux);
$this->operationUtils->executeOperations($flux); $this->operationUtils->executeOperations($flux);
// Apply cotisation rate, create new flux
$this->tavCotisationsUtils->applyTauxCotisation($flux);
$this->em->flush(); $this->em->flush();
$this->addFlash( $this->addFlash(
'success', 'success',
$this->translator->trans('Cotisation payée ! [Payzen désactivé en attent du compte ; bonification et mensualité pas encore implémentés]') $this->translator->trans('Cotisation payée ! [Payzen désactivé en attent du compte ; mensualité pas encore implémentée]')
); );
return $this->redirectToRoute('index'); return $this->redirectToRoute('index');
......
...@@ -12,8 +12,6 @@ use App\Entity\VenteComptoirAdherent; ...@@ -12,8 +12,6 @@ use App\Entity\VenteComptoirAdherent;
use App\Entity\VenteComptoirPrestataire; use App\Entity\VenteComptoirPrestataire;
use App\Entity\VenteEmlcComptoirAdherent; use App\Entity\VenteEmlcComptoirAdherent;
use App\Entity\VenteEmlcComptoirPrestataire; use App\Entity\VenteEmlcComptoirPrestataire;
use App\Entity\TauxCotisationReversement;
use App\Entity\TauxCotisationPrelevement;
use App\Form\Type\ChangeAdherentComptoirFormType; use App\Form\Type\ChangeAdherentComptoirFormType;
use App\Form\Type\ChangePrestataireComptoirFormType; use App\Form\Type\ChangePrestataireComptoirFormType;
use App\Form\Type\ComptoirInfosFormType; use App\Form\Type\ComptoirInfosFormType;
...@@ -24,7 +22,6 @@ use App\Form\Type\VenteComptoirPrestataireFormType; ...@@ -24,7 +22,6 @@ use App\Form\Type\VenteComptoirPrestataireFormType;
use App\Form\Type\VenteEmlcAdherentFormType; use App\Form\Type\VenteEmlcAdherentFormType;
use App\Form\Type\VenteEmlcPrestataireFormType; use App\Form\Type\VenteEmlcPrestataireFormType;
use App\Form\Type\EncaisserCotisationAdherentFormType; use App\Form\Type\EncaisserCotisationAdherentFormType;
use App\Enum\MoyenEnum;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -223,34 +220,8 @@ class UserComptoirController extends FluxController ...@@ -223,34 +220,8 @@ class UserComptoirController extends FluxController
$this->em->persist($flux); $this->em->persist($flux);
$this->operationUtils->executeOperations($flux); $this->operationUtils->executeOperations($flux);
// get the mlc amount the user will receive // Apply cotisation rate, create new flux
$cotisationTaux = $profile->getTauxCotisation(); $this->tavCotisationsUtils->applyTauxCotisation($flux);
$mlcAmount = $cotisationAmount * $cotisationTaux;
// get the difference between what the user paid and what he•she's supposed to receive
$amountDiff = $mlcAmount - $cotisationAmount;
if ($amountDiff > 0) {
// User should receive more than he•she paid: send a new flux to the user to complete its cotisation
$fluxCotis = new TauxCotisationReversement();
$fluxCotis->setExpediteur($flux->getExpediteur()->getGroupe()->getSiege());
$fluxCotis->setDestinataire($flux->getDestinataire());
$fluxCotis->setMontant($amountDiff);
$fluxCotis->setReference("Reversement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
} else {
// User should receive less than he•she paid: fetch the difference from his account
$fluxCotis = new TauxCotisationPrelevement();
$fluxCotis->setExpediteur($flux->getDestinataire());
$fluxCotis->setDestinataire($flux->getExpediteur()->getGroupe()->getSiege());
$fluxCotis->setMontant(-$amountDiff);
$fluxCotis->setReference("Prélèvement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
}
$fluxCotis->setOperateur($this->getUser());
$fluxCotis->setRole($this->getUser()->getGroups()[0]->__toString());
$fluxCotis->setMoyen(MoyenEnum::MOYEN_EMLC);
$this->em->persist($fluxCotis);
$this->operationUtils->executeOperations($fluxCotis);
$this->em->flush(); $this->em->flush();
......
<?php
namespace App\Utils;
use App\Entity\Siege;
use App\Entity\Flux;
use App\Entity\TauxCotisationReversement;
use App\Entity\TauxCotisationPrelevement;
use App\Enum\MoyenEnum;
use App\Utils\CustomEntityManager;
use Symfony\Component\Security\Core\Security;
class TAVCotisationUtils
{
private $em;
private $security;
private $operationUtils;
public function __construct (
CustomEntityManager $em,
Security $security,
OperationUtils $operationUtils
) {
$this->em = $em;
$this->security = $security;
$this->operationUtils = $operationUtils;
}
/**
* Apply the cotisation profile rate to the amount paid
* and register the complement as a new flux
*
* Warning: EntityManager not flushed here.
*/
public function applyTauxCotisation(Flux $flux)
{
// get the mlc amount the user will receive
$profile = $flux->getDestinataire()->getProfilDeCotisation();
$cotisationAmount = $profile->getMontant();
$cotisationTaux = $profile->getTauxCotisation();
$mlcAmount = $cotisationAmount * $cotisationTaux;
// get the difference between what the user paid and what he•she's supposed to receive
$amountDiff = $mlcAmount - $cotisationAmount;
if ($flux->getExpediteur() instanceof Siege) {
$siege = $flux->getExpediteur();
} else {
$siege = $flux->getExpediteur()->getGroupe()->getSiege();
}
if ($amountDiff > 0) {
// User should receive more than he•she paid: send a new flux to the user to complete its cotisation
$fluxCotis = new TauxCotisationReversement();
$fluxCotis->setExpediteur($siege);
$fluxCotis->setDestinataire($flux->getDestinataire());
$fluxCotis->setMontant($amountDiff);
$fluxCotis->setReference("Reversement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
} else {
// User should receive less than he•she paid: fetch the difference from his account
$fluxCotis = new TauxCotisationPrelevement();
$fluxCotis->setExpediteur($flux->getDestinataire());
$fluxCotis->setDestinataire($siege);
$fluxCotis->setMontant(-$amountDiff);
$fluxCotis->setReference("Prélèvement cotisation après paiement de " . $cotisationAmount . "€ et application du taux " . $cotisationTaux);
}
$fluxCotis->setOperateur($this->security->getUser());
$fluxCotis->setRole($this->security->getUser()->getGroups()[0]->__toString());
$fluxCotis->setMoyen(MoyenEnum::MOYEN_EMLC);
$this->em->persist($fluxCotis);
$this->operationUtils->executeOperations($fluxCotis);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment