if ($do_it) {
    $request->setLimit($count);
}
$mov = new CMovement();
$query = $request->makeSelect();
$list = $mov->_spec->ds->loadList($query);
if (!$do_it) {
    CAppUI::setMsg(count($list) . " doublons à traiter");
} else {
    foreach ($list as $_mvt) {
        $ids = explode(",", $_mvt["ids"]);
        sort($ids);
        // IMPORTANT, must use the first movement created as a reference
        $first = new CMovement();
        $first->load($ids[0]);
        $second = new CMovement();
        $second->load($ids[1]);
        $tag = CIdSante400::getMatch($second->_class, $second->getTagMovement(), null, $second->_id);
        if ($tag->_id) {
            $tag->tag = "trash_{$tag->tag}";
            $tag->last_update = CMbDT::dateTime();
            $tag->store();
        } else {
            CAppUI::setMsg("Aucun tag sur mouvement #{$second->_id}");
        }
        $msg = $first->merge(array($second->_id => $second));
        if ($msg) {
            CAppUI::setMsg($msg, UI_MSG_WARNING);
        } else {
            CAppUI::setMsg("Mouvements fusionnés");
        }
 /**
  * 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;
 }
        $diagramme['hospitalise']['chambre'] = $affectation->_ref_lit->_view;
        $diagramme['hospitalise']['affectation'] = $affectation->_id;
    }
}
if ($operation) {
    $operation->loadRefsFwd();
    $diagramme['bloc']['vue'] = $operation->_view;
    $diagramme['bloc']['id'] = $operation->_id;
    $diagramme['bloc']['salle'] = $operation->entree_salle;
    $diagramme['bloc']['sortieSalle'] = $operation->sortie_salle;
    $diagramme['bloc']['salleReveil'] = $operation->entree_reveil;
    $diagramme['bloc']['sortieSalleReveil'] = $operation->sortie_reveil_possible;
} else {
    $diagramme['bloc'] = null;
}
$movement = new CMovement();
$movement->sejour_id = $sejour_id;
/** @var CMovement[] $movements */
$movements = $movement->loadMatchingList();
foreach ($movements as $_movement) {
    $_movement->loadRefSejour();
    $_movement->loadRefAffectation();
    $_movement->_ref_affectation->loadView();
}
// Création du template
$smarty = new CSmartyDP();
$smarty->assign("sejour", $sejour);
$smarty->assign("operations", $operations);
$smarty->assign("affectations", $affectations);
$smarty->assign("diagramme", $diagramme);
$smarty->assign("movement", $movement);
 $_sejour->loadRefPatient();
 $_sejour->loadNDA($receiver->group_id);
 $_sejour->_ref_last_log->type = "create";
 if (CValue::get("only_pread") && $_sejour->_etat != "preadmission") {
     continue;
 }
 if (CValue::get("without_exchanges") && $_sejour->countExchanges() > 0) {
     continue;
 }
 if ($receiver instanceof CDestinataireHprim) {
     CAppUI::stepAjax("Le traitement pour ce destinataire n'est pas pris en charge", UI_MSG_ERROR);
 }
 if ($receiver instanceof CReceiverHL7v2) {
     $receiver->getInternationalizationCode("ITI31");
     $_sejour->_receiver = $receiver;
     $movement = new CMovement();
     $movement->sejour_id = $_sejour->_id;
     $movement->movement_type = CValue::get("movement_type");
     $movements = $movement->loadMatchingList();
     foreach ($movements as $_movement) {
         $code = $_movement->original_trigger_code;
         $iti31 = new CITI31DelegatedHandler();
         if (!$iti31->isMessageSupported("ITI31", "ADT", $code, $receiver)) {
             $errors++;
             CAppUI::stepAjax("Le destinataire ne prend pas en charge cet événement", UI_MSG_WARNING);
         }
         $_sejour->_ref_hl7_movement = $_movement;
         try {
             // Envoi de l'événement
             $iti31->sendITI("PAM", "ITI31", "ADT", $code, $_sejour);
             $exchange++;
 /**
  * Update the form (derived) fields plain fields
  *
  * @return void
  */
 function updateFormFields()
 {
     parent::updateFormFields();
     $this->_view = $this->libelle ? $this->libelle : $this->nom;
     $this->_type_echange = $this->_class;
     $this->_self_tag = $this->getTag($this->group_id);
     $this->_tag_patient = CPatient::getTagIPP($this->group_id);
     $this->_tag_sejour = CSejour::getTagNDA($this->group_id);
     $this->_tag_consultation = CConsultation::getObjectTag($this->group_id);
     $this->_tag_mediuser = CMediusers::getObjectTag($this->group_id);
     $this->_tag_service = CService::getObjectTag($this->group_id);
     $this->_tag_chambre = CChambre::getObjectTag($this->group_id);
     $this->_tag_lit = C**t::getObjectTag($this->group_id);
     $this->_tag_movement = CMovement::getObjectTag($this->group_id);
     $this->_tag_visit_number = CSmp::getObjectTag($this->group_id);
 }