Corrige les statuts et les codes internes

This commit is contained in:
Alexandre NOEL 2026-09-08 00:23:06 +02:00
commit a21f3f2bb4
7 changed files with 56 additions and 22 deletions

View file

@ -282,12 +282,7 @@ trait AnimalRecordActions
{
$year = date('Y');
$base = strtoupper(trim($name));
$base = preg_replace('/[^A-Z0-9]+/u', '_', $base);
$base = trim($base, '_');
if ($base === '') {
$base = 'ANIMAL';
}
$base = str_replace('-', '_', Ids::slugifyUpper($name));
$db = DB::pdo();
$stmt = $db->prepare('
@ -516,7 +511,22 @@ trait AnimalRecordActions
// Génère le code interne lorsqu'il est absent.
$internal_code = trim((string) ($_POST['internal_code'] ?? ''));
if ($internal_code === '') {
if ($internal_code === '' && $old && !empty($old['internal_code'])) {
$internal_code = (string) $old['internal_code'];
if (preg_match('/^(\d{4}-\d{3})-(.+)$/', $internal_code, $parts)) {
$legacyBase = strtoupper(trim($name));
$legacyBase = trim((string) preg_replace('/[^A-Z0-9]+/u', '_', $legacyBase), '_');
$normalizedBase = str_replace('-', '_', Ids::slugifyUpper($name));
if ($parts[2] === $legacyBase && $legacyBase !== $normalizedBase) {
$candidate = $parts[1] . '-' . $normalizedBase;
$duplicate = $db->prepare('SELECT 1 FROM animals WHERE internal_code=:code AND id<>:id');
$duplicate->execute([':code' => $candidate, ':id' => $id]);
if (!$duplicate->fetchColumn()) {
$internal_code = $candidate;
}
}
}
} elseif ($internal_code === '') {
$internal_code = self::nextInternalCode($name);
}