Commit 33b04f78 by Damien Moulard

apply taux cotisation at payment by adherent

parent 965b172b
......@@ -22,6 +22,7 @@ use App\Flux\FluxInterface;
use App\Security\LoginAuthenticator;
use App\Utils\CustomEntityManager;
use App\Utils\OperationUtils;
use App\Utils\TAVCotisationUtils;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
......@@ -107,6 +108,7 @@ class FluxController extends AbstractController
Payum $payum,
Environment $templating,
OperationUtils $operationUtils,
TAVCotisationUtils $tavCotisationsUtils,
TokenGeneratorInterface $tokenGenerator,
ValidatorInterface $validator,
CsrfTokenManagerInterface $tokenManager,
......@@ -127,6 +129,7 @@ class FluxController extends AbstractController
$this->validator = $validator;
$this->tokenManager = $tokenManager;
$this->router = $router;
$this->tavCotisationsUtils = $tavCotisationsUtils;
}
protected function manageFluxForm(Request $request, Form $form, $template = '@kohinos/flux/transaction.html.twig', $params = [])
......
......@@ -136,7 +136,7 @@ class UserAdherentController extends FluxController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// TODO: set CB payment when functionality validated
// TODO: set CB payment when the functionality is validated
$flux = $form->getData();
if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
......@@ -145,10 +145,14 @@ class UserAdherentController extends FluxController
$this->em->persist($flux);
$this->operationUtils->executeOperations($flux);
// Apply cotisation rate, create new flux
$this->tavCotisationsUtils->applyTauxCotisation($flux);
$this->em->flush();
$this->addFlash(
'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');
......
......@@ -12,8 +12,6 @@ use App\Entity\VenteComptoirAdherent;
use App\Entity\VenteComptoirPrestataire;
use App\Entity\VenteEmlcComptoirAdherent;
use App\Entity\VenteEmlcComptoirPrestataire;
use App\Entity\TauxCotisationReversement;
use App\Entity\TauxCotisationPrelevement;
use App\Form\Type\ChangeAdherentComptoirFormType;
use App\Form\Type\ChangePrestataireComptoirFormType;
use App\Form\Type\ComptoirInfosFormType;
......@@ -24,7 +22,6 @@ use App\Form\Type\VenteComptoirPrestataireFormType;
use App\Form\Type\VenteEmlcAdherentFormType;
use App\Form\Type\VenteEmlcPrestataireFormType;
use App\Form\Type\EncaisserCotisationAdherentFormType;
use App\Enum\MoyenEnum;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -223,34 +220,8 @@ class UserComptoirController extends FluxController
$this->em->persist($flux);
$this->operationUtils->executeOperations($flux);
// get the mlc amount the user will receive
$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 ($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);
// Apply cotisation rate, create new flux
$this->tavCotisationsUtils->applyTauxCotisation($flux);
$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