{#
 # This file is part of MedShakeEHR.
 #
 # Copyright (c) 2018
 # Bertrand Boutillier <b.boutillier@gmail.com>
 # http://www.medshake.net
 #
 # MedShakeEHR is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # any later version.
 #
 # MedShakeEHR is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with MedShakeEHR.  If not, see <http://www.gnu.org/licenses/>.
 #/

/##
 # Template > base de construction XML CDA 
 #
 # @author Bertrand Boutillier <b.boutillier@gmail.com>
 #}

{% import "macroCda.xml.twig" as m %}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 infrastructure/cda/CDA.xsd" xmlns="urn:hl7-org:v3">
  <realmCode code="FR"/>
  <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
  {% block templateId %}
  <templateId root="2.16.840.1.113883.2.8.2.1" assigningAuthorityName="HL7 France"/>
  <templateId root="1.2.250.1.213.1.1.1.1" assigningAuthorityName="Cadre InteropASIP"/>
  {% endblock templateId %}
  <id root="*************"/>
  <code code="{{ tag.cda_clinicalDocument_code_code|e('html_attr') }}" displayName="{{ tag.cda_clinicalDocument_code_displayName|e('html_attr') }}" codeSystem="{{ tag.cda_clinicalDocument_code_codeSystem|e('html_attr') }}" codeSystemName="{{ tag.cda_clinicalDocument_code_codeSystemName|e('html_attr') }}"/>
  <title>{{ tag.cda_clinicalDocument_title }}</title>
  <effectiveTime value="{{ tag.date|date('YmdHiO')}}"/>
  <confidentialityCode code="N" displayName="Normal" codeSystem="2.16.840.1.113883.5.25"/>
  <languageCode code="fr-FR"/>
  <recordTarget>
    <patientRole>
      <id root="1.2.250.1.213.1.4.2" extension="{{ tag.nss }}"/>
      {% if tag.street and tag.city %}
      <addr use="H">
        <streetAddressLine>{{ tag.streetNumber }} {{ tag.street }}</streetAddressLine>
        <streetAddressLine>{{ tag.postalCodePerso }} {{ tag.city }}</streetAddressLine>
      </addr>
      {% else %}
      <addr nullFlavor="NASK"/>
      {% endif %}
      {% if tag.homePhone %}<telecom value="tel:{{ tag.homePhone }}" use="H"/>{% endif %}
      {% if tag.mobilePhone %}<telecom value="tel:{{ tag.mobilePhone }}" use="MC"/>{% endif %}
      {% if tag.personalEmail %}<telecom value="mailto:{{ tag.personalEmail }}" use="H"/>{% endif %}
      {% if tag.homePhone is not defined and tag.mobilePhone is not defined and tag.personalEmail is not defined %}
      <telecom nullFlavor="NASK"/>
      {% endif %}
      <patient>
        {{ m.name(tag.birthname, tag.firstname, tag.lastname ) }}
        {% if tag.val_administrativeGenderCode == 'M' %}
        <administrativeGenderCode code="M" displayName="Masculin" codeSystem="2.16.840.1.113883.5.1"/>
        {% elseif tag.val_administrativeGenderCode == 'F' %}
        <administrativeGenderCode code="F" displayName="Féminin" codeSystem="2.16.840.1.113883.5.1"/>
        {% elseif tag.val_administrativeGenderCode == 'U' %}
        <administrativeGenderCode code="U" displayName="Inconnu" codeSystem="2.16.840.1.113883.5.1"/>
        {% endif %}
        <birthTime value="{{ tag.birthdate }}"/>
      </patient>
    </patientRole>
  </recordTarget>
  <author>
    <functionCode nullFlavor="UNK"/>
    <time value="{{ tag.date|date('YmdHiO')}}"/>
    <assignedAuthor>
      <id root="1.2.250.1.71.4.2.1" extension="{{ tag.AuteurInitial_PSIdNat }}" assigningAuthorityName="ASIP-CPS"/>
      <code code="{{ tag.AuteurInitial_PSCodeProSpe|e('html_attr') }}" displayName="{{ tag.AuteurInitial_PSCodeProSpe_displayName|e('html_attr') }}"
        codeSystem="{{ tag.AuteurInitial_PSCodeProSpe_codeSystem|e('html_attr') }}"/>
      <addr use="WP">
        <streetAddressLine>{{ tag.AuteurInitial_numAdressePro }} {{ tag.AuteurInitial_rueAdressePro }}</streetAddressLine>
        <streetAddressLine>{{ tag.AuteurInitial_codePostalPro }} {{ tag.AuteurInitial_villeAdressePro }}</streetAddressLine>
      </addr>
      {% if tag.AuteurInitial_telPro %}<telecom value="tel:{{ tag.AuteurInitial_telPro }}" use="WP"/>{% endif %}
      {% if tag.AuteurInitial_emailApicrypt %}<telecom value="mailto:{{ tag.AuteurInitial_emailApicrypt }}" use="WP"/>{% endif %}
      <assignedPerson>
        {{ m.name(tag.AuteurInitial_birthname, tag.AuteurInitial_firstname, tag.AuteurInitial_lastname, tag.AuteurInitial_titre ) }}
      </assignedPerson>
      <assignedAuthoringDevice>
        <softwareName>{{ tag.softwareName }}</softwareName>
      </assignedAuthoringDevice>
    </assignedAuthor>
  </author>
  <custodian>
    <assignedCustodian>
      <representedCustodianOrganization>
        <id root="1.2.250.1.71.4.2.2"/>
        <name>Cabinet médical</name>
        {% if tag.AuteurInitial_telPro %}<telecom value="tel:{{ tag.AuteurInitial_telPro }}" use="WP"/>{% endif %}
        <addr>
          <streetAddressLine>{{ tag.AuteurInitial_numAdressePro }} {{ tag.AuteurInitial_rueAdressePro }}</streetAddressLine>
          <streetAddressLine>{{ tag.AuteurInitial_codePostalPro }} {{ tag.AuteurInitial_villeAdressePro }}</streetAddressLine>
        </addr>
      </representedCustodianOrganization>
    </assignedCustodian>
  </custodian>
  <legalAuthenticator>
    <time value="{{ tag.date|date('YmdHiO')}}"/>
    <signatureCode code="S"/>
    <assignedEntity>
      <id root="1.2.250.1.71.4.2.1" extension="{{ tag.AuteurInitial_PSIdNat }}" assigningAuthorityName="ASIP-CPS"/>
      <assignedPerson>
        {{ m.name(tag.AuteurInitial_birthname, tag.AuteurInitial_firstname, tag.AuteurInitial_lastname, tag.AuteurInitial_titre ) }}
      </assignedPerson>
    </assignedEntity>
  </legalAuthenticator>
  <documentationOf>
    <serviceEvent>
      <code code="{{ tag.cda_serviceEvent_code|e('html_attr') }}" displayName="{{ tag.cda_serviceEvent_displayName|e('html_attr') }}"
      {% if tag.cda_serviceEvent_codeSystem == 'CCAM' %}
        codeSystem="1.2.250.1.213.2.5"
      {% elseif tag.cda_serviceEvent_codeSystem in ['CIM-10', 'CIM10'] %}
        codeSystem="2.16.840.1.113883.6.3"
      {% elseif tag.cda_serviceEvent_codeSystem == 'LOINC' %}
        codeSystem="2.16.840.1.113883.6.1"
      {% endif %}
      />
      <effectiveTime>
        <low value="{{ tag.date|date('YmdHiO')}}"/>
        <high value="{{ tag.date|date('YmdHiO')}}"/>
      </effectiveTime>
      <performer typeCode="PRF">
        <functionCode nullFlavor="UNK"/>
        <assignedEntity>
          <id root="1.2.250.1.71.4.2.1" extension="{{ tag.AuteurInitial_PSIdNat }}" assigningAuthorityName="ASIP-CPS"/>
          <assignedPerson>
            {{ m.name(tag.AuteurInitial_birthname, tag.AuteurInitial_firstname, tag.AuteurInitial_lastname, tag.AuteurInitial_titre ) }}
          </assignedPerson>
          <representedOrganization>
            <id root="1.2.250.1.71.4.2.2"/>
            <name>Cabinet médical</name>
            {% if tag.AuteurInitial_telPro %}<telecom value="tel:{{ tag.AuteurInitial_telPro }}" use="WP"/>{% endif %}
            <addr>
              <streetAddressLine>{{ tag.AuteurInitial_numAdressePro }} {{ tag.AuteurInitial_rueAdressePro }}</streetAddressLine>
              <streetAddressLine>{{ tag.AuteurInitial_codePostalPro }} {{ tag.AuteurInitial_villeAdressePro }}</streetAddressLine>
            </addr>
            <standardIndustryClassCode code="ETABLISSEMENT" displayName="ETABLISSEMENT"
              codeSystem="1.2.250.1.213.1.1.4.9"/>
          </representedOrganization>
        </assignedEntity>
      </performer>
    </serviceEvent>
  </documentationOf>
  <componentOf>
    <encompassingEncounter>
      <effectiveTime nullFlavor="UNK" />
      <location>
        <healthCareFacility>
          <code code="{{ tag.AuteurInitial_PSCodeStructureExercice }}" displayName="{{ tag.AuteurInitial_PSCodeStructureExercice_displayName }}"
            codeSystem="{{ tag.AuteurInitial_PSCodeStructureExercice_codeSystem }}"/>
        </healthCareFacility>
      </location>
      <encounterParticipant>
        <assignedEntity>
          <id root="1.2.250.1.71.4.2.1" extension="{{ tag.AuteurInitial_PSIdNat }}" assigningAuthorityName="ASIP-CPS"/>
          <assignedPerson>
            {{ m.name(tag.AuteurInitial_birthname, tag.AuteurInitial_firstname, tag.AuteurInitial_lastname, tag.AuteurInitial_titre ) }}
          </assignedPerson>
          <representedOrganization>
            <id root="1.2.250.1.71.4.2.2"/>
            <name>Cabinet médical</name>
            {% if tag.AuteurInitial_telPro %}<telecom value="tel:{{ tag.AuteurInitial_telPro }}" use="WP"/>{% endif %}
            <addr>
              <streetAddressLine>{{ tag.AuteurInitial_numAdressePro }} {{ tag.AuteurInitial_rueAdressePro }}</streetAddressLine>
              <streetAddressLine>{{ tag.AuteurInitial_codePostalPro }} {{ tag.AuteurInitial_villeAdressePro }}</streetAddressLine>
            </addr>
            <standardIndustryClassCode code="ETABLISSEMENT" displayName="ETABLISSEMENT"
              codeSystem="1.2.250.1.213.1.1.4.9"/>
          </representedOrganization>
        </assignedEntity>
      </encounterParticipant>
    </encompassingEncounter>
  </componentOf>
  <component>
    {% block component %}{% endblock component %}
  </component>
</ClinicalDocument>
