$sejour->loadLiaisonsForPrestation($prestation_id, $date);
        }
    }
    otherOrder($mouvementsNP, "np", $order_col, $order_way);
    $update_count = count($mouvements) . "/" . count($mouvementsNP);
    $mouvements_by_service = array();
    $mouvementsNP_by_service = array();
    $update_count = count($mouvements) . "/" . count($mouvementsNP);
    foreach ($mouvements as $_mouvement) {
        if (!isset($mouvements_by_service[$_mouvement->service_id])) {
            $mouvements_by_service[$_mouvement->service_id] = array();
        }
        $mouvements_by_service[$_mouvement->service_id][] = $_mouvement;
    }
    $mouvements = $mouvements_by_service;
    CSejour::massLoadCurrAffectation($mouvementsNP, "{$date} 00:00:00");
    foreach ($mouvementsNP as $_mouvement) {
        $_service_id = $_mouvement->service_id;
        if ($_mouvement->_ref_curr_affectation->service_id && !$_mouvement->_ref_curr_affectation->lit_id) {
            $_service_id = $_mouvement->_ref_curr_affectation->service_id;
        }
        if (!isset($mouvementsNP_by_service[$_service_id])) {
            $mouvementsNP_by_service[$_service_id] = array();
        }
        $mouvementsNP_by_service[$_service_id][] = $_mouvement;
    }
    $mouvementsNP = $mouvementsNP_by_service;
}
$list_mode_sortie = array();
if (CAppUI::conf("dPplanningOp CSejour use_custom_mode_sortie")) {
    $mode_sortie = new CModeSortieSejour();
        $where["sejour.sortie_reelle"] = "IS NULL";
        $where["sejour.annule"] = " = '0'";
        if (CAppUI::conf("dPurgences create_sejour_hospit")) {
            $where["rpu.mutation_sejour_id"] = "IS NULL";
        }
        break;
    default:
        $where["affectation.entree"] = "<= '{$datetime_max}'";
        $where["affectation.sortie"] = ">= '{$datetime_min}'";
        $where["affectation.service_id"] = " = '{$service_id}'";
}
$patients_offline = array();
/** @var CSejour[] $sejours */
$sejours = $sejour->loadList($where, null, null, "sejour.sejour_id", $ljoin);
CSejour::massLoadSurrAffectation($sejours, $datetime_avg);
CSejour::massLoadCurrAffectation($sejours, $datetime_avg, $service_id);
CSejour::massLoadNDA($sejours);
/** @var CPatient[] $patients */
$patients = CStoredObject::massLoadFwdRef($sejours, "patient_id");
CPatient::massLoadIPP($patients);
CStoredObject::massCountBackRefs($sejours, "operations");
// Recherche de transmissions // observations // consultations
$datetime_delta = CMbDT::date("-3 days", $datetime_avg);
$sejours_ids = CMbArray::pluck($sejours, "_id");
$where = array("sejour_id" => CSQLDataSource::prepareIn($sejours_ids));
// Transmissions
$whereTrans = $where;
$whereTrans["libelle_atc"] = "IS NOT NULL";
$whereTrans["date"] = "BETWEEN '{$datetime_delta}' AND '{$datetime_avg}'";
$transmission = new CTransmissionMedicale();
$transmissions = $transmission->loadList($whereTrans, "date");