Commit 4ce2d41f by Damien Moulard

add new cotisation and allocation amounts to adherent, add fields to adherent…

add new cotisation and allocation amounts to adherent, add fields to adherent admin, calculate allocation based on household at saving in admin
parent 45fbd986
......@@ -267,6 +267,7 @@ services:
- [ setUserManager, ['@fos_user.user_manager']]
- [ setSecurity, ['@security.helper']]
- [ setEventDispatcher, ['@event_dispatcher']]
- [ setTavCotisationUtils, ['@app.utils.tav_cotisations']]
admin.all.cotisations:
class: App\Admin\CotisationAdmin
......
......@@ -18,6 +18,7 @@ use App\Exporter\CustomDoctrineORMQuerySourceIterator;
use App\Form\Type\DependentChildFormType;
use App\Form\Type\GeolocFormType;
use App\Form\Type\UserFormType;
use App\Utils\TAVCotisationUtils;
use Doctrine\ORM\Query;
use FOS\UserBundle\Event\UserEvent;
use Knp\Menu\ItemInterface as MenuItemInterface;
......@@ -40,6 +41,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
......@@ -59,6 +61,7 @@ class AdherentAdmin extends AbstractAdmin
protected $baseRouteName = 'adherent';
protected $baseRoutePattern = 'adherent';
protected $security;
protected $tavCotisationUtils;
protected $datagridValues = [
// reverse order (default = 'ASC')
......@@ -74,6 +77,11 @@ class AdherentAdmin extends AbstractAdmin
$this->security = $security;
}
public function setTavCotisationUtils(TAVCotisationUtils $tavCotisationUtils)
{
$this->tavCotisationUtils = $tavCotisationUtils;
}
public function configure()
{
parent::configure();
......@@ -149,6 +157,11 @@ class AdherentAdmin extends AbstractAdmin
if (null == $adherent->getGeoloc()) {
$adherent->setGeoloc(new Geoloc());
}
// params
$tav_env = $this->getConfigurationPool()->getContainer()->getParameter('tav_env');
$household_based_allowance = $this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance');
$formMapper
->tab('General')
->with('Identité', ['class' => 'col-md-7'])
......@@ -162,12 +175,8 @@ class AdherentAdmin extends AbstractAdmin
'required' => true,
'with_geoloc' => false,
'with_latlon' => false,
'with_subterritory' =>
$this->getConfigurationPool()->getContainer()->getParameter('tav_env')
&& $this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance'),
'with_quartier' =>
$this->getConfigurationPool()->getContainer()->getParameter('tav_env')
&& $this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance')
'with_subterritory' => $tav_env && $household_based_allowance,
'with_quartier' => $tav_env && $household_based_allowance
])
->end()
->with('Groupe', ['class' => 'col-md-5'])
......@@ -182,8 +191,13 @@ class AdherentAdmin extends AbstractAdmin
->end()
;
if ($this->getConfigurationPool()->getContainer()->getParameter('tav_env')) {
if ($this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance')) {
/**
* In TAV env, 2 allowance processes possible:
* - household based (if param set)
* - cotisation profile and rate based (default)
*/
if ($tav_env) {
if ($household_based_allowance) {
$formMapper
->tab('General')
->with('Foyer', ['class' => 'col-md-7'])
......@@ -223,7 +237,24 @@ class AdherentAdmin extends AbstractAdmin
])
->end()
->end();
}
// Add cotisation info
$formMapper
->tab('General')
->with('Informations de cotisation', ['class' => 'col-md-5'])
->add('cotisationAmount', NumberType::class, [
'label' => 'Montant de la cotisation (en €)',
'help' => 'Montant minimum : 10€ par foyer + 5€/personne supplémentaire du foyer'
])
->add('allocationAmount', NumberType::class, [
'label' => 'Montant d\'allocation prévu en fonction du foyer (en MonA)',
'disabled' => true,
'required' => false,
'help' => 'Le montant de l\'allocation sera calculé automatiquement en fonction des informations du foyer une fois les informations sauvegardées.'
])
->end()
->end();
} else {
// For Comptoir role in edit mode, hide profile choice
$displayProfilChoice = true;
$isComptoirOnly =
......@@ -308,6 +339,7 @@ class AdherentAdmin extends AbstractAdmin
])
->end()
->end();
}
if (!empty($adherent) && !empty($adherent->getEmlcAccount()) ) {
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
......@@ -333,6 +365,8 @@ class AdherentAdmin extends AbstractAdmin
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
$formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($em) {
$adherent = $event->getData();
// Check user email
$user = $adherent->getUser();
if (!$user || null === $user->getId()) {
$repo = $em->getRepository(User::class);
......@@ -343,6 +377,17 @@ class AdherentAdmin extends AbstractAdmin
$user->setUsername($user->getEmail());
}
}
// check cotisation amount
if ($this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance')) {
$adultsCount = $adherent->getHouseholdAdultCount();
$dependentChildrenCount = count($adherent->getDependentChildren());
$minCotisationAmount = 10 + 5 * ( $adultsCount - 1 ) + 5 * $dependentChildrenCount;
if ($adherent->getCotisationAmount() < $minCotisationAmount) {
$event->getForm()->get('cotisationAmount')->addError(new FormError('Le montant minimum est de ' . $minCotisationAmount . '€ (selon les données du foyer indiquées)'));
}
}
});
parent::configureFormFields($formMapper);
}
......@@ -389,6 +434,23 @@ class AdherentAdmin extends AbstractAdmin
$em->flush();
}
public function postUpdate($adherent)
{
$this->postUpdateAdherent($adherent);
}
public function postPersist($adherent)
{
$this->postUpdateAdherent($adherent);
}
private function postUpdateAdherent($adherent)
{
if ($this->getConfigurationPool()->getContainer()->getParameter('household_based_allowance')) {
$this->tavCotisationUtils->calculateAllowanceAccordingToHousehold($adherent);
}
}
/**
* {@inheritdoc}
*/
......
......@@ -129,6 +129,21 @@ class Adherent extends AccountableObject implements AccountableInterface
*/
private $householdAdultCount;
/**
* On household based allowance process, define a cotisation amount for each adherent.
*
* @ORM\Column(type="float", nullable=true)
*/
private $cotisationAmount;
/**
* On household based allowance process, the allowance amountis calculated based on household data.
* Calculate and save the allocation amount when the household data is updated.
*
* @ORM\Column(type="float", nullable=true)
*/
private $allocationAmount;
public function __construct()
{
......@@ -407,4 +422,28 @@ class Adherent extends AccountableObject implements AccountableInterface
return $this;
}
public function getCotisationAmount(): ?float
{
return $this->cotisationAmount;
}
public function setCotisationAmount(?float $cotisationAmount): self
{
$this->cotisationAmount = $cotisationAmount;
return $this;
}
public function getAllocationAmount(): ?float
{
return $this->allocationAmount;
}
public function setAllocationAmount(?float $allocationAmount): self
{
$this->allocationAmount = $allocationAmount;
return $this;
}
}
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240313125437 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE adherent ADD cotisation_amount DOUBLE PRECISION DEFAULT NULL, ADD allocation_amount DOUBLE PRECISION DEFAULT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE adherent DROP cotisation_amount, DROP allocation_amount');
}
}
......@@ -110,19 +110,17 @@ class TAVCotisationUtils
* Once the full amount is calculated, cap user's balance.
* User account balance is capped at twice the amount previously calculated.
*/
public function applyAllowanceAccordingToHousehold(Flux $flux) {
$adherent = $flux->getDestinataire();
$profile = $adherent->getProfilDeCotisation();
$cotisationAmount = $profile->getMontant();
public function calculateAllowanceAccordingToHousehold($adherent) {
$cotisationAmount = $adherent->getCotisationAmount();
// TODO amounts param
// TODO base amounts to param in .env)
// base allowance, for one adult
$mlcAllowanceAmount = 150;
$adultsCount = $adherent->getHouseholdAdultCount();
if ($adultsCount == null) {
// error
return;
}
// increment for each other adult in the household
......@@ -133,13 +131,27 @@ class TAVCotisationUtils
foreach ($dependentChildren as $child) {
$childAllowanceAmount = 75;
if ($child->sharedCustodyPercentage != null) {
$childAllowanceAmount = $childAllowanceAmount * $child->sharedCustodyPercentage;
$sharedCustodyPercentage = $child->getSharedCustodyPercentage();
if ($sharedCustodyPercentage != null) {
$childAllowanceAmount = $childAllowanceAmount * $sharedCustodyPercentage;
}
$mlcAllowanceAmount += $childAllowanceAmount;
}
$adherent->setAllocationAmount($mlcAllowanceAmount);
$this->em->persist($adherent);
$this->em->flush();
}
/**
* Method called to create Flux
*
* TODO
*/
public function applyHouseholdAllowance(Flux $flux) {
// get allowance
// Apply cap
$currentBalance = $adherent->getEmlcAccount()->getBalance();
$capped = false;
......@@ -169,7 +181,6 @@ class TAVCotisationUtils
$this->em->persist($fluxCotis);
$this->operationUtils->executeOperations($fluxCotis);
}
/**
* Get the last cotisation of an adhérent
*
......
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