<?php

/**
 * Lecture d'une ligne ANO (fichier .ano dans le OUT.ZIP)
 *
 *
 *  Historique des formats :
 *  - année   | n°format ANO  | n°format vidhosp
 * -   2025    |   015     |   015
 * -   2023    |   014     |   014
 * -   2022    |   015     |   014
 * * - 2020    |   014     |   013
 * * - 2019    |   013     |   013
 * * - 2018    |   012     |   012
 * * - 2017    |   009     |   010
 * * - 2016    |   009     |   010
 * * - 2015    |   008     |   010
 * * - 2013    |   005     |   008
 * * - 2012    |   004     |   006
 * * - 2011    |   003     |   005
 * * - 2010    |   002     |   005
 * * - 2009    |   002     |   005
 * * - 2008    |   001     |   004
 * * - 2007    |   null    |   null
 *
 * /
 */
function read_ano_line($buffer)
{
    $buffer = Encoding::removeBOM($buffer);

    $BUF_record["ok"] = false;
    $BUF_record["type_enregistrement"] = "";

    $versionFormat = getVersionFormat($buffer);

    // Attention en 2020, il y a 32 caractères devant
    $anoVersionsToTruncate = array('014', '015');
    if (in_array($versionFormat, $anoVersionsToTruncate)){
        $buffer = substr($buffer, 32);
    }

    $BUF_record = init_ano_buffer();


    // On utilise l'année avant le format 013
    switch ($versionFormat) {
        case '015':
            $BUF_record = read_ano_line_015($buffer, $BUF_record);
            break;
        case '014':
            $BUF_record = read_ano_line_014($buffer, $BUF_record);
            break;
        case '013':
            $BUF_record = read_ano_line_013($buffer, $BUF_record);
            break;
        case '2018':
            $BUF_record = read_ano_line_2018($buffer, $BUF_record);
            break;
        case '2017':
            $BUF_record = read_ano_line_2017($buffer, $BUF_record);
            break;
        case '2016':
            $BUF_record = read_ano_line_2016($buffer, $BUF_record);
            break;
        case '2015':
            $BUF_record = read_ano_line_2015($buffer, $BUF_record);
            break;
        default:
            logWarnMsg("Format ano" . $versionFormat . " non reconnu");
    }


    return $BUF_record;
}


function getVersionFormat($buffer)
{
    // Attention en 2020, il y a 32 caractères devant
    $BUF_num_format = trim(substr($buffer, 41, 3));
    $BUF_num_format_vidhosp = trim(substr($buffer, 44, 3));

    if ($BUF_num_format === '015' && $BUF_num_format_vidhosp === '015') {
        return '015';
    }

    if (($BUF_num_format === '014' && $BUF_num_format_vidhosp === '014')
        || ($BUF_num_format === '015' && $BUF_num_format_vidhosp === "014")
        || ($BUF_num_format === '015' && $BUF_num_format_vidhosp === "013")) {
        return '014';
    }

    if ($BUF_num_format === '013' && $BUF_num_format_vidhosp === '013') {
        return '013';
    }

    // Pour les anciens formats, on utilise l'année
    return trim(substr($buffer, 9, 3));

}

/**
 *  Depuis Mars 2025
 *  Format vidhosp ano 015 et 015
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_015($buffer, $BUF_record)
{
    $BUF_record = read_ano_line_014($buffer, $BUF_record);

    //Nouveautés
    $BUF_record = extractDmt($buffer, $BUF_record);

    return $BUF_record;
}


/**
 *  Format vidhosp ano 014 et 015
 *  Entre 2020 et 2024
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_014($buffer, $BUF_record)
{
    global $anohosp_offsets_014;


    $BUF_record["BUF_offsets"] = $anohosp_offsets_014;

    $BUF_record["finess"] = trim(substr($buffer, 0, 9));
    $BUF_record["no_format"] = trim(substr($buffer, 9, 3));
    $BUF_record["no_format_vidhosp"] = trim(substr($buffer, 12, 3));

    $BUF_record["crc_immatriculation_assure"] = trim(substr($buffer, 21, 1));
    $BUF_record["crc_date_naissance"] = trim(substr($buffer, 22, 1));
    $BUF_record["crc_sexe"] = trim(substr($buffer, 23, 1));
    $BUF_record["crc_no_sejour_administratif"] = trim(substr($buffer, 24, 1));
    $BUF_record["crc_fusion_anohosp_hosppmsi"] = trim(substr($buffer, 25, 1));
    $BUF_record["crc_fusion_anohosp_pmsi"] = trim(substr($buffer, 26, 1));
    $BUF_record["crc_date_reference"] = trim(substr($buffer, 27, 1));
    $BUF_record["crc_coherence_date_naissance"] = trim(substr($buffer, 28, 1));
    $BUF_record["crc_coherence_sexe"] = trim(substr($buffer, 29, 1));
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["date_naissance"] = trim(substr($buffer, 107, 8));
    $BUF_record["delai_date_entree"] = trim(substr($buffer, 124, 5)); // modif
    $BUF_record["no_rsa"] = trim(substr($buffer, 129, 10));    // no sequenciel dans le fichier PMSI
    $BUF_record["date_entree_sejour_pmsi"] = trim(substr($buffer, 139, 8));
    $BUF_record["date_sortie_sejour_pmsi"] = trim(substr($buffer, 147, 8));
    $BUF_record["mois_sortie"] = trim(substr($buffer, 149, 2));
    $BUF_record["annee_sortie"] = trim(substr($buffer, 151, 4));


    $BUF_record["crc_grand_regime"] = trim(substr($buffer, 155, 1));
    $BUF_record["crc_code_gestion"] = trim(substr($buffer, 156, 1));
    $BUF_record["crc_justificatif_exo"] = trim(substr($buffer, 157, 1));
    $BUF_record["crc_code_pec_fj"] = trim(substr($buffer, 158, 1));
    $BUF_record["crc_nature_assurance"] = trim(substr($buffer, 159, 1));
    $BUF_record["crc_type_contrat_oc"] = trim(substr($buffer, 160, 1));
    $BUF_record["crc_code_pec"] = trim(substr($buffer, 161, 1));    // sejour facturable AMO
    $BUF_record["crc_motif_non_facturation"] = trim(substr($buffer, 162, 1));
    $BUF_record["crc_facturation_18euros"] = trim(substr($buffer, 163, 1));
    $BUF_record["crc_nombre_venues"] = trim(substr($buffer, 164, 1));
    $BUF_record["crc_montant_tm"] = trim(substr($buffer, 165, 1));
    $BUF_record["crc_montant_fj"] = trim(substr($buffer, 166, 1));
    $BUF_record["crc_total_remboursable_amo"] = trim(substr($buffer, 167, 1));
    $BUF_record["crc_montant_parcours_soins"] = trim(substr($buffer, 168, 1));
    $BUF_record["crc_base_remboursement_c"] = trim(substr($buffer, 169, 1));
    $BUF_record["crc_taux_remboursement_c"] = trim(substr($buffer, 170, 1));
    $BUF_record["crc_cmu"] = trim(substr($buffer, 171, 1));
    $BUF_record["crc_no_anonyme_mere_enfant"] = trim(substr($buffer, 172, 1));    // no administratif de séjour de la mère
    $BUF_record["crc_hospit_nouveau_ne"] = trim(substr($buffer, 173, 1));
    $BUF_record["crc_prelevement_organe"] = trim(substr($buffer, 174, 1));
    $BUF_record["crc_date_hospitalisation"] = trim(substr($buffer, 175, 1));
    $BUF_record["crc_total_remboursable_amc"] = trim(substr($buffer, 176, 1));
    $BUF_record["crc_code_participation_assure"] = trim(substr($buffer, 177, 1));
    $BUF_record["crc_no_entree"] = trim(substr($buffer, 178, 1));
    $BUF_record["crc_rang_naissance"] = trim(substr($buffer, 179, 1));
    $BUF_record["crc_rang_beneficiaire"] = trim(substr($buffer, 180, 1));
    $BUF_record["crc_no_caisse_gestionnaire"] = trim(substr($buffer, 181, 1));
    $BUF_record["crc_no_centre_gestionnaire"] = trim(substr($buffer, 182, 1));
    $BUF_record["crc_confirmation_pec"] = trim(substr($buffer, 183, 1));
    $BUF_record["crc_no_at_date_adc"] = trim(substr($buffer, 184, 1));
    $BUF_record["crc_no_organisme_complementaire"] = trim(substr($buffer, 185, 1));
    $BUF_record["crc_finess"] = trim(substr($buffer, 186, 1));
    $BUF_record["crc_immatriculation_indiv"] = trim(substr($buffer, 187, 1));
    $BUF_record["crc_nature_piece_justificative"] = trim(substr($buffer, 188, 1));
    $BUF_record["crc_date_pec"] = trim(substr($buffer, 189, 1));
    $BUF_record["crc_date_debut_pec"] = trim(substr($buffer, 190, 1));
    $BUF_record["crc_delivre_par"] = trim(substr($buffer, 191, 1));
    $BUF_record["crc_regime_assure"] = trim(substr($buffer, 192, 1));
    $BUF_record["crc_top_eclatement_flux_etab"] = trim(substr($buffer, 193, 1));
    $BUF_record["crc_date_entree"] = trim(substr($buffer, 194, 1));
    $BUF_record["crc_date_sortie"] = trim(substr($buffer, 195, 1));
    $BUF_record["crc_montant_total_sejour_patient"] = trim(substr($buffer, 196, 1));
    $BUF_record["crc_rejet_amo"] = trim(substr($buffer, 197, 1));
    $BUF_record["crc_date_facture_amo"] = trim(substr($buffer, 198, 1));
    $BUF_record["crc_date_facture_amc"] = trim(substr($buffer, 199, 1));
    $BUF_record["crc_date_facture_patient"] = trim(substr($buffer, 200, 1));
    $BUF_record["crc_date_paiement_amo"] = trim(substr($buffer, 201, 1));
    $BUF_record["crc_date_paiement_amc"] = trim(substr($buffer, 202, 1));
    $BUF_record["crc_date_paiement_patient"] = trim(substr($buffer, 203, 1));
    $BUF_record["crc_statut_ft_amo"] = trim(substr($buffer, 204, 1));
    $BUF_record["crc_statut_ft_amc"] = trim(substr($buffer, 205, 1));
    $BUF_record["crc_statut_ft_patient"] = trim(substr($buffer, 206, 1));
    $BUF_record["crc_pays_amo"] = trim(substr($buffer, 207, 1));
    $BUF_record["crc_no_identification_patient"] = trim(substr($buffer, 208,1));
    $BUF_record["crc_identifiant_national_sante"] = trim(substr($buffer, 209, 1));
    $BUF_record["crc_article_51"] = trim(substr($buffer, 210, 1));

    $BUF_record["grand_regime"] = trim(substr($buffer, 241, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 243, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 245, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 246, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 247, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 249, 2)); // blanc ??
    $BUF_record["code_pec"] = trim(substr($buffer, 251, 1)); // Séjour facturable à l'assurance maladie
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 252, 1)); // blanc ??
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 253, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 254, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 258, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 268, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 278, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 288, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 292, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 302, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 307, 1));

    $BUF_record["no_chainage_mere_enfant"] = trim(substr($buffer, 340, 32));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 372, 1));

    $BUF_record["identifiant_anonyme_hospit"] = trim(substr($buffer, 374, 8));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 382, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 392, 1));
    $BUF_record["id_first_hospit"] = trim(substr($buffer, 393, 5));
    $BUF_record["crc_id_first_hospit"] = trim(substr($buffer, 398, 1));

    $BUF_record["rang_naissance"] = trim(substr($buffer, 402, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 403, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 406, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 409, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 413, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 414, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 423, 10));
    $BUF_record["nature_piece_justificative"] = trim(substr($buffer, 433, 1));
    $BUF_record["date_pec"] = trim(substr($buffer, 434, 8));
    $BUF_record["date_debut_pec"] = trim(substr($buffer, 442, 8));
    $BUF_record["delivre_par"] = trim(substr($buffer, 450, 3));
    $BUF_record["regime_assure"] = trim(substr($buffer, 453, 3));
    $BUF_record["top_eclatement_flux_etab"] = trim(substr($buffer, 456, 1));
    $BUF_record["date_entree"] = trim(substr($buffer, 457, 8)); // Date de début de la période de facturation
    $BUF_record["date_sortie"] = trim(substr($buffer, 465, 8)); // Date de fin de la période de facturation
    $BUF_record["montant_total_sejour_patient"] = trim(substr($buffer, 473, 8));
    $BUF_record["nb_rejet_amo"] = trim(substr($buffer, 481, 1));
    $BUF_record["date_facture_amo"] = trim(substr($buffer, 482, 8));
    $BUF_record["date_facture_amc"] = trim(substr($buffer, 490, 8));
    $BUF_record["date_facture_patient"] = trim(substr($buffer, 498, 8));
    $BUF_record["date_paiement_amo"] = trim(substr($buffer, 506, 8));
    $BUF_record["date_paiement_amc"] = trim(substr($buffer, 514, 8));
    $BUF_record["date_paiement_patient"] = trim(substr($buffer, 522, 8));
    $BUF_record["statut_ft_amo"] = trim(substr($buffer, 530, 1));
    $BUF_record["statut_ft_amc"] = trim(substr($buffer, 531, 1));
    $BUF_record["statut_ft_patient"] = trim(substr($buffer, 532, 1));
    $BUF_record["pays_amo"] = trim(substr($buffer, 533, 3));

    $BUF_record["ipp_anonymise"] = trim(substr($buffer, 536, 44)); // N° d'identification permanent du patient anonymisé
    $BUF_record["ins_anonymise"] = trim(substr($buffer, 580, 64)); //Identtifiant nationale de santé anonymisé


    $BUF_record["ok"] = true;

    return $BUF_record;
}

/**
 *  Format vidhosp ano 013
 *  Année 2019
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_013($buffer, $BUF_record)
{

    global $anohosp_offsets_2015;

    $BUF_record["BUF_offsets"] = $anohosp_offsets_2015;
    $BUF_record["finess"] = trim(substr($buffer, 0, 9));
    $BUF_record["no_format"] = trim(substr($buffer, 9, 3));
    $BUF_record["no_format_vidhosp"] = trim(substr($buffer, 12, 3));
    $BUF_record["mois_sortie"] = trim(substr($buffer, 15, 2));
    $BUF_record["annee_sortie"] = trim(substr($buffer, 17, 4));
    $BUF_record["crc_immatriculation_assure"] = trim(substr($buffer, 21, 1));
    $BUF_record["crc_date_naissance"] = trim(substr($buffer, 22, 1));
    $BUF_record["crc_sexe"] = trim(substr($buffer, 23, 1));
    $BUF_record["crc_no_sejour_administratif"] = trim(substr($buffer, 24, 1));
    $BUF_record["crc_fusion_anohosp_hosppmsi"] = trim(substr($buffer, 25, 1));
    $BUF_record["crc_fusion_anohosp_pmsi"] = trim(substr($buffer, 26, 1));
    $BUF_record["crc_date_reference"] = trim(substr($buffer, 27, 1));
    $BUF_record["crc_coherence_date_naissance"] = trim(substr($buffer, 28, 1));
    $BUF_record["crc_coherence_sexe"] = trim(substr($buffer, 29, 1));
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["no_anonyme_individuel"] = trim(substr($buffer, 47, 17));
    $BUF_record["no_sejour"] = trim(substr($buffer, 64, 5));
    $BUF_record["no_rsa"] = trim(substr($buffer, 69, 10));    // no sequenciel dans le fichier PMSI
    $BUF_record["date_entree_sejour_pmsi"] = trim(substr($buffer, 79, 8));
    $BUF_record["date_sortie_sejour_pmsi"] = trim(substr($buffer, 87, 8));
    $BUF_record["crc_grand_regime"] = trim(substr($buffer, 95, 1));
    $BUF_record["crc_code_gestion"] = trim(substr($buffer, 96, 1));
    $BUF_record["crc_justificatif_exo"] = trim(substr($buffer, 97, 1));
    $BUF_record["crc_code_pec_fj"] = trim(substr($buffer, 98, 1));
    $BUF_record["crc_nature_assurance"] = trim(substr($buffer, 99, 1));
    $BUF_record["crc_type_contrat_oc"] = trim(substr($buffer, 100, 1));
    $BUF_record["crc_code_pec"] = trim(substr($buffer, 101, 1));    // sejour facturable AMO
    $BUF_record["crc_motif_non_facturation"] = trim(substr($buffer, 102, 1));
    $BUF_record["crc_facturation_18euros"] = trim(substr($buffer, 103, 1));
    $BUF_record["crc_nombre_venues"] = trim(substr($buffer, 104, 1));
    $BUF_record["crc_montant_tm"] = trim(substr($buffer, 105, 1));
    $BUF_record["crc_montant_fj"] = trim(substr($buffer, 106, 1));
    $BUF_record["crc_total_remboursable_amo"] = trim(substr($buffer, 107, 1));
    $BUF_record["crc_montant_parcours_soins"] = trim(substr($buffer, 108, 1));
    $BUF_record["crc_base_remboursement_c"] = trim(substr($buffer, 109, 1));
    $BUF_record["crc_taux_remboursement_c"] = trim(substr($buffer, 110, 1));
    $BUF_record["crc_cmu"] = trim(substr($buffer, 111, 1));
    $BUF_record["crc_no_anonyme_mere_enfant"] = trim(substr($buffer, 112, 1));    // no administratif de séjour de la mère
    $BUF_record["crc_hospit_nouveau_ne"] = trim(substr($buffer, 113, 1));
    $BUF_record["crc_prelevement_organe"] = trim(substr($buffer, 114, 1));
    $BUF_record["crc_date_hospitalisation"] = trim(substr($buffer, 115, 1));
    $BUF_record["crc_total_remboursable_amc"] = trim(substr($buffer, 116, 1));
    $BUF_record["crc_code_participation_assure"] = trim(substr($buffer, 117, 1));
    $BUF_record["crc_no_entree"] = trim(substr($buffer, 118, 1));
    $BUF_record["crc_rang_naissance"] = trim(substr($buffer, 119, 1));
    $BUF_record["crc_rang_beneficiaire"] = trim(substr($buffer, 120, 1));
    $BUF_record["crc_no_caisse_gestionnaire"] = trim(substr($buffer, 121, 1));
    $BUF_record["crc_no_centre_gestionnaire"] = trim(substr($buffer, 122, 1));
    $BUF_record["crc_confirmation_pec"] = trim(substr($buffer, 123, 1));
    $BUF_record["crc_no_at_date_adc"] = trim(substr($buffer, 124, 1));
    $BUF_record["crc_no_organisme_complementaire"] = trim(substr($buffer, 125, 1));
    $BUF_record["crc_finess"] = trim(substr($buffer, 126, 1));
    $BUF_record["crc_immatriculation_indiv"] = trim(substr($buffer, 127, 1));
    $BUF_record["crc_nature_piece_justificative"] = trim(substr($buffer, 128, 1));
    $BUF_record["crc_date_pec"] = trim(substr($buffer, 129, 1));
    $BUF_record["crc_date_debut_pec"] = trim(substr($buffer, 130, 1));
    $BUF_record["crc_delivre_par"] = trim(substr($buffer, 131, 1));
    $BUF_record["crc_regime_assure"] = trim(substr($buffer, 132, 1));
    $BUF_record["crc_top_eclatement_flux_etab"] = trim(substr($buffer, 133, 1));
    $BUF_record["crc_date_entree"] = trim(substr($buffer, 134, 1));
    $BUF_record["crc_date_sortie"] = trim(substr($buffer, 135, 1));
    $BUF_record["crc_montant_total_sejour_patient"] = trim(substr($buffer, 136, 1));
    $BUF_record["crc_rejet_amo"] = trim(substr($buffer, 137, 1));
    $BUF_record["crc_date_facture_amo"] = trim(substr($buffer, 138, 1));
    $BUF_record["crc_date_facture_amc"] = trim(substr($buffer, 139, 1));
    $BUF_record["crc_date_facture_patient"] = trim(substr($buffer, 140, 1));
    $BUF_record["crc_date_paiement_amo"] = trim(substr($buffer, 141, 1));
    $BUF_record["crc_date_paiement_amc"] = trim(substr($buffer, 142, 1));
    $BUF_record["crc_date_paiement_patient"] = trim(substr($buffer, 143, 1));
    $BUF_record["crc_statut_ft_amo"] = trim(substr($buffer, 144, 1));
    $BUF_record["crc_statut_ft_amc"] = trim(substr($buffer, 145, 1));
    $BUF_record["crc_statut_ft_patient"] = trim(substr($buffer, 146, 1));
    $BUF_record["crc_pays_amo"] = trim(substr($buffer, 147, 1));
    $BUF_record["xxxxxxxxxxxx"] = trim(substr($buffer, 148, 2));

    $BUF_record["grand_regime"] = trim(substr($buffer, 149, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 151, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 153, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 154, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 155, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 157, 2));
    $BUF_record["code_pec"] = trim(substr($buffer, 159, 1));
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 160, 1));
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 161, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 162, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 166, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 176, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 186, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 196, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 200, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 210, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 215, 1));
    $BUF_record["no_anonyme_mere_enfant"] = trim(substr($buffer, 216, 32));
    $BUF_record["no_chainage_mere_enfant"] = trim(substr($buffer, 248, 32));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 280, 1));
    $BUF_record["prelevement_organe"] = trim(substr($buffer, 281, 1));
    $BUF_record["identifiant_anonyme_hospit"] = trim(substr($buffer, 282, 8));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 290, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 300, 1));
    $BUF_record["id_first_hospit"] = trim(substr($buffer, 301, 5));
    $BUF_record["crc_id_first_hospit"] = trim(substr($buffer, 306, 1));
    $BUF_record["rang_naissance"] = trim(substr($buffer, 310, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 311, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 314, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 317, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 321, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 322, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 331, 10));
    $BUF_record["nature_piece_justificative"] = trim(substr($buffer, 341, 1));
    $BUF_record["date_pec"] = trim(substr($buffer, 342, 8));
    $BUF_record["date_debut_pec"] = trim(substr($buffer, 350, 8));
    $BUF_record["delivre_par"] = trim(substr($buffer, 358, 3));
    $BUF_record["regime_assure"] = trim(substr($buffer, 361, 3));
    $BUF_record["top_eclatement_flux_etab"] = trim(substr($buffer, 364, 1));
    $BUF_record["date_entree"] = trim(substr($buffer, 365, 8));
    $BUF_record["date_sortie"] = trim(substr($buffer, 373, 8));
    $BUF_record["montant_total_sejour_patient"] = trim(substr($buffer, 381, 8));
    $BUF_record["nb_rejet_amo"] = trim(substr($buffer, 389, 1));
    $BUF_record["date_facture_amo"] = trim(substr($buffer, 390, 8));
    $BUF_record["date_facture_amc"] = trim(substr($buffer, 398, 8));
    $BUF_record["date_facture_patient"] = trim(substr($buffer, 406, 8));
    $BUF_record["date_paiement_amo"] = trim(substr($buffer, 414, 8));
    $BUF_record["date_paiement_amc"] = trim(substr($buffer, 422, 8));
    $BUF_record["date_paiement_patient"] = trim(substr($buffer, 430, 8));
    $BUF_record["statut_ft_amo"] = trim(substr($buffer, 438, 1));
    $BUF_record["statut_ft_amc"] = trim(substr($buffer, 439, 1));
    $BUF_record["statut_ft_patient"] = trim(substr($buffer, 440, 1));
    $BUF_record["pays_amo"] = trim(substr($buffer, 441, 3));

    $BUF_record["ok"] = true;

    return $BUF_record;
}

/**
 * Format vidhosp ano année 2018
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_2018($buffer, $BUF_record)
{
    global $anohosp_offsets_2015;

    $BUF_record["BUF_offsets"] = $anohosp_offsets_2015;
    $BUF_record["finess"] = trim(substr($buffer, 0, 9));
    $BUF_record["no_format"] = trim(substr($buffer, 9, 3));
    $BUF_record["no_format_vidhosp"] = trim(substr($buffer, 12, 3));
    $BUF_record["mois_sortie"] = trim(substr($buffer, 15, 2));
    $BUF_record["annee_sortie"] = trim(substr($buffer, 17, 4));
    $BUF_record["crc_immatriculation_assure"] = trim(substr($buffer, 21, 1));
    $BUF_record["crc_date_naissance"] = trim(substr($buffer, 22, 1));
    $BUF_record["crc_sexe"] = trim(substr($buffer, 23, 1));
    $BUF_record["crc_no_sejour_administratif"] = trim(substr($buffer, 24, 1));
    $BUF_record["crc_fusion_anohosp_hosppmsi"] = trim(substr($buffer, 25, 1));
    $BUF_record["crc_fusion_anohosp_pmsi"] = trim(substr($buffer, 26, 1));
    $BUF_record["crc_date_reference"] = trim(substr($buffer, 27, 1));
    $BUF_record["crc_coherence_date_naissance"] = trim(substr($buffer, 28, 1));
    $BUF_record["crc_coherence_sexe"] = trim(substr($buffer, 29, 1));
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["no_anonyme_individuel"] = trim(substr($buffer, 47, 17));
    $BUF_record["no_sejour"] = trim(substr($buffer, 64, 5));
    $BUF_record["no_rsa"] = trim(substr($buffer, 69, 10));    // no sequenciel dans le fichier PMSI
    $BUF_record["date_entree_sejour_pmsi"] = trim(substr($buffer, 79, 8));
    $BUF_record["date_sortie_sejour_pmsi"] = trim(substr($buffer, 87, 8));
    $BUF_record["crc_grand_regime"] = trim(substr($buffer, 95, 1));
    $BUF_record["crc_code_gestion"] = trim(substr($buffer, 96, 1));
    $BUF_record["crc_justificatif_exo"] = trim(substr($buffer, 97, 1));
    $BUF_record["crc_code_pec_fj"] = trim(substr($buffer, 98, 1));
    $BUF_record["crc_nature_assurance"] = trim(substr($buffer, 99, 1));
    $BUF_record["crc_type_contrat_oc"] = trim(substr($buffer, 100, 1));
    $BUF_record["crc_code_pec"] = trim(substr($buffer, 101, 1));    // sejour facturable AMO
    $BUF_record["crc_motif_non_facturation"] = trim(substr($buffer, 102, 1));
    $BUF_record["crc_facturation_18euros"] = trim(substr($buffer, 103, 1));
    $BUF_record["crc_nombre_venues"] = trim(substr($buffer, 104, 1));
    $BUF_record["crc_montant_tm"] = trim(substr($buffer, 105, 1));
    $BUF_record["crc_montant_fj"] = trim(substr($buffer, 106, 1));
    $BUF_record["crc_total_remboursable_amo"] = trim(substr($buffer, 107, 1));
    $BUF_record["crc_montant_parcours_soins"] = trim(substr($buffer, 108, 1));
    $BUF_record["crc_base_remboursement_c"] = trim(substr($buffer, 109, 1));
    $BUF_record["crc_taux_remboursement_c"] = trim(substr($buffer, 110, 1));
    $BUF_record["crc_cmu"] = trim(substr($buffer, 111, 1));
    $BUF_record["crc_no_anonyme_mere_enfant"] = trim(substr($buffer, 112, 1));    // no administratif de séjour de la mère
    $BUF_record["crc_hospit_nouveau_ne"] = trim(substr($buffer, 113, 1));
    $BUF_record["crc_prelevement_organe"] = trim(substr($buffer, 114, 1));
    $BUF_record["crc_date_hospitalisation"] = trim(substr($buffer, 115, 1));
    $BUF_record["crc_total_remboursable_amc"] = trim(substr($buffer, 116, 1));
    $BUF_record["crc_code_participation_assure"] = trim(substr($buffer, 117, 1));
    $BUF_record["crc_no_entree"] = trim(substr($buffer, 118, 1));
    $BUF_record["crc_rang_naissance"] = trim(substr($buffer, 119, 1));
    $BUF_record["crc_rang_beneficiaire"] = trim(substr($buffer, 120, 1));
    $BUF_record["crc_no_caisse_gestionnaire"] = trim(substr($buffer, 121, 1));
    $BUF_record["crc_no_centre_gestionnaire"] = trim(substr($buffer, 122, 1));
    $BUF_record["crc_confirmation_pec"] = trim(substr($buffer, 123, 1));
    $BUF_record["crc_no_at_date_adc"] = trim(substr($buffer, 124, 1));
    $BUF_record["crc_no_organisme_complementaire"] = trim(substr($buffer, 125, 1));
    $BUF_record["crc_finess"] = trim(substr($buffer, 126, 1));
    $BUF_record["crc_immatriculation_indiv"] = trim(substr($buffer, 127, 1));
    $BUF_record["crc_nature_piece_justificative"] = trim(substr($buffer, 128, 1));
    $BUF_record["crc_date_pec"] = trim(substr($buffer, 129, 1));
    $BUF_record["crc_date_debut_pec"] = trim(substr($buffer, 130, 1));
    $BUF_record["crc_delivre_par"] = trim(substr($buffer, 131, 1));
    $BUF_record["crc_regime_assure"] = trim(substr($buffer, 132, 1));
    $BUF_record["crc_top_eclatement_flux_etab"] = trim(substr($buffer, 133, 1));
    $BUF_record["crc_date_entree"] = trim(substr($buffer, 134, 1));
    $BUF_record["crc_date_sortie"] = trim(substr($buffer, 135, 1));
    $BUF_record["crc_montant_total_sejour_patient"] = trim(substr($buffer, 136, 1));
    $BUF_record["crc_rejet_amo"] = trim(substr($buffer, 137, 1));
    $BUF_record["crc_date_facture_amo"] = trim(substr($buffer, 138, 1));
    $BUF_record["crc_date_facture_amc"] = trim(substr($buffer, 139, 1));
    $BUF_record["crc_date_facture_patient"] = trim(substr($buffer, 140, 1));
    $BUF_record["crc_date_paiement_amo"] = trim(substr($buffer, 141, 1));
    $BUF_record["crc_date_paiement_amc"] = trim(substr($buffer, 142, 1));
    $BUF_record["crc_date_paiement_patient"] = trim(substr($buffer, 143, 1));
    $BUF_record["crc_statut_ft_amo"] = trim(substr($buffer, 144, 1));
    $BUF_record["crc_statut_ft_amc"] = trim(substr($buffer, 145, 1));
    $BUF_record["crc_statut_ft_patient"] = trim(substr($buffer, 146, 1));
    $BUF_record["crc_pays_amo"] = trim(substr($buffer, 147, 1));
    $BUF_record["grand_regime"] = trim(substr($buffer, 148, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 150, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 152, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 153, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 154, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 156, 2));
    $BUF_record["code_pec"] = trim(substr($buffer, 158, 1));
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 159, 1));
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 160, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 161, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 165, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 175, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 185, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 195, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 199, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 209, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 214, 1));
    $BUF_record["no_anonyme_mere_enfant"] = trim(substr($buffer, 215, 32));
    $BUF_record["no_chainage_mere_enfant"] = trim(substr($buffer, 247, 32));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 279, 1));
    $BUF_record["prelevement_organe"] = trim(substr($buffer, 280, 1));
    $BUF_record["identifiant_anonyme_hospit"] = trim(substr($buffer, 281, 8));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 289, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 299, 1));
    $BUF_record["id_first_hospit"] = trim(substr($buffer, 300, 5));
    $BUF_record["crc_id_first_hospit"] = trim(substr($buffer, 305, 1));
    $BUF_record["rang_naissance"] = trim(substr($buffer, 309, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 310, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 313, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 316, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 320, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 321, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 330, 10));
    $BUF_record["nature_piece_justificative"] = trim(substr($buffer, 340, 1));
    $BUF_record["date_pec"] = trim(substr($buffer, 341, 8));
    $BUF_record["date_debut_pec"] = trim(substr($buffer, 349, 8));
    $BUF_record["delivre_par"] = trim(substr($buffer, 357, 3));
    $BUF_record["regime_assure"] = trim(substr($buffer, 360, 3));
    $BUF_record["top_eclatement_flux_etab"] = trim(substr($buffer, 363, 1));
    $BUF_record["date_entree"] = trim(substr($buffer, 364, 8));
    $BUF_record["date_sortie"] = trim(substr($buffer, 372, 8));
    $BUF_record["montant_total_sejour_patient"] = trim(substr($buffer, 380, 8));
    $BUF_record["nb_rejet_amo"] = trim(substr($buffer, 388, 1));
    $BUF_record["date_facture_amo"] = trim(substr($buffer, 389, 8));
    $BUF_record["date_facture_amc"] = trim(substr($buffer, 397, 8));
    $BUF_record["date_facture_patient"] = trim(substr($buffer, 405, 8));
    $BUF_record["date_paiement_amo"] = trim(substr($buffer, 413, 8));
    $BUF_record["date_paiement_amc"] = trim(substr($buffer, 421, 8));
    $BUF_record["date_paiement_patient"] = trim(substr($buffer, 429, 8));
    $BUF_record["statut_ft_amo"] = trim(substr($buffer, 437, 1));
    $BUF_record["statut_ft_amc"] = trim(substr($buffer, 438, 1));
    $BUF_record["statut_ft_patient"] = trim(substr($buffer, 439, 1));
    $BUF_record["pays_amo"] = trim(substr($buffer, 440, 3));

    $BUF_record["ok"] = true;

    return $BUF_record;
}


/**
 * Format vidhosp année 2017
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_2017($buffer, $BUF_record)
{
    global $anohosp_offsets_2015;

    $BUF_record["BUF_offsets"] = $anohosp_offsets_2015;
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["no_anonyme_individuel"] = trim(substr($buffer, 47, 17));
    $BUF_record["no_rsa"] = trim(substr($buffer, 69, 10));
    $BUF_record["grand_regime"] = trim(substr($buffer, 136, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 138, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 140, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 141, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 142, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 144, 2));
    $BUF_record["code_pec"] = trim(substr($buffer, 146, 1));
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 147, 1));
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 148, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 149, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 153, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 163, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 173, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 183, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 187, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 197, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 202, 1));
    $BUF_record["no_anonyme_mere_enfant"] = trim(substr($buffer, 203, 32));
    $BUF_record["no_chainage_mere_enfant"] = trim(substr($buffer, 235, 32));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 267, 1));
    $BUF_record["prelevement_organe"] = trim(substr($buffer, 268, 1));
    $BUF_record["identifiant_anonyme_hospit"] = trim(substr($buffer, 269, 8));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 277, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 287, 1));
    $BUF_record["no_entree"] = trim(substr($buffer, 288, 9));
    $BUF_record["rang_naissance"] = trim(substr($buffer, 297, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 298, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 301, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 304, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 308, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 309, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 318, 10));
    $BUF_record["regime_assure"] = trim(substr($buffer, 348, 3));
    $BUF_record["date_entree"] = trim(substr($buffer, 352, 8));
    $BUF_record["date_sortie"] = trim(substr($buffer, 360, 8));

    $BUF_record["ok"] = true;

    return $BUF_record;
}

/**
 * Format vidhosp année 2016
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_2016($buffer, $BUF_record)
{
    global $anohosp_offsets_2015;

    $BUF_record["BUF_offsets"] = $anohosp_offsets_2015;
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["no_rsa"] = trim(substr($buffer, 52, 10));
    $BUF_record["grand_regime"] = trim(substr($buffer, 109, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 111, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 113, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 114, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 115, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 117, 2));
    $BUF_record["code_pec"] = trim(substr($buffer, 119, 1));
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 120, 1));
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 121, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 122, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 126, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 136, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 146, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 156, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 160, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 170, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 175, 1));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 208, 1));
    $BUF_record["prelevement_organe"] = trim(substr($buffer, 209, 1));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 218, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 228, 1));
    $BUF_record["no_entree"] = trim(substr($buffer, 229, 9));
    $BUF_record["rang_naissance"] = trim(substr($buffer, 238, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 239, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 242, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 245, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 249, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 250, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 259, 10));

    $BUF_record["ok"] = true;

    return $BUF_record;
}


/**
 * Format vidhosp année 2015
 *
 * @param $buffer
 * @param $BUF_record
 * @return array
 */
function read_ano_line_2015($buffer, $BUF_record)
{
    global $anohosp_offsets_2015;

    $BUF_record["BUF_offsets"] = $anohosp_offsets_2015;
    $BUF_record["no_anonyme"] = trim(substr($buffer, 30, 17));
    $BUF_record["no_rsa"] = trim(substr($buffer, 52, 10));
    $BUF_record["grand_regime"] = trim(substr($buffer, 109, 2));
    $BUF_record["code_gestion"] = trim(substr($buffer, 111, 2));
    $BUF_record["justificatif_exo"] = trim(substr($buffer, 113, 1));
    $BUF_record["code_pec_fj"] = trim(substr($buffer, 114, 1));
    $BUF_record["nature_assurance"] = trim(substr($buffer, 115, 2));
    $BUF_record["type_contrat_oc"] = trim(substr($buffer, 117, 2));
    $BUF_record["code_pec"] = trim(substr($buffer, 119, 1));
    $BUF_record["motif_non_facturation"] = trim(substr($buffer, 120, 1));
    $BUF_record["facturation_18euros"] = trim(substr($buffer, 121, 1));
    $BUF_record["nombre_venues"] = trim(substr($buffer, 122, 4));
    $BUF_record["montant_tm"] = trim(substr($buffer, 126, 10));
    $BUF_record["montant_fj"] = trim(substr($buffer, 136, 10));
    $BUF_record["total_remboursable_amo"] = trim(substr($buffer, 146, 10));
    $BUF_record["montant_parcours_soins"] = trim(substr($buffer, 156, 4));
    $BUF_record["base_remboursement_c"] = trim(substr($buffer, 160, 10));
    $BUF_record["taux_remboursement_c"] = trim(substr($buffer, 170, 5));
    $BUF_record["cmu"] = trim(substr($buffer, 175, 1));
    $BUF_record["hospit_nouveau_ne"] = trim(substr($buffer, 208, 1));
    $BUF_record["prelevement_organe"] = trim(substr($buffer, 209, 1));
    $BUF_record["total_remboursable_amc"] = trim(substr($buffer, 218, 10));
    $BUF_record["code_participation_assure"] = trim(substr($buffer, 228, 1));
    $BUF_record["no_entree"] = trim(substr($buffer, 229, 9));
    $BUF_record["rang_naissance"] = trim(substr($buffer, 238, 1));
    $BUF_record["rang_beneficiaire"] = trim(substr($buffer, 239, 3));
    $BUF_record["no_caisse_gestionnaire"] = trim(substr($buffer, 242, 3));
    $BUF_record["no_centre_gestionnaire"] = trim(substr($buffer, 245, 4));
    $BUF_record["confirmation_pec"] = trim(substr($buffer, 249, 1));
    $BUF_record["no_at_date_adc"] = trim(substr($buffer, 250, 9));
    $BUF_record["no_organisme_complementaire"] = trim(substr($buffer, 259, 10));

    $BUF_record["ok"] = true;

    return $BUF_record;
}

function init_ano_buffer()
{
    $BUF_record["type_enregistrement"] = "ANO";
    $BUF_record["BUF_offsets"] = null;
    $BUF_record["no_rss"] = 0;
    $BUF_record["immatriculation_assure"] = "";
    $BUF_record["cle_immatriculation_assure"] = "";
    $BUF_record["date_naissance"] = "20991231";
    $BUF_record["sexe"] = "";
    $BUF_record["no_sejour_administratif"] = "";
    $BUF_record["no_anonyme"] = "";
    $BUF_record["no_anonyme_individuel"] = "";
    $BUF_record["no_rsa"] = "";
    $BUF_record["grand_regime"] = "";
    $BUF_record["code_gestion"] = "";
    $BUF_record["justificatif_exo"] = "";
    $BUF_record["code_pec_fj"] = "";
    $BUF_record["nature_assurance"] = "";
    $BUF_record["type_contrat_oc"] = "";
    $BUF_record["code_pec"] = "";
    $BUF_record["motif_non_facturation"] = "";
    $BUF_record["facturation_18euros"] = "";
    $BUF_record["nombre_venues"] = "";
    $BUF_record["montant_tm"] = "";
    $BUF_record["montant_fj"] = "";
    $BUF_record["total_remboursable_amo"] = "";
    $BUF_record["montant_parcours_soins"] = "";
    $BUF_record["base_remboursement_c"] = "";
    $BUF_record["taux_remboursement_c"] = "";
    $BUF_record["cmu"] = "";
    $BUF_record["no_anonyme_mere_enfant"] = "";
    $BUF_record["no_chainage_mere_enfant"] = "";
    $BUF_record["hospit_nouveau_ne"] = "";
    $BUF_record["prelevement_organe"] = "";
    $BUF_record["identifiant_anonyme_hospit"] = "";
    $BUF_record["total_remboursable_amc"] = "";
    $BUF_record["code_participation_assure"] = "";
    $BUF_record["no_entree"] = "";
    $BUF_record["rang_naissance"] = "";
    $BUF_record["rang_beneficiaire"] = "";
    $BUF_record["no_caisse_gestionnaire"] = "";
    $BUF_record["no_centre_gestionnaire"] = "";
    $BUF_record["confirmation_pec"] = "";
    $BUF_record["no_at_date_adc"] = "";
    $BUF_record["no_organisme_complementaire"] = "";
    $BUF_record["regime_assure"] = "";
    $BUF_record["date_entree"] = "";
    $BUF_record["date_sortie"] = "";
    // nouveautés 2018
    $BUF_record["montant_total_sejour_patient"] = "";    // montant facturé au titre de la part patient
    $BUF_record["nb_rejet_amo"] = "";    // nombre de fois ou FT AMO a été refetée (0 à 9)
    $BUF_record["date_facture_amo"] = "";    // date de l'envoi de la FT AMO
    $BUF_record["date_facture_amc"] = "";    // date de l'envoi de la FT AMC
    $BUF_record["date_facture_patient"] = "";    // date de l'envoi de la FT patient
    $BUF_record["date_paiement_amo"] = "";    // date à laquelle la FT AMO est payée en totalité (statut S16)
    $BUF_record["date_paiement_amc"] = "";    // date à laquelle la FT AMC est payée en totalité (statut S16)
    $BUF_record["date_paiement_patient"] = "";    // date à laquelle la FT patient est payée en totalité (statut S16)
    $BUF_record["statut_ft_amo"] = "";    // 0:avant validation, 1:validée, 2:NiNi, 3:payée, 9:S/O
    $BUF_record["statut_ft_amc"] = "";    // 0:avant validation, 1:validée, 2:NiNi, 3:payée, 9:S/O
    $BUF_record["statut_ft_patient"] = "";    // 0:avant validation, 1:validée, 2:NiNi, 3:payée, 9:S/O
    $BUF_record["pays_amo"] = "";    // Code INSEE à 5 chiffres, sans les deux premiers chiffres 99 ou code ISO 3166-1 Alpha-3. Blanc ou 000 par défaut
    // Ajouts full VIDHOSP IC-5288
    $BUF_record["annee_sortie"] = "";
    $BUF_record["crc_article_51"] = "";
    $BUF_record["crc_base_remboursement_c"] = "";
    $BUF_record["crc_cmu"] = "";
    $BUF_record["crc_code_gestion"] = "";
    $BUF_record["crc_code_participation_assure"] = "";
    $BUF_record["crc_code_pec"] = "";
    $BUF_record["crc_code_pec_fj"] = "";
    $BUF_record["crc_coherence_date_naissance"] = "";
    $BUF_record["crc_coherence_sexe"] = "";
    $BUF_record["crc_confirmation_pec"] = "";
    $BUF_record["crc_date_debut_pec"] = "";
    $BUF_record["crc_date_entree"] = "";
    $BUF_record["crc_date_facture_amc"] = "";
    $BUF_record["crc_date_facture_amo"] = "";
    $BUF_record["crc_date_facture_patient"] = "";
    $BUF_record["crc_date_hospitalisation"] = "";
    $BUF_record["crc_date_naissance"] = "";
    $BUF_record["crc_date_paiement_amc"] = "";
    $BUF_record["crc_date_paiement_amo"] = "";
    $BUF_record["crc_date_paiement_patient"] = "";
    $BUF_record["crc_date_pec"] = "";
    $BUF_record["crc_date_reference"] = "";
    $BUF_record["crc_date_sortie"] = "";
    $BUF_record["crc_delivre_par"] = "";
    $BUF_record["crc_facturation_18euros"] = "";
    $BUF_record["crc_finess"] = "";
    $BUF_record["crc_fusion_anohosp_hosppmsi"] = "";
    $BUF_record["crc_fusion_anohosp_pmsi"] = "";
    $BUF_record["crc_identifiant_national_sante"] = "";
    $BUF_record["crc_grand_regime"] = "";
    $BUF_record["crc_hospit_nouveau_ne"] = "";
    $BUF_record["crc_id_first_hospit"] = "";
    $BUF_record["crc_immatriculation_assure"] = "";
    $BUF_record["crc_immatriculation_indiv"] = "";
    $BUF_record["crc_justificatif_exo"] = "";
    $BUF_record["crc_montant_fj"] = "";
    $BUF_record["crc_montant_parcours_soins"] = "";
    $BUF_record["crc_montant_tm"] = "";
    $BUF_record["crc_montant_total_sejour_patient"] = "";
    $BUF_record["crc_motif_non_facturation"] = "";
    $BUF_record["crc_nature_assurance"] = "";
    $BUF_record["crc_nature_piece_justificative"] = "";
    $BUF_record["crc_no_anonyme_mere_enfant"] = "";
    $BUF_record["crc_no_at_date_adc"] = "";
    $BUF_record["crc_no_caisse_gestionnaire"] = "";
    $BUF_record["crc_no_centre_gestionnaire"] = "";
    $BUF_record["crc_no_entree"] = "";
    $BUF_record["crc_no_identification_patient"] = "";
    $BUF_record["crc_no_organisme_complementaire"] = "";
    $BUF_record["crc_no_sejour_administratif"] = "";
    $BUF_record["crc_nombre_venues"] = "";
    $BUF_record["crc_pays_amo"] = "";
    $BUF_record["crc_prelevement_organe"] = "";
    $BUF_record["crc_rang_beneficiaire"] = "";
    $BUF_record["crc_rang_naissance"] = "";
    $BUF_record["crc_regime_assure"] = "";
    $BUF_record["crc_rejet_amo"] = "";
    $BUF_record["crc_sexe"] = "";
    $BUF_record["crc_statut_ft_amc"] = "";
    $BUF_record["crc_statut_ft_amo"] = "";
    $BUF_record["crc_statut_ft_patient"] = "";
    $BUF_record["crc_taux_remboursement_c"] = "";
    $BUF_record["crc_top_eclatement_flux_etab"] = "";
    $BUF_record["crc_total_remboursable_amc"] = "";
    $BUF_record["crc_total_remboursable_amo"] = "";
    $BUF_record["crc_type_contrat_oc"] = "";
    $BUF_record["date_debut_pec"] = "";
    $BUF_record["date_entree_sejour_pmsi"] = "";
    $BUF_record["date_pec"] = "";
    $BUF_record["date_sortie_sejour_pmsi"] = "";
    $BUF_record["delivre_par"] = "";
    $BUF_record["finess"] = "";
    $BUF_record["id_first_hospit"] = "";
    $BUF_record["mois_sortie"] = "";
    $BUF_record["nature_piece_justificative"] = "";
    $BUF_record["no_format"] = "";
    $BUF_record["no_format_vidhosp"] = "";
    $BUF_record["no_sejour"] = "";
    $BUF_record["top_eclatement_flux_etab"] = "";

    $BUF_record["ipp_anonymise"] = "";
    $BUF_record["ins_anonymise"] = "";

    // DMT depuis 2025
    $BUF_record["nb_dmt"] = 0;
    $BUF_record["crc_nb_dmt"] = "";

    $BUF_record["crc_dmt_code"] = array();
    $BUF_record["crc_dmt_mode_traitement"] = array();
    $BUF_record["crc_dmt_date_debut_sejour"] = array();
    $BUF_record["crc_dmt_date_fin_sejour"] = array();
    $BUF_record["crc_dmt_quantite"] = array();
    $BUF_record["crc_dmt_prix_unitaire"] = array();
    $BUF_record["crc_dmt_base_remboursement"] = array();
    $BUF_record["crc_dmt_taux_applicable_prestation"] = array();
    $BUF_record["crc_dmt_montant_remboursable"] = array();


     $BUF_record["dmt_code"] = array();
     $BUF_record["dmt_mode_traitement"] = array();
     $BUF_record["dmt_date_debut_sejour"] = array();
     $BUF_record["dmt_date_fin_sejour"] = array();
     $BUF_record["dmt_quantite"] = array();
     $BUF_record["dmt_prix_unitaire"] = array();
     $BUF_record["dmt_base_remboursement"] = array();
     $BUF_record["dmt_taux_applicable_prestation"] = array();
     $BUF_record["dmt_montant_remboursable"] = array();

     $BUF_record["crc_dmt_coherence_dmt_mt"] = array();
    return $BUF_record;
}


/**
 *  Disciplines de prestations (DMT) présent dans ano depuis 2025 (V015)
 */
function extractDmt($buffer, $BUF_record)
{

    $BUF_start = 1071;
    $nbDmt = (int)extractAndMoveBuffer($buffer, $BUF_start, 2); // Doit être >= 1
    $BUF_record["nb_dmt"] = $nbDmt;

    if ($nbDmt < 1) {
        logWarnMsg("Le nombre de DMT doit être supèrieur à 1: $nbDmt");
        return $BUF_record;
    }

    $BUF_record["crc_nb_dmt"] = extractAndMoveBuffer($buffer, $BUF_start, 1);

    // Code retour contrôle "Disciplines de prestations (DMT)"
    for ($i = 0; $i < $nbDmt; $i++) {
        $BUF_record["crc_dmt_code"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_mode_traitement"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_date_debut_sejour"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_date_fin_sejour"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_quantite"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_prix_unitaire"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_base_remboursement"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_taux_applicable_prestation"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
        $BUF_record["crc_dmt_montant_remboursable"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
    }

    // DMT
    for ($i = 0; $i < $nbDmt; $i++) {
        $BUF_record["dmt_code"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 3);
        $BUF_record["dmt_mode_traitement"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 2);
        $BUF_record["dmt_date_debut_sejour"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 8);
        $BUF_record["dmt_date_fin_sejour"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 8);
        $BUF_record["dmt_quantite"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 3); // Durée de séjour
        $BUF_record["dmt_prix_unitaire"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 7); // 5+2
        $BUF_record["dmt_base_remboursement"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 8); // 6+2
        $BUF_record["dmt_taux_applicable_prestation"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 3);
        $BUF_record["dmt_montant_remboursable"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 8);// 6+2
    }

    // Code retour contrôle "DMT n°NbDNT cohérence DMT/MT"
    for ($i = 0; $i < $nbDmt; $i++) {
        $BUF_record["crc_dmt_coherence_dmt_mt"][$i] = extractAndMoveBuffer($buffer, $BUF_start, 1);
    }

    return $BUF_record;
}

function extractAndMoveBuffer($buffer, &$BUF_start, $taille)
{
    $return = substr($buffer, $BUF_start, $taille);
    $BUF_start += $taille;
    return $return;
}