Commit ac119167 by Julien Jorry

Fix import

parent 4ee144e4
...@@ -70,6 +70,7 @@ class ImportAdmin extends AbstractAdmin ...@@ -70,6 +70,7 @@ class ImportAdmin extends AbstractAdmin
->add('nbentityadded', null, ['label' => 'Lignes correctes']) ->add('nbentityadded', null, ['label' => 'Lignes correctes'])
->add('nbentityerror', null, ['label' => 'Lignes en erreurs']) ->add('nbentityerror', null, ['label' => 'Lignes en erreurs'])
->add('media', null, ['label' => 'Fichier']) ->add('media', null, ['label' => 'Fichier'])
->add('test', null, ['label' => 'Test ?'])
->add('_action', null, [ ->add('_action', null, [
'actions' => [ 'actions' => [
'show' => [], 'show' => [],
...@@ -84,6 +85,7 @@ class ImportAdmin extends AbstractAdmin ...@@ -84,6 +85,7 @@ class ImportAdmin extends AbstractAdmin
$showMapper $showMapper
->with('Import') ->with('Import')
->add('createdAt') ->add('createdAt')
->add('test')
->add('user') ->add('user')
->add('type') ->add('type')
->add('media') ->add('media')
......
...@@ -91,17 +91,15 @@ class ImportController extends CRUDController ...@@ -91,17 +91,15 @@ class ImportController extends CRUDController
$media = $import->getMedia(); $media = $import->getMedia();
$type = $import->getType(); $type = $import->getType();
$this->sendemail = (bool) $import->getSendemail(); $this->sendemail = (bool) $import->getSendemail();
$this->test = (bool) $import->getTest();
if (!$this->test) {
// Sauvegarder l'import en base de données avant l'essai d'import // Sauvegarder l'import en base de données avant l'essai d'import
$this->em->persist($import); $this->em->persist($import);
$this->em->flush(); $this->em->flush();
$idimport = $import->getId(); $idimport = $import->getId();
}
$this->importFromCSV($type, $media); $this->importFromCSV($type, $media);
if (!$this->test) {
$import = $this->em->getRepository(Import::class)->findOneById($idimport); $import = $this->em->getRepository(Import::class)->findOneById($idimport);
$import->setEnabled(true); $import->setEnabled(true);
...@@ -112,7 +110,6 @@ class ImportController extends CRUDController ...@@ -112,7 +110,6 @@ class ImportController extends CRUDController
$import->setNbentityerror($this->nberrors); $import->setNbentityerror($this->nberrors);
$this->em->persist($import); $this->em->persist($import);
$this->em->flush(); $this->em->flush();
}
if (empty($this->errors)) { if (empty($this->errors)) {
if ($this->test) { if ($this->test) {
...@@ -321,7 +318,7 @@ class ImportController extends CRUDController ...@@ -321,7 +318,7 @@ class ImportController extends CRUDController
// Importer les gestionnaires de comptoir s'ils existent // Importer les gestionnaires de comptoir s'ils existent
$groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Comptoir'); $groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Comptoir');
$gestionnaires = $this->importGestionnaires($row, $groupeGestionnaire); $gestionnaires = $this->importGestionnaires($row, $line, $groupeGestionnaire);
$comptoir->setGestionnaires($gestionnaires); $comptoir->setGestionnaires($gestionnaires);
$this->addSuccess($row, $line, 'comptoir', $this->translator->trans('Comptoir ajouté : ') . $nom); $this->addSuccess($row, $line, 'comptoir', $this->translator->trans('Comptoir ajouté : ') . $nom);
...@@ -390,7 +387,7 @@ class ImportController extends CRUDController ...@@ -390,7 +387,7 @@ class ImportController extends CRUDController
} }
// Importer les gestionnaires du groupe s'ils existent // Importer les gestionnaires du groupe s'ils existent
$groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Gestionnaire de Groupe'); $groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Gestionnaire de Groupe');
$gestionnaires = $this->importGestionnaires($row, $groupeGestionnaire); $gestionnaires = $this->importGestionnaires($row, $line, $groupeGestionnaire);
$groupe->setGestionnaires($gestionnaires); $groupe->setGestionnaires($gestionnaires);
$this->addSuccess($row, $line, 'groupe', $this->translator->trans('Groupe ajouté : ') . $name); $this->addSuccess($row, $line, 'groupe', $this->translator->trans('Groupe ajouté : ') . $name);
...@@ -561,7 +558,7 @@ class ImportController extends CRUDController ...@@ -561,7 +558,7 @@ class ImportController extends CRUDController
} }
// Importer les gestionnaires du prestataire s'ils existent // Importer les gestionnaires du prestataire s'ils existent
$groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Prestataire'); $groupeGestionnaire = $this->em->getRepository(Usergroup::class)->findOneByName('Prestataire');
$gestionnaires = $this->importGestionnaires($row, $groupeGestionnaire); $gestionnaires = $this->importGestionnaires($row, $line, $groupeGestionnaire);
$prestataire->setUsers($gestionnaires); $prestataire->setUsers($gestionnaires);
if (!empty($groupe)) { if (!empty($groupe)) {
...@@ -705,7 +702,7 @@ class ImportController extends CRUDController ...@@ -705,7 +702,7 @@ class ImportController extends CRUDController
$ville = array_key_exists('ville', $row) ? $row['ville'] : ''; $ville = array_key_exists('ville', $row) ? $row['ville'] : '';
$cotisations = array_key_exists('cotisations', $row) ? $row['cotisations'] : ''; $cotisations = array_key_exists('cotisations', $row) ? $row['cotisations'] : '';
if (!(array_key_exists('email', $row))) { if (empty($email)) {
$this->addError($row, $line, 'email', $this->translator->trans("La colonne 'email' est obligatoire !")); $this->addError($row, $line, 'email', $this->translator->trans("La colonne 'email' est obligatoire !"));
++$line; ++$line;
++$this->nberrors; ++$this->nberrors;
...@@ -717,11 +714,17 @@ class ImportController extends CRUDController ...@@ -717,11 +714,17 @@ class ImportController extends CRUDController
$adherent = $this->em->getRepository(Adherent::class)->findOneBy(['idmlc' => $idmlc]); $adherent = $this->em->getRepository(Adherent::class)->findOneBy(['idmlc' => $idmlc]);
} }
if (empty($adherent)) { if (empty($adherent)) {
$adherent = $this->em->getRepository(Adherent::class)->findOneBy(['email' => $email]); $userFound = $this->em->getRepository(User::class)->findOneBy(['username' => $email]);
if (!empty($userFound)) {
$adherent = $userFound->getAdherent();
}
} }
if (empty($adherent)) { if (empty($adherent)) {
$adherent = new Adherent(); $adherent = new Adherent();
$adherent->setIdmlc($idmlc);
$user = $this->userManager->createUser(); $user = $this->userManager->createUser();
$user->setUsername($email);
$user->setEmail($email);
$user->setConfirmationToken($this->tokenGenerator->generateToken()); $user->setConfirmationToken($this->tokenGenerator->generateToken());
$user->setEnabled(true); $user->setEnabled(true);
$user->setPassword(md5(random_bytes(10))); $user->setPassword(md5(random_bytes(10)));
...@@ -808,12 +811,14 @@ class ImportController extends CRUDController ...@@ -808,12 +811,14 @@ class ImportController extends CRUDController
$cotisation->getCotisationInfos()->setFin(DateTime::createFromFormat('Ymd', intval($cotisationDetailsArray[1]) . '1231')); $cotisation->getCotisationInfos()->setFin(DateTime::createFromFormat('Ymd', intval($cotisationDetailsArray[1]) . '1231'));
} }
// $this->em->persist($cotisation); // $this->em->persist($cotisation);
if (!$this->test) {
try { try {
$this->operationUtils->executeOperations($cotisation); $this->operationUtils->executeOperations($cotisation);
$this->addSuccess($row, $line, 'cotisations', $this->translator->trans('Cotisation(s) ajoutée(s) ligne : ') . $line); $this->addSuccess($row, $line, 'cotisations', $this->translator->trans('Cotisation(s) ajoutée(s) ligne : ') . $line);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->addError($row, $line, 'cotisations', $this->translator->trans('Cotisation problème : ') . $e->getMessage()); $this->addError($row, $line, 'cotisations', $this->translator->trans('Cotisation problème : ') . $e->getMessage());
} }
}
$this->addSuccess($row, $line, 'cotisations', $this->translator->trans('Cotisation(s) ajoutée(s) ligne : ') . $line); $this->addSuccess($row, $line, 'cotisations', $this->translator->trans('Cotisation(s) ajoutée(s) ligne : ') . $line);
} }
} }
...@@ -836,6 +841,7 @@ class ImportController extends CRUDController ...@@ -836,6 +841,7 @@ class ImportController extends CRUDController
$this->addSuccess($row, $line, 'user', $this->translator->trans('Adhérent bien ajouté ligne : ') . $line); $this->addSuccess($row, $line, 'user', $this->translator->trans('Adhérent bien ajouté ligne : ') . $line);
++$this->nbsuccess; ++$this->nbsuccess;
$user->setPasswordRequestedAt(new \DateTime()); $user->setPasswordRequestedAt(new \DateTime());
if (!$this->test) {
$this->userManager->updateUser($user); $this->userManager->updateUser($user);
if ($this->sendemail) { if ($this->sendemail) {
$this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_ADHERENT, new UserEvent($user, $this->getRequest())); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_ADHERENT, new UserEvent($user, $this->getRequest()));
...@@ -843,6 +849,7 @@ class ImportController extends CRUDController ...@@ -843,6 +849,7 @@ class ImportController extends CRUDController
$this->em->flush(); $this->em->flush();
$this->em->clear(); $this->em->clear();
} }
}
++$line; ++$line;
} }
ksort($this->errors); ksort($this->errors);
...@@ -1039,11 +1046,8 @@ class ImportController extends CRUDController ...@@ -1039,11 +1046,8 @@ class ImportController extends CRUDController
} }
} elseif ($type == 'adherent') { } elseif ($type == 'adherent') {
if (!empty($idNameOrEmail)) { if (!empty($idNameOrEmail)) {
// dump($idNameOrEmail);
// exit();
$userFound = $this->em->getRepository(User::class)->findOneBy(['username' => $idNameOrEmail]); $userFound = $this->em->getRepository(User::class)->findOneBy(['username' => $idNameOrEmail]);
if (!empty($userFound)) { if (!empty($userFound)) {
// $entity = $this->em->getRepository(Adherent::class)->findOneBy(['user' => $userFound->getId()]);
$entity = $userFound->getAdherent(); $entity = $userFound->getAdherent();
} }
} elseif (!empty($idmlc)) { } elseif (!empty($idmlc)) {
...@@ -1076,7 +1080,7 @@ class ImportController extends CRUDController ...@@ -1076,7 +1080,7 @@ class ImportController extends CRUDController
* *
* @return User Manager created * @return User Manager created
*/ */
private function importGestionnaires($row, Usergroup $groupe) private function importGestionnaires($row, $line, Usergroup $groupe)
{ {
$cptGestionnaire = 1; $cptGestionnaire = 1;
$users = new ArrayCollection(); $users = new ArrayCollection();
...@@ -1122,7 +1126,7 @@ class ImportController extends CRUDController ...@@ -1122,7 +1126,7 @@ class ImportController extends CRUDController
$this->em->flush(); $this->em->flush();
} }
} }
if ($this->sendemail) { if (!$this->test && $this->sendemail) {
$this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_ADHERENT, new UserEvent($user, $this->getRequest())); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_ADHERENT, new UserEvent($user, $this->getRequest()));
} }
++$cptGestionnaire; ++$cptGestionnaire;
......
...@@ -93,6 +93,11 @@ class Import ...@@ -93,6 +93,11 @@ class Import
*/ */
private $sendemail; private $sendemail;
/**
* @var string
*
* @ORM\Column(name="test", type="boolean", nullable=false, options={"default": false})
*/
private $test; private $test;
public function __construct() public function __construct()
......
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210704090118 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE import ADD test TINYINT(1) DEFAULT \'0\' NOT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE import DROP test');
}
}
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<li> <li>
<a class="sonata-action-element" href="{{ admin.generateUrl('list') }}"> <a class="sonata-action-element" href="{{ admin.generateUrl('list') }}">
<i class="fa fa-list" aria-hidden="true"></i> <i class="fa fa-list" aria-hidden="true"></i>
{{ 'link_action_list'|trans({}, 'SonataAdminBundle') }} {{ 'Liste des imports'|trans }}
</a> </a>
</li> </li>
</ul> </ul>
......
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