<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is part of Tryton.  The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.05"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xi="http://www.w3.org/2001/XInclude"
    xmlns:py="http://genshi.edgewall.org/">
    <xi:include href="base.xml"/>
    <CstmrCdtTrfInitn>
        <GrpHdr>
            <MsgId>${group.sepa_message_id[:35]}</MsgId>
            <CreDtTm>${datetime.datetime.now().replace(microsecond=0).isoformat()}</CreDtTm>
            <!-- Authstn -->
            <NbOfTxs>${sum(len(payments) for _, payments in group.sepa_payments)}</NbOfTxs>
            <CtrlSum>${sum(p.amount for p in group.payments)}</CtrlSum>
            <!-- PmtTpInf -->
            <!-- ReqdColltnDt -->
            <InitgPty>
                ${PartyIdentification(group.sepa_initiating_party, id=group.journal.sepa_payable_initiator_id)}
            </InitgPty>
            <!-- FwdgAgt -->
        </GrpHdr>
        <PmtInf py:for="key, payments in group.sepa_payments">
            <PmtInfId>${group.sepa_group_payment_id(key)[:35]}</PmtInfId>
            <PmtMtd>TRF</PmtMtd>
            <BtchBookg>${'true' if group.journal.sepa_batch_booking else 'false'}</BtchBookg>
            <NbOfTxs>${len(payments)}</NbOfTxs>
            <CtrlSum>${sum(p.amount for p in payments)}</CtrlSum>
            <PmtTpInf>
                <!-- InstrPrty -->
                <SvcLvl>
                    <Cd>SEPA</Cd>
                </SvcLvl>
                <!-- LclInstrm -->
                <!-- CtgyPurp -->
            </PmtTpInf>
            <ReqdExctnDt>${key['date'].isoformat()}</ReqdExctnDt>
            <!-- PoolgAdjstmntDt -->
            <Dbtr>
                ${PartyIdentification(group.company.party)}
            </Dbtr>
            <DbtrAcct>
                ${Account(group.journal.sepa_bank_account_number)}
            </DbtrAcct>
            <DbtrAgt>
                ${FinancialInstitution(group.journal.sepa_bank_account_number.account.bank, only_bic=True)}
            </DbtrAgt>
            <!-- DbtrAgtAcct -->
            <!-- InstrForDbtrAgt -->
            <!-- UltmtDbtr -->
            <ChrgBr>${group.journal.sepa_charge_bearer}</ChrgBr>
            <!-- ChrgsAcct -->
            <!-- ChrgsAcctAgt -->
            <CdtTrfTxInf py:for="payment in payments">
                <PmtId>
                    <InstrId>${payment.sepa_instruction_id}</InstrId>
                    <EndToEndId>${payment.sepa_end_to_end_id}</EndToEndId>
                </PmtId>
                <!-- PmtTpInf --> <!-- EPC only at payment information level -->
                <Amt>
                    <InstdAmt py:attrs="{'Ccy': payment.currency.code}">${payment.amount}</InstdAmt>
                    <!-- EqvtAmt -->
                </Amt>
                <!-- XchgRateInf -->
                <!-- ChrgBr --> <!-- EPC only at payment information level -->
                <!-- ChqInstr -->
                <!-- UltmtDbtr -->
                <!-- IntrmyAgt1 -->
                <!-- IntrmyAgt1Acct -->
                <!-- IntrmyAgt2 -->
                <!-- IntrmyAgt2Acct -->
                <!-- IntrmyAgt3 -->
                <!-- IntrmyAgt3Acct -->
                <CdtrAgt>
                    ${FinancialInstitution(payment.sepa_bank_account_number.account.bank, only_bic=True)}
                </CdtrAgt>
                <!-- CdtrAgtAcct -->
                <Cdtr>
                    ${PartyIdentification(payment.party)}
                </Cdtr>
                <CdtrAcct>
                    ${Account(payment.sepa_bank_account_number)}
                </CdtrAcct>
                <!-- UltmtCdtr -->
                <!-- InstrForCdtrAgt -->
                <!-- InstrForDbtrAgt -->
                <!-- Purp -->
                <!-- RgltryRptg -->
                <!-- Tax -->
                <!-- RltdRmtInf -->
                <RmtInf py:if="payment.sepa_remittance_information">
                    <Ustrd>${payment.sepa_remittance_information[:140]}</Ustrd>
                </RmtInf>
                <!-- SplmtryData -->
            </CdtTrfTxInf>
        </PmtInf>
        <!-- SplmtryData -->
    </CstmrCdtTrfInitn>
</Document>
