<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:mlpdsu="http://www.placeholder/schema/status-update"
           targetNamespace="http://www.placeholder/schema/status-update"
           elementFormDefault="qualified">

    <xs:annotation>
        <xs:documentation>
            This schema definition describes the information required by Magicline about changes of debt collection
            cases (sent by debt collection provider to Magicline). Such status updates might be intermediate changes
            (the debtor has paid some amount) or changes that close a case (due to full payment, derecognition, etc.).
            The XML must always contain the full state of a debtor, so if there are multiple payments of a debtor the
            XML must always contain the total amount of payments, not a delta between the last and current state.

            Intermediate payments and closure of cases are distinguished by the presence of a "Closure" element within
            "CCase". If no such Closure element is present it is treated as intermediate payment. If the Closure element
            is present the case is closed (the exact kind of closure then needs to be specified using a "ClosureType").

            A case can be reversed completely (e.g. because the Client requested this), in such a case the ClosureType
            "reversal" must be used. If only single payments need be reversed (e.g. because of a returned debit), the
            "PaidAmount" of a "Debt" should be reduced by the amount to be reversed.
        </xs:documentation>
    </xs:annotation>

    <xs:element name="StatusUpdate">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>
                    Version: schema version ("3")
                    Date: Date of XML generation.
                </xs:documentation>
            </xs:annotation>
            <xs:sequence>
                <xs:element name="Version" type="xs:decimal" />
                <xs:element name="Date" type="xs:date" />
                <xs:element name="Agency" type="mlpdsu:Agency" />
                <xs:element name="Client" type="mlpdsu:Client" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="Client">
        <xs:sequence>
            <xs:element name="Debtor" type="mlpdsu:Debtor" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:decimal" use="required" />
    </xs:complexType>

    <xs:complexType name="Debtor">
        <xs:sequence>
            <xs:element name="CCase" type="mlpdsu:CCase" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="CCase">
        <xs:annotation>
            <xs:documentation>
                agencyId: Unique ID of this case, used by the debt collection agency to identify a case
                Closure: Optional, MUST be omitted if this contains only an intermediate update of debts
                RefNo: Optional, PayDue "Aktenzeichen"
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="RefNo" type="xs:decimal" minOccurs="0" />
            <xs:element name="Closure" type="mlpdsu:Closure" minOccurs="0" />
            <xs:element name="CaseReferences" type="mlpdsu:CaseReferences" />
            <xs:element name="Debt" type="mlpdsu:Debt" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="agencyId" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="Closure">
        <xs:all>
            <xs:element name="Type" type="mlpdsu:ClosureType" />
            <xs:element name="Options" type="mlpdsu:ClosureOptions" minOccurs="0" />
            <xs:element name="Date" type="xs:date" />
        </xs:all>
    </xs:complexType>

    <xs:complexType name="CaseReferences">
        <xs:annotation>
            <xs:documentation>
                CaseID: This list must contain all transferred CCase.CCaseId (from spa_debt_transfer.xsd) that belong
                to this case.
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="CCaseId" type="xs:string" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="ClosureType">
        <xs:annotation>
            <xs:documentation>
                positive: Debtor has paid and should not be in debt collection anymore.
                negative: The case should be closed negatively, e.g. because of personal bankruptcy of the debtor.
                reversal: The client has withdrawn the debt collection.
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:NMTOKEN">
            <xs:enumeration value="positive"/>
            <xs:enumeration value="negative"/>
            <xs:enumeration value="reversal"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="ClosureOptions">
        <xs:all>
            <xs:element name="WriteOffRemainingDebts" type="mlpdsu:WriteOffRemainingDebts" minOccurs="0" />
        </xs:all>
    </xs:complexType>

    <xs:complexType name="WriteOffRemainingDebts">
        <xs:annotation>
            <xs:documentation>
                This option indicates whether the remaining, open debts should be written off upon case closure.

                Example use case: if there is an agreement between the debtor and client, that if the debtor pays half
                of his debts the remaining debts are forgiven, and the client also informs the debt collection
                agency about this agreement, the debt collection agency can set this flag upon case closure. When the
                Magicline processes the XML it will then automatically write off remaining debts in debt collection.

                This is a convenience flag so the client doesn't need to write off remaining debts by himself, if the
                case is closed without full payment.
            </xs:documentation>
        </xs:annotation>
    </xs:complexType>

    <xs:complexType name="Debt">
        <xs:annotation>
            <xs:documentation>
                OriginalAmount:  The amount sent by Magicline
                CanceledAmount:  E.g. if the studio doesn't want to collect a certain amount anymore.
                PaidAmount:      The total amount of payments
                RemainingAmount: Should always be OriginalAmount - PaidAmount - CanceledAmount
            </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="OriginalAmount" type="mlpdsu:TwoDecimalPlacesDecimal"/>
            <xs:element name="CanceledAmount" type="mlpdsu:TwoDecimalPlacesDecimal"/>
            <xs:element name="PaidAmount" type="mlpdsu:TwoDecimalPlacesDecimal"/>
            <xs:element name="RemainingAmount" type="mlpdsu:TwoDecimalPlacesDecimal"/>
            <xs:element name="Currency" type="mlpdsu:Currency"/>
        </xs:sequence>
        <xs:attribute name="voucherId" type="xs:string" use="required" />
    </xs:complexType>

    <xs:simpleType name="Currency">
        <xs:restriction base="xs:NMTOKEN">
            <xs:enumeration value="EUR"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="Agency">
        <xs:restriction base="xs:NMTOKEN">
            <xs:enumeration value="PayDue"/>
            <xs:enumeration value="Lexer"/>
            <xs:enumeration value="Intrum"/>
            <xs:enumeration value="External"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="TwoDecimalPlacesDecimal">
        <xs:restriction base="xs:decimal">
            <xs:totalDigits value="12"/>
            <xs:fractionDigits value="2"/>
            <xs:pattern value="[0-9]{1,10}[.][0-9]{1,2}"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>
