{% apply spaceless %}
<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 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">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent/>
        </ext:UBLExtension>
    </ext:UBLExtensions>
    {% set emp = doc.company %}
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <cbc:CustomizationID>2.0</cbc:CustomizationID>
    <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>
    {% if doc.fecVencimiento %}
    <cbc:DueDate>{{ doc.fecVencimiento|date('Y-m-d') }}</cbc:DueDate>
    {% endif %}
    <cbc:InvoiceTypeCode listID="{{ doc.tipoOperacion }}">{{ doc.tipoDoc }}</cbc:InvoiceTypeCode>
    {% for leg in doc.legends %}
    <cbc:Note languageLocaleID="{{ leg.code }}"><![CDATA[{{ leg.value }}]]></cbc:Note>
    {% endfor %}
    {% if doc.observacion %}
    <cbc:Note><![CDATA[{{ doc.observacion }}]]></cbc:Note>
    {% endif %}
    <cbc:DocumentCurrencyCode>{{ doc.tipoMoneda }}</cbc:DocumentCurrencyCode>
    {% if doc.compra %}
    <cac:OrderReference>
        <cbc:ID>{{ doc.compra }}</cbc:ID>
    </cac:OrderReference>
    {% endif %}
    {% if doc.guias %}
    {% for guia in doc.guias %}
    <cac:DespatchDocumentReference>
        <cbc:ID>{{ guia.nroDoc }}</cbc:ID>
        <cbc:DocumentTypeCode>{{ guia.tipoDoc }}</cbc:DocumentTypeCode>
    </cac:DespatchDocumentReference>
    {% endfor %}
    {% endif %}
    {% if doc.relDocs %}
    {% for rel in doc.relDocs %}
    <cac:AdditionalDocumentReference>
        <cbc:ID>{{ rel.nroDoc }}</cbc:ID>
        <cbc:DocumentTypeCode>{{ rel.tipoDoc }}</cbc:DocumentTypeCode>
    </cac:AdditionalDocumentReference>
    {% endfor %}
    {% endif %}
    {% if doc.anticipos %}
    {% for ant in doc.anticipos %}
    <cac:AdditionalDocumentReference>
        <cbc:ID>{{ ant.nroDocRel }}</cbc:ID>
        <cbc:DocumentTypeCode>{{ ant.tipoDocRel }}</cbc:DocumentTypeCode>
        <cbc:DocumentStatusCode>{{ loop.index }}</cbc:DocumentStatusCode>
        <cac:IssuerParty>
            <cac:PartyIdentification>
                <cbc:ID schemeID="6">{{ emp.ruc }}</cbc:ID>
            </cac:PartyIdentification>
        </cac:IssuerParty>
    </cac:AdditionalDocumentReference>
    {% endfor %}
    {% endif %}
    <cac:Signature>
        <cbc:ID>SIGN{{ 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>
    <cac:AccountingSupplierParty>
        <cac:Party>
            <cac:PartyIdentification>
                <cbc:ID schemeID="6">{{ emp.ruc }}</cbc:ID>
            </cac:PartyIdentification>
			{% if emp.nombreComercial %}
            <cac:PartyName>
                <cbc:Name><![CDATA[{{ emp.nombreComercial|raw }}]]></cbc:Name>
            </cac:PartyName>
			{% endif %}
            <cac:PartyLegalEntity>
                <cbc:RegistrationName><![CDATA[{{ emp.razonSocial|raw }}]]></cbc:RegistrationName>
                {% set addr = emp.address %}
                <cac:RegistrationAddress>
                    <cbc:ID>{{ addr.ubigueo }}</cbc:ID>
                    <cbc:AddressTypeCode>{{ addr.codLocal }}</cbc:AddressTypeCode>
                    {% if addr.urbanizacion %}
                    <cbc:CitySubdivisionName>{{ addr.urbanizacion }}</cbc:CitySubdivisionName>
                    {% endif %}
                    <cbc:CityName>{{ addr.provincia }}</cbc:CityName>
                    <cbc:CountrySubentity>{{ addr.departamento }}</cbc:CountrySubentity>
                    <cbc:District>{{ addr.distrito }}</cbc:District>
                    <cac:AddressLine>
                        <cbc:Line><![CDATA[{{ addr.direccion|raw }}]]></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:IdentificationCode>{{ addr.codigoPais }}</cbc:IdentificationCode>
                    </cac:Country>
                </cac:RegistrationAddress>
            </cac:PartyLegalEntity>
            {% if emp.email or emp.telephone %}
            <cac:Contact>
                {% if emp.telephone %}
                <cbc:Telephone>{{ emp.telephone }}</cbc:Telephone>
                {% endif %}
                {% if emp.email %}
                <cbc:ElectronicMail>{{ emp.email }}</cbc:ElectronicMail>
                {% endif %}
            </cac:Contact>
            {% endif %}
        </cac:Party>
    </cac:AccountingSupplierParty>
    {% set client = doc.client %}
    <cac:AccountingCustomerParty>
        <cac:Party>
            <cac:PartyIdentification>
                <cbc:ID schemeID="{{ client.tipoDoc }}">{{ client.numDoc }}</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyLegalEntity>
                <cbc:RegistrationName><![CDATA[{{ client.rznSocial|raw }}]]></cbc:RegistrationName>
                {% if client.address %}
                {% set addr = client.address %}
                <cac:RegistrationAddress>
                    {% if addr.ubigueo %}
                    <cbc:ID>{{ addr.ubigueo }}</cbc:ID>
                    {% endif %}
                    <cac:AddressLine>
                        <cbc:Line><![CDATA[{{ addr.direccion|raw }}]]></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:IdentificationCode>{{ addr.codigoPais }}</cbc:IdentificationCode>
                    </cac:Country>
                </cac:RegistrationAddress>
                {% endif %}
            </cac:PartyLegalEntity>
            {% if client.email or client.telephone %}
            <cac:Contact>
                {% if client.telephone %}
                <cbc:Telephone>{{ client.telephone }}</cbc:Telephone>
                {% endif %}
                {% if client.email %}
                <cbc:ElectronicMail>{{ client.email }}</cbc:ElectronicMail>
                {% endif %}
            </cac:Contact>
            {% endif %}
        </cac:Party>
    </cac:AccountingCustomerParty>
    {% set seller = doc.seller %}
    {% if seller %}
    <cac:SellerSupplierParty>
        <cac:Party>
            <cac:PartyIdentification>
                <cbc:ID schemeID="{{ seller.tipoDoc }}">{{ seller.numDoc }}</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyLegalEntity>
                <cbc:RegistrationName><![CDATA[{{ seller.rznSocial|raw }}]]></cbc:RegistrationName>
                {% if seller.address %}
                {% set addr = seller.address %}
                <cac:RegistrationAddress>
                    {% if addr.ubigueo %}
                    <cbc:ID>{{ addr.ubigueo }}</cbc:ID>
                    {% endif %}
                    <cac:AddressLine>
                        <cbc:Line><![CDATA[{{ addr.direccion|raw }}]]></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:IdentificationCode>{{ addr.codigoPais }}</cbc:IdentificationCode>
                    </cac:Country>
                </cac:RegistrationAddress>
                {% endif %}
            </cac:PartyLegalEntity>
            {% if seller.email or seller.telephone %}
            <cac:Contact>
                {% if seller.telephone %}
                <cbc:Telephone>{{ seller.telephone }}</cbc:Telephone>
                {% endif %}
                {% if seller.email %}
                <cbc:ElectronicMail>{{ seller.email }}</cbc:ElectronicMail>
                {% endif %}
            </cac:Contact>
            {% endif %}
        </cac:Party>
    </cac:SellerSupplierParty>
    {% endif %}
    {% if doc.direccionEntrega %}
        {% set addr = doc.direccionEntrega %}
        <cac:Delivery>
            <cac:DeliveryLocation>
                <cac:Address>
                    <cbc:ID>{{ addr.ubigueo }}</cbc:ID>
                    {% if addr.urbanizacion %}
                    <cbc:CitySubdivisionName>{{ addr.urbanizacion }}</cbc:CitySubdivisionName>
                    {% endif %}
                    <cbc:CityName>{{ addr.provincia }}</cbc:CityName>
                    <cbc:CountrySubentity>{{ addr.departamento }}</cbc:CountrySubentity>
                    <cbc:District>{{ addr.distrito }}</cbc:District>
                    <cac:AddressLine>
                        <cbc:Line><![CDATA[{{ addr.direccion|raw }}]]></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:IdentificationCode listID="ISO 3166-1" listAgencyName="United Nations Economic Commission for Europe" listName="Country">{{ addr.codigoPais }}</cbc:IdentificationCode>
                    </cac:Country>
                </cac:Address>
            </cac:DeliveryLocation>
        </cac:Delivery>
    {% endif %}
    {% if doc.detraccion %}
    {% set detr = doc.detraccion %}
    <cac:PaymentMeans>
        <cbc:ID>Detraccion</cbc:ID>
        <cbc:PaymentMeansCode>{{ detr.codMedioPago }}</cbc:PaymentMeansCode>
        <cac:PayeeFinancialAccount>
            <cbc:ID>{{ detr.ctaBanco }}</cbc:ID>
        </cac:PayeeFinancialAccount>
    </cac:PaymentMeans>
    <cac:PaymentTerms>
        <cbc:ID>Detraccion</cbc:ID>
        <cbc:PaymentMeansID>{{ detr.codBienDetraccion }}</cbc:PaymentMeansID>
        <cbc:PaymentPercent>{{ detr.percent }}</cbc:PaymentPercent>
        <cbc:Amount currencyID="PEN">{{ detr.mount|n_format }}</cbc:Amount>
    </cac:PaymentTerms>
    {% endif %}
    {% if doc.perception %}
    <cac:PaymentTerms>
        <cbc:ID>Percepcion</cbc:ID>
        <cbc:Amount currencyID="PEN">{{ doc.perception.mtoTotal|n_format }}</cbc:Amount>
    </cac:PaymentTerms>
    {% endif %}
    {% if doc.formaPago %}
    <cac:PaymentTerms>
        <cbc:ID>FormaPago</cbc:ID>
        <cbc:PaymentMeansID>{{ doc.formaPago.tipo }}</cbc:PaymentMeansID>
        {% if doc.formaPago.monto %}
        <cbc:Amount currencyID="{{ doc.formaPago.moneda|default(doc.tipoMoneda) }}">{{ doc.formaPago.monto|n_format }}</cbc:Amount>
        {% endif %}
    </cac:PaymentTerms>
    {% endif %}
    {% if doc.cuotas %}
    {% for cuota in doc.cuotas %}
    <cac:PaymentTerms>
        <cbc:ID>FormaPago</cbc:ID>
        <cbc:PaymentMeansID>Cuota{{ "%03d"|format(loop.index) }}</cbc:PaymentMeansID>
        <cbc:Amount currencyID="{{ cuota.moneda|default(doc.tipoMoneda) }}">{{ cuota.monto|n_format }}</cbc:Amount>
        <cbc:PaymentDueDate>{{ cuota.fechaPago|date('Y-m-d') }}</cbc:PaymentDueDate>
    </cac:PaymentTerms>
    {% endfor %}
    {% endif %}
    {% if doc.anticipos %}
    {% for ant in doc.anticipos %}
    <cac:PrepaidPayment>
        <cbc:ID>{{ loop.index }}</cbc:ID>
        <cbc:PaidAmount currencyID="{{ doc.tipoMoneda }}">{{ ant.total|n_format }}</cbc:PaidAmount>
    </cac:PrepaidPayment>
    {% endfor %}
    {% endif %}
    {% if doc.cargos %}
    {% for cargo in doc.cargos %}
    <cac:AllowanceCharge>
        <cbc:ChargeIndicator>true</cbc:ChargeIndicator>
        <cbc:AllowanceChargeReasonCode>{{ cargo.codTipo }}</cbc:AllowanceChargeReasonCode>
        <cbc:MultiplierFactorNumeric>{{ cargo.factor|n_format_limit(5) }}</cbc:MultiplierFactorNumeric>
        <cbc:Amount currencyID="{{ doc.tipoMoneda }}">{{ cargo.monto|n_format }}</cbc:Amount>
        <cbc:BaseAmount currencyID="{{ doc.tipoMoneda }}">{{ cargo.montoBase|n_format }}</cbc:BaseAmount>
    </cac:AllowanceCharge>
    {% endfor %}
    {% endif %}
    {% if doc.descuentos %}
    {% for desc in doc.descuentos %}
    <cac:AllowanceCharge>
        <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
        <cbc:AllowanceChargeReasonCode>{{ desc.codTipo }}</cbc:AllowanceChargeReasonCode>
        {% if desc.factor is not null %}
        <cbc:MultiplierFactorNumeric>{{ desc.factor|n_format_limit(5) }}</cbc:MultiplierFactorNumeric>
        {% endif %}
        <cbc:Amount currencyID="{{ doc.tipoMoneda }}">{{ desc.monto|n_format }}</cbc:Amount>
        <cbc:BaseAmount currencyID="{{ doc.tipoMoneda }}">{{ desc.montoBase|n_format }}</cbc:BaseAmount>
    </cac:AllowanceCharge>
    {% endfor %}
    {% endif %}
    {% if doc.perception %}
    {% set perc = doc.perception %}
    <cac:AllowanceCharge>
        <cbc:ChargeIndicator>true</cbc:ChargeIndicator>
        <cbc:AllowanceChargeReasonCode>{{ perc.codReg }}</cbc:AllowanceChargeReasonCode>
        <cbc:MultiplierFactorNumeric>{{ perc.porcentaje|n_format_limit(5) }}</cbc:MultiplierFactorNumeric>
        <cbc:Amount currencyID="PEN">{{ perc.mto|n_format }}</cbc:Amount>
        <cbc:BaseAmount currencyID="PEN">{{ perc.mtoBase|n_format }}</cbc:BaseAmount>
    </cac:AllowanceCharge>
    {% endif %}
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.totalImpuestos|n_format }}</cbc:TaxAmount>
        {% if doc.mtoISC %}
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoBaseIsc|n_format }}</cbc:TaxableAmount>
            <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoISC|n_format }}</cbc:TaxAmount>
            <cac:TaxCategory>
                <cac:TaxScheme>
                    <cbc:ID>2000</cbc:ID>
                    <cbc:Name>ISC</cbc:Name>
                    <cbc:TaxTypeCode>EXC</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOperGravadas is not null %}
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOperGravadas|n_format }}</cbc:TaxableAmount>
            <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoIGV|n_format }}</cbc:TaxAmount>
            <cac:TaxCategory>
                <cac:TaxScheme>
                    <cbc:ID>1000</cbc:ID>
                    <cbc:Name>IGV</cbc:Name>
                    <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOperInafectas is not null %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOperInafectas|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">0</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>9998</cbc:ID>
                        <cbc:Name>INA</cbc:Name>
                        <cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOperExoneradas is not null %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOperExoneradas|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">0</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>9997</cbc:ID>
                        <cbc:Name>EXO</cbc:Name>
                        <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOperGratuitas is not null %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOperGratuitas|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoIGVGratuitas|n_format }}</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>9996</cbc:ID>
                        <cbc:Name>GRA</cbc:Name>
                        <cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOperExportacion is not null %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOperExportacion|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">0</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>9995</cbc:ID>
                        <cbc:Name>EXP</cbc:Name>
                        <cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoIvap %}
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoBaseIvap|n_format }}</cbc:TaxableAmount>
            <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoIvap|n_format }}</cbc:TaxAmount>
            <cac:TaxCategory>
                <cac:TaxScheme>
                    <cbc:ID>1016</cbc:ID>
                    <cbc:Name>IVAP</cbc:Name>
                    <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
        {% endif %}
        {% if doc.mtoOtrosTributos %}
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoBaseOth|n_format }}</cbc:TaxableAmount>
            <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoOtrosTributos|n_format }}</cbc:TaxAmount>
            <cac:TaxCategory>
                <cac:TaxScheme>
                    <cbc:ID>9999</cbc:ID>
                    <cbc:Name>OTROS</cbc:Name>
                    <cbc:TaxTypeCode>OTH</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
        {% endif %}
        {% if doc.icbper %}
            <cac:TaxSubtotal>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.icbper|n_format }}</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cac:TaxScheme>
                        <cbc:ID>7152</cbc:ID>
                        <cbc:Name>ICBPER</cbc:Name>
                        <cbc:TaxTypeCode>OTH</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endif %}
    </cac:TaxTotal>
    <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.valorVenta|n_format }}</cbc:LineExtensionAmount>
        {% if doc.subTotal is not null %}
        <cbc:TaxInclusiveAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.subTotal|n_format }}</cbc:TaxInclusiveAmount>
        {% endif %}
        {% if doc.sumOtrosDescuentos is not null %}
        <cbc:AllowanceTotalAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.sumOtrosDescuentos|n_format }}</cbc:AllowanceTotalAmount>
        {% endif %}
        {% if doc.sumOtrosCargos is not null %}
        <cbc:ChargeTotalAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.sumOtrosCargos|n_format }}</cbc:ChargeTotalAmount>
        {% endif %}
        {% if doc.totalAnticipos is not null %}
        <cbc:PrepaidAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.totalAnticipos|n_format }}</cbc:PrepaidAmount>
        {% endif %}
        {% if doc.redondeo is not null %}
        <cbc:PayableRoundingAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.redondeo|n_format }}</cbc:PayableRoundingAmount>
        {% endif %}
        <cbc:PayableAmount currencyID="{{ doc.tipoMoneda }}">{{ doc.mtoImpVenta|n_format }}</cbc:PayableAmount>
    </cac:LegalMonetaryTotal>
    {% for detail in doc.details %}
    <cac:InvoiceLine>
        <cbc:ID>{{ loop.index }}</cbc:ID>
        <cbc:InvoicedQuantity unitCode="{{ detail.unidad }}">{{ detail.cantidad }}</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoValorVenta|n_format }}</cbc:LineExtensionAmount>
        <cac:PricingReference>
            {% if detail.mtoValorGratuito %}
            <cac:AlternativeConditionPrice>
                <cbc:PriceAmount currencyID="{{ doc.tipoMoneda }}">{{  detail.mtoValorGratuito|n_format_limit(10) }}</cbc:PriceAmount>
                <cbc:PriceTypeCode>02</cbc:PriceTypeCode>
            </cac:AlternativeConditionPrice>
            {% else %}
            <cac:AlternativeConditionPrice>
                <cbc:PriceAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoPrecioUnitario|n_format_limit(10) }}</cbc:PriceAmount>
                <cbc:PriceTypeCode>01</cbc:PriceTypeCode>
            </cac:AlternativeConditionPrice>
            {% endif %}
        </cac:PricingReference>
        {% if detail.cargos %}
        {% for cargo in detail.cargos %}
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>true</cbc:ChargeIndicator>
            <cbc:AllowanceChargeReasonCode>{{ cargo.codTipo }}</cbc:AllowanceChargeReasonCode>
            <cbc:MultiplierFactorNumeric>{{ cargo.factor|n_format_limit(5) }}</cbc:MultiplierFactorNumeric>
            <cbc:Amount currencyID="{{ doc.tipoMoneda }}">{{ cargo.monto }}</cbc:Amount>
            <cbc:BaseAmount currencyID="{{ doc.tipoMoneda }}">{{ cargo.montoBase }}</cbc:BaseAmount>
        </cac:AllowanceCharge>
        {% endfor %}
        {% endif %}
        {% if detail.descuentos %}
        {% for desc in detail.descuentos %}
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
            <cbc:AllowanceChargeReasonCode>{{ desc.codTipo }}</cbc:AllowanceChargeReasonCode>
            <cbc:MultiplierFactorNumeric>{{ desc.factor|n_format_limit(5) }}</cbc:MultiplierFactorNumeric>
            <cbc:Amount currencyID="{{ doc.tipoMoneda }}">{{ desc.monto }}</cbc:Amount>
            <cbc:BaseAmount currencyID="{{ doc.tipoMoneda }}">{{ desc.montoBase }}</cbc:BaseAmount>
        </cac:AllowanceCharge>
        {% endfor %}
        {% endif %}
        <cac:TaxTotal>
            <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.totalImpuestos|n_format }}</cbc:TaxAmount>
            {% if detail.isc %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoBaseIsc|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.isc|n_format }}</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cbc:Percent>{{ detail.porcentajeIsc }}</cbc:Percent>
                    <cbc:TierRange>{{ detail.tipSisIsc }}</cbc:TierRange>
                    <cac:TaxScheme>
                        <cbc:ID>2000</cbc:ID>
                        <cbc:Name>ISC</cbc:Name>
                        <cbc:TaxTypeCode>EXC</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
            {% endif %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoBaseIgv|n_format }}</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.igv|n_format }}</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cbc:Percent>{{ detail.porcentajeIgv }}</cbc:Percent>
                    <cbc:TaxExemptionReasonCode>{{ detail.tipAfeIgv }}</cbc:TaxExemptionReasonCode>
                    {% set afect = getTributoAfect(detail.tipAfeIgv) %}
                    <cac:TaxScheme>
                        <cbc:ID>{{ afect.id }}</cbc:ID>
                        <cbc:Name>{{ afect.name }}</cbc:Name>
                        <cbc:TaxTypeCode>{{ afect.code }}</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
            {% if detail.otroTributo %}
                <cac:TaxSubtotal>
                    <cbc:TaxableAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoBaseOth|n_format }}</cbc:TaxableAmount>
                    <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.otroTributo|n_format }}</cbc:TaxAmount>
                    <cac:TaxCategory>
                        <cbc:Percent>{{ detail.porcentajeOth }}</cbc:Percent>
                        <cac:TaxScheme>
                            <cbc:ID>9999</cbc:ID>
                            <cbc:Name>OTROS</cbc:Name>
                            <cbc:TaxTypeCode>OTH</cbc:TaxTypeCode>
                        </cac:TaxScheme>
                    </cac:TaxCategory>
                </cac:TaxSubtotal>
            {% endif %}
            {% if detail.icbper %}
            <cac:TaxSubtotal>
                <cbc:TaxAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.icbper|n_format }}</cbc:TaxAmount>
                <cbc:BaseUnitMeasure unitCode="NIU">{{ detail.cantidad }}</cbc:BaseUnitMeasure>
                <cac:TaxCategory>
                    <cbc:PerUnitAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.factorIcbper|n_format }}</cbc:PerUnitAmount>
                    <cac:TaxScheme>
                        <cbc:ID>7152</cbc:ID>
                        <cbc:Name>ICBPER</cbc:Name>
                        <cbc:TaxTypeCode>OTH</cbc:TaxTypeCode>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
            {% endif %}
        </cac:TaxTotal>
        <cac:Item>
            <cbc:Description><![CDATA[{{ detail.descripcion|raw }}]]></cbc:Description>
            {% if detail.codProducto %}
            <cac:SellersItemIdentification>
                <cbc:ID>{{ detail.codProducto }}</cbc:ID>
            </cac:SellersItemIdentification>
            {% endif %}
            {% if detail.codProdGS1 %}
            <cac:StandardItemIdentification>
                <cbc:ID>{{ detail.codProdGS1 }}</cbc:ID>
            </cac:StandardItemIdentification>
            {% endif %}
            {% if detail.codProdSunat %}
            <cac:CommodityClassification>
                <cbc:ItemClassificationCode>{{ detail.codProdSunat }}</cbc:ItemClassificationCode>
            </cac:CommodityClassification>
            {% endif %}
            {% if detail.atributos %}
                {% for atr in detail.atributos %}
                    <cac:AdditionalItemProperty >
                        <cbc:Name>{{ atr.name }}</cbc:Name>
                        <cbc:NameCode>{{ atr.code }}</cbc:NameCode>
                        {% if atr.value is not null %}
                        <cbc:Value>{{ atr.value }}</cbc:Value>
                        {% endif %}
                        {% if atr.fecInicio or atr.fecFin or atr.duracion %}
                            <cac:UsabilityPeriod>
                                {% if atr.fecInicio %}
                                <cbc:StartDate>{{ atr.fecInicio|date('Y-m-d') }}</cbc:StartDate>
                                {% endif %}
                                {% if atr.fecFin %}
                                <cbc:EndDate>{{ atr.fecFin|date('Y-m-d') }}</cbc:EndDate>
                                {% endif %}
                                {% if atr.duracion %}
                                <cbc:DurationMeasure unitCode="DAY">{{ atr.duracion }}</cbc:DurationMeasure>
                                {% endif %}
                            </cac:UsabilityPeriod>
                        {% endif %}
                    </cac:AdditionalItemProperty>
                {% endfor %}
            {% endif %}
        </cac:Item>
        <cac:Price>
            <cbc:PriceAmount currencyID="{{ doc.tipoMoneda }}">{{ detail.mtoValorUnitario|n_format_limit(10) }}</cbc:PriceAmount>
        </cac:Price>
    </cac:InvoiceLine>
    {% endfor %}
</Invoice>
{% endapply %}
