/** * Redéfinition du store * * @return void|string **/ function store() { $this->completeField("numero", "group_id"); if (!$this->group_id) { $this->group_id = CGroups::loadCurrent()->_id; } if ($this->_id && $this->_duplicate) { $this->_duplicate = null; if ($msg = $this->duplicate()) { return $msg; } $this->annule = 1; $this->definitive = 1; } if (!$this->cloture && $this->fieldModified("cloture") && count($this->_ref_reglements)) { return "Vous ne pouvez pas décloturer une facture ayant des règlements"; } if (!$this->cloture && $this->fieldModified("cloture") && count($this->_ref_relances)) { return "Vous ne pouvez pas décloturer une facture ayant des relances"; } $create_lignes = false; if (!$this->_id && CAppUI::conf("dPfacturation " . $this->_class . " use_auto_cloture")) { $this->cloture = CMbDT::date(); $create_lignes = true; } //Si on cloture la facture création des lignes de la facture //Si on décloture on les supprime if ($this->cloture && $this->fieldModified("cloture") && !$this->_old->cloture) { $create_lignes = true; } elseif (!$this->cloture && $this->fieldModified("cloture")) { //Suppression des tous les items de la facture $this->loadRefsItems(); foreach ($this->_ref_items as $item) { /** @var CFactureItem $item*/ $item->delete(); } $this->facture = 0; } // Etat des règlement à propager sur les consultations if ($this->fieldModified("patient_date_reglement") || $this->fieldModified("tiers_date_reglement")) { $this->loadRefsConsultation(); foreach ($this->_ref_consults as $_consultation) { $_consultation->patient_date_reglement = $this->patient_date_reglement; $_consultation->tiers_date_reglement = $this->tiers_date_reglement; if ($msg = $_consultation->store()) { return $msg; } } if ($this->isRelancable() && $this->_ref_last_relance->_id) { $this->_ref_last_relance->etat = $this->patient_date_reglement ? "regle" : "emise"; $this->_ref_last_relance->store(); } } $_object_id = null; $_object_class = null; //Lors de la validation de la cotation d'une consultation if ($this->_consult_id) { $consult = new CConsultation(); $consult->load($this->_consult_id); $consult->loadRefPlageConsult(); // Si la facture existe déjà on la met à jour $where = array(); $ljoin = array(); $plage = $consult->_ref_plageconsult; if (CAppUI::conf("ref_pays") == 2) { $where["patient_id"] = "= '{$consult->patient_id}'"; $where["praticien_id"] = "= '" . ($plage->pour_compte_id ? $plage->pour_compte_id : $plage->chir_id) . "'"; $where["cloture"] = "IS NULL"; } else { $table = $consult->sejour_id ? "facture_etablissement" : "facture_cabinet"; $ljoin["facture_liaison"] = "facture_liaison.facture_id = {$table}.facture_id"; $where["facture_liaison.object_id"] = " = '{$this->_consult_id}'"; $where["facture_liaison.object_class"] = " = 'CConsultation'"; $where["facture_liaison.facture_class"] = " = '{$this->_class}'"; $where["numero"] = " = '{$this->numero}'"; } //Si la facture existe déjà if ($this->loadObject($where, null, "facture_id", $ljoin)) { //Dans le cas Suisse if (CAppUI::conf("ref_pays") == 2 && CModule::getActive("dPfacturation")) { $ligne = new CFactureLiaison(); $ligne->facture_id = $this->_id; $ligne->facture_class = $this->_class; $ligne->object_id = $this->_consult_id; $ligne->object_class = 'CConsultation'; if (!$ligne->loadMatchingObject()) { $ligne->store(); } } } else { // Sinon on la crée $this->ouverture = CMbDT::date(); $this->patient_id = $consult->patient_id; $this->praticien_id = $plage->pour_compte_id ? $plage->pour_compte_id : $plage->chir_id; $this->type_facture = $consult->pec_at == 'arret' ? "accident" : "maladie"; if (CAppUI::conf("dPfacturation {$this->_class} use_auto_cloture")) { $this->cloture = CMbDT::date(); $create_lignes = true; } if ($this->numero > 1 && !count($consult->loadRefsFraisDivers(1)) && count($consult->loadRefsFraisDivers($this->numero)) && $consult->du_tva) { $frais = 0; foreach ($consult->loadRefsFraisDivers($this->numero) as $_frais) { $frais += $_frais->montant_base; } $this->du_patient = $frais + $consult->du_tva; $this->du_tva = $consult->du_tva; $this->taux_tva = $consult->taux_tva; } } $_object_id = $this->_consult_id; $_object_class = "CConsultation"; } //Lors de la création d'une facture de séjour if ($this->_sejour_id) { $_object_id = $this->_sejour_id; $_object_class = "CSejour"; } $this->completeField("assurance_maladie", "assurance_accident", "type_facture"); if ($this->type_facture != "accident" && $this->assurance_accident) { $this->assurance_accident = ""; } if ($this->type_facture != "maladie" && $this->assurance_maladie) { $this->assurance_maladie = ""; } // Standard store if ($msg = parent::store()) { return $msg; } if ($_object_id) { $ligne = new CFactureLiaison(); $ligne->facture_id = $this->_id; $ligne->facture_class = $this->_class; $ligne->object_id = $_object_id; $ligne->object_class = $_object_class; if (!$ligne->loadMatchingObject()) { $ligne->store(); } } if ($create_lignes) { $this->creationLignesFacture(); } }
/** * @see parent::store() */ function store() { $this->completeField("entree_reelle", "entree", "patient_id", "type_pec", "grossesse_id", "mode_sortie"); /** @var CSejour $old */ $old = $this->loadOldObject(); // Vérification de la validité des codes CIM if ($this->DP != null) { $dp = CCodeCIM10::get($this->DP); if (!$dp->exist) { CAppUI::setMsg("Le code CIM saisi n'est pas valide", UI_MSG_WARNING); $this->DP = ""; } } if ($this->DR != null) { $dr = CCodeCIM10::get($this->DR); if (!$dr->exist) { CAppUI::setMsg("Le code CIM saisi n'est pas valide", UI_MSG_WARNING); $this->DR = ""; } } // Mode de sortie normal par défaut si l'autorisation de sortie est réalisée if ($this->conf("specified_output_mode") && !$this->mode_sortie && $this->fieldModified("confirme")) { $this->mode_sortie = "normal"; } // Annulation de l'établissement de transfert si le mode de sortie n'est pas transfert if (null !== $this->mode_sortie) { if ("transfert" != $this->mode_sortie) { $this->etablissement_sortie_id = ""; } if ("mutation" != $this->mode_sortie) { $this->service_sortie_id = ""; } } // Mise à jour du type PEC si vide if (!$this->_id && !$this->type_pec) { $this->type_pec = $this->grossesse_id ? "O" : "M"; } // Annulation de la sortie réelle si on annule le mode de sortie if ($this->mode_sortie === "") { $this->sortie_reelle = ""; } // Annulation de l'établissement de provenance si le mode d'entrée n'est pas transfert if ($this->fieldModified("mode_entree")) { if ("7" != $this->mode_entree) { $this->etablissement_entree_id = ""; } if ("6" != $this->mode_entree) { $this->service_entree_id = ""; } } // Passage au mode transfert si on value un établissement de provenance if ($this->fieldModified("etablissement_entree_id")) { if ($this->etablissement_entree_id != null) { $this->mode_entree = 7; } } // Passage au mode mutation si on value un service de provenance if ($this->fieldModified("service_entree_id")) { if ($this->service_entree_id != null) { $this->mode_entree = 6; } } $patient_modified = $this->fieldModified("patient_id"); // Si le patient est modifié et qu'il y a des consultations, on cascade les consultations if (!$this->_forwardRefMerging && $this->sejour_id && $patient_modified) { /** @var CConsultation[] $consultations */ $consultations = $this->loadBackRefs("consultations"); foreach ($consultations as $_consult) { $_consult->_sync_consults_from_sejour = true; $_consult->patient_id = $this->patient_id; if ($msg = $_consult->store()) { return $msg; } } } // Pour un séjour non annulé, mise à jour de la date de décès du patient // suivant le mode de sortie if (!$this->annule) { $patient = new CPatient(); $patient->load($this->patient_id); if ($this->fieldModified("mode_sortie")) { if ("deces" == $this->mode_sortie) { $patient->deces = $this->_date_deces; } else { if ($this->_old->mode_sortie == "deces") { $patient->deces = ""; } } } // On verifie que le champ a été modifié pour faire le store (sinon probleme lors de la fusion de patients) if ($patient->fieldModified("deces")) { // Ne pas faire de return $msg ici, car ce n'est pas "bloquant" $patient->store(); } } // Si annulation possible que par le chef de bloc if (CAppUI::conf("dPplanningOp COperation cancel_only_for_resp_bloc") && $this->fieldModified("annule", 1) && $this->entree_reelle && !CModule::getCanDo("dPbloc")->edit) { foreach ($this->loadRefsOperations() as $_operation) { if ($_operation->rank) { CAppUI::setMsg("Impossible de sauvegarder : une des interventions du séjour est validée.\nContactez le responsable de bloc", UI_MSG_ERROR); return null; } } } // On fixe la récusation si pas définie pour un nouveau séjour if (!$this->_id && ($this->recuse === "" || $this->recuse === null)) { $this->recuse = CAppUI::conf("dPplanningOp CSejour use_recuse") ? -1 : 0; } // no matter of config, if sejour is "urgence" type: recusation 0 if ($this->type == "urg") { $this->recuse = 0; } // Si gestion en mode expert de l'isolement if (CAppUI::conf("dPplanningOp CSejour systeme_isolement") == "expert") { $this->isolement_date = $this->_isolement_date !== $this->entree && $this->isolement ? $this->_isolement_date : ""; if (!$this->isolement) { $this->isolement_fin = ""; } } $facture = null; if (CModule::getActive("dPfacturation") && CAppUI::conf("dPplanningOp CFactureEtablissement use_facture_etab")) { // Création de la facture de sejour $this->loadRefsFactureEtablissement(); $facture = $this->_ref_last_facture; if (!$facture->_id) { $facture->ouverture = CMbDT::date(); } if (CAppUI::conf("dPfacturation CFactureEtablissement use_temporary_bill")) { $facture->temporaire = 1; } $facture->group_id = $this->group_id; $facture->patient_id = $this->patient_id; $facture->praticien_id = $this->praticien_id; $facture->type_facture = $this->_type_sejour; $facture->dialyse = $this->_dialyse; $facture->cession_creance = $this->_cession_creance; $facture->statut_pro = $this->_statut_pro; $facture->assurance_maladie = $this->_assurance_maladie; $facture->assurance_accident = $this->_assurance_accident; $facture->rques_assurance_accident = $this->_rques_assurance_accident; $facture->rques_assurance_maladie = $this->_rques_assurance_maladie; //Store de la facture if ($msg = $facture->store()) { return $msg; } } $this->completeField("mode_entree_id"); if ($this->mode_entree_id) { /** @var CModeEntreeSejour $mode */ $mode = $this->loadFwdRef("mode_entree_id"); $this->mode_entree = $mode->mode; } $this->completeField("mode_sortie_id"); if ($this->mode_sortie_id) { /** @var CModeSortieSejour $mode */ $mode = $this->loadFwdRef("mode_sortie_id"); $this->mode_sortie = $mode->mode; } // Gestion du tarif et precodage des actes if ($this->_bind_tarif && $this->_id) { $this->getActeExecution(); if ($msg = $this->bindTarif()) { return $msg; } } // Si on change la grossesse d'un séjour, il faut remapper les naissances éventuelles $change_grossesse = $this->fieldModified("grossesse_id"); /** @var CNaissance[] $naissances */ $naissances = array(); if ($change_grossesse) { $naissances = $old->loadRefGrossesse()->loadRefsNaissances(); } // Sectorisation Rules $this->getServiceFromSectorisationRules(); if ($this->fieldModified("completion_sortie") && $this->completion_sortie && !$this->reception_sortie) { $this->reception_sortie = $this->completion_sortie; } if ($this->fieldModified("reception_sortie", "") && !$this->completion_sortie) { $this->completion_sortie = ""; } $this->getUFs(); $eai_sender_guid = $this->_eai_sender_guid; // On fait le store du séjour if ($msg = parent::store()) { return $msg; } $this->_eai_sender_guid = $eai_sender_guid; if ($change_grossesse) { foreach ($naissances as $_naissance) { $_naissance->grossesse_id = $this->grossesse_id; if ($msg = $_naissance->store()) { return $msg; } } } // Changement des liaisons de prestations si besoin // Seulement par rapport à l'entrée if (CAppUI::conf("dPhospi prestations systeme_prestations", CGroups::loadCurrent()) == "expert") { $decalage = CMbDT::daysRelative($old->entree, $this->entree); if ($decalage != 0) { $liaisons = $this->loadBackRefs("items_liaisons"); foreach ($liaisons as $_liaison) { $_liaison->date = CMbDT::date("{$decalage} days", $_liaison->date); if ($msg = $_liaison->store()) { return $msg; } } } } if (CModule::getActive("dPfacturation") && CAppUI::conf("dPplanningOp CFactureEtablissement use_facture_etab")) { if (count($this->_ref_factures) == 0) { $liaison = new CFactureLiaison(); $liaison->object_id = $this->_id; $liaison->object_class = $this->_class; $liaison->facture_id = $facture->_id; $liaison->facture_class = "CFactureEtablissement"; //Store de la table de liaison entre facture et séjour if ($msg = $liaison->store()) { return $msg; } } } if ($patient_modified) { $list_backrefs = array("contextes_constante", "deliveries", "consultations"); foreach ($list_backrefs as $_backname) { /** @var CConstantesMedicales[]|CProductDelivery[]|CConsultation[] $backobjects */ $backobjects = $this->loadBackRefs($_backname); if (!$backobjects) { continue; } foreach ($backobjects as $_object) { if ($_object->patient_id == $this->patient_id) { continue; } $_object->patient_id = $this->patient_id; if ($_object instanceof CConsultation) { $_object->_skip_count = true; } if ($msg = $_object->store()) { CAppUI::setMsg($msg, UI_MSG_WARNING); } } } } // Cas d'une annulation de séjour if ($this->annule) { // Suppression des affectations if ($msg = $this->delAffectations()) { return $msg; } // Suppression des opérations if ($msg = $this->cancelOperations()) { return $msg; } // Annulation des mouvements if ($msg = $this->cancelMovements()) { return $msg; } } // Synchronisation des affectations if (!$this->_no_synchro && !($this->type == "seances")) { $this->loadRefsAffectations(); $firstAff =& $this->_ref_first_affectation; $lastAff =& $this->_ref_last_affectation; // Cas où on a une premiere affectation différente de l'heure d'admission if ($firstAff->_id && $firstAff->entree != $this->_entree) { $firstAff->entree = $this->_entree; $firstAff->_no_synchro = 1; $firstAff->store(); } // Cas où on a une dernière affectation différente de l'heure de sortie if ($lastAff->_id && $lastAff->sortie != $this->_sortie) { $lastAff->sortie = $this->_sortie; $lastAff->_no_synchro = 1; $lastAff->store(); } //si le sejour a une sortie ==> compléter le champ effectue de la derniere affectation if ($lastAff->_id) { $this->_ref_last_affectation->effectue = $this->sortie_reelle ? 1 : 0; $this->_ref_last_affectation->store(); } } // try to assign an affectation $this->createAffectationLitUnique(); $this->createAffectationService(); // Génération du NDA ? if ($this->_generate_NDA) { // On ne synchronise pas un séjour d'urgences qui est un reliquat $rpu = $this->loadRefRPU(); if ($rpu && $rpu->mutation_sejour_id && $rpu->sejour_id != $rpu->mutation_sejour_id) { return null; } if ($msg = $this->generateNDA()) { return $msg; } } return null; }