<?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:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:modeler="http://camunda.org/schema/modeler/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="@bpmnkit/core" exporterVersion="0.0.23" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.9.0">
  <bpmn:message id="Message_payment_received" name="payment-received">
    <bpmn:extensionElements>
      <zeebe:subscription correlationKey="=invoiceId"/>
    </bpmn:extensionElements>
  </bpmn:message>
  <bpmn:process id="payment-collection" name="Payment Collection &amp; Dunning" isExecutable="true">
    <bpmn:extensionElements>
      <zeebe:versionTag value="1.0.0"/>
    </bpmn:extensionElements>
    <bpmn:startEvent id="invoice-issued" name="Invoice issued">
      <bpmn:outgoing>Flow_invoice-issued_start-reminders</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:scriptTask id="start-reminders" name="No reminders yet">
      <bpmn:extensionElements>
        <zeebe:script expression="=0" resultVariable="reminders"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_invoice-issued_start-reminders</bpmn:incoming>
      <bpmn:outgoing>Flow_start-reminders_send-invoice</bpmn:outgoing>
    </bpmn:scriptTask>
    <bpmn:serviceTask id="send-invoice" name="Send invoice">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="invoice-send"/>
        <zeebe:taskHeaders>
          <zeebe:header key="template" value="invoice"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_start-reminders_send-invoice</bpmn:incoming>
      <bpmn:outgoing>Flow_send-invoice_await-merge</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:exclusiveGateway id="await-merge">
      <bpmn:incoming>Flow_send-invoice_await-merge</bpmn:incoming>
      <bpmn:incoming>Flow_send-reminder_await-merge</bpmn:incoming>
      <bpmn:incoming>Flow_send-statement_await-merge</bpmn:incoming>
      <bpmn:outgoing>Flow_await-merge_await-payment</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:receiveTask id="await-payment" name="Await payment" messageRef="Message_payment_received">
      <bpmn:incoming>Flow_await-merge_await-payment</bpmn:incoming>
      <bpmn:outgoing>Flow_await-payment_apply-payment</bpmn:outgoing>
    </bpmn:receiveTask>
    <bpmn:boundaryEvent id="payment-overdue" name="14 days unpaid" attachedToRef="await-payment">
      <bpmn:outgoing>Flow_payment-overdue_count-reminder</bpmn:outgoing>
      <bpmn:timerEventDefinition>
        <bpmn:timeDuration>P14D</bpmn:timeDuration>
      </bpmn:timerEventDefinition>
    </bpmn:boundaryEvent>
    <bpmn:scriptTask id="count-reminder" name="Count reminder">
      <bpmn:extensionElements>
        <zeebe:script expression="=reminders + 1" resultVariable="reminders"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_payment-overdue_count-reminder</bpmn:incoming>
      <bpmn:outgoing>Flow_count-reminder_reminders-left</bpmn:outgoing>
    </bpmn:scriptTask>
    <bpmn:exclusiveGateway id="reminders-left" name="Third reminder?" default="Flow_reminders-left_send-reminder">
      <bpmn:incoming>Flow_count-reminder_reminders-left</bpmn:incoming>
      <bpmn:outgoing>Flow_reminders-left_hand-to-collections</bpmn:outgoing>
      <bpmn:outgoing>Flow_reminders-left_send-reminder</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:serviceTask id="hand-to-collections" name="Hand over to collections">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="collections-assign"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_reminders-left_hand-to-collections</bpmn:incoming>
      <bpmn:outgoing>Flow_hand-to-collections_in-collections</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="in-collections" name="In collections">
      <bpmn:incoming>Flow_hand-to-collections_in-collections</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:serviceTask id="send-reminder" name="Send payment reminder">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="invoice-send"/>
        <zeebe:taskHeaders>
          <zeebe:header key="template" value="reminder"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_reminders-left_send-reminder</bpmn:incoming>
      <bpmn:outgoing>Flow_send-reminder_await-merge</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="apply-payment" name="Apply payment to ledger">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="ledger-apply-payment"/>
        <zeebe:ioMapping>
          <zeebe:output source="=balance" target="outstanding"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_await-payment_apply-payment</bpmn:incoming>
      <bpmn:outgoing>Flow_apply-payment_settled</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:exclusiveGateway id="settled" name="Balance left?" default="Flow_settled_send-statement">
      <bpmn:incoming>Flow_apply-payment_settled</bpmn:incoming>
      <bpmn:outgoing>Flow_settled_settled_join</bpmn:outgoing>
      <bpmn:outgoing>Flow_settled_write-off</bpmn:outgoing>
      <bpmn:outgoing>Flow_settled_send-statement</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:serviceTask id="write-off" name="Write off small balance">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="ledger-write-off"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_settled_write-off</bpmn:incoming>
      <bpmn:outgoing>Flow_write-off_settled_join</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="send-statement" name="Send balance statement">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="invoice-send"/>
        <zeebe:taskHeaders>
          <zeebe:header key="template" value="statement"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_settled_send-statement</bpmn:incoming>
      <bpmn:outgoing>Flow_send-statement_await-merge</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="close-invoice" name="Close invoice">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="ledger-close-invoice"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_settled_join_close-invoice</bpmn:incoming>
      <bpmn:outgoing>Flow_close-invoice_invoice-paid</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="invoice-paid" name="Invoice paid">
      <bpmn:incoming>Flow_close-invoice_invoice-paid</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:exclusiveGateway id="settled_join">
      <bpmn:incoming>Flow_settled_settled_join</bpmn:incoming>
      <bpmn:incoming>Flow_write-off_settled_join</bpmn:incoming>
      <bpmn:outgoing>Flow_settled_join_close-invoice</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:sequenceFlow id="Flow_invoice-issued_start-reminders" sourceRef="invoice-issued" targetRef="start-reminders"/>
    <bpmn:sequenceFlow id="Flow_start-reminders_send-invoice" sourceRef="start-reminders" targetRef="send-invoice"/>
    <bpmn:sequenceFlow id="Flow_send-invoice_await-merge" sourceRef="send-invoice" targetRef="await-merge"/>
    <bpmn:sequenceFlow id="Flow_await-merge_await-payment" sourceRef="await-merge" targetRef="await-payment"/>
    <bpmn:sequenceFlow id="Flow_payment-overdue_count-reminder" sourceRef="payment-overdue" targetRef="count-reminder"/>
    <bpmn:sequenceFlow id="Flow_count-reminder_reminders-left" sourceRef="count-reminder" targetRef="reminders-left"/>
    <bpmn:sequenceFlow id="Flow_reminders-left_hand-to-collections" sourceRef="reminders-left" targetRef="hand-to-collections" name="collections">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=reminders &gt;= 3</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="Flow_hand-to-collections_in-collections" sourceRef="hand-to-collections" targetRef="in-collections"/>
    <bpmn:sequenceFlow id="Flow_reminders-left_send-reminder" sourceRef="reminders-left" targetRef="send-reminder" name="remind"/>
    <bpmn:sequenceFlow id="Flow_send-reminder_await-merge" sourceRef="send-reminder" targetRef="await-merge"/>
    <bpmn:sequenceFlow id="Flow_await-payment_apply-payment" sourceRef="await-payment" targetRef="apply-payment"/>
    <bpmn:sequenceFlow id="Flow_apply-payment_settled" sourceRef="apply-payment" targetRef="settled"/>
    <bpmn:sequenceFlow id="Flow_settled_settled_join" sourceRef="settled" targetRef="settled_join" name="settled">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=outstanding &lt;= 0</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="Flow_settled_write-off" sourceRef="settled" targetRef="write-off" name="write-off">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=outstanding &lt; 5</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="Flow_write-off_settled_join" sourceRef="write-off" targetRef="settled_join"/>
    <bpmn:sequenceFlow id="Flow_settled_send-statement" sourceRef="settled" targetRef="send-statement" name="statement"/>
    <bpmn:sequenceFlow id="Flow_send-statement_await-merge" sourceRef="send-statement" targetRef="await-merge"/>
    <bpmn:sequenceFlow id="Flow_close-invoice_invoice-paid" sourceRef="close-invoice" targetRef="invoice-paid"/>
    <bpmn:sequenceFlow id="Flow_settled_join_close-invoice" sourceRef="settled_join" targetRef="close-invoice"/>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="payment-collection_di">
    <bpmndi:BPMNPlane id="payment-collection_di_plane" bpmnElement="payment-collection">
      <bpmndi:BPMNShape id="invoice-issued_di" bpmnElement="invoice-issued">
        <dc:Bounds x="51" y="42" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="20" y="82" width="98" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="start-reminders_di" bpmnElement="start-reminders">
        <dc:Bounds x="187" y="20" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="send-invoice_di" bpmnElement="send-invoice">
        <dc:Bounds x="387" y="20" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="await-merge_di" bpmnElement="await-merge">
        <dc:Bounds x="587" y="35" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="await-payment_di" bpmnElement="await-payment">
        <dc:Bounds x="737" y="20" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="payment-overdue_di" bpmnElement="payment-overdue">
        <dc:Bounds x="769" y="82" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="738" y="122" width="98" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="count-reminder_di" bpmnElement="count-reminder">
        <dc:Bounds x="937" y="500" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reminders-left_di" bpmnElement="reminders-left">
        <dc:Bounds x="1137" y="515" width="50" height="50"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1110" y="569" width="105" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="hand-to-collections_di" bpmnElement="hand-to-collections">
        <dc:Bounds x="1287" y="500" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="in-collections_di" bpmnElement="in-collections">
        <dc:Bounds x="1494" y="522" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1463" y="562" width="98" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="send-reminder_di" bpmnElement="send-reminder">
        <dc:Bounds x="1287" y="660" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="apply-payment_di" bpmnElement="apply-payment">
        <dc:Bounds x="937" y="20" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="settled_di" bpmnElement="settled">
        <dc:Bounds x="1137" y="35" width="50" height="50"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1117" y="89" width="91" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="write-off_di" bpmnElement="write-off">
        <dc:Bounds x="1287" y="180" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="send-statement_di" bpmnElement="send-statement">
        <dc:Bounds x="1287" y="340" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="close-invoice_di" bpmnElement="close-invoice">
        <dc:Bounds x="1637" y="20" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="invoice-paid_di" bpmnElement="invoice-paid">
        <dc:Bounds x="1837" y="42" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1813" y="82" width="84" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="settled_join_di" bpmnElement="settled_join">
        <dc:Bounds x="1487" y="35" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_invoice-issued_start-reminders_di" bpmnElement="Flow_invoice-issued_start-reminders">
        <di:waypoint x="87" y="60"/>
        <di:waypoint x="187" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_start-reminders_send-invoice_di" bpmnElement="Flow_start-reminders_send-invoice">
        <di:waypoint x="287" y="60"/>
        <di:waypoint x="387" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_send-invoice_await-merge_di" bpmnElement="Flow_send-invoice_await-merge">
        <di:waypoint x="487" y="60"/>
        <di:waypoint x="587" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_await-merge_await-payment_di" bpmnElement="Flow_await-merge_await-payment">
        <di:waypoint x="637" y="60"/>
        <di:waypoint x="737" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_payment-overdue_count-reminder_di" bpmnElement="Flow_payment-overdue_count-reminder">
        <di:waypoint x="787" y="118"/>
        <di:waypoint x="787" y="540"/>
        <di:waypoint x="937" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_count-reminder_reminders-left_di" bpmnElement="Flow_count-reminder_reminders-left">
        <di:waypoint x="1037" y="540"/>
        <di:waypoint x="1137" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_reminders-left_hand-to-collections_di" bpmnElement="Flow_reminders-left_hand-to-collections">
        <di:waypoint x="1187" y="540"/>
        <di:waypoint x="1287" y="540"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1199" y="516" width="77" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_hand-to-collections_in-collections_di" bpmnElement="Flow_hand-to-collections_in-collections">
        <di:waypoint x="1387" y="540"/>
        <di:waypoint x="1494" y="540"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_reminders-left_send-reminder_di" bpmnElement="Flow_reminders-left_send-reminder">
        <di:waypoint x="1162" y="565"/>
        <di:waypoint x="1162" y="700"/>
        <di:waypoint x="1287" y="700"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1138" y="626" width="42" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_send-reminder_await-merge_di" bpmnElement="Flow_send-reminder_await-merge">
        <di:waypoint x="1337" y="740"/>
        <di:waypoint x="1337" y="780"/>
        <di:waypoint x="612" y="780"/>
        <di:waypoint x="612" y="85"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_await-payment_apply-payment_di" bpmnElement="Flow_await-payment_apply-payment">
        <di:waypoint x="837" y="60"/>
        <di:waypoint x="937" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_apply-payment_settled_di" bpmnElement="Flow_apply-payment_settled">
        <di:waypoint x="1037" y="60"/>
        <di:waypoint x="1137" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_settled_settled_join_di" bpmnElement="Flow_settled_settled_join">
        <di:waypoint x="1187" y="60"/>
        <di:waypoint x="1487" y="60"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1313" y="36" width="49" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_settled_write-off_di" bpmnElement="Flow_settled_write-off">
        <di:waypoint x="1162" y="85"/>
        <di:waypoint x="1162" y="220"/>
        <di:waypoint x="1287" y="220"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1138" y="146" width="63" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_write-off_settled_join_di" bpmnElement="Flow_write-off_settled_join">
        <di:waypoint x="1337" y="180"/>
        <di:waypoint x="1337" y="60"/>
        <di:waypoint x="1487" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_settled_send-statement_di" bpmnElement="Flow_settled_send-statement">
        <di:waypoint x="1162" y="85"/>
        <di:waypoint x="1162" y="380"/>
        <di:waypoint x="1287" y="380"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1138" y="226" width="63" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_send-statement_await-merge_di" bpmnElement="Flow_send-statement_await-merge">
        <di:waypoint x="1387" y="380"/>
        <di:waypoint x="1437" y="380"/>
        <di:waypoint x="1437" y="-20"/>
        <di:waypoint x="537" y="-20"/>
        <di:waypoint x="537" y="60"/>
        <di:waypoint x="587" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_close-invoice_invoice-paid_di" bpmnElement="Flow_close-invoice_invoice-paid">
        <di:waypoint x="1737" y="60"/>
        <di:waypoint x="1837" y="60"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_settled_join_close-invoice_di" bpmnElement="Flow_settled_join_close-invoice">
        <di:waypoint x="1537" y="60"/>
        <di:waypoint x="1637" y="60"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

