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