/**
  * Send event
  *
  * @param CHL7v2Event $evenement Event type
  * @param CMbObject   $mbObject  Object
  *
  * @return null|string
  *
  * @throws CMbException
  */
 function sendEvent($evenement, CMbObject $mbObject)
 {
     $evenement->_receiver = $this;
     // build_mode = Mode simplifié lors de la génération du message
     $this->loadConfigValues();
     CHL7v2Message::setBuildMode($this->_configs["build_mode"]);
     $evenement->build($mbObject);
     CHL7v2Message::resetBuildMode();
     if (!($msg = $evenement->flatten())) {
         return null;
     }
     $exchange = $evenement->_exchange_hl7v2;
     // Si l'échange est invalide
     if (!$exchange->message_valide) {
         return null;
     }
     // Si on n'est pas en synchrone
     if (!$this->synchronous) {
         return null;
     }
     // Si on n'a pas d'IPP et NDA
     if ($exchange->master_idex_missing) {
         return null;
     }
     $evt = $this->getEventMessage($evenement->profil);
     $source = CExchangeSource::get("{$this->_guid}-{$evt}");
     if (!$source->_id || !$source->active) {
         return null;
     }
     if ($this->_configs["encoding"] == "UTF-8") {
         $msg = utf8_encode($msg);
     }
     $source->setData($msg, null, $exchange);
     try {
         $source->send();
     } catch (Exception $e) {
         throw new CMbException("CExchangeSource-no-response");
     }
     $exchange->date_echange = CMbDT::dateTime();
     $ack_data = $source->getACQ();
     if (!$ack_data) {
         $exchange->store();
         return null;
     }
     $data_format = CIHE::getEvent($exchange);
     $ack = new CHL7v2Acknowledgment($data_format);
     $ack->handle($ack_data);
     $exchange->date_echange = CMbDT::dateTime();
     $exchange->statut_acquittement = $ack->getStatutAcknowledgment();
     $exchange->acquittement_valide = $ack->message->isOK(CHL7v2Error::E_ERROR) ? 1 : 0;
     $exchange->_acquittement = $ack_data;
     $exchange->store();
     return $ack_data;
 }
 /**
  * Send exchange
  *
  * @throws CMbException
  *
  * @return void
  */
 function send()
 {
     $this->loadRefsInteropActor();
     if (!$this->message_valide) {
         throw new CMbException("CExchangeDataFormat-msg-Invalid exchange");
     }
     $receiver = $this->_ref_receiver;
     $receiver->loadConfigValues();
     $evenement = null;
     $msg = $this->_message;
     if ($receiver instanceof CReceiverHL7v2) {
         if ($receiver->_configs["encoding"] == "UTF-8") {
             $msg = utf8_encode($msg);
         }
         $evenement = "evenementsPatient";
         $data_format = CIHE::getEvent($this);
     }
     if ($receiver instanceof CDestinataireHprim) {
         if ($receiver->_configs["encoding"] == "UTF-8") {
             $msg = utf8_encode($msg);
         }
         if ($this->type == "patients") {
             $evenement = "evenementPatient";
             $data_format = CHPrimXMLEventPatient::getHPrimXMLEvenements($this->_message);
         }
         if ($this->type == "pmsi") {
             $data_format = CHPrimXMLEventServeurActivitePmsi::getHPrimXMLEvenements($this->_message);
             $evenement = $data_format->sous_type;
         }
     }
     if ($receiver instanceof CPhastDestinataire) {
         $data_format = CPhastEvenementsPN13::getXMLEvenementsPN13($this->_message);
         $evenement = $data_format->sous_type;
     }
     if (!$evenement) {
         throw new CMbException("CExchangeDataFormat-msg-No events defined");
     }
     $source = CExchangeSource::get("{$receiver->_guid}-{$evenement}");
     if (!$source->_id || !$source->active) {
         throw new CMbException("CExchangeDataFormat-msg-No source for this actor");
     }
     // Si on n'a pas d'IPP et NDA
     if ($this->master_idex_missing) {
         throw new CMbException("CExchangeDataFormat-msg-Master idex missing");
     }
     $source->setData($msg, false, $this);
     $source->send();
     $this->date_echange = CMbDT::dateTime();
     // Si on n'a pas d'acquittement
     if (!($ack_data = $source->getACQ())) {
         $this->store();
         return;
     }
     if ($this instanceof CEchangeHprim) {
         $ack_data = utf8_decode($ack_data);
         $ack = CHPrimXMLAcquittements::getAcquittementEvenementXML($data_format);
         $ack->loadXML($ack_data);
         $doc_valid = $ack->schemaValidate();
         if ($doc_valid) {
             $this->statut_acquittement = $ack->getStatutAcquittement();
         }
     }
     if ($this instanceof CExchangeHL7v2) {
         $ack = new CHL7v2Acknowledgment($data_format);
         $ack->handle($ack_data);
         $this->statut_acquittement = $ack->getStatutAcknowledgment();
         $this->acquittement_valide = $ack->message->isOK(CHL7v2Error::E_ERROR) ? 1 : 0;
     }
     if ($this instanceof CExchangePhast) {
         $ack = new CPhastAcquittementsPN13();
         $ack->loadXML($ack_data);
         $doc_valid = $ack->schemaValidate();
         if ($doc_valid) {
             $this->statut_acquittement = $ack->getCodeAcquittement();
         }
     }
     $this->_acquittement = $ack_data;
     $this->store();
 }
 /**
  * Event
  *
  * @param CExchangeDataFormat $data_format Data format
  *
  * @return null|string
  */
 function event(CExchangeDataFormat $data_format)
 {
     $msg = $data_format->_message;
     /** @var CHL7v2Event $evt */
     $evt = $data_format->_family_message;
     $evt->_data_format = $data_format;
     // Récupération des informations du message
     /** @var CHL7v2MessageXML $dom_evt */
     $dom_evt = $evt->handle($msg);
     $dom_evt->_is_i18n = $evt->_is_i18n;
     try {
         // Création de l'échange
         $exchange_hl7v2 = new CExchangeHL7v2();
         $exchange_hl7v2->load($data_format->_exchange_id);
         // Récupération des données du segment MSH
         $data = $dom_evt->getMSHEvenementXML();
         // Gestion de l'acquittement
         $ack = $dom_evt->getEventACK($evt);
         $ack->message_control_id = $data['identifiantMessage'];
         // Message non supporté pour cet utilisateur
         $evt_class = CHL7Event::getEventClass($evt);
         if (!in_array($evt_class, $data_format->_messages_supported_class)) {
             $data_format->_ref_sender->_delete_file = false;
             // Pas de création d'échange dans le cas :
             // * où l'on ne souhaite pas traiter le message
             // * où le sender n'enregistre pas les messages non pris en charge
             if (!$data_format->_to_treatment || !$data_format->_ref_sender->save_unsupported_message) {
                 return;
             }
             $exchange_hl7v2->populateExchange($data_format, $evt);
             $exchange_hl7v2->loadRefsInteropActor();
             $exchange_hl7v2->populateErrorExchange(null, $evt);
             $ack->_ref_exchange_hl7v2 = $exchange_hl7v2;
             $msgAck = $ack->generateAcknowledgment("AR", "E001", "201");
             $exchange_hl7v2->populateErrorExchange($ack);
             return $msgAck;
         }
         $sender = $data_format->_ref_sender;
         $sender->getConfigs($data_format);
         // Acquittement d'erreur d'un document XML recu non valide
         if (!$sender->_configs["bypass_validating"] && !$evt->message->isOK(CHL7v2Error::E_ERROR)) {
             $exchange_hl7v2->populateExchange($data_format, $evt);
             $exchange_hl7v2->loadRefsInteropActor();
             $exchange_hl7v2->populateErrorExchange(null, $evt);
             $ack->_ref_exchange_hl7v2 = $exchange_hl7v2;
             $msgAck = $ack->generateAcknowledgment("AR", "E002", "207");
             $exchange_hl7v2->populateErrorExchange($ack);
             return $msgAck;
         }
         $exchange_hl7v2->populateExchange($data_format, $evt);
         $exchange_hl7v2->message_valide = 1;
         // Gestion des notifications ?
         if (!$exchange_hl7v2->_id) {
             $exchange_hl7v2->date_production = CMbDT::dateTime();
             $exchange_hl7v2->identifiant_emetteur = $data['identifiantMessage'];
         }
         $exchange_hl7v2->store();
         // Pas de traitement du message
         if (!$data_format->_to_treatment) {
             return;
         }
         $exchange_hl7v2->loadRefsInteropActor();
         // Chargement des configs de l'expéditeur
         $sender = $exchange_hl7v2->_ref_sender;
         $sender->getConfigs($data_format);
         if (!$dom_evt->checkApplicationAndFacility($data, $sender)) {
             return;
         }
         if (!empty($sender->_configs["handle_mode"])) {
             CHL7v2Message::setHandleMode($sender->_configs["handle_mode"]);
         }
         $dom_evt->_ref_exchange_hl7v2 = $exchange_hl7v2;
         $ack->_ref_exchange_hl7v2 = $exchange_hl7v2;
         // Message PAM / DEC / PDQ / SWF
         $msgAck = self::handleEvent($exchange_hl7v2, $dom_evt, $ack, $data);
         CHL7v2Message::resetBuildMode();
     } catch (Exception $e) {
         $exchange_hl7v2->populateExchange($data_format, $evt);
         $exchange_hl7v2->loadRefsInteropActor();
         $exchange_hl7v2->populateErrorExchange(null, $evt);
         $ack = new CHL7v2Acknowledgment($evt);
         $ack->message_control_id = isset($data['identifiantMessage']) ? $data['identifiantMessage'] : "000000000";
         $ack->_ref_exchange_hl7v2 = $exchange_hl7v2;
         $msgAck = $ack->generateAcknowledgment("AR", "E003", "207", "E", $e->getMessage());
         $exchange_hl7v2->populateErrorExchange($ack);
         CHL7v2Message::resetBuildMode();
     }
     return $msgAck;
 }