Newer
Older
use App\Entity\Commune;
use App\Entity\Departement;
use App\Entity\Region;
use App\Entity\Taxe;
use App\Entity\TypeTaxe;
use App\Service\CommuneService;
use App\Service\DepartementService;
use App\Service\RegionService;
use App\Service\TypeTaxeService;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function __construct(
private RegionService $regionService,
private DepartementService $departementService,
private CommuneService $communeService,
private TypeTaxeService $typeTaxeService,
) {}
'data/REI_2018.csv',
'data/REI_COMPLET_2019.csv',
'data/REI_COMPLET_2020.csv',
'data/REI_2021.csv',
'data/REI_2022.csv',
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 {
// $pool = new ApcuCachePool();
// $simpleCache = new SimpleCacheBridge($pool);
// \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);
}
$explodedPath = explode('_', str_replace('.csv', '', $path));
$annee = intval(end($explodedPath));
$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;
$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;
$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;
$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;
$comNumCol = 3;
$comNomCol = 10;
$tauxTFPNBCol = 13;
$volTFPNBCol = 14;
$tauxTFPBCol = 68;
$volTFPBCol = 69;
$tauxTHCol = 175;
$volTHCol = 176;
$tauxCFECol = 332;
$volCFECol = 333;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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);
$manager->flush();
$region = $this->regionService->findOneByNom($regNom);
}
$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);
$taxeTFPB = new Taxe();
$taxeTFPB->setType($typeTFPB);
$taxeTFPB->setCommune($commune);
$taxeTFPB->setTaux($tauxTFPB);
$taxeTFPB->setVolume($volTFPB);
$taxeTH = new Taxe();
$taxeTH->setType($typeTH);
$taxeTH->setCommune($commune);
$taxeTH->setTaux($tauxTH);
$taxeTH->setVolume($volTH);
$taxeCFE = new Taxe();
$taxeCFE->setType($typeCFE);
$taxeCFE->setCommune($commune);
$taxeCFE->setTaux($tauxCFE);
$taxeCFE->setVolume($volCFE);
}
fclose($handle);