<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
  xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
  xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
  xmlns:getriebe="https://tokenjanitor.github.io/getriebe/bpmn/extensions/v1"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="urn:getriebe:combined-order-approval"
  expressionLanguage="https://tokenjanitor.github.io/getriebe/languages/expression/v1">
  <bpmn:itemDefinition id="ReviewKinds" structureRef="xsd:string" isCollection="true" />
  <bpmn:itemDefinition id="ReviewVotes" structureRef="xsd:boolean" isCollection="true" />
  <bpmn:itemDefinition id="ReviewKind" structureRef="xsd:string" />
  <bpmn:itemDefinition id="ReviewVote" structureRef="xsd:boolean" />
  <bpmn:itemDefinition id="ReviewDecisionType" structureRef="xsd:boolean" />
  <bpmn:itemDefinition id="OrderAmount" structureRef="xsd:decimal" />
  <bpmn:resource id="review-team" name="Review team">
    <bpmn:resourceParameter id="review-role" name="Role" type="ReviewKind" isRequired="true" />
  </bpmn:resource>
  <bpmn:resource id="budget-reviewer" name="Budget reviewer" />
  <bpmn:resource id="purchasing-reviewer" name="Purchasing reviewer" />
  <bpmn:resource id="compliance-reviewer" name="Compliance reviewer" />
  <bpmn:resource id="requester" name="Requester" />
  <bpmn:resource id="reviewer" name="Order confirmer" />
  <bpmn:resource id="senior-reviewer" name="Senior confirmer" />
  <bpmn:process id="combinedOrderApproval" name="Order approval with parallel reviews and a confirmation deadline" isExecutable="true">
    <bpmn:dataObject id="approved" itemSubjectRef="ReviewDecisionType" />
    <bpmn:dataObject id="amount" itemSubjectRef="OrderAmount" />
    <bpmn:startEvent id="submitted" name="Order submitted" />
    <bpmn:exclusiveGateway id="reviewEntry" gatewayDirection="Converging" />
    <bpmn:subProcess id="reviewRound" name="Review round">
      <bpmn:dataObject id="reviewVotes" itemSubjectRef="ReviewVotes" isCollection="true" />
      <bpmn:dataObject id="reviewKinds" itemSubjectRef="ReviewKinds" isCollection="true" />
      <bpmn:startEvent id="roundStarted" />
      <bpmn:businessRuleTask id="determineReviews" name="Determine required reviews">
        <bpmn:extensionElements>
          <getriebe:decisionTable><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/"
  id="requiredReviewsDefinitions" name="Required reviews" namespace="urn:getriebe:required-reviews:dmn">
  <itemDefinition id="reviewKindList" name="ReviewKindList" isCollection="true">
    <typeRef>string</typeRef>
  </itemDefinition>
  <decision id="requiredReviewsDecision" name="Determine required reviews">
    <decisionTable id="requiredReviewsTable" hitPolicy="UNIQUE">
      <input id="amountColumn" label="Order amount">
        <inputExpression id="amountExpression" typeRef="number"><text>amount</text></inputExpression>
      </input>
      <output id="reviewKindsColumn" name="reviewKinds" typeRef="ReviewKindList" />
      <rule id="standardReviews">
        <inputEntry id="standardRange"><text>&lt; 10000</text></inputEntry>
        <outputEntry id="standardList"><text>["budget", "einkauf"]</text></outputEntry>
      </rule>
      <rule id="extendedReviews">
        <inputEntry id="extendedRange"><text>&gt;= 10000</text></inputEntry>
        <outputEntry id="extendedList"><text>["budget", "einkauf", "compliance"]</text></outputEntry>
      </rule>
    </decisionTable>
  </decision>
</definitions>
]]></getriebe:decisionTable>
        </bpmn:extensionElements>
        <bpmn:ioSpecification>
          <bpmn:dataInput id="amountIn" name="amount" itemSubjectRef="OrderAmount" />
          <bpmn:dataOutput id="kindsOut" name="reviewKinds" itemSubjectRef="ReviewKinds" />
          <bpmn:inputSet><bpmn:dataInputRefs>amountIn</bpmn:dataInputRefs></bpmn:inputSet>
          <bpmn:outputSet><bpmn:dataOutputRefs>kindsOut</bpmn:dataOutputRefs></bpmn:outputSet>
        </bpmn:ioSpecification>
        <bpmn:dataInputAssociation><bpmn:sourceRef>amount</bpmn:sourceRef><bpmn:targetRef>amountIn</bpmn:targetRef></bpmn:dataInputAssociation>
        <bpmn:dataOutputAssociation><bpmn:sourceRef>kindsOut</bpmn:sourceRef><bpmn:targetRef>reviewKinds</bpmn:targetRef></bpmn:dataOutputAssociation>
      </bpmn:businessRuleTask>
      <bpmn:userTask id="reviewOrder" name="Review order">
        <bpmn:ioSpecification>
          <bpmn:dataInput id="reviewKindsInput" name="reviewKinds" itemSubjectRef="ReviewKinds" isCollection="true" />
          <bpmn:dataInput id="reviewAmount" name="amount" itemSubjectRef="OrderAmount" />
          <bpmn:dataOutput id="reviewVotesOutput" name="reviewVotes" itemSubjectRef="ReviewVotes" isCollection="true" />
          <bpmn:inputSet>
            <bpmn:dataInputRefs>reviewKindsInput</bpmn:dataInputRefs>
            <bpmn:dataInputRefs>reviewAmount</bpmn:dataInputRefs>
          </bpmn:inputSet>
          <bpmn:outputSet>
            <bpmn:dataOutputRefs>reviewVotesOutput</bpmn:dataOutputRefs>
          </bpmn:outputSet>
        </bpmn:ioSpecification>
        <bpmn:dataInputAssociation>
          <bpmn:sourceRef>reviewKinds</bpmn:sourceRef>
          <bpmn:targetRef>reviewKindsInput</bpmn:targetRef>
        </bpmn:dataInputAssociation>
        <bpmn:dataInputAssociation>
          <bpmn:sourceRef>amount</bpmn:sourceRef>
          <bpmn:targetRef>reviewAmount</bpmn:targetRef>
        </bpmn:dataInputAssociation>
        <bpmn:dataOutputAssociation>
          <bpmn:sourceRef>reviewVotesOutput</bpmn:sourceRef>
          <bpmn:targetRef>reviewVotes</bpmn:targetRef>
        </bpmn:dataOutputAssociation>
        <bpmn:potentialOwner>
          <bpmn:resourceRef>review-team</bpmn:resourceRef>
          <bpmn:resourceParameterBinding parameterRef="review-role">
            <bpmn:expression xsi:type="bpmn:tFormalExpression">lookup(reviewKind, "budget", "budget-reviewer", "einkauf", "purchasing-reviewer", "compliance", "compliance-reviewer")</bpmn:expression>
          </bpmn:resourceParameterBinding>
        </bpmn:potentialOwner>
        <bpmn:multiInstanceLoopCharacteristics isSequential="false" behavior="All">
          <bpmn:loopDataInputRef>reviewKindsInput</bpmn:loopDataInputRef>
          <bpmn:loopDataOutputRef>reviewVotesOutput</bpmn:loopDataOutputRef>
          <bpmn:inputDataItem id="localReviewKind" name="reviewKind" itemSubjectRef="ReviewKind" />
          <bpmn:outputDataItem id="localReviewVote" name="reviewVote" itemSubjectRef="ReviewVote" />
        </bpmn:multiInstanceLoopCharacteristics>
      </bpmn:userTask>
      <bpmn:scriptTask id="evaluateReview" name="Evaluate votes" scriptFormat="application/vnd.getriebe.expression-v1">
        <bpmn:ioSpecification>
          <bpmn:dataInput id="evaluateVotes" name="reviewVotes" itemSubjectRef="ReviewVotes" isCollection="true" />
          <bpmn:dataOutput id="evaluateApproved" name="approved" itemSubjectRef="ReviewDecisionType" />
          <bpmn:inputSet><bpmn:dataInputRefs>evaluateVotes</bpmn:dataInputRefs></bpmn:inputSet>
          <bpmn:outputSet><bpmn:dataOutputRefs>evaluateApproved</bpmn:dataOutputRefs></bpmn:outputSet>
        </bpmn:ioSpecification>
        <bpmn:dataInputAssociation>
          <bpmn:sourceRef>reviewVotes</bpmn:sourceRef><bpmn:targetRef>evaluateVotes</bpmn:targetRef>
        </bpmn:dataInputAssociation>
        <bpmn:dataOutputAssociation>
          <bpmn:sourceRef>evaluateApproved</bpmn:sourceRef><bpmn:targetRef>approved</bpmn:targetRef>
        </bpmn:dataOutputAssociation>
      <bpmn:script>all(reviewVotes)</bpmn:script>
      </bpmn:scriptTask>
      <bpmn:endEvent id="roundCompleted" />
      <bpmn:sequenceFlow id="startDecision" sourceRef="roundStarted" targetRef="determineReviews" />
      <bpmn:sequenceFlow id="startReviews" sourceRef="determineReviews" targetRef="reviewOrder" />
      <bpmn:sequenceFlow id="evaluateReviews" sourceRef="reviewOrder" targetRef="evaluateReview" />
      <bpmn:sequenceFlow id="finishRound" sourceRef="evaluateReview" targetRef="roundCompleted" />
    </bpmn:subProcess>
    <bpmn:exclusiveGateway id="reviewDecision" name="All reviews approve?" gatewayDirection="Diverging" default="requiresRevision" />
    <bpmn:userTask id="reviseOrder" name="Revise order">
      <bpmn:ioSpecification>
        <bpmn:dataInput id="previousAmount" name="amount" itemSubjectRef="OrderAmount" />
        <bpmn:dataOutput id="revisedAmount" name="amount" itemSubjectRef="OrderAmount" />
        <bpmn:inputSet><bpmn:dataInputRefs>previousAmount</bpmn:dataInputRefs></bpmn:inputSet>
        <bpmn:outputSet><bpmn:dataOutputRefs>revisedAmount</bpmn:dataOutputRefs></bpmn:outputSet>
      </bpmn:ioSpecification>
      <bpmn:dataInputAssociation>
        <bpmn:sourceRef>amount</bpmn:sourceRef><bpmn:targetRef>previousAmount</bpmn:targetRef>
      </bpmn:dataInputAssociation>
      <bpmn:dataOutputAssociation>
        <bpmn:sourceRef>revisedAmount</bpmn:sourceRef><bpmn:targetRef>amount</bpmn:targetRef>
      </bpmn:dataOutputAssociation>
      <bpmn:potentialOwner><bpmn:resourceRef>requester</bpmn:resourceRef></bpmn:potentialOwner>
    </bpmn:userTask>
    <bpmn:subProcess id="confirmationScope" name="Await order confirmation">
      <bpmn:startEvent id="confirmationStarted" />
      <bpmn:userTask id="confirmOrder" name="Confirm order">
        <bpmn:potentialOwner><bpmn:resourceRef>reviewer</bpmn:resourceRef></bpmn:potentialOwner>
      </bpmn:userTask>
      <bpmn:endEvent id="confirmationCompleted">
        <bpmn:terminateEventDefinition />
      </bpmn:endEvent>
      <bpmn:boundaryEvent id="confirmationReminder" name="Reminder after 30 seconds" attachedToRef="confirmOrder" cancelActivity="false">
        <bpmn:timerEventDefinition>
          <bpmn:timeDuration xsi:type="bpmn:tFormalExpression">"PT30S"</bpmn:timeDuration>
        </bpmn:timerEventDefinition>
      </bpmn:boundaryEvent>
      <bpmn:serviceTask id="sendConfirmationReminder" name="Send confirmation reminder" getriebe:handler="send-confirmation-reminder" />
      <bpmn:endEvent id="reminderCompleted" />
      <bpmn:sequenceFlow id="openConfirmation" sourceRef="confirmationStarted" targetRef="confirmOrder" />
      <bpmn:sequenceFlow id="finishConfirmation" sourceRef="confirmOrder" targetRef="confirmationCompleted" />
      <bpmn:sequenceFlow id="startReminder" sourceRef="confirmationReminder" targetRef="sendConfirmationReminder" />
      <bpmn:sequenceFlow id="finishReminder" sourceRef="sendConfirmationReminder" targetRef="reminderCompleted" />
    </bpmn:subProcess>
    <bpmn:boundaryEvent id="confirmationDeadline" name="Confirmation deadline" attachedToRef="confirmationScope" cancelActivity="true">
      <bpmn:timerEventDefinition>
        <bpmn:timeDuration xsi:type="bpmn:tFormalExpression">"PT1M"</bpmn:timeDuration>
      </bpmn:timerEventDefinition>
    </bpmn:boundaryEvent>
    <bpmn:userTask id="seniorSignoff" name="Senior sign-off">
      <bpmn:potentialOwner><bpmn:resourceRef>senior-reviewer</bpmn:resourceRef></bpmn:potentialOwner>
    </bpmn:userTask>
    <bpmn:exclusiveGateway id="confirmationJoin" gatewayDirection="Converging" />
    <bpmn:endEvent id="orderApproved" name="Order approved" />
    <bpmn:sequenceFlow id="initialSubmission" sourceRef="submitted" targetRef="reviewEntry" />
    <bpmn:sequenceFlow id="beginReview" sourceRef="reviewEntry" targetRef="reviewRound" />
    <bpmn:sequenceFlow id="decideReview" sourceRef="reviewRound" targetRef="reviewDecision" />
    <bpmn:sequenceFlow id="allApproved" sourceRef="reviewDecision" targetRef="confirmationScope">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">approved</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="requiresRevision" sourceRef="reviewDecision" targetRef="reviseOrder" />
    <bpmn:sequenceFlow id="resubmit" name="Resubmit" sourceRef="reviseOrder" targetRef="reviewEntry" />
    <bpmn:sequenceFlow id="confirmRegular" sourceRef="confirmationScope" targetRef="confirmationJoin" />
    <bpmn:sequenceFlow id="confirmTimeout" sourceRef="confirmationDeadline" targetRef="seniorSignoff" />
    <bpmn:sequenceFlow id="confirmEscalated" sourceRef="seniorSignoff" targetRef="confirmationJoin" />
    <bpmn:sequenceFlow id="finishOrder" sourceRef="confirmationJoin" targetRef="orderApproved" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="combinedOrderApprovalDiagram">
    <bpmndi:BPMNPlane id="combinedOrderApprovalPlane" bpmnElement="combinedOrderApproval">
      <bpmndi:BPMNShape id="submittedShape" bpmnElement="submitted"><dc:Bounds x="54" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewEntryShape" bpmnElement="reviewEntry" isMarkerVisible="true"><dc:Bounds x="126" y="135" width="50" height="50" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewRoundShape" bpmnElement="reviewRound" isExpanded="true"><dc:Bounds x="207" y="91" width="537" height="139" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="roundStartedShape" bpmnElement="roundStarted"><dc:Bounds x="228" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="determineReviewsShape" bpmnElement="determineReviews"><dc:Bounds x="284" y="126" width="104" height="68" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewOrderShape" bpmnElement="reviewOrder"><dc:Bounds x="416" y="126" width="106" height="68" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="evaluateReviewShape" bpmnElement="evaluateReview"><dc:Bounds x="552" y="126" width="108" height="68" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="roundCompletedShape" bpmnElement="roundCompleted"><dc:Bounds x="684" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviewDecisionShape" bpmnElement="reviewDecision" isMarkerVisible="true"><dc:Bounds x="778" y="135" width="50" height="50" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationScopeShape" bpmnElement="confirmationScope" isExpanded="true"><dc:Bounds x="867" y="53" width="550" height="278" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationStartedShape" bpmnElement="confirmationStarted"><dc:Bounds x="894" y="105" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmOrderShape" bpmnElement="confirmOrder"><dc:Bounds x="1051" y="92" width="120" height="62" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationCompletedShape" bpmnElement="confirmationCompleted"><dc:Bounds x="1330" y="105" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationReminderShape" bpmnElement="confirmationReminder"><dc:Bounds x="1093" y="136" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="sendConfirmationReminderShape" bpmnElement="sendConfirmationReminder"><dc:Bounds x="1138" y="223" width="149" height="66" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reminderCompletedShape" bpmnElement="reminderCompleted"><dc:Bounds x="1330" y="238" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationDeadlineShape" bpmnElement="confirmationDeadline"><dc:Bounds x="1302" y="313" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="seniorSignoffShape" bpmnElement="seniorSignoff"><dc:Bounds x="1415" y="381" width="120" height="69" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confirmationJoinShape" bpmnElement="confirmationJoin" isMarkerVisible="true"><dc:Bounds x="1573" y="135" width="50" height="50" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orderApprovedShape" bpmnElement="orderApproved"><dc:Bounds x="1665" y="142" width="36" height="36" /></bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reviseOrderShape" bpmnElement="reviseOrder"><dc:Bounds x="424" y="246" width="118" height="70" /></bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="initialSubmissionEdge" bpmnElement="initialSubmission"><di:waypoint x="90" y="160" /><di:waypoint x="126" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="beginReviewEdge" bpmnElement="beginReview"><di:waypoint x="176" y="160" /><di:waypoint x="207" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="startDecisionEdge" bpmnElement="startDecision"><di:waypoint x="264" y="160" /><di:waypoint x="284" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="startReviewsEdge" bpmnElement="startReviews"><di:waypoint x="388" y="160" /><di:waypoint x="416" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="evaluateReviewsEdge" bpmnElement="evaluateReviews"><di:waypoint x="522" y="160" /><di:waypoint x="552" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishRoundEdge" bpmnElement="finishRound"><di:waypoint x="660" y="160" /><di:waypoint x="684" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="decideReviewEdge" bpmnElement="decideReview"><di:waypoint x="744" y="160" /><di:waypoint x="778" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="allApprovedEdge" bpmnElement="allApproved"><di:waypoint x="828" y="160" /><di:waypoint x="867" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="requiresRevisionEdge" bpmnElement="requiresRevision"><di:waypoint x="803" y="185" /><di:waypoint x="803" y="282" /><di:waypoint x="542" y="282" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="resubmitEdge" bpmnElement="resubmit"><di:waypoint x="424" y="282" /><di:waypoint x="151" y="282" /><di:waypoint x="151" y="185" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="openConfirmationEdge" bpmnElement="openConfirmation"><di:waypoint x="930" y="123" /><di:waypoint x="1051" y="123" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishConfirmationEdge" bpmnElement="finishConfirmation"><di:waypoint x="1171" y="123" /><di:waypoint x="1330" y="123" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="startReminderEdge" bpmnElement="startReminder"><di:waypoint x="1111" y="172" /><di:waypoint x="1111" y="256" /><di:waypoint x="1138" y="256" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishReminderEdge" bpmnElement="finishReminder"><di:waypoint x="1287" y="256" /><di:waypoint x="1330" y="256" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="confirmRegularEdge" bpmnElement="confirmRegular"><di:waypoint x="1417" y="160" /><di:waypoint x="1573" y="160" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="confirmTimeoutEdge" bpmnElement="confirmTimeout"><di:waypoint x="1320" y="349" /><di:waypoint x="1320" y="415" /><di:waypoint x="1415" y="415" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="confirmEscalatedEdge" bpmnElement="confirmEscalated"><di:waypoint x="1535" y="415" /><di:waypoint x="1598" y="415" /><di:waypoint x="1598" y="185" /></bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="finishOrderEdge" bpmnElement="finishOrder"><di:waypoint x="1623" y="160" /><di:waypoint x="1665" y="160" /></bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>
