{% apply spaceless %}
<?xml version="1.0" encoding="utf-8"?>
<Retention xmlns="urn:sunat:names:specification:ubl:peru:schema:xsd:Retention-1" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent/>
        </ext:UBLExtension>
    </ext:UBLExtensions>
    <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
    <cbc:CustomizationID>1.0</cbc:CustomizationID>
    {% set emp = doc.company %}
    <cac:Signature>
        <cbc:ID>{{ emp.ruc }}</cbc:ID>
        <cac:SignatoryParty>
            <cac:PartyIdentification>
                <cbc:ID>{{ emp.ruc }}</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyName>
                <cbc:Name><![CDATA[{{ emp.razonSocial|raw }}]]></cbc:Name>
            </cac:PartyName>
        </cac:SignatoryParty>
        <cac:DigitalSignatureAttachment>
            <cac:ExternalReference>
                <cbc:URI>#GREENTER-SIGN</cbc:URI>
            </cac:ExternalReference>
        </cac:DigitalSignatureAttachment>
    </cac:Signature>
    <cbc:ID>{{ doc.serie }}-{{ doc.correlativo }}</cbc:ID>
    <cbc:IssueDate>{{ doc.fechaEmision|date('Y-m-d') }}</cbc:IssueDate>
    <cbc:IssueTime>{{ doc.fechaEmision|date('H:i:s') }}</cbc:IssueTime>
    <cac:AgentParty>
        <cac:PartyIdentification>
            <cbc:ID schemeID="6">{{ emp.ruc }}</cbc:ID>
        </cac:PartyIdentification>
        <cac:PartyName>
            <cbc:Name><![CDATA[{{ emp.nombreComercial|raw }}]]></cbc:Name>
        </cac:PartyName>
        {% set addr = emp.address %}
        <cac:PostalAddress>
            <cbc:ID>{{ addr.ubigueo }}</cbc:ID>
            <cbc:StreetName><![CDATA[{{ addr.direccion|raw }}]]></cbc:StreetName>
            <cbc:CityName>{{ addr.departamento }}</cbc:CityName>
            <cbc:CountrySubentity>{{ addr.provincia }}</cbc:CountrySubentity>
            <cbc:District>{{ addr.distrito }}</cbc:District>
            <cac:Country>
                <cbc:IdentificationCode>{{ addr.codigoPais }}</cbc:IdentificationCode>
            </cac:Country>
        </cac:PostalAddress>
        <cac:PartyLegalEntity>
            <cbc:RegistrationName><![CDATA[{{ emp.razonSocial|raw }}]]></cbc:RegistrationName>
        </cac:PartyLegalEntity>
    </cac:AgentParty>
    <cac:ReceiverParty>
        <cac:PartyIdentification>
            <cbc:ID schemeID="{{ doc.proveedor.tipoDoc }}">{{ doc.proveedor.numDoc }}</cbc:ID>
        </cac:PartyIdentification>
        <cac:PartyLegalEntity>
            <cbc:RegistrationName><![CDATA[{{ doc.proveedor.rznSocial }}]]></cbc:RegistrationName>
        </cac:PartyLegalEntity>
    </cac:ReceiverParty>
    <sac:SUNATRetentionSystemCode>{{ doc.regimen }}</sac:SUNATRetentionSystemCode>
    <sac:SUNATRetentionPercent>{{ doc.tasa|n_format }}</sac:SUNATRetentionPercent>
    {% if doc.observacion %}
    <cbc:Note><![CDATA[{{ doc.observacion|raw }}]]></cbc:Note>
    {% endif %}
    <cbc:TotalInvoiceAmount currencyID="PEN">{{ doc.impRetenido|n_format }}</cbc:TotalInvoiceAmount>
    <sac:SUNATTotalPaid currencyID="PEN">{{ doc.impPagado|n_format }}</sac:SUNATTotalPaid>
    {% for det in doc.details %}
    <sac:SUNATRetentionDocumentReference>
        <cbc:ID schemeID="{{ det.tipoDoc }}">{{ det.numDoc }}</cbc:ID>
        <cbc:IssueDate>{{ det.fechaEmision|date('Y-m-d') }}</cbc:IssueDate>
        <cbc:TotalInvoiceAmount currencyID="{{ det.moneda }}">{{ det.impTotal|n_format }}</cbc:TotalInvoiceAmount>
        {% if det.pagos %}
        {% for pay in det.pagos %}
        <cac:Payment>
            <cbc:ID>{{ loop.index }}</cbc:ID>
            <cbc:PaidAmount currencyID="{{ pay.moneda }}">{{ pay.importe|n_format }}</cbc:PaidAmount>
            <cbc:PaidDate>{{ pay.fecha|date('Y-m-d') }}</cbc:PaidDate>
        </cac:Payment>
        {% endfor %}
        {% endif %}
        {% if det.impRetenido and det.impPagar and det.fechaRetencion %}
        <sac:SUNATRetentionInformation>
            <sac:SUNATRetentionAmount currencyID="PEN">{{ det.impRetenido|n_format }}</sac:SUNATRetentionAmount>
            <sac:SUNATRetentionDate>{{ det.fechaRetencion|date('Y-m-d') }}</sac:SUNATRetentionDate>
            <sac:SUNATNetTotalPaid currencyID="PEN">{{ det.impPagar|n_format }}</sac:SUNATNetTotalPaid>
            {% if det.tipoCambio %}
            <cac:ExchangeRate>
                <cbc:SourceCurrencyCode>{{ det.tipoCambio.monedaRef }}</cbc:SourceCurrencyCode>
                <cbc:TargetCurrencyCode>{{ det.tipoCambio.monedaObj }}</cbc:TargetCurrencyCode>
                <cbc:CalculationRate>{{ det.tipoCambio.factor|n_format(6)}}</cbc:CalculationRate>
                <cbc:Date>{{ det.tipoCambio.fecha|date('Y-m-d') }}</cbc:Date>
            </cac:ExchangeRate>
            {% endif %}
        </sac:SUNATRetentionInformation>
        {% endif %}
    </sac:SUNATRetentionDocumentReference>
    {% endfor %}
</Retention>
{% endapply %}