Commit 2651c7c6 by Julien Jorry

Solidoume : fix non recurrent taking + add prelevements to show view in admin +…

Solidoume : fix non recurrent taking + add prelevements to show view in admin + last month payed in list view
parent 20d569b8
...@@ -11,6 +11,7 @@ use Sonata\AdminBundle\Admin\AdminInterface; ...@@ -11,6 +11,7 @@ use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\Form\Type\DateTimeRangePickerType; use Sonata\Form\Type\DateTimeRangePickerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
...@@ -136,20 +137,53 @@ class SolidoumeAdmin extends AbstractAdmin ...@@ -136,20 +137,53 @@ class SolidoumeAdmin extends AbstractAdmin
'label' => 'Commentaires', 'label' => 'Commentaires',
'editable' => true, 'editable' => true,
]) ])
// ->addIdentifier('enabled', null, ['label' => 'Activé', 'datatype' => 'App.SolidoumeItem', 'template' => '@kohinos/bundles/SonataAdminBundle/Boolean/editable_boolean.html.twig']) ->add('lastMonthPayed', null, [
'label' => 'Dernier prélèvement',
'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list_solidoume_lastmonthpayed.html.twig',
])
->add('_action', null, [ ->add('_action', null, [
'actions' => [ 'actions' => [
'show' => [],
'edit' => [], 'edit' => [],
], ],
]); ]);
} }
/**
* {@inheritdoc}
*/
public function configure() public function configure()
{ {
parent::configure(); parent::configure();
$this->setTemplate('list', '@kohinos/bundles/SonataAdminBundle/CRUD/solidoume_list.html.twig'); $this->setTemplate('list', '@kohinos/bundles/SonataAdminBundle/CRUD/solidoume_list.html.twig');
} }
/**
* {@inheritdoc}
*/
protected function configureShowFields(ShowMapper $showMapper): void
{
$object = $showMapper->getAdmin()->getSubject();
$showMapper
->add('createdAt', null, ['label' => 'Date d\'adhésion'])
->add('user', null, ['label' => 'Utilisateur'])
->add('adherent', null, ['label' => 'Adhérent'])
->add('amount', null, ['label' => 'Montant'])
->add('paiementDate', null, ['label' => 'Date de prélèvement'])
->add('isDonation', null, ['label' => 'Donation'])
->add('isRecurrent', null, ['label' => 'Récurrent'])
->add('enabled', null, ['label' => 'Activé ?'])
->add('comments', null, ['label' => 'Commentaires'])
->add('allPrelevement', null, [
'label' => 'Prélèvements',
'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/show_solidoume_prelevements.html.twig',
])
;
}
/**
* {@inheritdoc}
*/
protected function configureSideMenu(ItemInterface $menu, string $action, AdminInterface $childAdmin = null) protected function configureSideMenu(ItemInterface $menu, string $action, AdminInterface $childAdmin = null)
{ {
if (!$childAdmin && !in_array($action, ['list'])) { if (!$childAdmin && !in_array($action, ['list'])) {
...@@ -161,6 +195,9 @@ class SolidoumeAdmin extends AbstractAdmin ...@@ -161,6 +195,9 @@ class SolidoumeAdmin extends AbstractAdmin
])->setAttribute('icon', 'fa fa-cogs'); ])->setAttribute('icon', 'fa fa-cogs');
} }
/**
* {@inheritdoc}
*/
public function getBatchActions() public function getBatchActions()
{ {
$actions = parent::getBatchActions(); $actions = parent::getBatchActions();
......
...@@ -72,11 +72,12 @@ class SolidoumeCommand extends Command ...@@ -72,11 +72,12 @@ class SolidoumeCommand extends Command
} }
/** /**
* Execute program Solidoume * Execute program Solidoume.
* *
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return integer *
* @return int
*/ */
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
...@@ -105,7 +106,7 @@ class SolidoumeCommand extends Command ...@@ -105,7 +106,7 @@ class SolidoumeCommand extends Command
} }
/** /**
* Execute email reminder : If balance of account if not enough x days before date of paiement => send email * Execute email reminder : If balance of account if not enough x days before date of paiement => send email.
*/ */
private function executeReminders() private function executeReminders()
{ {
...@@ -136,7 +137,7 @@ class SolidoumeCommand extends Command ...@@ -136,7 +137,7 @@ class SolidoumeCommand extends Command
} }
/** /**
* Send email reminder * Send email reminder.
* *
* @param SolidoumeItem $solidoumeItem * @param SolidoumeItem $solidoumeItem
*/ */
...@@ -165,7 +166,7 @@ class SolidoumeCommand extends Command ...@@ -165,7 +166,7 @@ class SolidoumeCommand extends Command
} }
/** /**
* Take money of participant of solidoume if date of today is same as paiementDate * Take money of participant of solidoume if date of today is same as paiementDate.
*/ */
private function executeTaking() private function executeTaking()
{ {
...@@ -191,7 +192,7 @@ class SolidoumeCommand extends Command ...@@ -191,7 +192,7 @@ class SolidoumeCommand extends Command
&& $item->getPaiementDate() == $nowDay && $item->getPaiementDate() == $nowDay
&& $item->getLastMonthPayed() != $nowMonth) { && $item->getLastMonthPayed() != $nowMonth) {
if ($accountEmlc->getBalance() < $amount) { if ($accountEmlc->getBalance() < $amount) {
$io->error($item->getAdherent()->__toString() . ' : Solde de l\'adherent inférieur au montant à prélevé (Solde : ' . $accountEmlc->getBalance() . ' eMLC) !'); $io->error($item->getAdherent()->__toString() . ' : Solde de l\'adherent inférieur au montant à prélever (Solde : ' . $accountEmlc->getBalance() . ' eMLC) !');
$item->setEnabled(false); $item->setEnabled(false);
if (!$isTest) { if (!$isTest) {
$em->persist($item); $em->persist($item);
...@@ -230,7 +231,7 @@ class SolidoumeCommand extends Command ...@@ -230,7 +231,7 @@ class SolidoumeCommand extends Command
} }
/** /**
* Execute solidoume repartition of money recolted if date of today is same as executionDate * Execute solidoume repartition of money recolted if date of today is same as executionDate.
*/ */
private function executeProgram() private function executeProgram()
{ {
...@@ -313,10 +314,11 @@ class SolidoumeCommand extends Command ...@@ -313,10 +314,11 @@ class SolidoumeCommand extends Command
} }
/** /**
* Has to execute reminder by email ? Check date of paiement choose by adherent and number of days before remind * Has to execute reminder by email ? Check date of paiement choose by adherent and number of days before remind.
* *
* @param SolidoumeItem $item * @param SolidoumeItem $item
* @return boolean *
* @return bool
*/ */
private function hasToExecuteReminders(SolidoumeItem $item) private function hasToExecuteReminders(SolidoumeItem $item)
{ {
...@@ -343,7 +345,8 @@ class SolidoumeCommand extends Command ...@@ -343,7 +345,8 @@ class SolidoumeCommand extends Command
} }
/** /**
* Get items for local testing purpose * Get items for local testing purpose.
*
* @return array * @return array
*/ */
private function getItemsForTest() private function getItemsForTest()
...@@ -363,11 +366,13 @@ class SolidoumeCommand extends Command ...@@ -363,11 +366,13 @@ class SolidoumeCommand extends Command
} }
/** /**
* Get one SolidouItem for local testing purpose * Get one SolidouItem for local testing purpose.
*
* @param int $amount * @param int $amount
* @param bool $donation * @param bool $donation
* @param bool $recurrent * @param bool $recurrent
* @param Adherent $adherent * @param Adherent $adherent
*
* @return SolidoumeItem * @return SolidoumeItem
*/ */
private function getItemForTest(int $amount, bool $donation, bool $recurrent, Adherent $adherent) private function getItemForTest(int $amount, bool $donation, bool $recurrent, Adherent $adherent)
......
...@@ -3,9 +3,14 @@ ...@@ -3,9 +3,14 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\GlobalParameter; use App\Entity\GlobalParameter;
use App\Entity\Prestataire;
use App\Entity\SolidoumeItem; use App\Entity\SolidoumeItem;
use App\Entity\SolidoumeParameter; use App\Entity\SolidoumeParameter;
use App\Entity\TransactionAdherentPrestataire;
use App\Enum\CurrencyEnum;
use App\Enum\MoyenEnum;
use App\Form\Type\SolidoumeFormType; use App\Form\Type\SolidoumeFormType;
use App\Utils\OperationUtils;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -20,13 +25,15 @@ class SolidoumeController extends AbstractController ...@@ -20,13 +25,15 @@ class SolidoumeController extends AbstractController
private $security; private $security;
private $mailer; private $mailer;
private $templating; private $templating;
private $operationUtils;
public function __construct(EntityManagerInterface $em, Security $security, \Swift_Mailer $mailer, Environment $templating) public function __construct(EntityManagerInterface $em, Security $security, \Swift_Mailer $mailer, Environment $templating, OperationUtils $operationUtils)
{ {
$this->em = $em; $this->em = $em;
$this->security = $security; $this->security = $security;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->templating = $templating; $this->templating = $templating;
$this->operationUtils = $operationUtils;
} }
/** /**
...@@ -94,16 +101,27 @@ class SolidoumeController extends AbstractController ...@@ -94,16 +101,27 @@ class SolidoumeController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$solidoumeItem = $form->getData(); $solidoumeItem = $form->getData();
$this->em->persist($solidoumeItem); if (!$solidoumeItem->getIsRecurrent()) {
$this->em->flush(); if (!$this->takeMoneyFromSolidoume($solidoumeItem)) {
// if (!$alreadyExist) { $referer = $request->headers->get('referer');
$this->sendMail($solidoumeItem);
// }
return $this->redirect($referer);
} else {
$this->addFlash( $this->addFlash(
'success', 'success',
$solidoumeParam->getName() . ' : Merci de votre participation !' $solidoumeParam->getName() . ' : Merci de votre participation, celle-ci vient d\'être prélevée !'
); );
}
} else {
$this->addFlash(
'success',
$solidoumeParam->getName() . ' : Merci de votre participation, elle sera prélevée le ' . $solidoumeItem->getPaiementDate() . ' !'
);
}
$this->em->persist($solidoumeItem);
$this->em->flush();
$this->sendMail($solidoumeItem);
$referer = $request->request->get('referer'); $referer = $request->request->get('referer');
if ($referer && !$request->isXmlHttpRequest()) { if ($referer && !$request->isXmlHttpRequest()) {
return $this->redirect($referer); return $this->redirect($referer);
...@@ -118,6 +136,45 @@ class SolidoumeController extends AbstractController ...@@ -118,6 +136,45 @@ class SolidoumeController extends AbstractController
]); ]);
} }
private function takeMoneyFromSolidoume(SolidoumeItem $item)
{
$solidoumeParam = $this->em->getRepository(SolidoumeParameter::class)->findTheOne();
$nowDay = (new \DateTime('now', new \DateTimeZone('UTC')))->format('d');
$nowMonth = intval((new \DateTime('now', new \DateTimeZone('UTC')))->format('Ym'));
$item->setPaiementDate($nowDay);
$amount = $item->getAmount();
$accountEmlc = $item->getAdherent()->getAccountWithCurrency(CurrencyEnum::CURRENCY_EMLC);
if (null != $accountEmlc
&& $item->getPaiementDate() == $nowDay
&& $item->getLastMonthPayed() != $nowMonth) {
if ($accountEmlc->getBalance() < $amount) {
$this->addFlash(
'error',
$solidoumeParam->getName() . ' : Solde inférieur au montant à prélever ! (Solde : ' . $accountEmlc->getBalance() . ' eMLC) !'
);
return false;
} else {
$flux = new TransactionAdherentPrestataire();
$flux->setExpediteur($item->getAdherent());
$flux->setDestinataire($this->em->getRepository(Prestataire::class)->getPrestataireSolidoume());
$flux->setMontant($amount);
$flux->setMoyen(MoyenEnum::MOYEN_EMLC);
$now = (new \Datetime('now'))->format('d/m/Y H:i:s');
$flux->setReference($this->em->getRepository(SolidoumeParameter::class)->getValueOf('name') . ' prélèvement en date du ' . $now);
$item->setLastMonthPayed($nowMonth);
$item->setEnabled(false);
$this->em->persist($flux);
$this->em->persist($item);
// Write operations for this flux !
$this->operationUtils->executeOperations($flux);
$this->em->flush();
}
}
return true;
}
private function sendMail(SolidoumeItem $solidoumeItem) private function sendMail(SolidoumeItem $solidoumeItem)
{ {
$subject = $this->em->getRepository(SolidoumeParameter::class)->getValueOf('name'); $subject = $this->em->getRepository(SolidoumeParameter::class)->getValueOf('name');
......
...@@ -81,6 +81,25 @@ class FluxRepository extends ServiceEntityRepository ...@@ -81,6 +81,25 @@ class FluxRepository extends ServiceEntityRepository
; ;
} }
public function getQueryByAdherentAndDestinataire(Adherent $adherent, Prestataire $prestataire)
{
$sqlQuery = "SELECT f.id FROM {$this->tableName} f WHERE (f.adherent_id = :id OR f.prestataire_id = :presta_id)";
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':id', $adherent->getId());
$statement->bindValue(':presta_id', $prestataire->getId());
$statement->execute();
$results = $statement->fetchAll();
$qb = $this->createQueryBuilder('f');
return $qb
->where($qb->expr()->in('f.id', ':ids'))
->setParameter('ids', $results)
->orderBy('f.createdAt', 'DESC')
->getQuery()
->getResult()
;
}
/** /**
* @param Adherent $adherent [description] * @param Adherent $adherent [description]
* @param string $parenttype Parent type of flux (cotisation, transfert, transaction, vente...) * @param string $parenttype Parent type of flux (cotisation, transfert, transaction, vente...)
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace App\Twig; namespace App\Twig;
use App\Entity\Adherent;
use App\Entity\Flux;
use App\Entity\Prestataire;
use App\Entity\SolidoumeItem; use App\Entity\SolidoumeItem;
use App\Entity\SolidoumeParameter; use App\Entity\SolidoumeParameter;
use App\Form\Type\SolidoumeFormType; use App\Form\Type\SolidoumeFormType;
...@@ -39,6 +42,7 @@ class SolidoumeExtension extends AbstractExtension ...@@ -39,6 +42,7 @@ class SolidoumeExtension extends AbstractExtension
new \Twig_SimpleFunction('getSolidoumeParam', [$this, 'getSolidoumeParam']), new \Twig_SimpleFunction('getSolidoumeParam', [$this, 'getSolidoumeParam']),
new \Twig_SimpleFunction('getSolidoumeForm', [$this, 'getSolidoumeForm']), new \Twig_SimpleFunction('getSolidoumeForm', [$this, 'getSolidoumeForm']),
new \Twig_SimpleFunction('getSolidoumeParticipation', [$this, 'getSolidoumeParticipation']), new \Twig_SimpleFunction('getSolidoumeParticipation', [$this, 'getSolidoumeParticipation']),
new \Twig_SimpleFunction('getSolidoumePrelevements', [$this, 'getSolidoumePrelevements']),
new \Twig_SimpleFunction('getSolidoumeDatas', [$this, 'getSolidoumeDatas']), new \Twig_SimpleFunction('getSolidoumeDatas', [$this, 'getSolidoumeDatas']),
]; ];
} }
...@@ -69,6 +73,13 @@ class SolidoumeExtension extends AbstractExtension ...@@ -69,6 +73,13 @@ class SolidoumeExtension extends AbstractExtension
return $data; return $data;
} }
public function getSolidoumePrelevements(Adherent $adherent)
{
$datas = $this->em->getRepository(Flux::class)->getQueryByAdherentAndDestinataire($adherent, $this->em->getRepository(Prestataire::class)->getPrestataireSolidoume());
return $datas;
}
public function getSolidoumeDatas() public function getSolidoumeDatas()
{ {
$param = $this->em->getRepository(SolidoumeParameter::class)->findTheOne(); $param = $this->em->getRepository(SolidoumeParameter::class)->findTheOne();
...@@ -96,7 +107,7 @@ class SolidoumeExtension extends AbstractExtension ...@@ -96,7 +107,7 @@ class SolidoumeExtension extends AbstractExtension
'countDonateurs' => ($countPerson - $countParticipants), 'countDonateurs' => ($countPerson - $countParticipants),
'countParticipants' => $countParticipants, 'countParticipants' => $countParticipants,
'totalByParticipant' => ($total / $countParticipants), 'totalByParticipant' => ($total / $countParticipants),
'totalByParticipantWithCommission' => (($total / $countParticipants) * ((100 - $param->getCommission()) / 100)), 'totalByParticipantWithCommission' => round((($total / $countParticipants) * ((100 - $param->getCommission()) / 100)), 2, PHP_ROUND_HALF_DOWN),
'commission' => $param->getCommission(), 'commission' => $param->getCommission(),
]; ];
} }
......
{% extends admin.getTemplate('base_list_field') %}
{% block field %}
{%- spaceless %}
{% if object.lastMonthPayed is not null %}
{{ object.paiementDate }}/{{ object.lastMonthPayed|slice(4, 2) }}/{{ object.lastMonthPayed|slice(0, 4) }}
{% endif %}
{% endspaceless -%}
{% endblock %}
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}
{% block field %}
{%- spaceless %}
{% set datas = getSolidoumePrelevements(object.adherent) %}
{% for data in datas %}
{{ data.montant}}€ ({{ data.createdAt|date('d/m/Y H:i') }})<br/>
{% endfor %}
{% endspaceless -%}
{% endblock %}
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