Commit 80c3554a authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Fanalisation de la modif du validateur INSEE avec la mise à jour UnicaenApp

parent 4e155767
Loading
Loading
Loading
Loading
+0 −50
Original line number Diff line number Diff line
@@ -7,53 +7,3 @@
 * @var $viewName   string
 * @var $viewFile   string
 */

/** @var \Application\Service\IntervenantService $si */
$si = $container->get(\Application\Service\IntervenantService::class);


$is = $si->getEntityManager()->getConnection()->fetchAll('
SELECT 
  ID, ANNEE_ID, NOM_USUEL, PRENOM, NUMERO_INSEE, NUMERO_INSEE_CLE, DATE_NAISSANCE, CIVILITE_ID, PAYS_NAISSANCE_ID, DEP_NAISSANCE_ID , NUMERO_INSEE_PROVISOIRE
FROM 
  INTERVENANT 
WHERE 
  NUMERO_INSEE IS NOT NULL 
  AND HISTO_DESTRUCTION IS NULL 
--AND ID = 6391
');


$iv = new \Application\Validator\NumeroINSEEValidator(['provisoire' => false]);
$ov = new \Application\Validator\NumeroINSEEValidatorOld(['provisoire' => false]);

echo '<table class="table table-bordered">';
foreach ($is as $i) {
    $dn      = substr($i['DATE_NAISSANCE'], 0, 10);
    $dn      = \DateTime::createFromFormat('Y-m-d', $dn);
    $dn      = $dn->format(\Application\Constants::DATE_FORMAT);
    $context = [
        'civilite'             => $i['CIVILITE_ID'],
        'dateNaissance'        => $dn,
        'paysNaissance'        => $i['PAYS_NAISSANCE_ID'],
        'departementNaissance' => $i['DEP_NAISSANCE_ID'],
    ];
    $insee   = $i['NUMERO_INSEE'] . $i['NUMERO_INSEE_CLE'];
    $valid   = $iv->isValid($insee, $context);

    $ovValid = $ov->isValid($insee, $context);

    if ($valid != $ovValid) {
        echo '<tr>
<th>' . $i['ID'] . '</th>
<th>' . $i['ANNEE_ID'] . '</th>
<th>' . $i['NOM_USUEL'] . '</th>
<th>' . $i['PRENOM'] . '</th>
<td>' . ($i['NUMERO_INSEE_PROVISOIRE'] ? 'oui' : 'non') . '</td>
<td>' . $insee . '</td>
<td>' . ($valid ? 'oui' : 'non') . '</td>
<td>' . ($ovValid ? 'oui' : 'non') . '</td>
</tr>';
    }
}
echo '</table>';
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
    "require"     : {
        "php"                    : ">=7.3 || >=7.4",
        "mpdf/mpdf"              : "8.0.4 as 7.1.7",
        "unicaen/app"            : "3.1.1",
        "unicaen/app"            : "3.1.4",
        "unicaen/auth"           : "3.0.9",
        "zendframework/zend-code": "3.3.2",
        "unicaen/bjy-authorize"  : "3.0.4",
+131 −127

File changed.

Preview size limit exceeded, changes collapsed.

+4 −316
Original line number Diff line number Diff line
@@ -207,10 +207,10 @@ class NumeroINSEEValidator extends NumeroINSEE
        $iCodeDepartement = str_pad((string)$iDepartement, 3, '0', STR_PAD_LEFT);
        if ($this->departement->getCode() == $iCodeDepartement) return true;

        $ileDeFrance    = [78, 91, 92, 93, 94, 95];
        $ileDeFrance    = [75, 78, 91, 92, 93, 94, 95];
        $anneeNaissance = (int)$this->dateNaissance->format('Y');

        if ($anneeNaissance <= 1968 && in_array($iDepartement, $ileDeFrance) && 75 === (int)$this->departement->getCode()) {
        if ($anneeNaissance <= 1968 && in_array($iDepartement, $ileDeFrance)) {
            return true; // Pour les personnes nées en seine et oise, département disparu
        }

@@ -272,7 +272,7 @@ class NumeroINSEEValidator extends NumeroINSEE



    private function isValidDepartementHorsFrance($value)
    private function isValidDepartementHorsFrance()
    {
        if (!$this->getDepartement() === 99) {
            $this->error(self::MSG_DEPT);
@@ -303,315 +303,3 @@ class NumeroINSEEValidator extends NumeroINSEE
        return (int)$iDepartement;
    }
}
 No newline at end of file





class NumeroINSEEValidatorOld extends NumeroINSEE
{
    use DepartementServiceAwareTrait;
    use CiviliteServiceAwareTrait;
    use PaysServiceAwareTrait;

    const MSG_CIVILITE = 'msgCivilite';
    const MSG_ANNEE    = 'msgAnnee';
    const MSG_MOIS     = 'msgMois';
    const MSG_DEPT     = 'msgDepartement';

    /**
     * @var ?int
     */
    protected $algerieId;

    /**
     * @var ?int
     */
    protected $franceId;



    public function __construct($options = null)
    {
        $this->messageTemplates = array_merge($this->messageTemplates, [
            self::MSG_CIVILITE => "Le numéro n'est pas cohérent avec la civilité saisie",
            self::MSG_ANNEE    => "Le numéro n'est pas cohérent avec l'année de naissance saisi",
            self::MSG_MOIS     => "Le numéro n'est pas cohérent avec le mois de naissance saisi",
            self::MSG_DEPT     => "Le numéro n'est pas cohérent avec le pays et l'éventuel département de naissance saisi",
        ]);

        $this->franceId  = $this->getServicePays()->getIdByLibelle('FRANCE');
        $this->algerieId = $this->getServicePays()->getIdByLibelle('ALGERIE');

        parent::__construct($options);
    }



    public function isValid($value, $context = null)
    {
        if (!parent::isValid($value)) {
            return false;
        }

        if (!$this->isValidSexe($value, $context)) {
            return false;
        }
        if (!$this->isValidAnnee($value, $context)) {
            return false;
        }
        if (!$this->isValidMois($value, $context)) {
            return false;
        }
        if (!$this->isValidDepartement($value, $context)) {
            return false;
        }

        return true;
    }



    private function isValidSexe($value, $context)
    {
        if (empty($context['civilite'])) {
            return true;
        }

        $civilite = $this->getServiceCivilite()->get((int)$context['civilite']);

        if ($civilite->estUneFemme()) {
            $sexes = [2, 4]; // femme, personne étrangère de sexe féminin en cours d'immatriculation en France
        } else {
            $sexes = [1, 3]; // homme, personne étrangère de sexe masculin en cours d'immatriculation en France
        }

        $sexe = (int)substr($value, 0, 1);

        if (!in_array($sexe, $sexes)) {
            $this->error(self::MSG_CIVILITE);

            return false;
        }

        return true;
    }



    private function isValidAnnee($value, $context)
    {
        if (empty($context['dateNaissance'])) {
            return true;
        }

        $dateNaissance = $context['dateNaissance'];
        [, , $annee] = explode('/', $dateNaissance);

        if (substr($annee, -2) !== substr($value, 1, 2)) {
            $this->error(self::MSG_ANNEE);

            return false;
        }

        return true;
    }



    private function isValidMois($value, $context)
    {
        if (empty($context['dateNaissance'])) {
            return true;
        }

        $dateNaissance = $context['dateNaissance'];
        [, $mois,] = explode('/', $dateNaissance);

        $moisInsee = (int)substr($value, 3, 2);

        if ($this->hasCodeDepartementEtranger($value)) {
            if ($moisInsee == 20 || $moisInsee == 99 || ($moisInsee > 30 && $moisInsee < 42) || ($moisInsee > 50 && $moisInsee < 99)) {
                return true;
            }
        }

        if ((int)$mois !== $moisInsee) {
            $this->error(self::MSG_MOIS);

            return false;
        }

        return true;
    }



    private function isValidDepartement($value, $context)
    {
        if (empty($context['paysNaissance'])) {
            return true;
        }

        $paysNaissance  = (int)$context['paysNaissance'];
        $estNeEnFrance  = $paysNaissance === $this->franceId;
        $estNeEnAlgerie = $paysNaissance === $this->algerieId;

        if ($estNeEnFrance) {
            // on doit avoir un code département français valide
            if (!$this->isValidDepartementFrance($value, $context)) {
                return false;
            }
        } elseif ($estNeEnAlgerie) {
            // on doit avoir un code département français valide
            if (!$this->isValidDepartementAlgerie($value, $context)) {
                return false;
            }
        } else {
            // on doit avoir un code pays étranger valide
            if (!$this->isValidDepartementHorsFrance($value)) {
                return false;
            }
        }

        return true;
    }



    private function isValidDepartementHorsFrance($value)
    {
        if (!$this->hasCodeDepartementEtranger($value)) {
            $this->error(self::MSG_DEPT);

            return false;
        }

        return true;
    }



    private function isValidDepartementFrance($value, $context)
    {
        if (empty($context['departementNaissance'])) {
            return true;
        }

        // Si on trouve un code de département en métropole ou outre-mer valide,
        // on vérifie qu'il est cohérent avec le code du département de naissance saisi
        if (
            ($d = $this->getDepartementEnMetropoleValide($value))
            ||
            ($d = $this->getDepartementOutreMerValide($value))
        ) {
            /* @var $departementNaissance Departement */
            $departementNaissance = $this->getServiceDepartement()->get($context['departementNaissance'], true);
            if ($d !== $departementNaissance->getCode()) {
                $return        = false;
                $dateNaissance = \DateTime::createFromFormat(Constants::DATE_FORMAT, $context['dateNaissance']);
                if ($dateNaissance) {
                    $anneeNaissance = (int)$dateNaissance->format('Y');
                    if ($anneeNaissance <= 1968) {
                        if ($departementNaissance->inIleDeFrance() && $d === '075') {
                            $return = true;
                        }
                    }
                }
                if (!$return) {
                    $this->error(self::MSG_DEPT);

                    return false;
                }
            }
        } // Sinon, le code département n'est pas valide
        else {
            $this->error(self::MSG_DEPT);

            return false;
        }

        return true;
    }



    /**
     *
     * @param string $value
     *
     * @return int|string|null
     */
    private function getDepartementEnMetropoleValide($value)
    {
        $departement = substr($value, 5, 2);

        if (is_numeric($departement)) {
            $d = (int)$departement;
            if (1 <= $d && $d <= 95) {
                return '0' . (string)$departement;
            }
        } else {
            if (in_array($departement, ["2A", "2B"])) {
                return '0' . $departement;
            }
        }

        return null;
    }



    /**
     *
     * @param string $value
     *
     * @return int|null
     */
    private function getDepartementOutreMerValide($value)
    {
        $departement = substr($value, 5, 3);

        if (is_numeric($departement)) {
            $d = (int)$departement;
            if (970 <= $d && $d <= 989) {
                return $departement;
            }
        }

        return null;
    }



    private function isValidDepartementAlgerie($value, $context)
    {
        $departement = substr($value, 5, 2);

        if (is_numeric($departement)) {
            $d = (int)$departement;
            if (in_array($d, [91, 92, 93, 94, 99])) {
                return '0' . (string)$departement;
            }
        }

        return null;
    }



    /**
     * Teste si un numéro INSEE possède le code département de naissance associé à un pays étranger.
     *
     * @param string Numéro INSEE à tester
     *
     * @return bool
     */
    static public function hasCodeDepartementEtranger($value)
    {
        $departement = substr($value, 5, 2);

        // le code département doit être "99" pour un pays étranger
        return $departement === '99';
    }
}