<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!--
        Batch (cashbook) import.

        Each <batch> element becomes one UNPOSTED batch transaction line (BatTran)
        in an unposted batch (BatHead) - exactly what the desktop's capture screens
        create. Nothing reaches the ledgers until a LegalSuite user posts the batch
        in the desktop program, which is where the double entry, the account
        balances, the bank reconciliation record and the VAT entries are written.

        Version one covers the cashbook book types only:
          Receipt  (BookType R) - money in
          Payment  (BookType P) - money out
        Journals and the other book types are not accepted yet.
    -->

    <!-- Yes/No style flag. Accepts Yes/No, Y/N, 1/0, true/false in any case -
         the import normalizes all of these. -->
    <xs:simpleType name="YesNoFlag">
        <xs:restriction base="xs:string">
            <xs:pattern value="[Yy]([Ee][Ss])?|[Nn]([Oo])?|1|0|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="batches">
        <xs:complexType>
            <xs:sequence>
                <!-- Email address to receive validation/import errors and notifications -->
                <xs:element name="EmailAddress" type="xs:string" minOccurs="1">
                    <xs:annotation>
                        <xs:documentation>Email address to receive validation/import error notifications.</xs:documentation>
                    </xs:annotation>
                </xs:element>

                <!-- The desktop's generic import takes a CreatedBy employee per row
                     (commo011.clw, column 24) because a batch belongs to the person
                     who captured it. The API has no logged-in user, so the file must
                     name the employee the batch is created under. -->
                <xs:element name="CreatedBy" type="xs:string" minOccurs="1">
                    <xs:annotation>
                        <xs:documentation>The name of the Employee the batch is created under, exactly as it appears in LegalSuite. This is who the batch belongs to in the desktop's batch list.</xs:documentation>
                    </xs:annotation>
                </xs:element>

                <xs:element name="BatchComments" type="xs:string" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Optional description for the batch this file creates, shown in the desktop's batch list (e.g. "Trust bank - September"). Defaults to "API Import".</xs:documentation>
                    </xs:annotation>
                </xs:element>

                <xs:element name="batch" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <!-- xs:all - the elements of a batch line may appear in any order -->
                        <xs:all>
                            <xs:element name="Type" minOccurs="1">
                                <xs:annotation>
                                    <xs:documentation>Receipt (money into the bank) or Payment (money out of the bank), any case.</xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="[Rr][Ee][Cc][Ee][Ii][Pp][Tt]|[Pp][Aa][Yy][Mm][Ee][Nn][Tt]"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="Date" type="xs:date" minOccurs="1">
                                <xs:annotation>
                                    <xs:documentation>The date (YYYY-MM-DD) of the transaction, as it appears on the bank statement.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="BankAccount" type="xs:string" minOccurs="1">
                                <xs:annotation>
                                    <xs:documentation>The description of the bank account the money moved through, exactly as it appears in LegalSuite (e.g. "Trust Bank Account"). Must be a Trust Bank, Business Bank or Petty Cash ledger account.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="Amount" type="xs:decimal" minOccurs="1">
                                <xs:annotation>
                                    <xs:documentation>The amount of the transaction, always a positive number. Whether it is money in or money out is decided by Type.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="Description" type="xs:string" minOccurs="1">
                                <xs:annotation>
                                    <xs:documentation>The narrative for the transaction, as it should read in the ledger.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <!-- The other side of the entry. Supply EXACTLY ONE of
                                 FileReference, LedgerAccount or Creditor. -->
                            <xs:element name="FileReference" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>For money received into or paid out of a Matter: the Matter file reference (e.g. BRA1/0005). The Matter must already exist. Supply only one of FileReference, LedgerAccount or Creditor.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="LedgerAccount" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>For a transaction against a general ledger account (e.g. bank charges, interest received): the account description exactly as it appears in LegalSuite. Supply only one of FileReference, LedgerAccount or Creditor.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="Creditor" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>For a transaction against a creditor's account: the creditor description exactly as it appears in LegalSuite. Supply only one of FileReference, LedgerAccount or Creditor.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="AllocateTo" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>For a Matter: whether the amount affects the Matter's Actual (trust) balance or its Reserved balance. Defaults to Actual.</xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="[Aa][Cc][Tt][Uu][Aa][Ll]|[Rr][Ee][Ss][Ee][Rr][Vv][Ee][Dd]"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="VatRate" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>The VAT rate number as configured in LegalSuite (e.g. 1), or N for no VAT, E for exempt, Z for zero rated. Defaults to N.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="VatIE" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>Whether the Amount is VAT Inclusive (I) or Exclusive (E). Defaults to I.</xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="[IiEe]"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="Voucher" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>The receipt or cheque/payment number. Left blank, the line carries no voucher number and the LegalSuite user can allocate one.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="BankStatementReference" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>Your own reference for the bank statement line, carried onto the transaction for the bank reconciliation.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="Employee" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>The name of the Employee the transaction is allocated to. Required when LedgerAccount is an income or expense account.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="CostCentre" type="xs:string" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation>The Cost Centre description. Required when LedgerAccount is an income or expense account.</xs:documentation>
                                </xs:annotation>
                            </xs:element>

                        </xs:all>
                    </xs:complexType>
                </xs:element>

            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
