<?php

/* $Id $ */
/**
 * @package Mediboard
 * @subpackage hl7
 * @version $Revision: 15199 $
 * @author SARL OpenXtrem
 * @license GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 */
$er7 = CValue::post("er7");
$exchange_id = CValue::post("exchange_id");
$exchange = new CExchangeHL7v2();
$exchange->load($exchange_id);
$exchange->loadRefsInteropActor();
if ($exchange->receiver_id) {
    /** @var CInteropReceiver $actor */
    $actor = $exchange->_ref_receiver;
    $actor->loadConfigValues();
} else {
    /** @var CInteropSender $actor */
    $actor = $exchange->_ref_sender;
    $actor->getConfigs($exchange);
}
$hl7_message = new CHL7v2Message();
$hl7_message->parse($er7);
/** @var CHL7v2MessageXML $xml */
$xml = $hl7_message->toXML(null, false);
$MSH = $xml->queryNode("MSH");
$EVN = $xml->queryNode("EVN");
$PID = $xml->queryNode("PID");
 /**
  * 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;
 }