<StructureDefinition xmlns="http://hl7.org/fhir">
  <id value="AID-MedicationRequest" />
  <url value="https://fhir.dosing-gmbh.de/StructureDefinition/AID-MedicationRequest" />
  <version value="1.0.3" />
  <name value="AIDMedicationRequest" />
  <title value="AiD Medication Request" />
  <status value="draft" />
  <date value="2026-08-21" />
  <publisher value="Dosing GmbH" />
  <contact>
    <name value="Dosing GmbH" />
    <telecom>
      <system value="url" />
      <value value="https://www.dosing-gmbh.de" />
    </telecom>
  </contact>
  <description value="MedicationRequest for a write-back callback to a host clinical system. Binds DosageDgMP for dosageInstruction; subject/requester/insurance do not require literal references." />
  <purpose value="Dosage information (structured and/or free text) for a write-back callback, without requiring literal Patient/Practitioner/Coverage references." />
  <fhirVersion value="4.0.1" />
  <mapping>
    <identity value="workflow" />
    <uri value="http://hl7.org/fhir/workflow" />
    <name value="Workflow Pattern" />
  </mapping>
  <kind value="resource" />
  <abstract value="false" />
  <type value="MedicationRequest" />
  <baseDefinition value="http://hl7.org/fhir/StructureDefinition/MedicationRequest" />
  <derivation value="constraint" />
  <differential>
    <element id="MedicationRequest">
      <path value="MedicationRequest" />
      <short value="Prescription in a write-back callback" />
      <definition value="One issued prescription: status, dosage, and a reference to the associated Medication resource within the same payload." />
      <constraint>
        <key value="aid-dosageFlag-true" />
        <severity value="error" />
        <human value="If the dosage-present indicator is true, a dosage instruction must be present." />
        <expression value="extension('https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_DosageFlag').exists().not() or ((extension('https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_DosageFlag').value as boolean)=true implies dosageInstruction.exists())" />
      </constraint>
      <constraint>
        <key value="aid-dosageFlag-false" />
        <severity value="error" />
        <human value="If the dosage-present indicator is false, no dosage instruction may be present." />
        <expression value="extension('https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_DosageFlag').exists().not() or ((extension('https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_DosageFlag').value as boolean)=false implies dosageInstruction.exists().not())" />
      </constraint>
    </element>
    <element id="MedicationRequest.extension">
      <path value="MedicationRequest.extension" />
      <slicing>
        <discriminator>
          <type value="value" />
          <path value="url" />
        </discriminator>
        <ordered value="false" />
        <rules value="open" />
      </slicing>
      <short value="Extensions" />
    </element>
    <element id="MedicationRequest.extension:dosageFlag">
      <path value="MedicationRequest.extension" />
      <sliceName value="dosageFlag" />
      <short value="Dosage-present indicator" />
      <definition value="Whether a dosage instruction is given at all (KBV_EX_ERP_DosageFlag). Governs, together with the aid-dosageFlag-true/aid-dosageFlag-false constraints, whether dosageInstruction may be present." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_DosageFlag" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:renderedDosageInstruction">
      <path value="MedicationRequest.extension" />
      <sliceName value="renderedDosageInstruction" />
      <short value="Rendered dosage text (e.g. &quot;1-2-0-1&quot;)" />
      <definition value="Human-readable text generated from the structured dosage. Required once dosageInstruction is structured (see DosageDgMP: DosageStructuredRequiresGeneratedText)." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="http://hl7.org/fhir/5.0/StructureDefinition/extension-MedicationRequest.renderedDosageInstruction" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:generatedDosageInstructionsMeta">
      <path value="MedicationRequest.extension" />
      <sliceName value="generatedDosageInstructionsMeta" />
      <short value="Metadata for the generated dosage text" />
      <definition value="Language and algorithm version of the generated dosage text. algorithmVersion is the version of the reference implementation, not of our own code." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="http://ig.fhir.de/igs/medication/StructureDefinition/GeneratedDosageInstructionsMeta" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:multiplePrescription">
      <path value="MedicationRequest.extension" />
      <sliceName value="multiplePrescription" />
      <short value="Multiple-prescription indicator" />
      <definition value="Indicator, numbering and period of a multiple prescription (KBV_EX_ERP_Multiple_Prescription)." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_Multiple_Prescription" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:legalBasis">
      <path value="MedicationRequest.extension" />
      <sliceName value="legalBasis" />
      <short value="Legal basis (incl. replacement prescription)" />
      <definition value="Legal basis of the prescription (KBV_EX_FOR_Legal_basis). Codes relevant here: &quot;00&quot; (standard) and &quot;10&quot; (replacement prescription per § 31 Abs. 3 Satz 7 SGB V)." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_FOR_Legal_basis" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:statusCoPayment">
      <path value="MedicationRequest.extension" />
      <sliceName value="statusCoPayment" />
      <short value="Co-payment status" />
      <definition value="Co-payment status of the prescription (KBV_EX_FOR_StatusCoPayment). Codes: &quot;0&quot; co-payment required, &quot;1&quot; co-payment exempt, &quot;2&quot; assisted reproduction per § 27a SGB V." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_FOR_StatusCoPayment" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:narcotic">
      <path value="MedicationRequest.extension" />
      <sliceName value="narcotic" />
      <short value="Narcotic (BtM)" />
      <definition value="Special marker per § 9 Abs. 1 Nr. 6 BtMVV plus supplementary notes on the substitution medicine (KBV_EX_ERP_Narcotic)." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_Narcotic" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.extension:teratogenic">
      <path value="MedicationRequest.extension" />
      <sliceName value="teratogenic" />
      <short value="T-prescription (teratogenic medicine)" />
      <definition value="Declarations required for prescribing a teratogenic medicine (KBV_EX_ERP_Teratogenic): off-label status, childbearing potential, compliance with safety measures, provision of information material, and declared expertise." />
      <min value="0" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="https://fhir.kbv.de/StructureDefinition/KBV_EX_ERP_Teratogenic" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.medicationReference">
      <path value="MedicationRequest.medication[x]" />
      <sliceName value="medicationReference" />
      <short value="Reference to the Medication resource" />
      <definition value="urn:uuid reference to the Medication resource of the same prescription, resolvable only within the payload, not as a Bundle." />
      <min value="1" />
      <max value="1" />
      <type>
        <code value="Reference" />
        <targetProfile value="http://hl7.org/fhir/StructureDefinition/Medication" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.subject">
      <path value="MedicationRequest.subject" />
      <short value="Patient (display-only is sufficient)" />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.authoredOn">
      <path value="MedicationRequest.authoredOn" />
      <short value="Date the prescription was issued" />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.requester">
      <path value="MedicationRequest.requester" />
      <short value="Requesting practitioner (optional)" />
      <max value="1" />
      <mustSupport value="false" />
    </element>
    <element id="MedicationRequest.insurance">
      <path value="MedicationRequest.insurance" />
      <short value="Insurance coverage (optional)" />
      <mustSupport value="false" />
    </element>
    <element id="MedicationRequest.dosageInstruction">
      <path value="MedicationRequest.dosageInstruction" />
      <short value="Dosage, structured and/or free text (DosageDgMP)" />
      <definition value="Binds DosageDgMP from de.fhir.medication for structured and/or free-text dosage. Whether an entry is present is governed by the dosageFlag extension, not by cardinality." />
      <min value="0" />
      <type>
        <code value="Dosage" />
        <profile value="http://ig.fhir.de/igs/medication/StructureDefinition/DosageDgMP" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.dispenseRequest">
      <path value="MedicationRequest.dispenseRequest" />
      <short value="Dispense authorization" />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.dispenseRequest.quantity">
      <path value="MedicationRequest.dispenseRequest.quantity" />
      <short value="Package quantity to dispense" />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.dispenseRequest.expectedSupplyDuration">
      <path value="MedicationRequest.dispenseRequest.expectedSupplyDuration" />
      <short value="Expected supply duration (days for narcotics, weeks for T-prescriptions)" />
      <definition value="Expected supply duration of the medicine: in days for narcotics, in weeks for teratogenic medicines." />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.substitution">
      <path value="MedicationRequest.substitution" />
      <short value="Substitution permission (aut idem)" />
      <mustSupport value="true" />
    </element>
    <element id="MedicationRequest.substitution.allowed[x]">
      <path value="MedicationRequest.substitution.allowed[x]" />
      <short value="Substitution permission (aut idem)" />
      <type>
        <code value="boolean" />
      </type>
      <mustSupport value="true" />
    </element>
  </differential>
</StructureDefinition>