Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BO - Liste signalement][Back] Implémentation des filtres manquants #2421

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions migrations/Version20240405172355.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240405172355 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add epci table';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE epci (id INT AUTO_INCREMENT NOT NULL, code VARCHAR(255) NOT NULL, nom VARCHAR(255) NOT NULL, UNIQUE INDEX code_unique (code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE commune ADD epci_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE commune ADD CONSTRAINT FK_E2E2D1EE4E7C18CB FOREIGN KEY (epci_id) REFERENCES epci (id)');
$this->addSql('CREATE INDEX IDX_E2E2D1EE4E7C18CB ON commune (epci_id)');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE commune DROP FOREIGN KEY FK_E2E2D1EE4E7C18CB');
$this->addSql('DROP TABLE epci');
$this->addSql('DROP INDEX IDX_E2E2D1EE4E7C18CB ON commune');
$this->addSql('ALTER TABLE commune DROP epci_id');
}
}
113 changes: 113 additions & 0 deletions src/Command/LoadEpciCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace App\Command;

use App\Entity\Epci;
use App\Repository\CommuneRepository;
use App\Repository\EpciRepository;
use App\Repository\TerritoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsCommand(
name: 'app:load-epci',
description: 'Load EPCI from geo.api.gouv.fr',
)]
class LoadEpciCommand extends Command
{
public const API_EPCI_ALL_URL = 'https://geo.api.gouv.fr/epcis?fields=nom';
public const API_EPCI_COMMUNE_URL = 'https://geo.api.gouv.fr/epcis/%d/communes?fields=nom,codesPostaux';
private array $epcis = [];
private array $communes = [];

public function __construct(
private HttpClientInterface $httpClient,
private TerritoryRepository $territoryRepository,
private CommuneRepository $communeRepository,
private EpciRepository $epciRepository,
private EntityManagerInterface $entityManager,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->initDataFromRepository();

$response = $this->httpClient->request('GET', self::API_EPCI_ALL_URL);
if (Response::HTTP_OK !== $response->getStatusCode()) {
$io->error('API failed');

return Command::FAILURE;
}

$epciList = json_decode($response->getContent(), true);
$progressBar = new ProgressBar($output, \count($epciList));
$progressBar->start();
foreach ($epciList as $epciItem) {
$epci = $this->epcis[$epciItem['code']] ?? null;
if (!$epci) {
$epci = (new Epci())
->setNom($epciItem['nom'])
->setCode($epciItem['code']);
}
$response = $this->httpClient->request(
'GET',
$epciCommunesUrl = sprintf(self::API_EPCI_COMMUNE_URL, $epciItem['code'])
);

if (Response::HTTP_OK === $response->getStatusCode()) {
$communeList = json_decode($response->getContent(), true);
foreach ($communeList as $communeItem) {
foreach ($communeItem['codesPostaux'] as $codePostal) {
if (isset($this->communes[$communeItem['nom']][$codePostal])) {
$epci->addCommune($this->communes[$communeItem['nom']][$codePostal]);
}
}
}
} else {
$io->error(sprintf('API failed for: %s', $epciCommunesUrl));
}
$this->entityManager->persist($epci);
$progressBar->advance();
}

$progressBar->finish();
$this->entityManager->flush();
$nbCommunesWithECPI = $this->communeRepository->count([]);
$nbCommunesWithoutECPI = $this->communeRepository->count(['epci' => null]);
$io->success(sprintf(
'EPCI loaded with %d communes that belong to EPCI',
$nbCommunesWithECPI - $nbCommunesWithoutECPI
));
if ($nbCommunesWithoutECPI > 0) {
$io->warning(sprintf(
'%d communes code postal might be obsolete.',
$nbCommunesWithoutECPI
));
}

return Command::SUCCESS;
}

private function initDataFromRepository(): void
{
$epcis = $this->epciRepository->findAll();
foreach ($epcis as $epci) {
$this->epcis[$epci->getCode()] = $epci;
}

$communes = $this->communeRepository->findAll();
foreach ($communes as $commune) {
$this->communes[$commune->getNom()][$commune->getCodePostal()] = $commune;
}
}
}
35 changes: 35 additions & 0 deletions src/DataFixtures/Files/Epci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
epcis:
-
code: "200054807"
nom: "Métropole d'Aix-Marseille-Provence"
communes:
-
nom: "Marseille"
codesPostaux:
- "13001"
- "13002"
- "13003"
- "13004"
- "13005"
- "13006"
- "13007"
- "13008"
- "13009"
- "13010"
- "13011"
- "13012"
- "13013"
- "13014"
- "13015"
- "13016"
-
code: "244400503"
nom: "CC d'Erdre et Gesvres"
communes:
-
nom: "Petit-Mars"
codesPostaux:
- "44390"
- nom: "Saint-Mars-du-Désert"
codesPostaux:
- "44850"
1 change: 1 addition & 0 deletions src/DataFixtures/Files/NewSignalement.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ signalements:
score_logement: 13.990384615385
score_batiment: 50
created_at: "2024-01-11 14:00"
is_relogement: 1
desordre_categorie:
- "Usage / entretien"
- "Etanchéité / isolation"
Expand Down
7 changes: 4 additions & 3 deletions src/DataFixtures/Files/Signalement.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ signalements:
nb_enfants_m6: "1"
nb_enfants_p6: "2"
is_allocataire: ""
is_logement_social: 0
nature_logement: "Appartement"
type_logement: "T2"
superficie: 30
Expand Down Expand Up @@ -966,7 +965,7 @@ signalements:
nb_adultes: "2"
nb_enfants_m6: "1"
nb_enfants_p6: "2"
is_allocataire: ""
is_allocataire: "MSA"
is_logement_social: 0
nature_logement: "Appartement"
type_logement: "T2"
Expand Down Expand Up @@ -1008,7 +1007,6 @@ signalements:
nb_adultes: "2"
nb_enfants_m6: "1"
nb_enfants_p6: "2"
is_allocataire: ""
is_logement_social: 0
nature_logement: "Appartement"
type_logement: "T2"
Expand Down Expand Up @@ -1682,6 +1680,7 @@ signalements:
details: "Absence d’isolation toiture<br><br>Courrier adressé non pas à la mairie mais au bailleur social"
is_proprio_averti: 1
is_logement_social: 1
is_preavis_depart: 1
nb_adultes: "2"
nb_enfants_m6: "1"
nb_enfants_p6: "2"
Expand Down Expand Up @@ -1712,6 +1711,7 @@ signalements:
uuid: "00000000-0000-0000-2024-000000000001"
details: "Actuellement locataire, j'attire votre attention sur certaines conditions de non-décence et de confort qui affectent ma qualité de vie quotidienne."
is_proprio_averti: 1
is_relogement: 1
is_logement_social: 0
nb_adultes: "2"
nb_enfants_m6: "0"
Expand Down Expand Up @@ -1751,6 +1751,7 @@ signalements:
details: "Actuellement locataire, j'attire votre attention sur certaines conditions de non-décence et de confort qui affectent ma qualité de vie quotidienne."
is_proprio_averti: 1
is_logement_social: 1
is_preavis_depart: 1
bailleur: "13 Habitat"
nb_adultes: "2"
nb_enfants_m6: "0"
Expand Down
52 changes: 52 additions & 0 deletions src/DataFixtures/Loader/LoadEpciData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\DataFixtures\Loader;

use App\Entity\Epci;
use App\Repository\CommuneRepository;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Yaml\Yaml;

class LoadEpciData extends Fixture implements OrderedFixtureInterface
{
public function __construct(private CommuneRepository $communeRepository)
{
}

public function load(ObjectManager $manager)
{
$partnersRows = Yaml::parseFile(__DIR__.'/../Files/Epci.yml');
foreach ($partnersRows['epcis'] as $row) {
$this->loadEpci($manager, $row);
}
$manager->flush();
}

private function loadEpci(ObjectManager $manager, array $row): void
{
$communeList = $this->communeRepository->findAll();
$communes = [];
foreach ($communeList as $communeItem) {
$communes[$communeItem->getNom()][$communeItem->getCodePostal()] = $communeItem;
}

$epci = (new Epci())
->setNom($row['nom'])
->setCode($row['code']);

foreach ($row['communes'] as $commune) {
foreach ($commune['codesPostaux'] as $codePostal) {
$epci->addCommune($communes[$commune['nom']][$codePostal]);
}
}

$manager->persist($epci);
}

public function getOrder(): int
{
return 17;
}
}
14 changes: 7 additions & 7 deletions src/DataFixtures/Loader/LoadSignalementData.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ private function loadSignalements(ObjectManager $manager, array $row)
->setTelProprio($phoneNumber)
->setAdresseProprio($faker->address())
->setIsCguAccepted(true)
->setIsAllocataire($row['is_allocataire'])
->setIsAllocataire($row['is_allocataire'] ?? null)
->setNumAllocataire($faker->randomNumber(6))
->setStatut($row['statut'])
->setScore($row['score'])
->setReference($row['reference'])
->setIsBailEnCours(false)
->setIsRelogement(false)
->setIsLogementSocial($row['is_logement_social'])
->setIsPreavisDepart(false)
->setIsRelogement($row['is_relogement'] ?? false)
->setIsLogementSocial($row['is_logement_social'] ?? null)
->setIsPreavisDepart($row['is_preavis_depart'] ?? false)
->setIsRefusIntervention(false)
->setGeoloc(json_decode($row['geoloc'], true))
->setIsRsa(false)
Expand Down Expand Up @@ -319,17 +319,17 @@ private function loadNewSignalements(ObjectManager $manager, array $row)
->setTelProprio($phoneNumber)
->setAdresseProprio($faker->address())
->setIsCguAccepted(true)
->setIsAllocataire($row['is_allocataire'])
->setIsAllocataire($row['is_allocataire'] ?? null)
->setNumAllocataire($faker->randomNumber(6))
->setStatut($row['statut'])
->setScore($row['score'])
->setScoreBatiment($row['score_batiment'])
->setScoreLogement($row['score_logement'])
->setReference($row['reference'])
->setIsBailEnCours(true)
->setIsRelogement(false)
->setIsRelogement($row['is_relogement'] ?? false)
->setIsLogementSocial($row['is_logement_social'] ?? false)
->setIsPreavisDepart(false)
->setIsPreavisDepart($row['is_preavis_depart'] ?? false)
->setIsRefusIntervention(false)
->setGeoloc(json_decode($row['geoloc'], true))
->setIsRsa(false)
Expand Down
Loading
Loading