/**
  * Create movement
  *
  * @param string       $code        HL7 event code
  * @param CSejour      $sejour      Admit
  * @param CAffectation $affectation Affectation
  *
  * @return CMovement|mixed
  */
 function createMovement($code, CSejour $sejour, CAffectation $affectation = null)
 {
     $insert = in_array($code, CHL7v2SegmentZBE::$actions["INSERT"]);
     $update = in_array($code, CHL7v2SegmentZBE::$actions["UPDATE"]);
     $cancel = in_array($code, CHL7v2SegmentZBE::$actions["CANCEL"]);
     $movement = new CMovement();
     // Initialise le mouvement
     $movement->sejour_id = $sejour->_id;
     $receiver = $sejour->_receiver;
     $configs = $receiver->_configs;
     $affectation_id = null;
     if ($affectation) {
         $current_log = $affectation->_ref_current_log;
         $first_affectation = $sejour->loadRefFirstAffectation();
         /** @var CService $service */
         $service = $affectation->loadRefService();
         // Si le service est d'UHCD, de radiologie, d'urgence ou
         // Dans le cas où il s'agit de la première affectation du séjour et qu'on est en type "création" on ne recherche pas
         // un mouvement avec l'affectation, mais on va prendre le mouvement d'admission
         if ($service->uhcd || $service->radiologie || $service->urgence || $current_log && $current_log->type == "create" && $first_affectation && $first_affectation->_id == $affectation->_id) {
             switch ($configs["send_first_affectation"]) {
                 case 'Z99':
                     $affectation_id = $affectation->_id;
                     $affectation = null;
                     break;
                 default:
                     $movement->affectation_id = $affectation->_id;
             }
         } else {
             $movement->affectation_id = $affectation->_id;
         }
     }
     if ($insert) {
         // Dans le cas d'un insert le type correspond nécessairement au type actuel du séjour
         $movement->movement_type = $sejour->getMovementType($code);
         $movement->original_trigger_code = $code;
         $movement->start_of_movement = $this->getStartOfMovement($code, $sejour, $affectation);
         $movement->loadMatchingObject();
         $movement->store();
         return $sejour->_ref_hl7_movement = $movement;
     } elseif ($update) {
         // Dans le cas d'un update le type correspond à celui du trigger
         $movement_type = null;
         // Mise à jour entrée réelle
         if ($sejour->fieldModified("entree_reelle")) {
             $movement_type = "ADMI";
         }
         // Mise à jour sortie réelle
         if ($sejour->fieldModified("sortie_reelle")) {
             $movement_type = "SORT";
         }
         $movement->movement_type = $movement_type;
         // On ne recherche pas parmi les mouvements annulés
         $movement->cancel = 0;
     }
     $order = "affectation_id DESC";
     $movements = $movement->loadMatchingList($order);
     if (!empty($movements)) {
         $movement = reset($movements);
     }
     if ($update) {
         if ($movement->original_trigger_code == "A02") {
             if (!$affectation) {
                 $affectation = new CAffectation();
             }
             $affectation->load($movement->affectation_id);
         }
         $movement->start_of_movement = $this->getStartOfMovement($movement->original_trigger_code, $sejour, $affectation, $movement);
     }
     // on annule un mouvement sauf dans le cas d'une annulation de mutation et que
     if ($cancel && !($code == "A12" && $movement->original_trigger_code != "A02")) {
         $movement->cancel = 1;
     }
     if ($affectation_id) {
         $movement->affectation_id = $affectation_id;
     }
     $movement->store();
     return $sejour->_ref_hl7_movement = $movement;
 }
 /**
  * Récupération du segment ZBE
  *
  * @param DOMNode   $node     Node
  * @param CSejour   $newVenue Admit
  * @param CMovement $movement Movement
  *
  * @return CMovement|string|null
  */
 function getZBE(DOMNode $node, CSejour $newVenue, CMovement $movement)
 {
     $sender = $this->_ref_sender;
     $idex_create = false;
     $event_code = $this->_ref_exchange_hl7v2->code;
     $own_movement = null;
     $sender_movement = null;
     foreach ($this->queryNodes("ZBE.1", $node) as $ZBE_1) {
         $EI_1 = $this->queryTextNode("EI.1", $ZBE_1);
         $EI_2 = $this->queryTextNode("EI.2", $ZBE_1);
         $EI_3 = $this->queryTextNode("EI.3", $ZBE_1);
         // Notre propre identifiant de mouvement
         if ($EI_2 == CAppUI::conf("hl7 assigning_authority_namespace_id") || $EI_3 == CAppUI::conf("hl7 assigning_authority_universal_id")) {
             $own_movement = $EI_1;
             break;
         }
         // L'identifiant de mouvement du sender
         if ($EI_3 == $sender->_configs["assigning_authority_universal_id"] || $EI_2 == $sender->_configs["assigning_authority_universal_id"]) {
             $sender_movement = $EI_1;
             continue;
         }
     }
     if (!$own_movement && !$sender_movement) {
         return "Impossible d'identifier le mouvement";
     }
     $movement_id = $own_movement ? $own_movement : $sender_movement;
     if (!$movement_id) {
         return null;
     }
     $start_movement_dt = $this->queryTextNode("ZBE.2/TS.1", $node);
     $action = $this->queryTextNode("ZBE.4", $node);
     $original_trigger = $this->queryTextNode("ZBE.6", $node);
     if (!$original_trigger) {
         $original_trigger = $event_code;
     }
     $movement->sejour_id = $newVenue->_id;
     $movement->original_trigger_code = $original_trigger;
     $movement->cancel = 0;
     $idexMovement = new CIdSante400();
     // Notre propre ID de mouvement
     if ($own_movement) {
         $movement_id_split = explode("-", $movement_id);
         $movement->movement_type = $movement_id_split[0];
         $movement->_id = $movement_id_split[1];
         $movement->loadMatchingObjectEsc();
         if (!$movement->_id) {
             return null;
         }
         if ($sender_movement) {
             $idexMovement = CIdSante400::getMatch("CMovement", $sender->_tag_movement, $sender_movement);
             if (!$idexMovement->_id) {
                 $idex_create = true;
             }
         }
     } else {
         $idexMovement = CIdSante400::getMatch("CMovement", $sender->_tag_movement, $movement_id);
         if ($idexMovement->_id) {
             $movement->load($idexMovement->object_id);
         } else {
             $idex_create = true;
             if ($event_code != "A02" && $event_code != "A21") {
                 $movement->cancel = 0;
                 $movement->loadMatchingObjectEsc();
             }
         }
         $movement->movement_type = $newVenue->getMovementType($original_trigger);
     }
     // Erreur dans le cas où le type du mouvement est UPDATE ou CANCEL et que l'on a pas retrouvé le mvt
     if (($action == "UPDATE" || $action == "CANCEL") && !$movement->_id) {
         return null;
     }
     if ($action == "CANCEL") {
         $movement->cancel = true;
     }
     $movement->start_of_movement = $start_movement_dt;
     $movement->last_update = CMbDT::dateTime();
     $movement->_eai_sender_guid = $sender->_guid;
     if ($msg = $movement->store()) {
         return $msg;
     }
     if ($idex_create) {
         $idexMovement->last_update = CMbDT::dateTime();
         $idexMovement->object_id = $movement->_id;
         $idexMovement->_eai_sender_guid = $sender->_guid;
         if ($msg = $idexMovement->store()) {
             return $msg;
         }
     }
     return $movement;
 }