Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions migrations/Version20260611053328.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260611053328 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add category column to cim_11_modifier_value';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE cim_11_modifier_value ADD category VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE cim_11_modifier_value DROP COLUMN category');
}
}
107 changes: 96 additions & 11 deletions src/Command/Cim11Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand('app:cim11:import')]
class Cim11Import extends Command
{
private array $modifierValues = [];

/** @var array<string, array{title: string, code: string, parent_id: string}> */
private array $modifierTree = [];

/** @var array<string, list<string>> */
private array $modifierChildren = [];

private array $diseases = [];

private array $cim11Mapping = [];

private string $importId;

private bool $forceModifiers = false;

private OutputInterface $output;

public function __construct(
Expand All @@ -32,12 +41,19 @@ public function __construct(
parent::__construct();
}

protected function configure(): void
{
$this->addOption('force-modifiers', null, InputOption::VALUE_NONE, 'Force re-import of modifiers even if already set on a disease');
}

public function execute(InputInterface $input, OutputInterface $output): int
{
ini_set('memory_limit', '-1');
$this->importId = uniqid();
$this->output = $output;
$this->forceModifiers = (bool) $input->getOption('force-modifiers');

$this->buildModifierTree();
$this->importModifiers();
$this->importDiseases();

Expand Down Expand Up @@ -100,8 +116,11 @@ private function importDiseases(): void
}

if ($cim11Disease->hasModifier($case)) {
$this->output->writeln("Modifier {$case->value} already set in $cim11Disease");
continue;
if (!$this->forceModifiers) {
$this->output->writeln("Modifier {$case->value} already set in $cim11Disease");
continue;
}
$cim11Disease->removeModifier($case);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Delete replaced modifiers on the owning side

When --force-modifiers is used for a disease that already has this modifier type, this call only removes the old Cim11Modifier from Cim11::$modifiers, which is the inverse side of the OneToMany(mappedBy: 'cim11') association; the owning Cim11Modifier::$cim11 still points at the disease and there is no orphan removal, so flush keeps the old row and adds the new one. Reloading the disease then returns duplicate modifiers of the same type instead of replacing the existing values, so the force re-import does not actually clean up stale modifiers.

Useful? React with 👍 / 👎.

}

$modifier = new Cim11Modifier();
Expand All @@ -112,13 +131,7 @@ private function importDiseases(): void

$modifier->setMultiple('NotAllowed' !== $elem['allowMultipleValues']);

foreach ($elem['ids'] as $id) {
$value = $this->modifierValues[$id] ?? null;

if (!$value) {
$this->output->writeln("Value {$id} not found");
continue;
}
foreach ($this->resolveModifierValues($elem['ids']) as $value) {
$modifier->addValue($value);
}
$modifier->setCim11($cim11Disease);
Expand All @@ -136,6 +149,27 @@ private function importDiseases(): void
$this->em->flush();
}

private function buildModifierTree(): void
{
$file = "{$this->projectDir}/var/modifiers.csv";

$this->readCsv($file, ',', function (array $data) {
if (!$data['id']) {
return;
}

$this->modifierTree[$data['id']] = [
'title' => $data['title'],
'code' => $data['code'],
'parent_id' => $data['parent_id'],
];

if ($data['parent_id']) {
$this->modifierChildren[$data['parent_id']][] = $data['id'];
}
});
}

private function importModifiers(): void
{
$this->output->writeln('Importing modifiers...');
Expand All @@ -144,8 +178,6 @@ private function importModifiers(): void

$this->readCsv($file, ',', function (array $data) {
if (!$data['code']) {
$this->output->writeln("No code found, skipping {$data['title']}");

return;
}

Expand All @@ -159,6 +191,9 @@ private function importModifiers(): void
'code' => $data['code'],
]);
if ($existing) {
if (!$existing->getCategory()) {
$existing->setCategory($this->resolveCategory($data['parent_id']));
}
$this->modifierValues[$existing->getWhoId()] = $existing;
$this->output->writeln("{$data['title']} already exists");

Expand All @@ -171,6 +206,7 @@ private function importModifiers(): void
$value->setCode($data['code']);
$value->setName($data['title']);
$value->setSynonyms(explode(';', $data['synonyms']));
$value->setCategory($this->resolveCategory($data['parent_id']));
$value->setImportId($this->importId);

$this->em->persist($value);
Expand All @@ -183,6 +219,55 @@ private function importModifiers(): void
$this->output->writeln('All modifiers imported successfully...');
}

/**
* Returns the title of the nearest block ancestor (direct parent block).
* Used to group coded modifier values under a human-readable category.
*/
private function resolveCategory(?string $parentId): ?string
{
if (!$parentId || !isset($this->modifierTree[$parentId])) {
return null;
}

return $this->modifierTree[$parentId]['title'] ?: null;
}

/**
* Resolves a list of modifier IDs, expanding block entries (no code)
* into their coded leaf descendants recursively.
*
* @param list<string> $ids
* @return list<Cim11ModifierValue>
*/
private function resolveModifierValues(array $ids): array
{
$values = [];

foreach ($ids as $id) {
if (isset($this->modifierValues[$id])) {
$values[] = $this->modifierValues[$id];
continue;
}

// Block entry — expand to coded descendants
$this->collectLeafValues($id, $values);
}

return $values;
}

/** @param list<Cim11ModifierValue> $values */
private function collectLeafValues(string $id, array &$values): void
{
foreach ($this->modifierChildren[$id] ?? [] as $childId) {
if (isset($this->modifierValues[$childId])) {
$values[] = $this->modifierValues[$childId];
} else {
$this->collectLeafValues($childId, $values);
}
}
}

private function readCsv(string $fileName, string $separator, callable $callback, bool $convertToUtf8 = false): void
{
$row = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/Entity/Cim11.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ public function addModifier(Cim11Modifier $modifier): void
}
}

public function removeModifier(ModifierType $type): void
{
foreach ($this->modifiers as $modifier) {
if ($modifier->getType() === $type) {
$this->modifiers->removeElement($modifier);

return;
}
}
}

public function getCim10Code(): ?string
{
return $this->cim10Code;
Expand Down
14 changes: 14 additions & 0 deletions src/Entity/Cim11ModifierValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Cim11ModifierValue extends BaseEntity implements ImportableEntityInterface
#[ORM\Column(type: 'string', length: 32, unique: true)]
protected ?string $whoId = null;

#[Groups(['read'])]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
protected ?string $category = null;

#[ORM\Column(type: 'simple_array')]
protected array $synonyms = [];

Expand Down Expand Up @@ -90,6 +94,16 @@ public function setWhoId(?string $whoId): void
$this->whoId = $whoId;
}

public function getCategory(): ?string
{
return $this->category;
}

public function setCategory(?string $category): void
{
$this->category = $category;
}

public function getSynonyms(): array
{
return $this->synonyms;
Expand Down
Loading