Commit cd0105f3 by Damien Moulard

1 tav cotisation per month

parent 33b04f78
......@@ -2,6 +2,7 @@
namespace App\Controller;
use App\Entity\Flux;
use App\Entity\AchatMonnaieAConfirmerAdherent;
use App\Entity\AchatMonnaieAdherent;
use App\Entity\Adherent;
......@@ -139,6 +140,16 @@ class UserAdherentController extends FluxController
// TODO: set CB payment when the functionality is validated
$flux = $form->getData();
// Look for existing cotisation
if ($this->tavCotisationsUtils->checkExistingCotisation($flux)) {
$this->addFlash(
'error',
$this->translator->trans('Cotisation déjà payée ce mois-ci.')
);
return $this->redirectToRoute('index');
}
if (null == $flux->getDon() || 0 == $flux->getDon()->getMontant()) {
$flux->setDon(null);
}
......@@ -152,7 +163,7 @@ class UserAdherentController extends FluxController
$this->em->flush();
$this->addFlash(
'success',
$this->translator->trans('Cotisation payée ! [Payzen désactivé en attent du compte ; mensualité pas encore implémentée]')
$this->translator->trans('Cotisation payée ! [Payzen désactivé en attent du compte]')
);
return $this->redirectToRoute('index');
......
......@@ -202,6 +202,16 @@ class UserComptoirController extends FluxController
if ($form->isSubmitted() && $form->isValid()) {
$flux = $form->getData();
// Look for existing cotisation
if ($this->tavCotisationsUtils->checkExistingCotisation($flux)) {
$this->addFlash(
'error',
$this->translator->trans('L\'adhérent•e a déjà payé sa cotisation ce mois-ci.')
);
return $this->redirectToRoute('index');
}
$destinataire = $flux->getDestinataire();
$profile = $destinataire->getProfilDeCotisation();
......
......@@ -239,4 +239,29 @@ class FluxRepository extends ServiceEntityRepository
->getQuery()
->getSingleScalarResult();
}
/**
* @param Adherent $adherent the user to look the cotisations for
* @param String $from, $to dates (ISO format) between wich to look for
*
* @return Array list of cotisations between the dates
*/
public function getTavCotisationsBetweenDates($adherent, $from, $to)
{
$sqlQuery = "SELECT f.id
FROM {$this->tableName} f
WHERE f.type IN ('achat_monnaie_adherent', 'vente_emlc_adherent')
AND f.created_at >= :f
AND f.created_at <= :t
AND f.adherent_id = :adh_id";
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':f', $from);
$statement->bindValue(':t', $to);
$statement->bindValue(':adh_id', $adherent->getId());
$statement->execute();
$results = $statement->fetchAll();
return $results;
}
}
......@@ -27,6 +27,23 @@ class TAVCotisationUtils
}
/**
* Check if cotisation already exist this month the the recipient of the given Flux.
*/
public function checkExistingCotisation(Flux $flux)
{
$first_day_this_month = date('Y-m-01');
$last_day_this_month = date('Y-m-t');
$existing = $this->em->getRepository(Flux::class)->getTavCotisationsBetweenDates(
$flux->getDestinataire(),
$first_day_this_month,
$last_day_this_month
);
return count($existing) > 0;
}
/**
* Apply the cotisation profile rate to the amount paid
* and register the complement as a new flux
*
......
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