$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");
        }
    }
 /**
  * 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;
 }