<?xml version="1.0" encoding="UTF-8"?>
<!-- Purchase Order Approval (template purchase-order).

     Started with: vendor, amount, requestedBy.

     Purchase requested
       -> Check the budget         user task; complete with budgetAvailable = true
       -> Budget available?        no -> Tell the requester -> Refused
       -> Manager approval         user task; complete with managerApproved = true
       -> Manager approved?        up to 5,000 -> Issue the purchase order
                                   over 5,000  -> Director approval
       -> Director approval        user task; complete with directorApproved = true
       -> Director approved?       up to 25,000 -> Issue the purchase order
                                   over 25,000  -> CFO approval
       -> CFO approval             user task; complete with cfoApproved = true
       -> Issue the purchase order user task; complete with poNumber
       -> Record the order in the ERP   user task
       -> Ordered

     Each level answers in its own variable, so one level's yes is never read as
     the next level's. Anything other than true is a refusal, which goes to
     Tell the requester. The thresholds are 5,000 and 25,000 in the request's
     currency: change them on the gateways' flows.

     The answers are variables their steps are completed with: budgetAvailable, managerApproved, directorApproved, cfoApproved. 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_purchase_order">

  <process id="purchase_order" name="Purchase Order Approval" isExecutable="true">
    <documentation>A purchase request: a budget check, then approval by the manager, by a director over 5,000 and by the CFO over 25,000, then the order issued and recorded in the ERP.</documentation>

    <startEvent id="purchase_requested" name="Purchase requested"/>

    <userTask id="check_budget" name="Check the budget">
      <documentation>Finance checks that the cost centre can pay for it this period. Complete with budgetAvailable = true or false.</documentation>
    </userTask>

    <exclusiveGateway id="gw_budget" name="Budget available?" default="f_no_budget"/>

    <userTask id="manager_approval" name="Manager approval">
      <documentation>Complete with managerApproved = true to approve the purchase, or false to refuse it.</documentation>
    </userTask>

    <exclusiveGateway id="gw_manager" name="Manager approved?" default="f_manager_refused"/>

    <userTask id="director_approval" name="Director approval">
      <documentation>A purchase over 5,000. Complete with directorApproved = true to approve it, or false to refuse it.</documentation>
    </userTask>

    <exclusiveGateway id="gw_director" name="Director approved?" default="f_director_refused"/>

    <userTask id="cfo_approval" name="CFO approval">
      <documentation>A purchase over 25,000. Complete with cfoApproved = true to approve it, or false to refuse it.</documentation>
    </userTask>

    <exclusiveGateway id="gw_cfo" name="CFO approved?" default="f_cfo_refused"/>

    <userTask id="issue_po" name="Issue the purchase order">
      <documentation>Send the order to the vendor. Complete with poNumber = the order's number.</documentation>
    </userTask>

    <userTask id="record_in_erp" name="Record the order in the ERP">
      <documentation>Enter the order and its number in your ERP. To do it automatically, replace this step with a service task whose worker calls your ERP.</documentation>
    </userTask>

    <endEvent id="ordered" name="Ordered"/>

    <userTask id="tell_requester" name="Tell the requester">
      <documentation>Tell the requester the purchase was refused, at which step, and why.</documentation>
    </userTask>

    <endEvent id="refused" name="Refused"/>

    <sequenceFlow id="f_requested" sourceRef="purchase_requested" targetRef="check_budget"/>
    <sequenceFlow id="f_budget_checked" sourceRef="check_budget" targetRef="gw_budget"/>
    <sequenceFlow id="f_budget_ok" name="yes" sourceRef="gw_budget" targetRef="manager_approval">
      <conditionExpression xsi:type="tFormalExpression">budgetAvailable = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_no_budget" name="no" sourceRef="gw_budget" targetRef="tell_requester"/>
    <sequenceFlow id="f_manager_done" sourceRef="manager_approval" targetRef="gw_manager"/>
    <sequenceFlow id="f_manager_up_to_5000" name="approved, up to 5,000" sourceRef="gw_manager" targetRef="issue_po">
      <conditionExpression xsi:type="tFormalExpression">managerApproved = true and amount &lt;= 5000</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_manager_over_5000" name="approved, over 5,000" sourceRef="gw_manager" targetRef="director_approval">
      <conditionExpression xsi:type="tFormalExpression">managerApproved = true and amount &gt; 5000</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_manager_refused" name="refused" sourceRef="gw_manager" targetRef="tell_requester"/>
    <sequenceFlow id="f_director_done" sourceRef="director_approval" targetRef="gw_director"/>
    <sequenceFlow id="f_director_up_to_25000" name="approved, up to 25,000" sourceRef="gw_director" targetRef="issue_po">
      <conditionExpression xsi:type="tFormalExpression">directorApproved = true and amount &lt;= 25000</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_director_over_25000" name="approved, over 25,000" sourceRef="gw_director" targetRef="cfo_approval">
      <conditionExpression xsi:type="tFormalExpression">directorApproved = true and amount &gt; 25000</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_director_refused" name="refused" sourceRef="gw_director" targetRef="tell_requester"/>
    <sequenceFlow id="f_cfo_done" sourceRef="cfo_approval" targetRef="gw_cfo"/>
    <sequenceFlow id="f_cfo_approved" name="approved" sourceRef="gw_cfo" targetRef="issue_po">
      <conditionExpression xsi:type="tFormalExpression">cfoApproved = true</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="f_cfo_refused" name="refused" sourceRef="gw_cfo" targetRef="tell_requester"/>
    <sequenceFlow id="f_issued" sourceRef="issue_po" targetRef="record_in_erp"/>
    <sequenceFlow id="f_recorded" sourceRef="record_in_erp" targetRef="ordered"/>
    <sequenceFlow id="f_told" sourceRef="tell_requester" targetRef="refused"/>
  </process>

  <bpmndi:BPMNDiagram id="diagram_purchase_order">
    <bpmndi:BPMNPlane id="plane_purchase_order" bpmnElement="purchase_order">
      <bpmndi:BPMNShape id="shape_purchase_requested" bpmnElement="purchase_requested">
        <dc:Bounds x="82" y="102" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_check_budget" bpmnElement="check_budget">
        <dc:Bounds x="215" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_budget" bpmnElement="gw_budget" isMarkerVisible="true">
        <dc:Bounds x="415" y="95" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_manager_approval" bpmnElement="manager_approval">
        <dc:Bounds x="555" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_manager" bpmnElement="gw_manager" isMarkerVisible="true">
        <dc:Bounds x="755" y="95" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_director_approval" bpmnElement="director_approval">
        <dc:Bounds x="895" y="220" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_director" bpmnElement="gw_director" isMarkerVisible="true">
        <dc:Bounds x="1095" y="235" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_cfo_approval" bpmnElement="cfo_approval">
        <dc:Bounds x="1235" y="360" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_gw_cfo" bpmnElement="gw_cfo" isMarkerVisible="true">
        <dc:Bounds x="1435" y="375" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_issue_po" bpmnElement="issue_po">
        <dc:Bounds x="1575" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_record_in_erp" bpmnElement="record_in_erp">
        <dc:Bounds x="1745" y="80" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_ordered" bpmnElement="ordered">
        <dc:Bounds x="1952" y="102" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_tell_requester" bpmnElement="tell_requester">
        <dc:Bounds x="1745" y="500" width="110" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape_refused" bpmnElement="refused">
        <dc:Bounds x="1952" y="522" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge_f_requested" bpmnElement="f_requested">
        <di:waypoint x="118" y="120"/>
        <di:waypoint x="215" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_budget_checked" bpmnElement="f_budget_checked">
        <di:waypoint x="325" y="120"/>
        <di:waypoint x="415" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_budget_ok" bpmnElement="f_budget_ok">
        <di:waypoint x="465" y="120"/>
        <di:waypoint x="555" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_no_budget" bpmnElement="f_no_budget">
        <di:waypoint x="440" y="145"/>
        <di:waypoint x="440" y="540"/>
        <di:waypoint x="1745" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_manager_done" bpmnElement="f_manager_done">
        <di:waypoint x="665" y="120"/>
        <di:waypoint x="755" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_manager_up_to_5000" bpmnElement="f_manager_up_to_5000">
        <di:waypoint x="805" y="120"/>
        <di:waypoint x="1575" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_manager_over_5000" bpmnElement="f_manager_over_5000">
        <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_manager_refused" bpmnElement="f_manager_refused">
        <di:waypoint x="780" y="145"/>
        <di:waypoint x="780" y="540"/>
        <di:waypoint x="1745" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_director_done" bpmnElement="f_director_done">
        <di:waypoint x="1005" y="260"/>
        <di:waypoint x="1095" y="260"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_director_up_to_25000" bpmnElement="f_director_up_to_25000">
        <di:waypoint x="1120" y="235"/>
        <di:waypoint x="1120" y="120"/>
        <di:waypoint x="1575" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_director_over_25000" bpmnElement="f_director_over_25000">
        <di:waypoint x="1120" y="285"/>
        <di:waypoint x="1120" y="400"/>
        <di:waypoint x="1235" y="400"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_director_refused" bpmnElement="f_director_refused">
        <di:waypoint x="1120" y="285"/>
        <di:waypoint x="1120" y="540"/>
        <di:waypoint x="1745" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_cfo_done" bpmnElement="f_cfo_done">
        <di:waypoint x="1345" y="400"/>
        <di:waypoint x="1435" y="400"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_cfo_approved" bpmnElement="f_cfo_approved">
        <di:waypoint x="1460" y="375"/>
        <di:waypoint x="1460" y="120"/>
        <di:waypoint x="1575" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_cfo_refused" bpmnElement="f_cfo_refused">
        <di:waypoint x="1460" y="425"/>
        <di:waypoint x="1460" y="540"/>
        <di:waypoint x="1745" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_issued" bpmnElement="f_issued">
        <di:waypoint x="1685" y="120"/>
        <di:waypoint x="1745" y="120"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_f_recorded" bpmnElement="f_recorded">
        <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="540"/>
        <di:waypoint x="1952" y="540"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
