<?xml version="1.0" encoding="UTF-8"?>
<!--
    Schema for the Dintero settlement report in XML format.

    Derived from generateXML in the settlements repo
    (app/settlements/lambda/src/reportGenerator/xmlGenerator/xmlGenerator.ts).
    Element order follows the order the generator builds the report object in.
-->
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- Dates formatted as YYYY-MM-DD. Empty when the source date cannot be parsed. -->
    <xs:simpleType name="IsoDateOrEmpty">
        <xs:union memberTypes="xs:date">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:length value="0"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:union>
    </xs:simpleType>

    <!-- Amounts in the main currency unit, with "." as the decimal separator. -->
    <xs:simpleType name="Amount">
        <xs:restriction base="xs:decimal"/>
    </xs:simpleType>

    <xs:element name="Report">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="PaymentsInfo">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="IsoDateOrEmpty" name="ReportDateFrom"/>
                            <xs:element type="IsoDateOrEmpty" name="ReportDateTo"/>
                            <xs:element type="xs:string" name="SettlementCurrencyCode"/>
                            <xs:element type="xs:string" name="ReportReferenceNum"/>
                            <xs:element type="xs:string" name="DinteroSettlementId"/>
                            <xs:element type="xs:string" name="SettlementAccountNum"/>
                            <xs:element name="SettlementInfo">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element type="IsoDateOrEmpty" name="SettlementDate"/>
                                        <xs:element type="IsoDateOrEmpty" name="SettlementPeriodStart"/>
                                        <xs:element type="IsoDateOrEmpty" name="SettlementPeriodEnd"/>
                                        <xs:element type="xs:string" name="SettlementProvider"/>
                                        <xs:element type="xs:string" name="AccountId"/>
                                        <xs:element type="xs:string" name="SettlementReference"/>
                                        <xs:element type="xs:string" name="SettlementCurrencyCode"/>
                                        <xs:element name="TransactionInfo" maxOccurs="unbounded" minOccurs="0">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <!-- Record type: settlement, chargeback, transfer or transaction_operation -->
                                                    <xs:element type="xs:string" name="TransactionType"/>
                                                    <!-- Transaction date as reported by the payment provider. Not normalized. -->
                                                    <xs:element type="xs:string" name="TransactionTime"/>
                                                    <xs:element type="Amount" name="TransactionAmount"/>
                                                    <xs:element type="Amount" name="TransactionVatAmount"/>
                                                    <xs:element type="Amount" name="TransactionCapture"/>
                                                    <xs:element type="Amount" name="TransactionRefund"/>
                                                    <xs:element type="Amount" name="TransactionFee"/>
                                                    <xs:element type="Amount" name="TransactionChargeback"/>
                                                    <xs:element type="xs:string" name="SettlementCurrencyCode"/>
                                                    <xs:element type="xs:string" name="TransactionID"/>
                                                    <xs:element type="xs:string" name="Reference"/>
                                                    <xs:element type="xs:string" name="Reference2"/>
                                                    <!-- Payment product type, for example bambora.creditcard -->
                                                    <xs:element type="xs:string" name="ProductType"/>
                                                    <xs:element type="xs:string" name="EventReference"/>
                                                    <xs:element type="xs:string" name="StoreId"/>
                                                    <xs:element type="xs:string" name="PayoutDestinationId"/>
                                                    <xs:element type="xs:string" name="TransactionAccountId"/>
                                                    <xs:element type="xs:string" name="CardBrand"/>
                                                    <xs:element type="xs:string" name="CardRegion"/>
                                                    <xs:element type="xs:string" name="CardType"/>
                                                    <xs:element type="xs:string" name="CardCategory"/>
                                                    <xs:element type="xs:string" name="CardInterchangeDomain"/>
                                                    <xs:element type="xs:string" name="Channel"/>
                                                    <xs:element type="xs:string" name="EventCorrelationId" minOccurs="0"/>
                                                    <xs:element type="xs:string" name="StoreTerminalId" minOccurs="0"/>
                                                    <xs:element type="xs:string" name="StorePayoutDestinationId" minOccurs="0"/>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                        <xs:element type="Amount" name="SettlementAmount"/>
                                        <xs:element type="Amount" name="SettlementCapture"/>
                                        <xs:element type="Amount" name="SettlementRefund"/>
                                        <xs:element type="Amount" name="SettlementFee"/>
                                        <xs:element type="Amount" name="SettlementOtherAmount"/>
                                        <xs:element type="Amount" name="SettlementVat"/>
                                        <xs:element type="Amount" name="SettlementChargeback"/>
                                        <!-- The three fee VAT elements appear together or not at all -->
                                        <xs:sequence minOccurs="0">
                                            <xs:element type="Amount" name="SettlementFeeVatAmount"/>
                                            <xs:element type="Amount" name="SettlementFeeVatBase"/>
                                            <!-- Percentage, for example 25.00% -->
                                            <xs:element type="xs:string" name="SettlementFeeVatRate"/>
                                        </xs:sequence>
                                        <xs:element type="xs:string" name="SettlementBankAccountNumber" minOccurs="0"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="SettlementReport">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:string" name="OrganizationName"/>
                            <xs:element type="xs:string" name="OrganizationNumber"/>
                            <xs:element type="xs:string" name="MainAddress"/>
                            <xs:element type="xs:string" name="MainAddressZipCode"/>
                            <xs:element type="xs:string" name="MainAddressCity"/>
                            <xs:element type="xs:string" name="MainAddressCountryCode"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
