<?xml version="1.0" encoding="UTF-8"?>
<!-- Contract Approval (template contract-approval).

     Started with: contractTitle, vendor, value.
     Deploy contract_routing.dmn with it: the business rule task evaluates it.

     Contract received
       -> Route by value          decision contract_routing: financeReview (from 10,000),
                                  executiveSignOff (from 100,000) and approvalLevel
       -> Legal review            user task; complete with legalApproved = true
       -> Legal approved?         no -> Tell the vendor -> Declined
       -> Finance review?         financeReview -> Finance review (financeApproved)
       -> Executive sign-off?     executiveSignOff -> Executive sign-off (executiveApproved)
       -> Sign and archive        user task
       -> Signed

     Every review answers in its own variable, and anything other than true is a
     refusal, which goes to Tell the vendor. A contract with no value gets every
     review (contract_routing.dmn).

     The answers are variables their steps are completed with: legalApproved, financeApproved, executiveApproved. The
     template's sample variables start each of them as null, so a step closed
     without its answer takes the safe branch. A run started without them stops with
     an incident at the first gateway whose variable was never set, because the
     engine never reads a missing variable as a no, and the hosted API cannot resume
     a run in INCIDENT. -->
<definitions
  xmlns="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:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:zeebe="http://camunda.org/schema/zeebe/1.0"
  targetNamespace="http://priostack.com/templates"
  id="def_contract_approval">

  <process id="contract_approval" name="Contract Approval" isExecutable="true">
    <documentation>A contract through legal review, a finance review from 10,000 and an executive sign-off from 100,000, as contract_routing.dmn decides from its value, then signed and archived.</documentation>

    <startEvent id="contract_received" name="Contract received"/>

    <businessRuleTask id="route_by_value" name="Route by value">
      <extensionElements>
        <zeebe:calledDecision decisionId="contract_routing"/>
      </extensionElements>
    </businessRuleTask>

    <userTask id="legal_review" name="Legal review">
      <documentation>Terms, liability, data protection and termination. Complete with legalApproved = true, or false with your reasons.</documentation>
    </userTask>

    <exclusiveGateway id="gw_legal" name="Legal approved?" default="f_legal_refused"/>

    <exclusiveGateway id="gw_needs_finance" name="Finance review?" default="f_no_finance"/>

    <userTask id="finance_review" name="Finance review">
      <documentation>Price, payment terms and budget, for a contract worth 10,000 or more. Complete with financeApproved = true, or false with your reasons.</documentation>
    </userTask>

    <exclusiveGateway id="gw_finance" name="Finance approved?" default="f_finance_refused"/>

    <exclusiveGateway id="gw_needs_executive" name="Executive sign-off?" default="f_no_executive"/>

    <userTask id="executive_sign_off" name="Executive sign-off">
      <documentation>A contract worth 100,000 or more. Complete with executiveApproved = true to sign it off, or false with your reasons.</documentation>
    </userTask>

    <exclusiveGateway id="gw_executive" name="Executive approved?" default="f_executive_refused"/>

    <userTask id="sign_and_archive" name="Sign and archive">
      <documentation>Both parties sign; file the signed copy with its renewal date.</documentation>
    </userTask>

    <endEvent id="signed" name="Signed"/>

    <userTask id="tell_vendor" name="Tell the vendor">
      <documentation>Tell the vendor the contract is declined as it stands, and what would change the answer.</documentation>
    </userTask>

    <endEvent id="declined" name="Declined"/>

    <sequenceFlow id="f_received" sourceRef="contract_received" targetRef="route_by_value"/>
    <sequenceFlow id="f_routed" sourceRef="route_by_value" targetRef="legal_review"/>
    <sequenceFlow id="f_legal_done" sourceRef="legal_review" targetRef="gw_legal"/>
    <sequenceFlow id="f_legal_approved" name="yes" sourceRef="gw_legal" targetRef="gw_needs_finance">
      <conditionExpression xsi:type="tFormalExpression">legalApproved = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_legal_refused" name="no" sourceRef="gw_legal" targetRef="tell_vendor"/>
    <sequenceFlow id="f_finance_needed" name="10,000 or more" sourceRef="gw_needs_finance" targetRef="finance_review">
      <conditionExpression xsi:type="tFormalExpression">financeReview = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_no_finance" name="under 10,000" sourceRef="gw_needs_finance" targetRef="gw_needs_executive"/>
    <sequenceFlow id="f_finance_done" sourceRef="finance_review" targetRef="gw_finance"/>
    <sequenceFlow id="f_finance_approved" name="yes" sourceRef="gw_finance" targetRef="gw_needs_executive">
      <conditionExpression xsi:type="tFormalExpression">financeApproved = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_finance_refused" name="no" sourceRef="gw_finance" targetRef="tell_vendor"/>
    <sequenceFlow id="f_executive_needed" name="100,000 or more" sourceRef="gw_needs_executive" targetRef="executive_sign_off">
      <conditionExpression xsi:type="tFormalExpression">executiveSignOff = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_no_executive" name="under 100,000" sourceRef="gw_needs_executive" targetRef="sign_and_archive"/>
    <sequenceFlow id="f_executive_done" sourceRef="executive_sign_off" targetRef="gw_executive"/>
    <sequenceFlow id="f_executive_approved" name="yes" sourceRef="gw_executive" targetRef="sign_and_archive">
      <conditionExpression xsi:type="tFormalExpression">executiveApproved = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_executive_refused" name="no" sourceRef="gw_executive" targetRef="tell_vendor"/>
    <sequenceFlow id="f_signed" sourceRef="sign_and_archive" targetRef="signed"/>
    <sequenceFlow id="f_told" sourceRef="tell_vendor" targetRef="declined"/>
  </process>

  <bpmndi:BPMNDiagram id="diagram_contract_approval">
    <bpmndi:BPMNPlane id="plane_contract_approval" bpmnElement="contract_approval">
      <bpmndi:BPMNShape id="shape_contract_received" bpmnElement="contract_received">
        <dc:Bounds x="82" y="102" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_route_by_value" bpmnElement="route_by_value">
        <dc:Bounds x="215" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_legal_review" bpmnElement="legal_review">
        <dc:Bounds x="385" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_legal" bpmnElement="gw_legal" isMarkerVisible="true">
        <dc:Bounds x="585" y="95" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_needs_finance" bpmnElement="gw_needs_finance" isMarkerVisible="true">
        <dc:Bounds x="755" y="95" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_finance_review" bpmnElement="finance_review">
        <dc:Bounds x="895" y="220" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_finance" bpmnElement="gw_finance" isMarkerVisible="true">
        <dc:Bounds x="1095" y="235" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_needs_executive" bpmnElement="gw_needs_executive" isMarkerVisible="true">
        <dc:Bounds x="1265" y="95" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_executive_sign_off" bpmnElement="executive_sign_off">
        <dc:Bounds x="1405" y="220" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_executive" bpmnElement="gw_executive" isMarkerVisible="true">
        <dc:Bounds x="1605" y="235" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_sign_and_archive" bpmnElement="sign_and_archive">
        <dc:Bounds x="1745" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_signed" bpmnElement="signed">
        <dc:Bounds x="1952" y="102" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_tell_vendor" bpmnElement="tell_vendor">
        <dc:Bounds x="1745" y="360" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_declined" bpmnElement="declined">
        <dc:Bounds x="1952" y="382" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge_f_received" bpmnElement="f_received">
        <di:waypoint x="118" y="120"/>
        <di:waypoint x="215" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_routed" bpmnElement="f_routed">
        <di:waypoint x="325" y="120"/>
        <di:waypoint x="385" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_legal_done" bpmnElement="f_legal_done">
        <di:waypoint x="495" y="120"/>
        <di:waypoint x="585" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_legal_approved" bpmnElement="f_legal_approved">
        <di:waypoint x="635" y="120"/>
        <di:waypoint x="755" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_legal_refused" bpmnElement="f_legal_refused">
        <di:waypoint x="610" y="145"/>
        <di:waypoint x="610" y="400"/>
        <di:waypoint x="1745" y="400"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_finance_needed" bpmnElement="f_finance_needed">
        <di:waypoint x="780" y="145"/>
        <di:waypoint x="780" y="260"/>
        <di:waypoint x="895" y="260"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_no_finance" bpmnElement="f_no_finance">
        <di:waypoint x="805" y="120"/>
        <di:waypoint x="1265" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_finance_done" bpmnElement="f_finance_done">
        <di:waypoint x="1005" y="260"/>
        <di:waypoint x="1095" y="260"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_finance_approved" bpmnElement="f_finance_approved">
        <di:waypoint x="1120" y="235"/>
        <di:waypoint x="1120" y="120"/>
        <di:waypoint x="1265" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_finance_refused" bpmnElement="f_finance_refused">
        <di:waypoint x="1120" y="285"/>
        <di:waypoint x="1120" y="400"/>
        <di:waypoint x="1745" y="400"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_executive_needed" bpmnElement="f_executive_needed">
        <di:waypoint x="1290" y="145"/>
        <di:waypoint x="1290" y="260"/>
        <di:waypoint x="1405" y="260"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_no_executive" bpmnElement="f_no_executive">
        <di:waypoint x="1315" y="120"/>
        <di:waypoint x="1745" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_executive_done" bpmnElement="f_executive_done">
        <di:waypoint x="1515" y="260"/>
        <di:waypoint x="1605" y="260"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_executive_approved" bpmnElement="f_executive_approved">
        <di:waypoint x="1630" y="235"/>
        <di:waypoint x="1630" y="120"/>
        <di:waypoint x="1745" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_executive_refused" bpmnElement="f_executive_refused">
        <di:waypoint x="1630" y="285"/>
        <di:waypoint x="1630" y="400"/>
        <di:waypoint x="1745" y="400"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_signed" bpmnElement="f_signed">
        <di:waypoint x="1855" y="120"/>
        <di:waypoint x="1952" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_told" bpmnElement="f_told">
        <di:waypoint x="1855" y="400"/>
        <di:waypoint x="1952" y="400"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
