<?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. -->
<py:strip xmlns:py="http://genshi.edgewall.org/">
    <!-- only ASCII -->
    <py:def function="PartyIdentification(party, id=None, with_name=True, with_address=True)">
        <!-- EPC limits to 70 instead of 140 -->
        <Nm py:if="with_name">${normalize('NFKD', party.name).encode('ascii', 'replace')[:70]}</Nm>
        <py:if test="with_address">
            <py:with vars="address = party.address_get()">
                <PstlAdr py:if="address">
                    ${PostalAddress(address)}
                </PstlAdr>
            </py:with>
        </py:if>
        <Id py:if="id">
            <py:with vars="identifier = party.get_sepa_identifier(id)">
                <OrgId py:if="identifier['Type'] == 'OrgId'">
                    <BICOrBEI py:if="identifier.get('BICOrBEI')">${identifier['BICOrBEI']}</BICOrBEI>
                    <Othr py:if="identifier.get('Id')">
                        <Id>${identifier['Id']}</Id>
                        <SchmeNm py:if="identifier.get('SchmeNm')">
                            <Cd py:if="identifier['SchmeNm'].get('Cd')">${identifier['SchmeNm']['Cd']}</Cd>
                            <Prtry py:if="identifier['SchmeNm'].get('Prtry')">${identifier['SchmeNm']['Cd']}</Prtry>
                        </SchmeNm>
                        <Issr py:if="identifier.get('Issr')">${identifier['Issr']}</Issr>
                    </Othr>
                </OrgId>
                <PrvtId py:if="identifier['Type'] == 'PrvtId'">
                    <DtAndPlcOfBirth py:if="identifier.get('DtAndPlcOfBirth')">
                        <BirthDt>${identifier['DtAndPlcOfBirth']['BirthDt'].isoformat()}</BirthDt>
                        <PrvcOfBirth py:if="identifier['DtAndPlcOfBirth'].get('PrvcOfBirth')">${identifier['DtAndPlcOfBirth']['PrvcOfBirth']}</PrvcOfBirth>
                        <CityOfBirth>${identifier['DtAndPlcOfBirth']['CityOfBirth']}</CityOfBirth>
                        <CtryOfBirth>${identifier['DtAndPlcOfBirth']['CtryOfBirth']}</CtryOfBirth>
                    </DtAndPlcOfBirth>
                    <Othr py:if="identifier.get('Id')">
                        <Id>${identifier['Id']}</Id>
                        <SchmeNm py:if="identifier.get('SchmeNm')">
                            <Cd py:if="identifier['SchmeNm'].get('Cd')">${identifier['SchmeNm']['Cd']}</Cd>
                            <Prtry py:if="identifier['SchmeNm'].get('Prtry')">${identifier['SchmeNm']['Prtry']}</Prtry>
                        </SchmeNm>
                        <Issr py:if="identifier.get('Issr')">${identifier['Issr']}</Issr>
                    </Othr>
                </PrvtId>
            </py:with>
        </Id>
        <!-- CtryOfRes -->
        <!-- CtctDtls -->
    </py:def>
    <py:def function="PostalAddress(address)">
        <Ctry py:if="address.country">${normalize('NFKD', address.country.code).encode('ascii', 'replace')}</Ctry>
        <py:if test="address.street">
            <py:for each="line in address.street.splitlines()[1:8]">
                <AdrLine py:if="address.street">${normalize('NFKD', line).encode('ascii', 'replace')[:70]}</AdrLine>
            </py:for>
        </py:if>
    </py:def>
    <py:def function="Account(account_number, with_ccy=True)">
        <Id>
            <!-- EPC only IBAN is allowed -->
            <IBAN>${account_number.compact_iban}</IBAN>
        </Id>
        <!-- Tp -->
        <Ccy py:if="with_ccy and account_number.account.currency">${account_number.account.currency.code}</Ccy>
        <!-- Nm -->
    </py:def>
    <!-- uses BIC instead of BICFI -->
    <py:def function="FinancialInstitution(bank, only_bic=False)">
        <FinInstnId>
            <BIC py:if="bank.bic">${bank.bic}</BIC>
            <Othr py:if="not bank.bic">
                <Id>NOTPROVIDED</Id>
            </Othr>
        </FinInstnId>
    </py:def>
</py:strip>
