/**
  * Get XPN : extended composite name & ID for organizations
  *
  * @param CMbObject        $object   Object
  * @param CInteropReceiver $receiver Receiver
  *
  * @return array
  */
 function getXPN(CMbObject $object, CInteropReceiver $receiver = null)
 {
     $names = array();
     if ($object instanceof CPatient) {
         $anonyme = is_numeric($object->nom);
         $mode_identito_vigilance = "light";
         if ($receiver) {
             $mode_identito_vigilance = $receiver->_configs["mode_identito_vigilance"];
         }
         $nom = CPatient::applyModeIdentitoVigilance($object->nom, false, $mode_identito_vigilance, $anonyme);
         $prenom = CPatient::applyModeIdentitoVigilance($object->prenom, true, $mode_identito_vigilance, $anonyme);
         $prenom_2 = CPatient::applyModeIdentitoVigilance($object->prenom_2, true, $mode_identito_vigilance, $anonyme);
         $prenom_3 = CPatient::applyModeIdentitoVigilance($object->prenom_3, true, $mode_identito_vigilance, $anonyme);
         $prenom_4 = CPatient::applyModeIdentitoVigilance($object->prenom_4, true, $mode_identito_vigilance, $anonyme);
         $prenoms = array($prenom_2, $prenom_3, $prenom_4);
         CMbArray::removeValue("", $prenoms);
         // Nom usuel
         $patient_usualname = array($nom, $prenom, implode(",", $prenoms), null, $object->civilite, null, is_numeric($nom) ? "S" : "L", "A");
         $patient_birthname = array();
         // Cas nom de naissance
         if ($object->nom_jeune_fille) {
             $nom_jeune_fille = CPatient::applyModeIdentitoVigilance($object->nom_jeune_fille, true, $mode_identito_vigilance, $anonyme);
             $patient_birthname = $patient_usualname;
             $patient_birthname[0] = $nom_jeune_fille;
             // Legal Name devient Display Name
             $patient_usualname[6] = "D";
         }
         $names[] = $patient_usualname;
         if ($object->nom_jeune_fille && $receiver && $receiver->_configs["build_PID_6"] == "none") {
             $names[] = $patient_birthname;
         }
     }
     if ($object instanceof CCorrespondantPatient) {
         $names[] = array($object->nom, $object->prenom, null, null, null, null, is_numeric($object->nom) ? "S" : "L", "A");
     }
     return $names;
 }
 /**
  * Build PID segement
  *
  * @param CHL7v2Event $event Event
  *
  * @return null
  */
 function build(CHL7v2Event $event)
 {
     parent::build($event);
     $message = $event->message;
     $receiver = $event->_receiver;
     $group = $receiver->loadRefGroup();
     $patient = $this->patient;
     $mother = null;
     $sejour_maman = null;
     $naissance = null;
     if ($patient->_naissance_id) {
         $naissance = new CNaissance();
         $naissance->load($patient->_naissance_id);
         $sejour_maman = $naissance->loadRefSejourMaman();
         $sejour_maman->loadNDA($group->_id);
         $sejour_maman->loadRefPatient()->loadIPP($group->_id);
         $mother = $sejour_maman->_ref_patient;
     }
     $data = array();
     // PID-1: Set ID - PID (SI) (optional)
     $data[] = $this->set_id;
     // PID-2: Patient ID (CX) (optional)
     $data[] = null;
     // PID-3: Patient Identifier List (CX) (repeating)
     $data[] = $this->getPersonIdentifiers($patient, $group, $receiver);
     // PID-4: Alternate Patient ID - PID (CX) (optional repeating)
     $data[] = null;
     // PID-5: Patient Name (XPN) (repeating)
     $data[] = $this->getXPN($patient, $receiver);
     // PID-6: Mother's Maiden Name (XPN) (optional repeating)
     if ($patient->nom_jeune_fille && $receiver->_configs["build_PID_6"] == "nom_naissance") {
         $anonyme = is_numeric($patient->nom);
         $mode_identito_vigilance = $receiver->_configs["mode_identito_vigilance"];
         $prenom = CPatient::applyModeIdentitoVigilance($patient->prenom, true, $mode_identito_vigilance, $anonyme);
         $nom_jf = CPatient::applyModeIdentitoVigilance($patient->nom_jeune_fille, true, $mode_identito_vigilance, $anonyme);
         $data[] = array(array($nom_jf, $prenom, null, null, null, null, null, "L"));
     } else {
         // Dans le cas d'une naissance on va mettre l'identité de la mère
         $data[] = $mother ? $this->getXPN($mother, $receiver) : null;
     }
     // PID-7: Date/Time of Birth (TS) (optional)
     if ($patient->_naissance_id) {
         $data[] = $naissance->date_time;
     } else {
         if ($patient->naissance) {
             $data[] = CMbDT::isLunarDate($patient->naissance) ? null : $patient->naissance;
         } else {
             $data[] = null;
         }
     }
     // PID-8: Administrative Sex (IS) (optional)
     // Table - 0001
     // F - Female
     // M - Male
     // O - Other
     // U - Unknown
     // A - Ambiguous
     // N - Not applicable
     $sexe = CHL7v2TableEntry::mapTo("1", $patient->sexe);
     $data[] = $sexe ?: "U";
     // PID-9: Patient Alias (XPN) (optional repeating)
     $data[] = null;
     // PID-10: Race (CE) (optional repeating)
     $data[] = null;
     // PID-11: Patient Address (XAD) (optional repeating)
     $address = array();
     if ($patient->adresse || $patient->ville || $patient->cp) {
         $linesAdress = explode("\n", $patient->adresse, 2);
         $address[] = array(CValue::read($linesAdress, 0), str_replace("\n", $message->componentSeparator, CValue::read($linesAdress, 1)), $patient->ville, null, $patient->cp, CPaysInsee::getAlpha3($patient->pays_insee), "H");
     }
     if ($receiver->_configs["build_PID_11"] == "simple") {
         $address = array(reset($address));
     } else {
         if ($patient->lieu_naissance || $patient->cp_naissance || $patient->pays_naissance_insee) {
             $address[] = array(null, null, $patient->lieu_naissance, null, $patient->cp_naissance, CPaysInsee::getAlpha3($patient->pays_naissance_insee), "BDL");
         }
     }
     $data[] = $address;
     // PID-12: County Code (IS) (optional)
     $data[] = null;
     // PID-13: Phone Number - Home (XTN) (optional repeating)
     // Table - 0201
     // ASN - Answering Service Number
     // BPN - Beeper Number
     // EMR - Emergency Number
     // NET - Network (email) Address
     // ORN - Other Residence Number
     // PRN - Primary Residence Number
     // VHN - Vacation Home Number
     // WPN - Work Number
     // Table - 0202
     // BP       - Beeper
     // CP       - Cellular Phone
     // FX       - Fax
     // Internet - Internet Address: Use Only If Telecommunication Use Code Is NET
     // MD       - Modem
     // PH       - Telephone
     // TDD      - Telecommunications Device for the Deaf
     // TTY      - Teletypewriter
     $phones = array();
     if ($patient->tel) {
         $phones[] = $this->getXTN($receiver, $patient->tel, "PRN", "PH");
     }
     if ($patient->tel2) {
         // Pour le portable on met soit PRN ou ORN
         $phones[] = $this->getXTN($receiver, $patient->tel2, $receiver->_configs["build_cellular_phone"], "CP");
     }
     if ($patient->tel_autre) {
         $phones[] = $this->getXTN($receiver, $patient->tel_autre, $receiver->_configs["build_other_residence_number"], "PH");
     }
     if ($patient->email) {
         $phones[] = array(null, "NET", "Internet", $patient->email);
     }
     if ($receiver->_configs["build_PID_13"] === "simple") {
         $phones = array(reset($phones));
     }
     $data[] = $phones;
     // PID-14: Phone Number - Business (XTN) (optional repeating)
     $data[] = null;
     // PID-15: Primary Language (CE) (optional)
     $data[] = null;
     // PID-16: Marital Status (CE) (table 0002)(optional)
     $data[] = $patient->situation_famille;
     // PID-17: Religion (CE) (optional)
     $data[] = null;
     // PID-18: Patient Account Number (CX) (optional)
     if ($this->sejour && $receiver->_configs["build_NDA"] == "PID_18") {
         switch ($build_PID_18 = $receiver->_configs["build_PID_18"]) {
             case 'normal':
             case 'simple':
             case 'sejour_id':
                 $sejour = $this->sejour;
                 $sejour->loadNDA($group->_id);
                 $NDA = $sejour->_NDA;
                 if ($build_PID_18 == "sejour_id" && !$sejour->_NDA) {
                     $data[] = array(array($sejour->_id, null, null, $this->getAssigningAuthority("mediboard"), "RI"));
                     break;
                 }
                 if (!$sejour->_NDA && !CValue::read($receiver->_configs, "send_not_master_NDA")) {
                     $NDA = "===NDA_MISSING===";
                 }
                 if ($build_PID_18 == "simple") {
                     $data[] = $NDA;
                 } else {
                     // Même traitement que pour l'IPP
                     switch ($receiver->_configs["build_PID_3_4"]) {
                         case 'actor':
                             $assigning_authority = $this->getAssigningAuthority("actor", null, $receiver);
                             break;
                         case 'domain':
                             // Master domain
                             $group_domain = new CGroupDomain();
                             $group_domain->group_id = $group->_id;
                             $group_domain->master = 1;
                             $group_domain->object_class = "CSejour";
                             $group_domain->loadMatchingObject();
                             $domain = $group_domain->loadRefDomain();
                             $assigning_authority = $this->getAssigningAuthority("domain", null, null, $domain);
                             break;
                         default:
                             $assigning_authority = $this->getAssigningAuthority("FINESS", $group->finess);
                             break;
                     }
                     $data[] = $NDA ? array(array($NDA, null, null, $assigning_authority, "AN")) : null;
                 }
                 break;
             default:
                 $data[] = null;
         }
     } else {
         $data[] = null;
     }
     // PID-19: SSN Number - Patient (ST) (forbidden)
     switch ($receiver->_configs["build_PID_19"]) {
         case 'matricule':
             $data[] = $patient->matricule;
             break;
         default:
             $data[] = null;
             break;
     }
     // PID-20: Driver's License Number - Patient (DLN) (optional)
     $data[] = null;
     // PID-21: Mother's Identifier (CX) (optional repeating)
     // Même traitement que pour l'IPP
     switch ($receiver->_configs["build_PID_3_4"]) {
         case 'actor':
             $assigning_authority = $this->getAssigningAuthority("actor", null, $receiver);
             break;
         default:
             $assigning_authority = $this->getAssigningAuthority("FINESS", $group->finess);
             break;
     }
     if ($this->sejour) {
         $naissance = new CNaissance();
         $naissance->sejour_enfant_id = $this->sejour->_id;
         $naissance->loadMatchingObject();
         if ($naissance->_id) {
             $sejour_maman = $naissance->loadRefSejourMaman();
             $sejour_maman->loadNDA($group->_id);
             $sejour_maman->loadRefPatient()->loadIPP($group->_id);
             $mother = $sejour_maman->_ref_patient;
             $identifiers = array();
             if ($mother->_IPP) {
                 $identifiers[] = array($mother->_IPP, null, null, $assigning_authority, "PI");
             }
             if ($sejour_maman->_NDA) {
                 $identifiers[] = array($sejour_maman->_NDA, null, null, $assigning_authority, "AN");
             }
             $data[] = $identifiers;
         } else {
             $data[] = null;
         }
     } else {
         if ($mother) {
             $identifiers = array();
             if ($mother->_IPP) {
                 $identifiers[] = array($mother->_IPP, null, null, $assigning_authority, "PI");
             }
             if ($sejour_maman->_NDA) {
                 $identifiers[] = array($sejour_maman->_NDA, null, null, $assigning_authority, "AN");
             }
             $data[] = $identifiers;
         } else {
             $data[] = null;
         }
     }
     // PID-22: Ethnic Group (CE) (optional repeating)
     $data[] = null;
     // PID-23: Birth Place (ST) (optional)
     $data[] = null;
     // PID-24: Multiple Birth Indicator (ID) (optional)
     $data[] = null;
     // PID-25: Birth Order (NM) (optional)
     $data[] = $patient->rang_naissance;
     // PID-26: Citizenship (CE) (optional repeating)
     $data[] = null;
     // PID-27: Veterans Military Status (CE) (optional)
     $data[] = null;
     // PID-28: Nationality (CE) (optional)
     $data[] = null;
     // PID-29: Patient Death Date and Time (TS) (optional)
     $data[] = $patient->deces ? $patient->deces : null;
     // PID-30: Patient Death Indicator (ID) (optional)
     $data[] = $patient->deces ? "Y" : "N";
     // PID-31: Identity Unknown Indicator (ID) (optional)
     switch ($receiver->_configs["build_PID_31"]) {
         case 'avs':
             $data[] = $patient->avs;
             break;
         default:
             $data[] = null;
             break;
     }
     // PID-32: Identity Reliability Code (IS) (optional repeating)
     // Table - 0445
     // VIDE  - Identité non encore qualifiée
     // PROV  - Provisoire
     // VALI  - Validé
     // DOUB  - Doublon ou esclave
     // DESA  - Désactivé
     // DPOT  - Doublon potentiel
     // DOUA  - Doublon avéré
     // COLP  - Collision potentielle
     // COLV  - Collision validée
     // FILI  - Filiation
     // CACH  - Cachée
     // ANOM  - Anonyme
     // IDVER - Identité vérifiée par le patient
     // RECD  - Reçue d'un autre domaine
     // IDRA  - Identité rapprochée dans un autre domaine
     // USUR  - Usurpation
     // HOMD  - Homonyme detecté
     // HOMA  - Homonyme avéré
     if (CAppUI::conf("dPpatients CPatient manage_identity_status", $receiver->_ref_group)) {
         //todo voir pour DPOT
         $data[] = array($patient->status, $patient->vip ? "CACH" : null);
     } else {
         $data[] = array(is_numeric($patient->nom) ? "ANOM" : "VALI");
     }
     // PID-33: Last Update Date/Time (TS) (optional)
     $data[] = $event->last_log->date;
     // PID-34: Last Update Facility (HD) (optional)
     $data[] = null;
     // PID-35: Species Code (CE) (optional)
     $data[] = null;
     // PID-36: Breed Code (CE) (optional)
     $data[] = null;
     // PID-37: Strain (ST) (optional)
     $data[] = null;
     // PID-38: Production Class Code (CE) (optional)
     $data[] = null;
     // PID-39: Tribal Citizenship (CWE) (optional repeating)
     $data[] = null;
     $this->fill($data);
 }
 /**
  * Build IN1 segement
  *
  * @param CHL7v2Event $event Event
  *
  * @return null
  */
 function build(CHL7v2Event $event)
 {
     parent::build($event);
     $message = $event->message;
     $receiver = $event->_receiver;
     $group = $receiver->loadRefGroup();
     /** @var CPatient $patient */
     $patient = $this->patient;
     $data = array();
     // IN1-1: Insured's Employee ID
     $data[] = $this->set_id;
     // IN1-2: Insured's Social Security Number
     $data[] = $this->getIN12($patient);
     // IN1-3: Insurance Company ID (CX) (repeating)
     $data[] = array($patient->code_regime . $patient->caisse_gest . $patient->centre_gest);
     // IN1-4: Insurance Company Name (XON) (optional repeating)
     $data[] = null;
     // IN1-5: Insurance Company Address (XAD) (optional repeating)
     $data[] = null;
     // IN1-6: Insurance Co Contact Person (XPN) (optional repeating)
     $data[] = null;
     // IN1-7: Insurance Co Phone Number (XTN) (optional repeating)
     $data[] = null;
     // IN1-8: Group Number (ST) (optional)
     $data[] = null;
     // IN1-9: Group Name (XON) (optional repeating)
     $data[] = null;
     // IN1-10: Insured's Group Emp ID (CX) (optional repeating)
     $data[] = null;
     // IN1-11: Insured's Group Emp Name (XON) (optional repeating)
     $data[] = null;
     // IN1-12: Plan Effective Date (DT) (optional)
     $data[] = $patient->deb_amo;
     // IN1-13: Plan Expiration Date (DT) (optional)
     $data[] = $patient->fin_amo;
     // IN1-14: Authorization Information (AUI) (optional)
     $data[] = null;
     // IN1-15: Plan Type (IS) (optional)
     $data[] = null;
     // IN1-16: Name Of Insured (XPN) (optional repeating)
     $names = array();
     $mode_identito_vigilance = "light";
     if ($receiver) {
         $mode_identito_vigilance = $receiver->_configs["mode_identito_vigilance"];
     }
     $anonyme = is_numeric($patient->assure_nom);
     $nom = CPatient::applyModeIdentitoVigilance($patient->assure_nom, false, $mode_identito_vigilance);
     $prenom = CPatient::applyModeIdentitoVigilance($patient->assure_prenom, true, $mode_identito_vigilance, $anonyme);
     $prenom_2 = CPatient::applyModeIdentitoVigilance($patient->assure_prenom_2, true, $mode_identito_vigilance, $anonyme);
     $prenom_3 = CPatient::applyModeIdentitoVigilance($patient->assure_prenom_3, true, $mode_identito_vigilance, $anonyme);
     $prenom_4 = CPatient::applyModeIdentitoVigilance($patient->assure_prenom_4, true, $mode_identito_vigilance, $anonyme);
     $prenoms = array($prenom_2, $prenom_3, $prenom_4);
     CMbArray::removeValue("", $prenoms);
     // Nom usuel
     $assure_usualname = array($nom, $prenom, implode(",", $prenoms), null, $patient->assure_civilite, null, is_numeric($nom) ? "S" : "L", "A");
     $assure_birthname = array();
     // Cas nom de naissance
     if ($patient->assure_nom_jeune_fille) {
         $nom_jeune_fille = CPatient::applyModeIdentitoVigilance($patient->assure_nom_jeune_fille, true, $mode_identito_vigilance, $anonyme);
         $assure_birthname = $assure_usualname;
         $assure_birthname[0] = $nom_jeune_fille;
         // Legal Name devient Display Name
         $assure_usualname[6] = "D";
     }
     $names[] = $assure_usualname;
     if ($patient->assure_nom_jeune_fille && $receiver && $receiver->_configs["build_PID_6"] == "none") {
         $names[] = $assure_birthname;
     }
     $data[] = $names;
     // IN1-17: Insured's Relationship To Patient (CE) (optional)
     $data[] = $patient->qual_beneficiaire;
     // IN1-18: Insured's Date Of Birth (TS) (optional)
     $data[] = $patient->assure_naissance;
     // IN1-19: Insured's Address (XAD) (optional repeating)
     $address = array();
     $linesAdress = explode("\n", $patient->assure_adresse, 2);
     $address[] = array(CValue::read($linesAdress, 0), str_replace("\n", $message->componentSeparator, CValue::read($linesAdress, 1)), $patient->assure_ville, null, $patient->assure_cp, CPaysInsee::getAlpha3($patient->assure_pays_insee), "H");
     $data[] = $address;
     // IN1-20: Assignment Of Benefits (IS) (optional)
     $data[] = null;
     // IN1-21: Coordination Of Benefits (IS) (optional)
     $data[] = null;
     // IN1-22: Coord Of Ben. Priority (ST) (optional)
     $data[] = null;
     // IN1-23: Notice Of Admission Flag (ID) (optional)
     $data[] = null;
     // IN1-24: Notice Of Admission Date (DT) (optional)
     $data[] = null;
     // IN1-25: Report Of Eligibility Flag (ID) (optional)
     $data[] = null;
     // IN1-26: Report Of Eligibility Date (DT) (optional)
     $data[] = null;
     // IN1-27: Release Information Code (IS) (optional)
     $data[] = null;
     // IN1-28: Pre-Admit Cert (PAC) (ST) (optional)
     $data[] = null;
     // IN1-29: Verification Date/Time (TS) (optional)
     $data[] = null;
     // IN1-30: Verification By (XCN) (optional repeating)
     $data[] = null;
     // IN1-31: Type Of Agreement Code (IS) (optional)
     $data[] = null;
     // IN1-32: Billing Status (IS) (optional)
     $data[] = null;
     // IN1-33: Lifetime Reserve Days (NM) (optional)
     $data[] = null;
     // IN1-34: Delay Before L.R. Day (NM) (optional)
     $data[] = null;
     // IN1-35: Company Plan Code (IS) (optional)
     $data[] = $patient->code_gestion;
     // IN1-36: Policy Number (ST) (optional)
     $data[] = null;
     // IN1-37: Policy Deductible (CP) (optional)
     $data[] = null;
     // IN1-38: Policy Limit - Amount (CP) (optional)
     $data[] = null;
     // IN1-39: Policy Limit - Days (NM) (optional)
     $data[] = null;
     // IN1-40: Room Rate - Semi-Private (CP) (optional)
     $data[] = null;
     // IN1-41: Room Rate - Private (CP) (optional)
     $data[] = null;
     // IN1-42: Insured's Employment Status (CE) (optional)
     $data[] = null;
     // IN1-43: Insured's Administrative Sex (IS) (optional)
     $data[] = null;
     // IN1-44: Insured's Employer's Address (XAD) (optional repeating)
     $data[] = null;
     // IN1-45: Verification Status (ST) (optional)
     $data[] = null;
     // IN1-46: Prior Insurance Plan ID (IS) (optional)
     $data[] = null;
     // IN1-47: Coverage Type (IS) (optional)
     $data[] = null;
     // IN1-48: Handicap (IS) (optional)
     $data[] = null;
     // IN1-49: Insured's ID Number (CX) (optional repeating)
     $data[] = $patient->matricule;
     // IN1-50: Signature Code (IS) (optional)
     $data[] = null;
     // IN1-51: Signature Code Date (DT) (optional)
     $data[] = null;
     // IN1-52: Insured_s Birth Place (ST) (optional)
     $data[] = null;
     // IN1-53: VIP Indicator (IS) (optional)
     $data[] = null;
     $this->fill($data);
 }