Commit 91314e67 by Julien Jorry

Presta / Adherent Admin : add cotisation up to date and first cotisation in export

......@@ -112,6 +112,7 @@ services:
app.utils.cotisations:
class: App\Utils\CotisationUtils
autowire: false
public: true
arguments: ['@app.utils.custom_entity_manager', '@session', '@security.helper', '@event_dispatcher']
app.utils.account:
......
......@@ -11,17 +11,21 @@ use App\Entity\User;
use App\Entity\Usergroup;
use App\Enum\CurrencyEnum;
use App\Events\MLCEvents;
use App\Exporter\CustomDoctrineORMQuerySourceIterator;
use App\Form\Type\GeolocFormType;
use App\Form\Type\UserFormType;
use Doctrine\ORM\Query;
use FOS\UserBundle\Event\UserEvent;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\DoctrineORMAdminBundle\Datagrid\OrderByToSelectWalker;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\Form\Type\DateTimeRangePickerType;
use Sonata\UserBundle\Model\UserManagerInterface;
......@@ -415,7 +419,62 @@ class AdherentAdmin extends AbstractAdmin
'Groupe' => 'groupe.name',
'Téléphone' => 'user.phone',
'Mobile' => 'user.mobile',
'E-compte' => 'ecompte',
'Crée le' => 'createdAt',
'Mise à jour le' => 'updatedAt',
];
}
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$fields = [];
foreach ($this->getExportFields() as $key => $field) {
// NEXT_MAJOR: Remove the following code in favor of the commented one.
$label = $this->getTranslationLabel($field, 'export', 'label');
$transLabel = $this->getTranslator()->trans($label, [], $this->getTranslationDomain());
if ($transLabel === $label) {
$fields[$key] = $field;
} else {
$fields[$transLabel] = $field;
}
}
return $this->getModelDataSourceIterator($datagrid, $fields);
}
/**
* @return DoctrineORMQuerySourceIterator
*/
public function getModelDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null)
{
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('DISTINCT '.current($query->getRootAliases()));
$query->setFirstResult($firstResult);
$query->setMaxResults($maxResult);
if ($query instanceof ProxyQueryInterface) {
$sortBy = $query->getSortBy();
if (!empty($sortBy)) {
$query->addOrderBy($sortBy, $query->getSortOrder());
$query = $query->getQuery();
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [OrderByToSelectWalker::class]);
} else {
$query = $query->getQuery();
}
}
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
$cotisationUtils = $this->getConfigurationPool()->getContainer()->get('app.utils.cotisations');
$iterator = new CustomDoctrineORMQuerySourceIterator($cotisationUtils, $em, $query, $fields);
$iterator->setDateTimeFormat('d/m/Y H:i:s'); //change this to suit your needs
return $iterator;
}
}
......@@ -17,22 +17,27 @@ use App\Entity\Usergroup;
use App\Enum\CurrencyEnum;
use App\Events\MLCEvents;
use App\Events\PrestataireEvent;
use App\Exporter\CustomDoctrineORMQuerySourceIterator;
use App\Form\Type\ContactEntityFormType;
use App\Form\Type\GeolocPrestataireFormType;
use App\Form\Type\UserFormType;
use Doctrine\ORM\Query;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\Model\UserManagerInterface;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Object\Metadata;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\Form\Type\DateTimeRangePickerType;
use Sonata\DoctrineORMAdminBundle\Datagrid\OrderByToSelectWalker;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\Form\Type\DateTimeRangePickerType;
use Sonata\MediaBundle\Form\Type\MediaType;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
......@@ -800,12 +805,68 @@ class PrestataireAdmin extends AbstractAdmin
{
return [
'Id' => 'id',
'Activé ?' => 'enabled',
'Raison' => 'raison',
'Groupe' => 'groupe.name',
'E-compte' => 'ecompte',
'Groupe local' => 'groupe.name',
'Rubriques' => 'rubriquesString',
'Gestionnaires' => 'usersString',
'Tags' => 'etatsString',
'Crée le' => 'createdAt',
'Mise à jour le' => 'updatedAt',
];
}
public function getDataSourceIterator()
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$fields = [];
foreach ($this->getExportFields() as $key => $field) {
// NEXT_MAJOR: Remove the following code in favor of the commented one.
$label = $this->getTranslationLabel($field, 'export', 'label');
$transLabel = $this->getTranslator()->trans($label, [], $this->getTranslationDomain());
if ($transLabel === $label) {
$fields[$key] = $field;
} else {
$fields[$transLabel] = $field;
}
}
return $this->getModelDataSourceIterator($datagrid, $fields);
}
/**
* @return DoctrineORMQuerySourceIterator
*/
public function getModelDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null)
{
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('DISTINCT '.current($query->getRootAliases()));
$query->setFirstResult($firstResult);
$query->setMaxResults($maxResult);
if ($query instanceof ProxyQueryInterface) {
$sortBy = $query->getSortBy();
if (!empty($sortBy)) {
$query->addOrderBy($sortBy, $query->getSortOrder());
$query = $query->getQuery();
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [OrderByToSelectWalker::class]);
} else {
$query = $query->getQuery();
}
}
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
$cotisationUtils = $this->getConfigurationPool()->getContainer()->get('app.utils.cotisations');
$iterator = new CustomDoctrineORMQuerySourceIterator($cotisationUtils, $em, $query, $fields);
$iterator->setDateTimeFormat('d/m/Y H:i:s'); //change this to suit your needs
return $iterator;
}
}
......@@ -670,7 +670,7 @@ class Prestataire extends AccountableObject implements AccountableInterface
public function getUsersString()
{
return join(' - ', array_map(function ($user) {
return $user->getName() . ':' . $user->getEmail();
return $user->getName() . ($user->getEmail() == $user->getName()?'':(':' . $user->getEmail()));
}, $this->users->getValues()));
}
......
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Exporter;
use App\Entity\Adherent;
use App\Entity\Prestataire;
use App\Utils\CotisationUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Sonata\Exporter\Source\AbstractPropertySourceIterator;
use Sonata\Exporter\Source\SourceIteratorInterface;
/**
* @final since sonata-project/exporter 2.4.
*/
class CustomDoctrineORMQuerySourceIterator extends AbstractPropertySourceIterator implements SourceIteratorInterface
{
/**
* @var Query
*/
protected $query;
protected $em;
protected $cotisationUtils;
/**
* @param array<string> $fields Fields to export
*/
public function __construct(CotisationUtils $cotisationUtils, EntityManagerInterface $em, Query $query, array $fields, string $dateTimeFormat = 'r')
{
$this->em = $em;
$this->cotisationUtils = $cotisationUtils;
$this->query = clone $query;
$this->query->setParameters($query->getParameters());
foreach ($query->getHints() as $name => $value) {
$this->query->setHint($name, $value);
}
parent::__construct($fields, $dateTimeFormat);
}
public function current()
{
$current = $this->iterator->current();
$data = $this->getCurrentData($current[0]);
// Add for kohinos
if (!empty($data['Id']) && !empty($data['Raison'])) {
//presta
$presta = $this->em->getRepository(Prestataire::class)->findOneById($data['Id']);
$cotisEnd = $this->cotisationUtils->isCotisationValidForPresta($presta);
$data['Cotisation à jour'] = $cotisEnd == false ? '' : ($cotisEnd->format('d/m/Y'));
$firstCotis = $this->cotisationUtils->getFirstCotisationForPresta($presta);
$data['Première Cotisation'] = $firstCotis == false ? '' : ($firstCotis->format('d/m/Y'));
} else if (!empty($data['Id']) && !empty($data['Email'])) {
//adherent
$adherent = $this->em->getRepository(Adherent::class)->findOneById($data['Id']);
$cotisEnd = $this->cotisationUtils->isCotisationValidForAdherent($adherent);
$data['Cotisation à jour'] = $cotisEnd == false ? '' : ($cotisEnd->format('d/m/Y'));
$firstCotis = $this->cotisationUtils->getFirstCotisationForAdherent($adherent);
$data['Première Cotisation'] = $firstCotis == false ? '' : ($firstCotis->format('d/m/Y'));
}
$this->query->getEntityManager()->clear();
return $data;
}
final public function rewind(): void
{
$this->iterator = $this->query->iterate();
$this->iterator->rewind();
}
}
......@@ -64,6 +64,28 @@ class CotisationUtils
}
/**
* Get first Cotisation For Presta ?
* @param Prestataire $presta
* @return boolean|date
*/
public function getFirstCotisationForPresta(?Prestataire $presta)
{
$query = null;
if (null !== $presta) {
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($presta, 'cotisation');
}
if (null !== $query) {
$cotisations = $query->getResult();
if (count($cotisations) > 0) {
$firstCotis = $cotisations[0];
return $firstCotis->getCotisationInfos()->getDebut();
}
}
return false;
}
/**
* Is the Cotisation Valid For Presta ?
* @param Prestataire $presta
* @return boolean|date
......@@ -87,6 +109,28 @@ class CotisationUtils
}
/**
* Get first Cotisation For Adherent
* @param Adherent $adherent
* @return boolean|date
*/
public function getFirstCotisationForAdherent(?Adherent $adherent)
{
$query = null;
if (null !== $adherent) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($adherent, 'cotisation');
}
if (null !== $query) {
$cotisations = $query->getResult();
if (count($cotisations) > 0) {
$firstCotis = $cotisations[0];
return $firstCotis->getCotisationInfos()->getDebut();
}
}
return false;
}
/**
* Is the Cotisation Valid For Adherent
* @param Adherent $adherent
* @return boolean|date
......
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