From 30da2429e61d70f708b6e55dbd1dc3d27a1c5613 Mon Sep 17 00:00:00 2001 From: NOPepin Date: Wed, 11 Feb 2026 16:46:49 +0100 Subject: [PATCH 1/2] fix-fixtures --- .../api/src/DataFixtures/AppFixtures.php | 537 ++++++++++-------- api-platform/api/src/Entity/Commune.php | 43 +- api-platform/api/src/Entity/Departement.php | 43 +- api-platform/api/src/Entity/Region.php | 41 +- api-platform/api/src/Entity/Taxe.php | 4 +- api-platform/api/src/Entity/TypeTaxe.php | 41 +- 6 files changed, 303 insertions(+), 406 deletions(-) diff --git a/api-platform/api/src/DataFixtures/AppFixtures.php b/api-platform/api/src/DataFixtures/AppFixtures.php index e6b8886..4603467 100644 --- a/api-platform/api/src/DataFixtures/AppFixtures.php +++ b/api-platform/api/src/DataFixtures/AppFixtures.php @@ -31,14 +31,14 @@ class AppFixtures extends Fixture 'data/REI_2022.csv', ]; + const BATCH_SIZE = 1000; + public function load(ObjectManager $manager): void { ini_set('memory_limit', -1); // les fichiers excels sont tellement énormes qu'il faut beaucoup de RAM $this->loadCsv($manager); $manager->flush(); - - ini_set('memory_limit', '1024M'); } public function loadCsv(ObjectManager $manager): void { @@ -47,251 +47,304 @@ class AppFixtures extends Fixture // \PhpOffice\PhpSpreadsheet\Settings::setCache($simpleCache); - $typeTFPNB = $this->typeTaxeService->findOneByCode(TypeTaxe::TFPNB_CODE); - $typeTFPB = $this->typeTaxeService->findOneByCode(TypeTaxe::TFPB_CODE); - $typeTH = $this->typeTaxeService->findOneByCode(TypeTaxe::TH_CODE); - $typeCFE = $this->typeTaxeService->findOneByCode(TypeTaxe::CFE_CODE); - - if(is_null($typeTFPNB)) { - $typeTFPNB = new TypeTaxe(); - $typeTFPNB->setCode(TypeTaxe::TFPNB_CODE); - $typeTFPNB->setLabel("Taxe foncière sur les propriétés non bâties"); - - $manager->persist($typeTFPNB); - } - if(is_null($typeTFPB)) { - $typeTFPB = new TypeTaxe(); - $typeTFPB->setCode(TypeTaxe::TFPB_CODE); - $typeTFPB->setLabel("Taxe foncière sur les propriétés bâties"); - - $manager->persist($typeTFPB); - } - if(is_null($typeTH)) { - $typeTH = new TypeTaxe(); - $typeTH->setCode(TypeTaxe::TH_CODE); - $typeTH->setLabel("Taxe d'habitation"); - - $manager->persist($typeTH); - } - if(is_null($typeCFE)) { - $typeCFE = new TypeTaxe(); - $typeCFE->setCode(TypeTaxe::CFE_CODE); - $typeCFE->setLabel("Cotisation foncière des entreprises"); - - $manager->persist($typeCFE); - } - - foreach (self::DATA_FILES as $path) { - $explodedPath = explode('_', str_replace('.csv', '', $path)); - $annee = intval(end($explodedPath)); - - $depNumCol = null; - $depNomCol = null; - $regNomCol = null; - $comNumCol = null; - $comNomCol = null; - $tauxTFPNBCol = null; - $volTFPNBCol = null; - $tauxTFPBCol = null; - $volTFPBCol = null; - $tauxTHCol = null; - $volTHCol = null; - $tauxCFECol = null; - $volCFECol = null; - - $colsFound = false; - - switch ($annee) { - case 2018: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 935; - $regNomCol = 936; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 167; - $volTHCol = 168; - $tauxCFECol = 327; - $volCFECol = 328; - break; - case 2019: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 935; - $regNomCol = 936; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 167; - $volTHCol = 168; - $tauxCFECol = 327; - $volCFECol = 328; - break; - case 2020: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1066; - $regNomCol = 1067; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 185; - $volTHCol = 186; - $tauxCFECol = 347; - $volCFECol = 348; - break; - case 2021: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1108; - $regNomCol = 1109; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 171; - $volTHCol = 172; - $tauxCFECol = 322; - $volCFECol = 323; - break; - case 2022: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1145; - $regNomCol = 1146; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 175; - $volTHCol = 176; - $tauxCFECol = 332; - $volCFECol = 333; - break; - } - - if (!$colsFound) { - continue; - } - - $titleLine = true; - if (($handle = fopen($path, "r")) !== FALSE) { - while (($data = fgetcsv($handle, separator: ",")) !== FALSE) { - if ($titleLine) { - $titleLine = false; - continue; - } - - $depNum = intval($data[$depNumCol - 1] ?? null); - $depNom = $data[$depNomCol - 1] ?? null; - $regNom = $data[$regNomCol - 1] ?? null; - $comNum = intval($data[$comNumCol - 1] ?? null); - $comNom = $data[$comNomCol - 1] ?? null; - $tauxTFPNB = floatval($data[$tauxTFPNBCol - 1] ?? null); - $volTFPNB = floatval($data[$volTFPNBCol - 1] ?? null); - $tauxTFPB = floatval($data[$tauxTFPBCol - 1] ?? null); - $volTFPB = floatval($data[$volTFPBCol - 1] ?? null); - $tauxTH = floatval($data[$tauxTHCol - 1] ?? null); - $volTH = floatval($data[$volTHCol - 1] ?? null); - $tauxCFE = floatval($data[$tauxCFECol - 1] ?? null); - $volCFE = floatval($data[$volCFECol - 1] ?? null); - - $args = [ - $depNum, $depNom, $regNom, $comNum, $comNom, $tauxTFPNB, $volTFPNB, - $tauxTFPB, $volTFPB, $tauxTH, $volTH, $tauxCFE, $volCFE - ]; - - if(in_array(null, $args)) { continue; } - - $departement = $this->departementService->findOneByCode($depNum); - $commune = $this->communeService->findOneByCode($comNum); - - if(is_null($departement)) { - $region = $this->regionService->findOneByNom($regNom); - - if(is_null($region)) { - $region = new Region(); - $region->setNom($regNom); - - $manager->persist($region); + $typeTFPNB = new TypeTaxe(); + $typeTFPNB->setCode(TypeTaxe::TFPNB_CODE); + $typeTFPNB->setLabel("Taxe foncière sur les propriétés non bâties"); + $manager->persist($typeTFPNB); + + $typeTFPB = new TypeTaxe(); + $typeTFPB->setCode(TypeTaxe::TFPB_CODE); + $typeTFPB->setLabel("Taxe foncière sur les propriétés bâties"); + $manager->persist($typeTFPB); + + $typeTH = new TypeTaxe(); + $typeTH->setCode(TypeTaxe::TH_CODE); + $typeTH->setLabel("Taxe d'habitation"); + $manager->persist($typeTH); + + $typeCFE = new TypeTaxe(); + $typeCFE->setCode(TypeTaxe::CFE_CODE); + $typeCFE->setLabel("Cotisation foncière des entreprises"); + $manager->persist($typeCFE); + + $manager->flush(); + // $manager->clear(); + + $departements = []; + $communes = []; + $regions = []; + + try { + + foreach (self::DATA_FILES as $path) { + $explodedPath = explode('_', str_replace('.csv', '', $path)); + $annee = intval(end($explodedPath)); + + echo $annee . "\r\n"; + + $depNumCol = null; + $depNomCol = null; + $regNomCol = null; + $comNumCol = null; + $comNomCol = null; + $tauxTFPNBCol = null; + $volTFPNBCol = null; + $tauxTFPBCol = null; + $volTFPBCol = null; + $tauxTHCol = null; + $volTHCol = null; + $tauxCFECol = null; + $volCFECol = null; + + $colsFound = false; + + switch ($annee) { + case 2018: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 935; + $regNomCol = 936; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 167; + $volTHCol = 168; + $tauxCFECol = 327; + $volCFECol = 328; + break; + case 2019: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1010; + $regNomCol = 1011; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 185; + $volTHCol = 186; + $tauxCFECol = 355; + $volCFECol = 356; + break; + case 2020: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1066; + $regNomCol = 1067; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 185; + $volTHCol = 186; + $tauxCFECol = 347; + $volCFECol = 348; + break; + case 2021: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1108; + $regNomCol = 1109; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 171; + $volTHCol = 172; + $tauxCFECol = 322; + $volCFECol = 323; + break; + case 2022: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1145; + $regNomCol = 1146; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 175; + $volTHCol = 176; + $tauxCFECol = 332; + $volCFECol = 333; + break; + } + + if (!$colsFound) { + continue; + } + + $titleLine = true; + $i = 0; + if (($handle = fopen($path, "r")) !== FALSE) { + while (($data = fgetcsv($handle, separator: ",")) !== FALSE) { + if ($titleLine) { + $titleLine = false; + continue; + } + + $depNum = intval(self::decoderExcelHex($data[$depNumCol - 1] ?? '')); + $depNom = self::decoderExcelHex($data[$depNomCol - 1] ?? ''); + $regNom = self::decoderExcelHex($data[$regNomCol - 1] ?? ''); + $comNum = intval(self::decoderExcelHex($data[$comNumCol - 1] ?? '')); + $comNom = self::decoderExcelHex($data[$comNomCol - 1] ?? ''); + $tauxTFPNB = floatval(self::decoderExcelHex($data[$tauxTFPNBCol - 1] ?? '')); + $volTFPNB = floatval(self::decoderExcelHex($data[$volTFPNBCol - 1] ?? '')); + $tauxTFPB = floatval(self::decoderExcelHex($data[$tauxTFPBCol - 1] ?? '')); + $volTFPB = floatval(self::decoderExcelHex($data[$volTFPBCol - 1] ?? '')); + $tauxTH = floatval(self::decoderExcelHex($data[$tauxTHCol - 1] ?? '')); + $volTH = floatval(self::decoderExcelHex($data[$volTHCol - 1] ?? '')); + $tauxCFE = floatval(self::decoderExcelHex($data[$tauxCFECol - 1] ?? '')); + $volCFE = floatval(self::decoderExcelHex($data[$volCFECol - 1] ?? '')); + + $args = [ + $annee, $depNum, $depNom, $regNom, $comNum, $comNom, $tauxTFPNB, $volTFPNB, + $tauxTFPB, $volTFPB, $tauxTH, $volTH, $tauxCFE, $volCFE + ]; + + $departement = $departements[$depNum] ?? null; + $commune = $communes[$comNum] ?? null; + + if(is_null($departement)) { + $region = $regions[$regNom] ?? null; + + if(is_null($region)) { + $region = new Region(); + $region->setNom($regNom); + + $manager->persist($region); + } + + $departement = new Departement(); + $departement->setCode($depNum); + $departement->setNom($depNom); + $departement->setRegion($region); + + $manager->persist($departement); $manager->flush(); - $region = $this->regionService->findOneByNom($regNom); + $departements[$depNum] = $departement; + $regions[$regNom] ??= $region; + } + + if (is_null($commune)) { + $commune = new Commune(); + $commune->setCode($comNum); + $commune->setNom($comNom); + $commune->setDepartement($departement); + + $manager->persist($commune); + $manager->flush(); + + $communes[$comNum] = $commune; + } + + $lineUseful = false; + if(in_array(null, [ + $typeTFPNB->getId(), + $typeTFPB->getId(), + $typeTH->getId(), + $typeCFE->getId(), + ])) { + } + + if($tauxTFPNB > 0 && $volTFPNB > 0) { + $taxeTFPNB = new Taxe(); + $taxeTFPNB->setType($typeTFPNB); + $taxeTFPNB->setCommune($commune); + $taxeTFPNB->setTaux($tauxTFPNB); + $taxeTFPNB->setVolume($volTFPNB); + $taxeTFPNB->setAnnee($annee); + $manager->persist($taxeTFPNB); + + $lineUseful = true; + } + + if($tauxTFPB > 0 && $volTFPB > 0) { + $taxeTFPB = new Taxe(); + $taxeTFPB->setType($typeTFPB); + $taxeTFPB->setCommune($commune); + $taxeTFPB->setTaux($tauxTFPB); + $taxeTFPB->setVolume($volTFPB); + $taxeTFPB->setAnnee($annee); + $manager->persist($taxeTFPB); + + $lineUseful = true; + } + + if($tauxTH > 0 && $volTH > 0) { + $taxeTH = new Taxe(); + $taxeTH->setType($typeTH); + $taxeTH->setCommune($commune); + $taxeTH->setTaux($tauxTH); + $taxeTH->setVolume($volTH); + $taxeTH->setAnnee($annee); + $manager->persist($taxeTH); + + $lineUseful = true; + } + + if($tauxCFE > 0 && $volCFE > 0) { + $taxeCFE = new Taxe(); + $taxeCFE->setType($typeCFE); + $taxeCFE->setCommune($commune); + $taxeCFE->setTaux($tauxCFE); + $taxeCFE->setVolume($volCFE); + $taxeCFE->setAnnee($annee); + $manager->persist($taxeCFE); + + $lineUseful = true; + } + + if(!$lineUseful) { + // print_r($args); + continue; + } + + if (++$i % self::BATCH_SIZE == 0) { + $manager->flush(); + // $manager->clear('App\Entity\Taxe'); } - - $departement = new Departement(); - $departement->setCode($depNum); - $departement->setNom($depNom); - $departement->setRegion($region); - - $manager->persist($departement); - $manager->flush(); - $departement = $this->departementService->findOneByCode($depNum); - } - - if (is_null($commune)) { - $commune = new Commune(); - $commune->setCode($comNum); - $commune->setNom($comNom); - $commune->setDepartement($departement); - - $manager->persist($commune); - $manager->flush(); - $commune = $this->communeService->findOneByCode($comNum); } - - $taxeTFPNB = new Taxe(); - $taxeTFPNB->setType($typeTFPNB); - $taxeTFPNB->setCommune($commune); - $taxeTFPNB->setTaux($tauxTFPNB); - $taxeTFPNB->setVolume($volTFPNB); - $taxeTFPNB->setAnnee($annee); - $manager->persist($taxeTFPNB); - - $taxeTFPB = new Taxe(); - $taxeTFPB->setType($typeTFPB); - $taxeTFPB->setCommune($commune); - $taxeTFPB->setTaux($tauxTFPB); - $taxeTFPB->setVolume($volTFPB); - $taxeTFPB->setAnnee($annee); - $manager->persist($taxeTFPB); - - $taxeTH = new Taxe(); - $taxeTH->setType($typeTH); - $taxeTH->setCommune($commune); - $taxeTH->setTaux($tauxTH); - $taxeTH->setVolume($volTH); - $taxeTH->setAnnee($annee); - $manager->persist($taxeTH); - - $taxeCFE = new Taxe(); - $taxeCFE->setType($typeCFE); - $taxeCFE->setCommune($commune); - $taxeCFE->setTaux($tauxCFE); - $taxeCFE->setVolume($volCFE); - $taxeCFE->setAnnee($annee); - $manager->persist($taxeCFE); + fclose($handle); } - fclose($handle); } + } catch (\Throwable $th) { + dd($th, [ + $typeTFPNB, + $typeTFPB, + $typeTH, + $typeCFE, + ]); } } + + private static function decoderExcelHex($chaine) { + // Le regex cherche le motif : _x suivi de 4 caractères hexadécimaux (0-9, A-F), suivi de _ + return trim(preg_replace_callback( + '/_x([0-9A-Fa-f]{4})_/', + function ($matches) { + // $matches[1] contient le code hexadécimal (ex: "0020") + // hexdec() convertit l'hexa en nombre décimal + // mb_chr() convertit le nombre décimal en caractère (compatible UTF-8) + return mb_chr(hexdec($matches[1])); + }, + $chaine + )); + } } diff --git a/api-platform/api/src/Entity/Commune.php b/api-platform/api/src/Entity/Commune.php index 384a105..a58e74e 100644 --- a/api-platform/api/src/Entity/Commune.php +++ b/api-platform/api/src/Entity/Commune.php @@ -30,20 +30,11 @@ class Commune #[ORM\Column(length: 255)] private ?string $nom = null; - #[ORM\ManyToOne(inversedBy: 'communes')] + #[ORM\ManyToOne()] #[ORM\JoinColumn(nullable: false)] private ?Departement $departement = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: Taxe::class, mappedBy: 'commune', orphanRemoval: true)] - private Collection $taxes; - - public function __construct() - { - $this->taxes = new ArrayCollection(); - } + public function __construct() {} public function getId(): ?int { @@ -81,36 +72,6 @@ class Commune return $this; } - /** - * @return Collection - */ - public function getTaxes(): Collection - { - return $this->taxes; - } - - public function addTax(Taxe $tax): static - { - if (!$this->taxes->contains($tax)) { - $this->taxes->add($tax); - $tax->setCommune($this); - } - - return $this; - } - - public function removeTax(Taxe $tax): static - { - if ($this->taxes->removeElement($tax)) { - // set the owning side to null (unless already changed) - if ($tax->getCommune() === $this) { - $tax->setCommune(null); - } - } - - return $this; - } - public function getCode(): ?int { return $this->code; diff --git a/api-platform/api/src/Entity/Departement.php b/api-platform/api/src/Entity/Departement.php index 3e83636..afb8d1c 100644 --- a/api-platform/api/src/Entity/Departement.php +++ b/api-platform/api/src/Entity/Departement.php @@ -30,20 +30,11 @@ class Departement #[ORM\Column(length: 255)] private ?string $nom = null; - #[ORM\ManyToOne(inversedBy: 'departements')] + #[ORM\ManyToOne()] #[ORM\JoinColumn(nullable: false)] private ?Region $region = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: Commune::class, mappedBy: 'departement', orphanRemoval: true)] - private Collection $communes; - - public function __construct() - { - $this->communes = new ArrayCollection(); - } + public function __construct() {} public function getId(): ?int { @@ -74,36 +65,6 @@ class Departement return $this; } - /** - * @return Collection - */ - public function getCommunes(): Collection - { - return $this->communes; - } - - public function addCommune(Commune $commune): static - { - if (!$this->communes->contains($commune)) { - $this->communes->add($commune); - $commune->setDepartement($this); - } - - return $this; - } - - public function removeCommune(Commune $commune): static - { - if ($this->communes->removeElement($commune)) { - // set the owning side to null (unless already changed) - if ($commune->getDepartement() === $this) { - $commune->setDepartement(null); - } - } - - return $this; - } - public function getNom(): ?string { return $this->nom; diff --git a/api-platform/api/src/Entity/Region.php b/api-platform/api/src/Entity/Region.php index 95ba0d7..3d64fd1 100644 --- a/api-platform/api/src/Entity/Region.php +++ b/api-platform/api/src/Entity/Region.php @@ -27,16 +27,7 @@ class Region #[ORM\Column(length: 255)] private ?string $nom = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: Departement::class, mappedBy: 'region', orphanRemoval: true)] - private Collection $departements; - - public function __construct() - { - $this->departements = new ArrayCollection(); - } + public function __construct() {} public function getId(): ?int { @@ -54,34 +45,4 @@ class Region return $this; } - - /** - * @return Collection - */ - public function getDepartements(): Collection - { - return $this->departements; - } - - public function addDepartement(Departement $departement): static - { - if (!$this->departements->contains($departement)) { - $this->departements->add($departement); - $departement->setRegion($this); - } - - return $this; - } - - public function removeDepartement(Departement $departement): static - { - if ($this->departements->removeElement($departement)) { - // set the owning side to null (unless already changed) - if ($departement->getRegion() === $this) { - $departement->setRegion(null); - } - } - - return $this; - } } diff --git a/api-platform/api/src/Entity/Taxe.php b/api-platform/api/src/Entity/Taxe.php index d3fd048..1ccb6aa 100644 --- a/api-platform/api/src/Entity/Taxe.php +++ b/api-platform/api/src/Entity/Taxe.php @@ -33,14 +33,14 @@ class Taxe #[ORM\Column] private ?int $id = null; - #[ORM\ManyToOne(inversedBy: 'taxes')] + #[ORM\ManyToOne()] #[ORM\JoinColumn(nullable: false)] private ?Commune $commune = null; #[ORM\Column] private ?int $annee = null; - #[ORM\ManyToOne(inversedBy: 'taxes')] + #[ORM\ManyToOne()] #[ORM\JoinColumn(nullable: false)] private ?TypeTaxe $type = null; diff --git a/api-platform/api/src/Entity/TypeTaxe.php b/api-platform/api/src/Entity/TypeTaxe.php index 91ff8fe..9ac5ec3 100644 --- a/api-platform/api/src/Entity/TypeTaxe.php +++ b/api-platform/api/src/Entity/TypeTaxe.php @@ -35,16 +35,7 @@ class TypeTaxe #[ORM\Column(length: 255)] private ?string $label = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: Taxe::class, mappedBy: 'type', orphanRemoval: true)] - private Collection $taxes; - - public function __construct() - { - $this->taxes = new ArrayCollection(); - } + public function __construct() {} public function getId(): ?int { @@ -74,34 +65,4 @@ class TypeTaxe return $this; } - - /** - * @return Collection - */ - public function getTaxes(): Collection - { - return $this->taxes; - } - - public function addTax(Taxe $tax): static - { - if (!$this->taxes->contains($tax)) { - $this->taxes->add($tax); - $tax->setType($this); - } - - return $this; - } - - public function removeTax(Taxe $tax): static - { - if ($this->taxes->removeElement($tax)) { - // set the owning side to null (unless already changed) - if ($tax->getType() === $this) { - $tax->setType(null); - } - } - - return $this; - } } -- GitLab From c69413d56add5dfc00e0412ea472913ab54c04ad Mon Sep 17 00:00:00 2001 From: NOPepin Date: Wed, 11 Feb 2026 16:56:14 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fix-fixtures=20-=20code=20inutile=20retir?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/src/DataFixtures/AppFixtures.php | 496 +++++++++--------- 1 file changed, 242 insertions(+), 254 deletions(-) diff --git a/api-platform/api/src/DataFixtures/AppFixtures.php b/api-platform/api/src/DataFixtures/AppFixtures.php index 4603467..1cbb2c2 100644 --- a/api-platform/api/src/DataFixtures/AppFixtures.php +++ b/api-platform/api/src/DataFixtures/AppFixtures.php @@ -68,269 +68,257 @@ class AppFixtures extends Fixture $manager->persist($typeCFE); $manager->flush(); - // $manager->clear(); $departements = []; $communes = []; $regions = []; - try { - - foreach (self::DATA_FILES as $path) { - $explodedPath = explode('_', str_replace('.csv', '', $path)); - $annee = intval(end($explodedPath)); - - echo $annee . "\r\n"; - - $depNumCol = null; - $depNomCol = null; - $regNomCol = null; - $comNumCol = null; - $comNomCol = null; - $tauxTFPNBCol = null; - $volTFPNBCol = null; - $tauxTFPBCol = null; - $volTFPBCol = null; - $tauxTHCol = null; - $volTHCol = null; - $tauxCFECol = null; - $volCFECol = null; - - $colsFound = false; - - switch ($annee) { - case 2018: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 935; - $regNomCol = 936; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 167; - $volTHCol = 168; - $tauxCFECol = 327; - $volCFECol = 328; - break; - case 2019: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1010; - $regNomCol = 1011; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 185; - $volTHCol = 186; - $tauxCFECol = 355; - $volCFECol = 356; - break; - case 2020: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1066; - $regNomCol = 1067; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 185; - $volTHCol = 186; - $tauxCFECol = 347; - $volCFECol = 348; - break; - case 2021: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1108; - $regNomCol = 1109; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 171; - $volTHCol = 172; - $tauxCFECol = 322; - $volCFECol = 323; - break; - case 2022: - $colsFound = true; - - $depNumCol = 1; - $depNomCol = 1145; - $regNomCol = 1146; - $comNumCol = 3; - $comNomCol = 10; - $tauxTFPNBCol = 13; - $volTFPNBCol = 14; - $tauxTFPBCol = 68; - $volTFPBCol = 69; - $tauxTHCol = 175; - $volTHCol = 176; - $tauxCFECol = 332; - $volCFECol = 333; - break; - } - - if (!$colsFound) { - continue; - } - - $titleLine = true; - $i = 0; - if (($handle = fopen($path, "r")) !== FALSE) { - while (($data = fgetcsv($handle, separator: ",")) !== FALSE) { - if ($titleLine) { - $titleLine = false; - continue; - } - - $depNum = intval(self::decoderExcelHex($data[$depNumCol - 1] ?? '')); - $depNom = self::decoderExcelHex($data[$depNomCol - 1] ?? ''); - $regNom = self::decoderExcelHex($data[$regNomCol - 1] ?? ''); - $comNum = intval(self::decoderExcelHex($data[$comNumCol - 1] ?? '')); - $comNom = self::decoderExcelHex($data[$comNomCol - 1] ?? ''); - $tauxTFPNB = floatval(self::decoderExcelHex($data[$tauxTFPNBCol - 1] ?? '')); - $volTFPNB = floatval(self::decoderExcelHex($data[$volTFPNBCol - 1] ?? '')); - $tauxTFPB = floatval(self::decoderExcelHex($data[$tauxTFPBCol - 1] ?? '')); - $volTFPB = floatval(self::decoderExcelHex($data[$volTFPBCol - 1] ?? '')); - $tauxTH = floatval(self::decoderExcelHex($data[$tauxTHCol - 1] ?? '')); - $volTH = floatval(self::decoderExcelHex($data[$volTHCol - 1] ?? '')); - $tauxCFE = floatval(self::decoderExcelHex($data[$tauxCFECol - 1] ?? '')); - $volCFE = floatval(self::decoderExcelHex($data[$volCFECol - 1] ?? '')); - - $args = [ - $annee, $depNum, $depNom, $regNom, $comNum, $comNom, $tauxTFPNB, $volTFPNB, - $tauxTFPB, $volTFPB, $tauxTH, $volTH, $tauxCFE, $volCFE - ]; - - $departement = $departements[$depNum] ?? null; - $commune = $communes[$comNum] ?? null; - - if(is_null($departement)) { - $region = $regions[$regNom] ?? null; - - if(is_null($region)) { - $region = new Region(); - $region->setNom($regNom); - - $manager->persist($region); - } - - $departement = new Departement(); - $departement->setCode($depNum); - $departement->setNom($depNom); - $departement->setRegion($region); - - $manager->persist($departement); - $manager->flush(); - $departements[$depNum] = $departement; - $regions[$regNom] ??= $region; - } - - if (is_null($commune)) { - $commune = new Commune(); - $commune->setCode($comNum); - $commune->setNom($comNom); - $commune->setDepartement($departement); - - $manager->persist($commune); - $manager->flush(); - - $communes[$comNum] = $commune; - } - - $lineUseful = false; - if(in_array(null, [ - $typeTFPNB->getId(), - $typeTFPB->getId(), - $typeTH->getId(), - $typeCFE->getId(), - ])) { - } - - if($tauxTFPNB > 0 && $volTFPNB > 0) { - $taxeTFPNB = new Taxe(); - $taxeTFPNB->setType($typeTFPNB); - $taxeTFPNB->setCommune($commune); - $taxeTFPNB->setTaux($tauxTFPNB); - $taxeTFPNB->setVolume($volTFPNB); - $taxeTFPNB->setAnnee($annee); - $manager->persist($taxeTFPNB); - - $lineUseful = true; - } - - if($tauxTFPB > 0 && $volTFPB > 0) { - $taxeTFPB = new Taxe(); - $taxeTFPB->setType($typeTFPB); - $taxeTFPB->setCommune($commune); - $taxeTFPB->setTaux($tauxTFPB); - $taxeTFPB->setVolume($volTFPB); - $taxeTFPB->setAnnee($annee); - $manager->persist($taxeTFPB); - - $lineUseful = true; - } - - if($tauxTH > 0 && $volTH > 0) { - $taxeTH = new Taxe(); - $taxeTH->setType($typeTH); - $taxeTH->setCommune($commune); - $taxeTH->setTaux($tauxTH); - $taxeTH->setVolume($volTH); - $taxeTH->setAnnee($annee); - $manager->persist($taxeTH); - - $lineUseful = true; - } - - if($tauxCFE > 0 && $volCFE > 0) { - $taxeCFE = new Taxe(); - $taxeCFE->setType($typeCFE); - $taxeCFE->setCommune($commune); - $taxeCFE->setTaux($tauxCFE); - $taxeCFE->setVolume($volCFE); - $taxeCFE->setAnnee($annee); - $manager->persist($taxeCFE); - - $lineUseful = true; - } - - if(!$lineUseful) { - // print_r($args); - continue; - } - - if (++$i % self::BATCH_SIZE == 0) { - $manager->flush(); - // $manager->clear('App\Entity\Taxe'); + foreach (self::DATA_FILES as $path) { + $explodedPath = explode('_', str_replace('.csv', '', $path)); + $annee = intval(end($explodedPath)); + + echo $annee . "\r\n"; + + $depNumCol = null; + $depNomCol = null; + $regNomCol = null; + $comNumCol = null; + $comNomCol = null; + $tauxTFPNBCol = null; + $volTFPNBCol = null; + $tauxTFPBCol = null; + $volTFPBCol = null; + $tauxTHCol = null; + $volTHCol = null; + $tauxCFECol = null; + $volCFECol = null; + + $colsFound = false; + + switch ($annee) { + case 2018: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 935; + $regNomCol = 936; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 167; + $volTHCol = 168; + $tauxCFECol = 327; + $volCFECol = 328; + break; + case 2019: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1010; + $regNomCol = 1011; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 185; + $volTHCol = 186; + $tauxCFECol = 355; + $volCFECol = 356; + break; + case 2020: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1066; + $regNomCol = 1067; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 185; + $volTHCol = 186; + $tauxCFECol = 347; + $volCFECol = 348; + break; + case 2021: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1108; + $regNomCol = 1109; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 171; + $volTHCol = 172; + $tauxCFECol = 322; + $volCFECol = 323; + break; + case 2022: + $colsFound = true; + + $depNumCol = 1; + $depNomCol = 1145; + $regNomCol = 1146; + $comNumCol = 3; + $comNomCol = 10; + $tauxTFPNBCol = 13; + $volTFPNBCol = 14; + $tauxTFPBCol = 68; + $volTFPBCol = 69; + $tauxTHCol = 175; + $volTHCol = 176; + $tauxCFECol = 332; + $volCFECol = 333; + break; + } + + if (!$colsFound) { + continue; + } + + $titleLine = true; + $i = 0; + if (($handle = fopen($path, "r")) !== FALSE) { + while (($data = fgetcsv($handle, separator: ",")) !== FALSE) { + if ($titleLine) { + $titleLine = false; + continue; + } + + $depNum = intval(self::decoderExcelHex($data[$depNumCol - 1] ?? '')); + $depNom = self::decoderExcelHex($data[$depNomCol - 1] ?? ''); + $regNom = self::decoderExcelHex($data[$regNomCol - 1] ?? ''); + $comNum = intval(self::decoderExcelHex($data[$comNumCol - 1] ?? '')); + $comNom = self::decoderExcelHex($data[$comNomCol - 1] ?? ''); + $tauxTFPNB = floatval(self::decoderExcelHex($data[$tauxTFPNBCol - 1] ?? '')); + $volTFPNB = floatval(self::decoderExcelHex($data[$volTFPNBCol - 1] ?? '')); + $tauxTFPB = floatval(self::decoderExcelHex($data[$tauxTFPBCol - 1] ?? '')); + $volTFPB = floatval(self::decoderExcelHex($data[$volTFPBCol - 1] ?? '')); + $tauxTH = floatval(self::decoderExcelHex($data[$tauxTHCol - 1] ?? '')); + $volTH = floatval(self::decoderExcelHex($data[$volTHCol - 1] ?? '')); + $tauxCFE = floatval(self::decoderExcelHex($data[$tauxCFECol - 1] ?? '')); + $volCFE = floatval(self::decoderExcelHex($data[$volCFECol - 1] ?? '')); + + $args = [ + $annee, $depNum, $depNom, $regNom, $comNum, $comNom, $tauxTFPNB, $volTFPNB, + $tauxTFPB, $volTFPB, $tauxTH, $volTH, $tauxCFE, $volCFE + ]; + + $departement = $departements[$depNum] ?? null; + $commune = $communes[$comNum] ?? null; + + if(is_null($departement)) { + $region = $regions[$regNom] ?? null; + + if(is_null($region)) { + $region = new Region(); + $region->setNom($regNom); + + $manager->persist($region); } + + $departement = new Departement(); + $departement->setCode($depNum); + $departement->setNom($depNom); + $departement->setRegion($region); + + $manager->persist($departement); + $manager->flush(); + $departements[$depNum] = $departement; + $regions[$regNom] ??= $region; + } + + if (is_null($commune)) { + $commune = new Commune(); + $commune->setCode($comNum); + $commune->setNom($comNom); + $commune->setDepartement($departement); + + $manager->persist($commune); + $manager->flush(); + + $communes[$comNum] = $commune; + } + + $lineUseful = false; + if(in_array(null, [ + $typeTFPNB->getId(), + $typeTFPB->getId(), + $typeTH->getId(), + $typeCFE->getId(), + ])) { + } + + if($tauxTFPNB > 0 && $volTFPNB > 0) { + $taxeTFPNB = new Taxe(); + $taxeTFPNB->setType($typeTFPNB); + $taxeTFPNB->setCommune($commune); + $taxeTFPNB->setTaux($tauxTFPNB); + $taxeTFPNB->setVolume($volTFPNB); + $taxeTFPNB->setAnnee($annee); + $manager->persist($taxeTFPNB); + + $lineUseful = true; + } + + if($tauxTFPB > 0 && $volTFPB > 0) { + $taxeTFPB = new Taxe(); + $taxeTFPB->setType($typeTFPB); + $taxeTFPB->setCommune($commune); + $taxeTFPB->setTaux($tauxTFPB); + $taxeTFPB->setVolume($volTFPB); + $taxeTFPB->setAnnee($annee); + $manager->persist($taxeTFPB); + + $lineUseful = true; + } + + if($tauxTH > 0 && $volTH > 0) { + $taxeTH = new Taxe(); + $taxeTH->setType($typeTH); + $taxeTH->setCommune($commune); + $taxeTH->setTaux($tauxTH); + $taxeTH->setVolume($volTH); + $taxeTH->setAnnee($annee); + $manager->persist($taxeTH); + + $lineUseful = true; + } + + if($tauxCFE > 0 && $volCFE > 0) { + $taxeCFE = new Taxe(); + $taxeCFE->setType($typeCFE); + $taxeCFE->setCommune($commune); + $taxeCFE->setTaux($tauxCFE); + $taxeCFE->setVolume($volCFE); + $taxeCFE->setAnnee($annee); + $manager->persist($taxeCFE); + + $lineUseful = true; + } + + if(!$lineUseful) { + // print_r($args); + continue; + } + + if (++$i % self::BATCH_SIZE == 0) { + $manager->flush(); } - fclose($handle); } + fclose($handle); } - } catch (\Throwable $th) { - dd($th, [ - $typeTFPNB, - $typeTFPB, - $typeTH, - $typeCFE, - ]); } } -- GitLab