$firstName = CValue::get("firstName");
$naissance_day = CValue::get("naissance_day");
$naissance_month = CValue::get("naissance_month");
$naissance_year = CValue::get("naissance_year");
$useVitale = CValue::get("useVitale");
$covercard = CValue::get("covercard");
$callback = CValue::get("callback");
$modal = CValue::get("modal", 0);
$patient = new CPatient();
$patient->load($patient_id);
$patient->loadRefPhotoIdentite();
$patient->countDocItems();
$patient->loadRefsCorrespondantsPatient();
$patient->countINS();
// Chargement de l'ipp
$patient->loadIPP();
if (CModule::getActive("fse")) {
    $cv = CFseFactory::createCV();
    if ($cv) {
        $cv->loadIdVitale($patient);
    }
}
if (!$modal) {
    // Save history
    $params = array("patient_id" => $patient_id, "name" => $name, "firstName" => $firstName, "naissance_day" => $naissance_day, "naissance_month" => $naissance_month, "naissance_year" => $naissance_year);
    CViewHistory::save($patient, $patient_id ? CViewHistory::TYPE_EDIT : CViewHistory::TYPE_NEW, $params);
}
if (!$patient_id) {
    $patient->nom = $name;
    $patient->prenom = $firstName;
    $patient->assure_nom = $name;
 /**
  * @see parent::onBeforeMerge
  */
 function onBeforeMerge(CMbObject $mbObject)
 {
     if (!parent::onBeforeMerge($mbObject)) {
         return;
     }
     // Si pas en mode alternatif
     if (!CAppUI::conf("alternative_mode")) {
         throw new CMbException("no_alternative_mode");
     }
     $patient = $mbObject;
     $patient_elimine = new CPatient();
     $patient_elimine->load(reset($mbObject->_merging));
     // Si Client
     if (!CAppUI::conf('sip server')) {
         $mbObject->_fusion = array();
         foreach (CGroups::loadGroups() as $_group) {
             /** @var CInteropSender $sender */
             $sender = $mbObject->_eai_sender_guid ? CMbObject::loadFromGuid($mbObject->_eai_sender_guid) : null;
             if ($sender && $sender->group_id == $_group->_id) {
                 continue;
             }
             $patient->_IPP = null;
             $patient->loadIPP($_group->_id);
             $patient1_ipp = $patient->_IPP;
             $patient_elimine->_IPP = null;
             $patient_elimine->loadIPP($_group->_id);
             $patient2_ipp = $patient_elimine->_IPP;
             // Passage en trash des IPP des patients
             $tap_IPP = CPatient::getTagIPP($_group->_id);
             if (!$tap_IPP) {
                 continue;
             }
             $idexPatient = new CIdSante400();
             $idexPatient->tag = $tap_IPP;
             $idexPatient->object_class = "CPatient";
             $idexPatient->object_id = $patient->_id;
             $idexsPatient = $idexPatient->loadMatchingList();
             $idexPatientElimine = new CIdSante400();
             $idexPatientElimine->tag = $tap_IPP;
             $idexPatientElimine->object_class = "CPatient";
             $idexPatientElimine->object_id = $patient_elimine->_id;
             $idexsPatientElimine = $idexPatientElimine->loadMatchingList();
             $idexs = array_merge($idexsPatient, $idexsPatientElimine);
             $idexs_changed = array();
             if (count($idexs) > 1) {
                 foreach ($idexs as $_idex) {
                     // On continue pour ne pas mettre en trash l'IPP du patient que l'on garde
                     if ($_idex->id400 == $patient1_ipp) {
                         continue;
                     }
                     $old_tag = $_idex->tag;
                     $_idex->tag = CAppUI::conf('dPpatients CPatient tag_ipp_trash') . $tap_IPP;
                     $_idex->last_update = CMbDT::dateTime();
                     if (!($msg = $_idex->store())) {
                         if ($_idex->object_id == $patient_elimine->_id) {
                             $idexs_changed[$_idex->_id] = $old_tag;
                         }
                     }
                 }
             }
             if (!$patient1_ipp && !$patient2_ipp) {
                 continue;
             }
             $mbObject->_fusion[$_group->_id] = array("patientElimine" => $patient_elimine, "patient1_ipp" => $patient1_ipp, "patient2_ipp" => $patient2_ipp, "idexs_changed" => $idexs_changed);
         }
     }
     $this->sendFormatAction("onBeforeMerge", $mbObject);
 }
 /**
  * Get person identifiers
  *
  * @param CPatient      $patient Person
  * @param CGroups       $group   Group
  * @param CInteropActor $actor   Actor
  *
  * @return array
  */
 function getPersonIdentifiers(CPatient $patient, CGroups $group, CInteropActor $actor = null)
 {
     if (!$patient->_IPP) {
         $patient->loadIPP($group->_id);
     }
     $assigning_authority = $this->getAssigningAuthority("FINESS", $group->finess);
     if (CValue::read($actor->_configs, "build_PID_3_4") === "actor") {
         $assigning_authority = $this->getAssigningAuthority("actor", null, $actor);
     } elseif (CValue::read($actor->_configs, "build_PID_3_4") === "domain") {
         // Master domain
         $group_domain = new CGroupDomain();
         $group_domain->group_id = $group->_id;
         $group_domain->master = 1;
         $group_domain->object_class = "CPatient";
         $group_domain->loadMatchingObject();
         $domain = $group_domain->loadRefDomain();
         $assigning_authority = $this->getAssigningAuthority("domain", null, null, $domain);
     }
     $IPP = $patient->_IPP;
     if (!$patient->_IPP && !CValue::read($actor->_configs, "send_not_master_IPP")) {
         $IPP = "===IPP_MISSING===";
     }
     // Table - 0203
     // RI - Resource identifier
     // PI - Patient internal identifier
     // INS-C - Identifiant national de santé calculé
     $identifiers = array();
     if (CHL7v2Message::$build_mode == "simple") {
         if ($actor->_configs["send_own_identifier"]) {
             $identifiers[] = array($IPP, null, null, !$actor->_configs["send_assigning_authority"] ? null : $assigning_authority, "PI");
             $identifiers[] = array($patient->_id, null, null, !$actor->_configs["send_assigning_authority"] ? null : $this->getAssigningAuthority("mediboard"), "RI");
         } else {
             $identifiers[] = array(!$IPP ? 0 : $IPP);
         }
         return $identifiers;
     }
     if ($IPP) {
         $identifiers[] = array($IPP, null, null, empty($actor->_configs["send_assigning_authority"]) ? null : $assigning_authority, "PI");
     }
     // Ajout des identifiants des acteurs d'intégration
     $this->fillActorsIdentifiers($identifiers, $patient, $actor);
     // Ajout d'auutres identifiants
     $this->fillOtherIdentifiers($identifiers, $patient, $actor);
     return $identifiers;
 }
 /**
  * Recording IPP
  * 
  * @param CIdSante400    $IPP     Object id400
  * @param CPatient       $patient Patient
  * @param CInteropSender $sender  Sender
  * 
  * @return null|string null if successful otherwise returns and error message
  */
 static function storeIPP(CIdSante400 $IPP, CPatient $patient, CInteropSender $sender)
 {
     /* Gestion du numéroteur */
     $group = new CGroups();
     $group->load($sender->group_id);
     $group->loadConfigValues();
     // Purge de l'IPP existant sur le patient et on le remplace par le nouveau
     if ($sender->_configs && $sender->_configs["purge_idex_movements"]) {
         // On charge l'IPP courant du patient
         $patient->loadIPP($sender->group_id);
         $ref_IPP = $patient->_ref_IPP;
         if ($ref_IPP) {
             // Si l'IPP actuel est identique à celui qu'on reçoit on ne fait rien
             if ($ref_IPP->id400 == $IPP->id400) {
                 return;
             }
             // On passe l'IPP courant en trash
             $ref_IPP->tag = CAppUI::conf("dPpatients CPatient tag_ipp_trash") . $ref_IPP->tag;
             $ref_IPP->_eai_sender_guid = $sender->_guid;
             $ref_IPP->store();
             $patient->trashIPP($ref_IPP);
         }
         // On sauvegarde le nouveau
         $IPP->tag = $sender->_tag_patient;
         $IPP->object_class = "CPatient";
         $IPP->object_id = $patient->_id;
         $IPP->last_update = CMbDT::dateTime();
         $IPP->_eai_sender_guid = $sender->_guid;
         return $IPP->store();
     }
     // Génération de l'IPP ?
     /* @todo sip_idex_generator doit être remplacé par isIPPSupplier */
     if ($sender->_configs && !$group->_configs["sip_idex_generator"]) {
         if (!$IPP->id400) {
             return null;
         }
         if ($patient) {
             $IPP->object_id = $patient->_id;
         }
         $IPP->last_update = CMbDT::dateTime();
         $IPP->_eai_sender_guid = $sender->_guid;
         return $IPP->store();
     } else {
         $IPP_temp = CIdSante400::getMatch("CPatient", $sender->_tag_patient, null, $patient->_id);
         // Pas d'IPP passé
         if (!$IPP->id400) {
             if ($IPP_temp->_id) {
                 return null;
             }
             if (!CIncrementer::generateIdex($patient, $sender->_tag_patient, $sender->group_id)) {
                 return CAppUI::tr("CEAIPatient-error-generate-idex");
             }
             return null;
         } else {
             // Si j'ai déjà un identifiant
             if ($IPP_temp->_id) {
                 // On passe l'IPP courant en trash
                 $IPP_temp->tag = CAppUI::conf("dPpatients CPatient tag_ipp_trash") . $IPP_temp->tag;
                 $IPP_temp->_eai_sender_guid = $sender->_guid;
                 $IPP_temp->store();
             }
             $incrementer = $sender->loadRefGroup()->loadDomainSupplier("CPatient");
             if ($incrementer && $IPP->id400 < $incrementer->range_min || $IPP->id400 > $incrementer->range_max) {
                 return CAppUI::tr("CEAIPatient-idex-not-in-the-range");
             }
             $IPP->object_id = $patient->_id;
             $IPP->last_update = CMbDT::dateTime();
             $IPP->_eai_sender_guid = $sender->_guid;
             return $IPP->store();
         }
     }
 }