CustomDoctrineORMQuerySourceIterator.php 6.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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;
18 19
use App\Entity\Flux;
use App\Entity\GlobalParameter;
20 21 22 23 24
use App\Utils\CotisationUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Sonata\Exporter\Source\AbstractPropertySourceIterator;
use Sonata\Exporter\Source\SourceIteratorInterface;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26 27 28 29 30 31 32 33 34 35 36 37

/**
 * @final since sonata-project/exporter 2.4.
 */
class CustomDoctrineORMQuerySourceIterator extends AbstractPropertySourceIterator implements SourceIteratorInterface
{
    /**
     * @var Query
     */
    protected $query;
    protected $em;
    protected $cotisationUtils;
38
    protected $container;
39 40 41 42

    /**
     * @param array<string> $fields Fields to export
     */
43 44 45 46 47 48 49 50
    public function __construct(
        CotisationUtils $cotisationUtils,
        EntityManagerInterface $em,
        ContainerInterface $container,
        Query $query,
        array $fields,
        string $dateTimeFormat = 'r'
    ) {
51 52
        $this->em = $em;
        $this->cotisationUtils = $cotisationUtils;
53
        $this->container = $container;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        $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']);
73 74 75 76 77 78
            $gestionnaires = [];
            $users = $presta->getUsers();
            foreach ($users as $user) {
                $gestionnaires[] = $user->getLastname() . ' ' . $user->getFirstname() . ' (' . $user->getEmail() . ')';
            }
            $data['Gestionnaires'] = implode(' - ', $gestionnaires);
79
            $cotisEnd = $this->cotisationUtils->isCotisationValidForPresta($presta);
80
            $cotisEnd = is_string($cotisEnd) ? new \DateTime( $cotisEnd ) : $cotisEnd;
81
            $data['Cotisation à jour'] = false == $cotisEnd ? '' : ($cotisEnd->format('d/m/Y'));
82
            $firstCotis = $this->cotisationUtils->getFirstCotisationForPresta($presta);
83
            $firstCotis = is_string($firstCotis) ? new \DateTime( $firstCotis ) : $firstCotis;
84 85
            $data['Première Cotisation'] = false == $firstCotis ? '' : ($firstCotis->format('d/m/Y'));
        } elseif (!empty($data['Id']) && !empty($data['Email'])) {
86 87
            //adherent
            $adherent = $this->em->getRepository(Adherent::class)->findOneById($data['Id']);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

            if ($this->container->getParameter('tav_env')) {
                // Last TAV cotisation
                $cotisations = $this->em->getRepository(Flux::class)->getLastTavCotisation($adherent);
                if (count($cotisations) > 0) {
                    $cotisDate = date("d/m/Y H:i:s", strtotime($cotisations[0]["created_at"]));
                    $data['Dernière Cotisation'] = $cotisDate;
                } else {
                    $data['Dernière Cotisation'] = "Aucune";
                }

                // Current emlc balance
                $balance = $adherent->getEmlcAccount()->getBalance();
                $mlc = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_SYMBOL);
                $data['Solde'] = $balance . " " . $mlc;

                // Profil de cotisation
                $cotisationProfile = $adherent->getProfilDeCotisation();
                if (!is_null($cotisationProfile) ) {
                    $data['Profil de cotisation'] = $cotisationProfile;
                } else {
                    $data['Profil de cotisation'] = "Aucun profil assigné";
                }
111 112 113
                if ($this->container->getParameter('ccas_mode')) {
                    $data['Numéro d\'anonymisation'] = $adherent->getAnonymousToken();
                }
114 115 116 117 118 119 120 121
            } else {
                $cotisEnd = $this->cotisationUtils->isCotisationValidForAdherent($adherent);
                $cotisEnd = is_string($cotisEnd) ? new \DateTime( $cotisEnd ) : $cotisEnd;
                $data['Cotisation à jour'] = false == $cotisEnd ? '' : ($cotisEnd->format('d/m/Y'));
                $firstCotis = $this->cotisationUtils->getFirstCotisationForAdherent($adherent);
                $firstCotis = is_string($firstCotis) ? new \DateTime( $firstCotis ) : $firstCotis;
                $data['Première Cotisation'] = false == $firstCotis ? '' : ($firstCotis->format('d/m/Y'));
            }
122 123 124 125 126 127 128
            $adminRoles = '';
            if (!empty($adherent->getUser())) {
                foreach ($adherent->getUser()->getPossiblegroups() as $group) {
                    if (!empty($adminRoles)) {
                        $adminRoles .= ', ';
                    }
                    $adminRoles .= $group->getName();
129
                    if ('Comptoir' == $group->getName()) {
130
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getComptoirsgeres()->toArray()) . ')';
131
                    } elseif ('Prestataire' == $group->getName()) {
132
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getPrestataires()->toArray()) . ')';
133
                    } elseif ('Caissier' == $group->getName()) {
134
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getCaissiers()->toArray()) . ')';
135
                    } elseif ('Gestionnaire de Groupe' == $group->getName() || 'Contact' == $group->getName()) {
136 137 138 139 140
                        $adminRoles .= '(' . implode(',', $adherent->getUser()->getGroupesgeres()->toArray()) . ')';
                    }
                }
            }
            $data["Droits d'administration"] = $adminRoles;
141 142 143 144 145 146 147 148 149 150 151 152 153
        }

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

        return $data;
    }

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