$sejour->praticien_id = CValue::post("praticien_id"); if ($msg = $sejour->store()) { CAppUI::stepAjax($msg, UI_MSG_ERROR); } break; case "A02": $sejour_id = CValue::post("sejour_id"); $unique_lit_id = CValue::post("_unique_lit_id"); $sejour = new CSejour(); $sejour->load($sejour_id); $affectation = new CAffectation(); $affectation->sejour_id = $sejour_id; $affectation->lit_id = $unique_lit_id; $affectation->entree = $sejour->entree; $affectation->sortie = $sejour->sortie; if ($msg = $affectation->store()) { CAppUI::stepAjax($msg, UI_MSG_ERROR); } break; case "A03": $sejour_id = CValue::post("sejour_id"); $sejour = new CSejour(); $sejour->load($sejour_id); $sejour->sortie_reelle = CValue::post("sortie_reelle"); if ($msg = $sejour->store()) { CAppUI::stepAjax($msg, UI_MSG_ERROR); } break; case "A04": $sejour = new CSejour(); $sejour->sortie_prevue = CValue::post("sortie_prevue");
/** * @see parent::store() */ function store() { $this->completeField("sejour_id", "lit_id", "entree", "sortie"); $create_affectations = false; $sejour = $this->loadRefSejour(); $sejour->loadRefPatient(); // Conserver l'ancien objet avant d'enregistrer $old = new CAffectation(); if ($this->_id) { $old->load($this->_id); // Si ce n'est pas la première affectation de la série, alors la ref_prev et la ref_next sont erronées // si prises depuis l'affectation old if (isset($this->_is_prev) || isset($this->_is_next)) { $this->loadRefsAffectations(); $old->_ref_prev = $this->_ref_prev; $old->_ref_next = $this->_ref_next; } else { $old->loadRefsAffectations(); } } // Gestion du service_id if ($this->lit_id) { $this->service_id = $this->loadRefLit(false)->loadRefChambre(false)->service_id; } // Gestion des UFs $this->makeUF(); // Si c'est une création d'affectation, avec ni une précédente ni une suivante, // que le séjour est relié à une grossesse, et que le module maternité est actif, // alors il faut créer les affectations des bébés. if (CModule::getActive("maternite") && !is_numeric($sejour->_ref_patient->nom) && $sejour->grossesse_id && !$this->_id) { $this->loadRefsAffectations(); if (!$this->_ref_prev->_id && !$this->_ref_next->_id) { $create_affectations = true; } } $store_prestations = false; if ($this->lit_id && $this->sejour_id && (!$this->_id || $this->fieldModified("lit_id"))) { $store_prestations = true; } // Si on place le patient alors que le séjour a déjà une sortie réelle // alors on passe le flag effectue à 1 sur l'affectation if (!$this->_id && $sejour->sortie_reelle) { $this->effectue = 1; } // Enregistrement standard if ($msg = parent::store()) { return $msg; } // Niveaux de prestations réalisées à créer // pour une nouvelle affectation (par rapport aux niveaux de prestations du lit) if ($store_prestations) { $this->loadRefsAffectations(); $lit = $this->_ref_lit; $liaisons_lit = $lit->loadRefsLiaisonsItems(); CMbObject::massLoadFwdRef($liaisons_lit, "item_prestation_id"); $where = array(); $ljoin = array(); $where["sejour_id"] = "= '{$sejour->_id}'"; $where["item_prestation.object_class"] = "= 'CPrestationJournaliere'"; // On teste également le réalisé, si une affectation avait déjà été faite puis supprimée. $ljoin["item_prestation"] = "item_prestation.item_prestation_id = item_liaison.item_souhait_id\r\n OR item_prestation.item_prestation_id = item_liaison.item_realise_id"; $filter_entree = CMbDT::date($this->entree); foreach ($liaisons_lit as $_liaison) { $item_liaison = new CItemLiaison(); $_item = $_liaison->loadRefItemPrestation(); // Recherche d'une liaison : // - date de début si première affectation ou dans la même journée // - le jour suivant sinon, car il doit y avoir un passage d'une case pour le calcul des prestations $where["item_prestation.object_id"] = "= '{$_item->object_id}'"; $where["date"] = "= '" . $filter_entree . "'"; $item_liaison->loadObject($where, null, null, $ljoin); // Si existante, alors on affecte le réalisé au niveau de prestation du lit if ($item_liaison->_id) { $item_liaison->item_realise_id = $_liaison->item_prestation_id; if ($msg = $item_liaison->store()) { CAppUI::setMsg($msg, UI_MSG_ERROR); } } else { $item_liaison->sejour_id = $sejour->_id; $item_liaison->date = $filter_entree; $item_liaison->quantite = 0; $item_liaison->item_realise_id = $_liaison->item_prestation_id; // Recherche d'une précédente liaison pour appliquer l'item souhaité s'il existe $where["date"] = "<= '" . CMbDT::date($this->entree) . "'"; $ljoin["item_prestation"] = "item_prestation.item_prestation_id = item_liaison.item_souhait_id"; $_item_liaison_souhait = new CItemLiaison(); $_item_liaison_souhait->loadObject($where, "date DESC", null, $ljoin); if ($_item_liaison_souhait->_id) { $item_liaison->item_souhait_id = $_item_liaison_souhait->item_souhait_id; $item_liaison->sous_item_id = $_item_liaison_souhait->sous_item_id; } if ($msg = $item_liaison->store()) { CAppUI::setMsg($msg, UI_MSG_ERROR); } } // Dans tous les cas, il faut parcourir les liaisons existantes entre les dates de début et fin de l'affectation $where["date"] = "BETWEEN '" . $filter_entree . "' AND '" . CMbDT::date($this->sortie) . "'"; $ljoin["item_prestation"] = "item_prestation.item_prestation_id = item_liaison.item_souhait_id\r\n OR item_prestation.item_prestation_id = item_liaison.item_realise_id"; $liaisons_existantes = $item_liaison->loadList($where, null, null, null, $ljoin); foreach ($liaisons_existantes as $_liaison_existante) { $_liaison_existante->item_realise_id = $_liaison->item_prestation_id; if ($msg = $_liaison_existante->store()) { CAppUI::setMsg($msg, UI_MSG_ERROR); } } } } if ($create_affectations) { $grossesse = $this->_ref_sejour->loadRefGrossesse(); $naissances = $grossesse->loadRefsNaissances(); $sejours = CMbObject::massLoadFwdRef($naissances, "sejour_enfant_id"); foreach ($sejours as $_sejour) { $_affectation = new CAffectation(); $_affectation->lit_id = $this->lit_id; $_affectation->sejour_id = $_sejour->_id; $_affectation->parent_affectation_id = $this->_id; $_affectation->entree = CMbDT::dateTime(); $_affectation->sortie = $this->sortie; if ($msg = $_affectation->store()) { return $msg; } } } // Pas de problème de synchro pour les blocages de lits if (!$this->sejour_id || $this->_no_synchro) { return $msg; } // Modification de la date d'admission et de la durée de l'hospi $this->load($this->_id); if ($old->_id) { $this->_ref_prev = $old->_ref_prev; $this->_ref_next = $old->_ref_next; } else { $this->loadRefsAffectations(); } $changeSejour = 0; $changePrev = 0; $changeNext = 0; $prev = $this->_ref_prev; $next = $this->_ref_next; // Mise à jour vs l'entrée if (!$prev->_id) { if ($this->entree != $sejour->entree) { $field = $sejour->entree_reelle ? "entree_reelle" : "entree_prevue"; $sejour->{$field} = $this->entree; $changeSejour = 1; } } elseif ($this->entree != $prev->sortie) { $prev->sortie = $this->entree; $changePrev = 1; } // Mise à jour vs la sortie if (!$next->_id) { if ($this->sortie != $sejour->sortie) { $field = $sejour->sortie_reelle ? "sortie_reelle" : "sortie_prevue"; $sejour->{$field} = $this->sortie; $changeSejour = 1; } } elseif ($this->sortie != $next->entree) { $next->entree = $this->sortie; $changeNext = 1; } if ($changePrev) { $prev->_is_prev = 1; $prev->store(); } if ($changeNext) { $next->_is_next = 1; $next->store(); } if ($changeSejour) { $sejour->_no_synchro = 1; $sejour->updateFormFields(); if ($msg = $sejour->store()) { return $msg; } } return $msg; }
/** * Mapping et enregistrement de l'affectation * * @param CSejour $newVenue Admit * @param array $data Datas * @param CMovement $movement Movement * * @return CAffectation|string|null */ function mapAndStoreAffectation(CSejour $newVenue, $data, CMovement $movement = null) { $sender = $this->_ref_sender; if ($newVenue->annule) { return null; } $PV1_3 = $this->queryNode("PV1.3", $data["PV1"]); $affectation = new CAffectation(); $affectation->sejour_id = $newVenue->_id; $event_code = $this->_ref_exchange_hl7v2->code; // Récupération de la date de réalisation de l'évènement // Dans le cas spécifique de quelques évènements, on récupère le code sur le ZBE $datetime = $this->queryTextNode("EVN.6/TS.1", $data["EVN"]); if (array_key_exists("ZBE", $data) && $data["ZBE"] && CMbArray::in($event_code, array("A01", "A02", "A04", "A15", "Z80", "Z84"))) { $datetime = $this->queryTextNode("ZBE.2/TS.1", $data["ZBE"]); } switch ($event_code) { // Cas d'une suppression de mutation ou d'une permission d'absence case "A12": case "A52": // Quand on a un mouvement (provenant d'un ZBE) if (array_key_exists("ZBE", $data) && $data["ZBE"]) { if (!$movement) { return null; } $affectation->load($movement->affectation_id); if (!$affectation->_id) { return "Le mouvement '{$movement->_id}' n'est pas lié à une affectation dans Mediboard"; } } else { $affectation->entree = $datetime; $affectation->loadMatchingObject(); if (!$affectation->_id) { return null; } } // Pas de synchronisation $affectation->_no_synchro = true; if ($msgAffectation = $affectation->delete()) { return $msgAffectation; } return null; // Annulation admission // Annulation admission case "A11": if (!$movement) { return null; } $affectation = $newVenue->getCurrAffectation($datetime); // Si le mouvement n'a pas d'affectation associée, et que l'on a déjà une affectation dans MB if (!$movement->affectation_id && $affectation->_id) { return "Le mouvement '{$movement->_id}' n'est pas lié à une affectation dans Mediboard"; } // Si on a une affectation associée, alors on charge celle-ci if ($movement->affectation_id) { $affectation = $movement->loadRefAffectation(); } // Pas de synchronisation $affectation->_no_synchro = true; if ($msg = $affectation->delete()) { return $msg; } return null; // Annuler le retour du patient // Annuler le retour du patient case "A53": if (!$movement) { return null; } $affectation->load($movement->affectation_id); if (!$affectation->_id) { return "Le mouvement '{$movement->_id}' n'est pas lié à une affectation dans Mediboard"; } $affectation->effectue = 0; // Pas de synchronisation $affectation->_no_synchro = true; $affectation->_eai_sender_guid = $sender->_guid; if ($msg = $affectation->store()) { return $msg; } return $affectation; // Cas d'un départ pour une permission d'absence // Cas d'un départ pour une permission d'absence case "A21": $affectation->entree = $datetime; $affectation->loadMatchingObject(); // Si on ne retrouve pas une affectation // Création de l'affectation // et mettre à 'effectuee' la précédente si elle existe sinon création de celle-ci if (!$affectation->_id) { $service_externe = CService::loadServiceExterne($sender->group_id); if (!$service_externe->_id) { return "CService-externe-none"; } $affectation->service_id = $service_externe->_id; $return_affectation = $newVenue->forceAffectation($affectation, true); //$datetime, $affectation->lit_id, $affectation->service_id); if (is_string($return_affectation)) { return $return_affectation; } $affectation = $return_affectation; } return $affectation; // Cas d'un retour pour une permission d'absence // Cas d'un retour pour une permission d'absence case "A22": $service_externe = CService::loadServiceExterne($sender->group_id); if (!$service_externe->_id) { return "CService-externe-none"; } // Recherche de l'affectation correspondant à une permission d'absence $search = new CAffectation(); $where = array(); $where["sejour_id"] = "= '{$newVenue->_id}'"; $where["service_id"] = "= '{$service_externe->_id}'"; $where["effectue"] = "= '0'"; $where["entree"] = "<= '{$datetime}'"; $where["sortie"] = ">= '{$datetime}'"; $search->loadObject($where); // Si on ne la retrouve pas on prend la plus proche if (!$search->_id) { $where = array(); $where["sejour_id"] = "= '{$newVenue->_id}'"; $where["service_id"] = "= '{$service_externe->_id}'"; $where["effectue"] = "= '0'"; $search->loadObject($where); } $search->effectue = 1; $search->sortie = $datetime; $search->_eai_sender_guid = $sender->_guid; if ($msg = $search->store()) { return $msg; } return $search; // Cas mutation // Cas mutation case "A02": $affectation->entree = $datetime; $affectation->loadMatchingObject(); // Si on ne retrouve pas une affectation // Création de l'affectation // et mettre à 'effectuee' la précédente si elle existe sinon création de celle-ci if (!$affectation->_id) { // Récupération du Lit et UFs $this->getPL($PV1_3, $affectation, $newVenue); $return_affectation = $newVenue->forceAffectation($affectation, true); //$datetime, $affectation->lit_id, $affectation->service_id); if (is_string($return_affectation)) { return $return_affectation; } $affectation = $return_affectation; } break; // Cas modification // Cas modification case "Z99": if (!$movement) { return null; } // Si on a une affectation associée, alors on charge celle-ci if ($movement->affectation_id) { $affectation = $movement->loadRefAffectation(); } else { // On recherche l'affectation "courante" // Si qu'une affectation sur le séjour $newVenue->loadRefsAffectations(); if (count($newVenue->_ref_affectations) == 1) { $affectation = reset($newVenue->_ref_affectations); } else { // On recherche l'affectation "courante" $affectation = $newVenue->getCurrAffectation($datetime); } // Sinon on récupère et on met à jour la première affectation if (!$affectation->_id) { $affectation->sejour_id = $newVenue->_id; $affectation->entree = $newVenue->entree; $affectation->sortie = $newVenue->sortie; } } break; // Tous les autres cas on récupère et on met à jour la première affectation // Tous les autres cas on récupère et on met à jour la première affectation default: $newVenue->loadRefsAffectations(); $affectation = $newVenue->_ref_first_affectation; if (!$affectation->_id) { $affectation->sejour_id = $newVenue->_id; $affectation->entree = $newVenue->entree; $affectation->sortie = $newVenue->sortie; } } // Si pas d'UF/service/chambre/lit on retourne une affectation vide if (!$PV1_3) { if ($msgVenue = self::storeUFMedicaleSoinsSejour($data, $newVenue)) { return $msgVenue; } return $affectation; } if ($this->queryTextNode("PL.1", $PV1_3) == $sender->_configs["handle_PV1_3_null"]) { if ($msgVenue = self::storeUFMedicaleSoinsSejour($data, $newVenue)) { return $msgVenue; } return $affectation; } // Si pas de lit on affecte le service sur le séjour if (!$this->queryTextNode("PL.3", $PV1_3)) { $affectation_uf = new CAffectationUniteFonctionnelle(); // On essaye de récupérer le service dans ce cas depuis l'UF d'hébergement $date_deb = $affectation->_id ? CMbDT::date($affectation->sortie) : CMbDT::date($newVenue->sortie); $date_fin = $affectation->_id ? CMbDT::date($affectation->entree) : CMbDT::date($newVenue->entree); $uf = CUniteFonctionnelle::getUF($this->queryTextNode("PL.1", $PV1_3), "hebergement", $newVenue->group_id, $date_deb, $date_fin); if ($uf->code && $uf->_id) { $affectation_uf->uf_id = $uf->_id; $affectation_uf->object_class = "CService"; $affectation_uf->loadMatchingObject(); } // Dans le cas où l'on retrouve un service associé à l'UF d'hébergement if ($affectation_uf->_id) { $newVenue->service_id = $affectation_uf->object_id; $newVenue->uf_hebergement_id = $affectation_uf->uf_id; } $uf_med = $this->mappingUFMedicale($data, $newVenue, $affectation); $newVenue->uf_medicale_id = $uf_med ? $uf_med->_id : null; $uf_soins = $this->mappingUFSoins($data, $newVenue, $affectation); $newVenue->uf_soins_id = $uf_soins ? $uf_soins->_id : null; // On ne check pas la cohérence des dates des consults/intervs $newVenue->_skip_date_consistencies = true; $newVenue->_eai_sender_guid = $sender->_guid; if ($msgVenue = self::storeUFMedicaleSoinsSejour($data, $newVenue)) { return $msgVenue; } // Si on a pas d'UF on retourne une affectation vide if (!$uf->_id || !$affectation_uf->_id) { return $affectation; } } // Récupération du Lit et UFs $this->getPL($PV1_3, $affectation, $newVenue); $uf_med = $this->mappingUFMedicale($data, $newVenue, $affectation); $affectation->uf_medicale_id = $uf_med ? $uf_med->_id : null; $uf_soins = $this->mappingUFSoins($data, $newVenue, $affectation); $affectation->uf_soins_id = $uf_soins ? $uf_soins->_id : null; $affectation->_eai_sender_guid = $sender->_guid; if ($msg = $affectation->store()) { return $msg; } return $affectation; }
/** * Force la création d'une affectation en fonction de la tolérance(?) * * @param CAffectation $affectation Affectation concernée * @param bool $no_synchro No synchro * * @todo A détailler * @return CAffectation|null|string|void */ function forceAffectation(CAffectation $affectation, $no_synchro = false) { $datetime = $affectation->entree; $lit_id = $affectation->lit_id; $service_id = $affectation->service_id; $tolerance = CAppUI::conf("dPhospi CAffectation create_affectation_tolerance", CGroups::loadCurrent()); $splitting = new CAffectation(); $where["sejour_id"] = "= '{$this->_id}'"; $where["entree"] = "<= '{$datetime}'"; $where["sortie"] = ">= '{$datetime}'"; $splitting->loadObject($where); $create = new CAffectation(); // On retrouve une affectation a spliter if ($splitting->_id) { //on ne splite pas et on ne créé pas d'affectation si la tolérance n'est pas atteinte if (CMbDT::addDateTime("00:{$tolerance}:00", $splitting->entree) <= $affectation->entree || $affectation->_mutation_urg) { // Affecte la sortie de l'affectation a créer avec l'ancienne date de sortie $create->sortie = $splitting->sortie; $create->entree = $datetime; // On passe à effectuer la split $splitting->effectue = 1; $splitting->sortie = $datetime; $splitting->_no_synchro = $no_synchro; $splitting->_mutation_urg = $affectation->_mutation_urg; if ($msg = $splitting->store()) { return $msg; } } else { $create->affectation_id = $splitting->affectation_id; $create->sortie = $splitting->sortie; } } else { $create->entree = $datetime; $create->sortie = $this->sortie; } // Créé la nouvelle affectation $create->sejour_id = $this->_id; $create->lit_id = $lit_id; $create->service_id = $service_id; $create->_mutation_urg = $affectation->_mutation_urg; $create->_no_synchro = $no_synchro; if ($msg = $create->store()) { return $msg; } return $create; }