diff --git a/api-platform/api/src/DataFixtures/AppFixtures.php b/api-platform/api/src/DataFixtures/AppFixtures.php index e6b8886c02f4506fdc7c6db75859b34a6317fa9a..1cbb2c272696c06f466f876020c87ad39ee72657 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,44 +47,38 @@ 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"); + $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->persist($typeCFE); - } + $departements = []; + $communes = []; + $regions = []; 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; @@ -123,18 +117,18 @@ class AppFixtures extends Fixture $colsFound = true; $depNumCol = 1; - $depNomCol = 935; - $regNomCol = 936; + $depNomCol = 1010; + $regNomCol = 1011; $comNumCol = 3; $comNomCol = 10; $tauxTFPNBCol = 13; $volTFPNBCol = 14; $tauxTFPBCol = 68; $volTFPBCol = 69; - $tauxTHCol = 167; - $volTHCol = 168; - $tauxCFECol = 327; - $volCFECol = 328; + $tauxTHCol = 185; + $volTHCol = 186; + $tauxCFECol = 355; + $volCFECol = 356; break; case 2020: $colsFound = true; @@ -194,6 +188,7 @@ class AppFixtures extends Fixture } $titleLine = true; + $i = 0; if (($handle = fopen($path, "r")) !== FALSE) { while (($data = fgetcsv($handle, separator: ",")) !== FALSE) { if ($titleLine) { @@ -201,40 +196,36 @@ class AppFixtures extends Fixture 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); + $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 = [ - $depNum, $depNom, $regNom, $comNum, $comNom, $tauxTFPNB, $volTFPNB, + $annee, $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); + $departement = $departements[$depNum] ?? null; + $commune = $communes[$comNum] ?? null; if(is_null($departement)) { - $region = $this->regionService->findOneByNom($regNom); + $region = $regions[$regNom] ?? null; if(is_null($region)) { $region = new Region(); $region->setNom($regNom); $manager->persist($region); - $manager->flush(); - $region = $this->regionService->findOneByNom($regNom); } $departement = new Departement(); @@ -244,7 +235,8 @@ class AppFixtures extends Fixture $manager->persist($departement); $manager->flush(); - $departement = $this->departementService->findOneByCode($depNum); + $departements[$depNum] = $departement; + $regions[$regNom] ??= $region; } if (is_null($commune)) { @@ -255,43 +247,92 @@ class AppFixtures extends Fixture $manager->persist($commune); $manager->flush(); - $commune = $this->communeService->findOneByCode($comNum); + + $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; } - $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); + 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); } } } + + 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 384a105193d209727e6fa02966ebf80b9a523164..a58e74ea68159ce95b594e14dac8c53f394fcd8e 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 3e836362a9a88cf4a0d90a7d5f2ab28159da6a87..afb8d1cb364d9832d2a559a6da37e5640ef68bb4 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 95ba0d7b1d75ae1640b4000379179a626a0bb9bf..3d64fd1c4de3a48cf51b68b79db35537718880b1 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 d3fd0481d6d6565047c97f0aa345de1f15214ae1..1ccb6aa5fd7c90d5247ccd95a3bdf018efc5dfa3 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 91ff8fe6cc60946472627b191d07e14280263a6c..9ac5ec39b279ce619e5313be7967270368fe364e 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; - } }