Commit 34594ec8 by Yvon Kerdoncuff

Merge branch '7928-import-adherent-data' into 'develop'

add tables and scripts to import adherent data

See merge request !149
parents 4c2b3755 e75f1994
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\Adherent;
use App\Entity\ExternalAdherentData;
use App\Entity\ExternalDataAgeGroup;
use App\Entity\ExternalDataCCMemberStatus;
use App\Entity\GlobalParameter;
use App\Utils\CustomEntityManager;
use App\Utils\TAVCotisationUtils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
use Symfony\Component\Console\Input\InputArgument;
/**
* This command is used to import data to complete external adherents data
*/
class ImportExternalAdherentData extends Command
{
protected static $defaultName = 'kohinos:ssa:external-adherent-data-import';
protected $em;
protected $io;
protected $param;
protected $tavCotisationUtils;
public function __construct(
CustomEntityManager $em,
TAVCotisationUtils $tavCotisationUtils
) {
$this->em = $em;
$this->tavCotisationUtils = $tavCotisationUtils;
parent::__construct();
}
protected function configure()
{
$this
->setDescription('SSA : importer via CSV des informations externes non datées concernant les adhérents.')
->addArgument('filepath', InputArgument::OPTIONAL, 'Chemin du fichier csv contenant les données non datées.')
;
}
private function readCsv() {
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
$this->io->title('START - importing external adherent data');
$csvFilePath = $input->getArgument('filepath');
//Prepare some tables if they do not exist yet
if (!$this->em->getRepository(ExternalDataAgeGroup::class)->findAll()) {
$one = new ExternalDataAgeGroup(1);
$two = new ExternalDataAgeGroup(2);
$three = new ExternalDataAgeGroup(3);
$this->em->persist($one);
$this->em->persist($two);
$this->em->persist($three);
}
if (!$this->em->getRepository(ExternalDataCCMemberStatus::class)->findAll()) {
$one = new ExternalDataCCMemberStatus(1);
$two = new ExternalDataCCMemberStatus(2);
$this->em->persist($one);
$this->em->persist($two);
}
$this->em->flush();
$ageGroups = [
"1" => $this->em->getRepository(ExternalDataAgeGroup::class)->find(1),
"2" => $this->em->getRepository(ExternalDataAgeGroup::class)->find(2),
"3" => $this->em->getRepository(ExternalDataAgeGroup::class)->find(3)
];
$ccStatus = [
"1" => $this->em->getRepository(ExternalDataCCMemberStatus::class)->find(1),
"2" => $this->em->getRepository(ExternalDataCCMemberStatus::class)->find(2)
];
if (($handle = fopen($csvFilePath, "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($row[0] === 'mail') {
// headers row
continue;
}
try {
$adherent = $this->em->getRepository(Adherent::class)->findOneByEmail($row[0], false);
} catch (\Exception $e) {
$this->io->warning('Adherent not found: ' . $row[0]);
continue;
}
$updated = false;
$data = $this->em->getRepository(ExternalAdherentData::class)->findBy([
'adherent' => $adherent
]);
if(!$data) {
$data = new ExternalAdherentData();
$data->setAdherent($adherent);
$updated = true;
} else {
$data = $data[0];
}
#If a field is already stored in database, do not update it
#If no data is provided in the file cell, do not fill (let it empty)
if (!$data->getExternalDataAgeGroup() && $row[1] !== "") {
$data->setExternalDataAgeGroup($ageGroups[$row[1]]);
$updated = true;
}
if (!$data->getBirthYear() && $row[2] !== "") {
$data->setBirthYear(intval($row[2]));
$updated = true;
}
if (!$data->getAgeIncomeRatioGroup() && $row[3] !== "") {
$data->setAgeIncomeRatioGroup(intval($row[3]));
$updated = true;
}
if (!$data->getHouseholdIncomeRatioGroup() && $row[4] !== "") {
$data->setHouseholdIncomeRatioGroup(intval($row[4]));
$updated = true;
}
if (!$data->getExternalId() && $row[5] !== "") {
$data->setExternalId($row[5]);
$updated = true;
}
if (!$data->getCohort() && $row[6] !== "") {
$data->setCohort($row[6]);
$updated = true;
}
if(count($row) > 7) {
if (!$data->getExternalDataCCMemberStatus() && $row[7] !== "") {
$data->setExternalDataCCMemberStatus($ccStatus[$row[7]]);
$updated = true;
}
}
if ($updated) {
$this->em->persist($data);
$this->io->success('Succesfully updated: ' . $row[0]);
} else {
$this->io->warning('No new data for Adherent: '
. $row[0] . ' (update ignored)');
}
}
$this->em->flush();
fclose($handle);
}
$this->io->success('End');
$memoryUsage = memory_get_usage(true) / 1024 / 1024;
$this->io->text("Batch finished with memory: ${memoryUsage}M");
return 0;
}
}
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\Adherent;
use App\Entity\ExternalAdherentDatedData;
use App\Entity\GlobalParameter;
use App\Utils\CustomEntityManager;
use App\Utils\TAVCotisationUtils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
use Symfony\Component\Console\Input\InputArgument;
/**
* This command is used to import data to complete external adherents data
*/
class ImportExternalAdherentDatedData extends Command
{
protected static $defaultName = 'kohinos:ssa:external-adherent-dated-data-import';
protected $em;
protected $io;
protected $param;
protected $tavCotisationUtils;
public function __construct(
CustomEntityManager $em,
TAVCotisationUtils $tavCotisationUtils
) {
$this->em = $em;
$this->tavCotisationUtils = $tavCotisationUtils;
parent::__construct();
}
protected function configure()
{
$this
->setDescription('SSA : importer via CSV des informations externes datées concernant les adhérents,.')
->addArgument('filepath', InputArgument::OPTIONAL, 'Chemin du fichier csv contenant les données datées.')
;
}
private function readCsv() {
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
$this->io->title('START - importing external dated adherent data');
$csvFilePath = $input->getArgument('filepath');
if (($handle = fopen($csvFilePath, "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ";")) !== FALSE) {
if ($row[0] === 'mail') {
// headers row
continue;
}
try {
$adherent = $this->em->getRepository(Adherent::class)->findOneByEmail($row[0], false);
} catch (\Exception $e) {
$this->io->warning('Adherent not found: ' . $row[0]);
continue;
}
$annee = $row[2];
if(!$annee) {
$this->io->warning('Year not provided: ' . $row[2]);
continue;
}
$annee = intval($annee);
$updated = false;
$datedData = $this->em->getRepository(ExternalAdherentDatedData::class)->findBy([
'adherent' => $adherent,
'year' => $annee
]);
if(!$datedData) {
$datedData = new ExternalAdherentDatedData();
$datedData->setAdherent($adherent);
$datedData->setYear($annee);
$updated = true;
} else {
$datedData = $datedData[0];
}
#If a field is already stored in database, do not update it
#If no data is provided in the file cell, do not fill (let it empty)
if (!$datedData->getHouseholdUnitShares() && $row[1] !== "") {
$datedData->setHouseholdUnitShares(floatval($row[1]));
$updated = true;
}
if (!$datedData->getAnnualIncome() && $row[3] !== "") {
$datedData->setAnnualIncome(intval($row[3]));
$updated = true;
}
if (!$datedData->getMonthlyIncome() && $row[4] !== "") {
$datedData->setMonthlyIncome(intval($row[4]));
$updated = true;
}
if ($updated) {
$this->em->persist($datedData);
$this->io->success('Succesfully updated: ' . $row[0]);
} else {
$this->io->warning('No new dated data for Adherent: '
. $row[0] . ' and Year: ' . $row[2] . ' (update ignored)');
}
}
$this->em->flush();
fclose($handle);
}
$this->io->success('End');
$memoryUsage = memory_get_usage(true) / 1024 / 1024;
$this->io->text("Batch finished with memory: ${memoryUsage}M");
return 0;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;
/**
* @ORM\Entity
* @ORM\Table(name="external_adherent_data")
*/
class ExternalAdherentData
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Adherent")
* @ORM\JoinColumn(name="adherent_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $adherent;
/**
* @ORM\ManyToOne(targetEntity="ExternalDataAgeGroup")
* @ORM\JoinColumn(name="external_data_age_group_id", referencedColumnName="id", nullable=true)
*/
private $externalDataAgeGroup;
/** @ORM\Column(type="integer", nullable=true) */
private $birthYear;
/** @ORM\Column(type="integer", nullable=true) */
private $ageIncomeRatioGroup;
/** @ORM\Column(type="integer", nullable=true) */
private $householdIncomeRatioGroup;
/** @ORM\Column(type="string", length=255, nullable=true) */
private $externalId;
/** @ORM\Column(type="string", length=255, nullable=true) */
private $cohort;
/**
* @ORM\ManyToOne(targetEntity="ExternalDataCCMemberStatus")
* @ORM\JoinColumn(name="external_data_cc_member_status_id", referencedColumnName="id", nullable=true)
*/
private $externalDataCCMemberStatus;
public function setAdherent(Adherent $adherent): self
{
$this->adherent = $adherent;
return $this;
}
public function setExternalDataAgeGroup(?ExternalDataAgeGroup $externalDataAgeGroup): self
{
$this->externalDataAgeGroup = $externalDataAgeGroup;
return $this;
}
public function setBirthYear(?int $birthYear): self
{
$this->birthYear = $birthYear;
return $this;
}
public function setAgeIncomeRatioGroup(?int $ageIncomeRatioGroup): self
{
$this->ageIncomeRatioGroup = $ageIncomeRatioGroup;
return $this;
}
public function setHouseholdIncomeRatioGroup(?int $householdIncomeRatioGroup): self
{
$this->householdIncomeRatioGroup = $householdIncomeRatioGroup;
return $this;
}
public function setExternalId(?string $externalId): self
{
$this->externalId = $externalId;
return $this;
}
public function setCohort(?string $cohort): self
{
$this->cohort = $cohort;
return $this;
}
public function setExternalDataCCMemberStatus(?ExternalDataCCMemberStatus $externalDataCCMemberStatus): self
{
$this->externalDataCCMemberStatus = $externalDataCCMemberStatus;
return $this;
}
public function getAdherent(): Adherent
{
return $this->adherent;
}
public function getExternalDataAgeGroup(): ?ExternalDataAgeGroup
{
return $this->externalDataAgeGroup;
}
public function getBirthYear(): ?int
{
return $this->birthYear;
}
public function getAgeIncomeRatioGroup(): ?int
{
return $this->ageIncomeRatioGroup;
}
public function getHouseholdIncomeRatioGroup(): ?int
{
return $this->householdIncomeRatioGroup;
}
public function getExternalId(): ?string
{
return $this->externalId;
}
public function getCohort(): ?string
{
return $this->cohort;
}
public function getExternalDataCCMemberStatus(): ?ExternalDataCCMemberStatus
{
return $this->externalDataCCMemberStatus;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;
/**
* @ORM\Entity
* @ORM\Table(name="external_adherent_dated_data")
*/
class ExternalAdherentDatedData
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Adherent")
* @ORM\JoinColumn(name="adherent_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $adherent;
/** @ORM\Column(type="integer") */
private $year;
/** @ORM\Column(type="float", nullable=true) */
private $householdUnitShares;
/** @ORM\Column(type="integer", nullable=true) */
private $annualIncome;
/** @ORM\Column(type="integer", nullable=true) */
private $monthlyIncome;
public function setAdherent(Adherent $adherent): self
{
$this->adherent = $adherent;
return $this;
}
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
public function setHouseholdUnitShares(?float $householdUnitShares): self
{
$this->householdUnitShares = $householdUnitShares;
return $this;
}
public function setAnnualIncome(?int $annualIncome): self
{
$this->annualIncome = $annualIncome;
return $this;
}
public function setMonthlyIncome(?int $monthlyIncome): self
{
$this->monthlyIncome = $monthlyIncome;
return $this;
}
public function getAdherent(): Adherent
{
return $this->adherent;
}
public function getYear(): int
{
return $this->year;
}
public function getHouseholdUnitShares(): ?float
{
return $this->householdUnitShares;
}
public function getAnnualIncome(): ?int
{
return $this->annualIncome;
}
public function getMonthlyIncome(): ?int
{
return $this->monthlyIncome;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="external_data_age_group")
*/
class ExternalDataAgeGroup {
public function __construct(int $id)
{
if ($id === 1) {
$this->id = 1;
$this->ageGroup = "moins de 30 ans";
}
if ($id === 2) {
$this->id = 2;
$this->ageGroup = "entre 30 et 59 ans";
}
if ($id === 3) {
$this->id = 3;
$this->ageGroup = "plus de 59 ans";
}
}
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/** @ORM\Column(type="string", length=50) */
private $ageGroup;
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="external_data_cc_member_status")
*/
class ExternalDataCCMemberStatus {
public function __construct(int $id)
{
if ($id === 1) {
$this->id = 1;
$this->memberStatus = "membre CC";
}
if ($id === 2) {
$this->id = 2;
$this->memberStatus = "ancien membre CC";
}
}
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/** @ORM\Column(type="string", length=50) */
private $memberStatus;
}
<?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 Version20250715091147 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('CREATE TABLE external_adherent_data (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', adherent_id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', external_data_age_group_id INT DEFAULT NULL, external_data_cc_member_status_id INT DEFAULT NULL, birth_year INT DEFAULT NULL, age_income_ratio_group INT DEFAULT NULL, household_income_ratio_group INT DEFAULT NULL, external_id VARCHAR(255) DEFAULT NULL, cohort VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_791F593A25F06C53 (adherent_id), INDEX IDX_791F593A3D72A779 (external_data_age_group_id), INDEX IDX_791F593AC1CBBD4F (external_data_cc_member_status_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_general_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE external_adherent_dated_data (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', adherent_id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', year INT NOT NULL, household_unit_shares DOUBLE PRECISION DEFAULT NULL, annual_income INT DEFAULT NULL, monthly_income INT DEFAULT NULL, UNIQUE INDEX UNIQ_23A421FE25F06C53 (adherent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_general_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE external_data_age_group (id INT AUTO_INCREMENT NOT NULL, age_group VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_general_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE external_data_cc_member_status (id INT AUTO_INCREMENT NOT NULL, member_status VARCHAR(50) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_general_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE external_adherent_data ADD CONSTRAINT FK_791F593A25F06C53 FOREIGN KEY (adherent_id) REFERENCES adherent (id)');
$this->addSql('ALTER TABLE external_adherent_data ADD CONSTRAINT FK_791F593A3D72A779 FOREIGN KEY (external_data_age_group_id) REFERENCES external_data_age_group (id)');
$this->addSql('ALTER TABLE external_adherent_data ADD CONSTRAINT FK_791F593AC1CBBD4F FOREIGN KEY (external_data_cc_member_status_id) REFERENCES external_data_cc_member_status (id)');
$this->addSql('ALTER TABLE external_adherent_dated_data ADD CONSTRAINT FK_23A421FE25F06C53 FOREIGN KEY (adherent_id) REFERENCES adherent (id)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE external_adherent_data DROP FOREIGN KEY FK_791F593A3D72A779');
$this->addSql('ALTER TABLE external_adherent_data DROP FOREIGN KEY FK_791F593AC1CBBD4F');
$this->addSql('DROP TABLE external_adherent_data');
$this->addSql('DROP TABLE external_adherent_dated_data');
$this->addSql('DROP TABLE external_data_age_group');
$this->addSql('DROP TABLE external_data_cc_member_status');
}
}
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