CustomDoctrineORMQuerySourceIterator.php 4.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?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']);
62 63 64 65 66 67
            $gestionnaires = [];
            $users = $presta->getUsers();
            foreach ($users as $user) {
                $gestionnaires[] = $user->getLastname() . ' ' . $user->getFirstname() . ' (' . $user->getEmail() . ')';
            }
            $data['Gestionnaires'] = implode(' - ', $gestionnaires);
68
            $cotisEnd = $this->cotisationUtils->isCotisationValidForPresta($presta);
69
            $data['Cotisation à jour'] = false == $cotisEnd ? '' : ($cotisEnd->format('d/m/Y'));
70
            $firstCotis = $this->cotisationUtils->getFirstCotisationForPresta($presta);
71 72
            $data['Première Cotisation'] = false == $firstCotis ? '' : ($firstCotis->format('d/m/Y'));
        } elseif (!empty($data['Id']) && !empty($data['Email'])) {
73 74 75
            //adherent
            $adherent = $this->em->getRepository(Adherent::class)->findOneById($data['Id']);
            $cotisEnd = $this->cotisationUtils->isCotisationValidForAdherent($adherent);
76
            $data['Cotisation à jour'] = false == $cotisEnd ? '' : ($cotisEnd->format('d/m/Y'));
77
            $firstCotis = $this->cotisationUtils->getFirstCotisationForAdherent($adherent);
78
            $data['Première Cotisation'] = false == $firstCotis ? '' : ($firstCotis->format('d/m/Y'));
79 80 81 82 83 84 85
            $adminRoles = '';
            if (!empty($adherent->getUser())) {
                foreach ($adherent->getUser()->getPossiblegroups() as $group) {
                    if (!empty($adminRoles)) {
                        $adminRoles .= ', ';
                    }
                    $adminRoles .= $group->getName();
86
                    if ('Comptoir' == $group->getName()) {
87
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getComptoirsgeres()->toArray()) . ')';
88
                    } elseif ('Prestataire' == $group->getName()) {
89
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getPrestataires()->toArray()) . ')';
90
                    } elseif ('Caissier' == $group->getName()) {
91
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getCaissiers()->toArray()) . ')';
92
                    } elseif ('Gestionnaire de Groupe' == $group->getName() || 'Contact' == $group->getName()) {
93 94 95 96 97
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getGroupesgeres()->toArray()) . ')';
                    }
                }
            }
            $data["Droits d'administration"] = $adminRoles;
98 99 100 101 102 103 104 105 106 107 108 109 110
        }

        $this->query->getEntityManager()->clear();

        return $data;
    }

    final public function rewind(): void
    {
        $this->iterator = $this->query->iterate();
        $this->iterator->rewind();
    }
}