{% apply spaceless %}
<?xml version="1.0" encoding="utf-8"?>
<Perception xmlns="urn:sunat:names:specification:ubl:peru:schema:xsd:Perception-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|raw }}]]></cbc:RegistrationName>
		</cac:PartyLegalEntity>
	</cac:ReceiverParty>
	<sac:SUNATPerceptionSystemCode>{{ doc.regimen }}</sac:SUNATPerceptionSystemCode>
	<sac:SUNATPerceptionPercent>{{ doc.tasa|n_format }}</sac:SUNATPerceptionPercent>
    {% if doc.observacion %}
	<cbc:Note><![CDATA[{{ doc.observacion|raw }}]]></cbc:Note>
	{% endif %}
	<cbc:TotalInvoiceAmount currencyID="PEN">{{ doc.impPercibido|n_format }}</cbc:TotalInvoiceAmount>
	<sac:SUNATTotalCashed currencyID="PEN">{{ doc.impCobrado|n_format }}</sac:SUNATTotalCashed>
	{% for det in doc.details %}
	<sac:SUNATPerceptionDocumentReference>
		<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.cobros %}
        {% for cob in det.cobros %}
		<cac:Payment>
			<cbc:ID>{{ loop.index }}</cbc:ID>
			<cbc:PaidAmount currencyID="{{ cob.moneda }}">{{ cob.importe|n_format }}</cbc:PaidAmount>
			<cbc:PaidDate>{{ cob.fecha|date('Y-m-d') }}</cbc:PaidDate>
		</cac:Payment>
        {% endfor %}
		{% endif %}
		{% if det.impPercibido and det.impCobrar and det.fechaPercepcion %}
		<sac:SUNATPerceptionInformation>
			<sac:SUNATPerceptionAmount currencyID="PEN">{{ det.impPercibido|n_format }}</sac:SUNATPerceptionAmount>
			<sac:SUNATPerceptionDate>{{ det.fechaPercepcion|date('Y-m-d') }}</sac:SUNATPerceptionDate>
			<sac:SUNATNetTotalCashed currencyID="PEN">{{ det.impCobrar|n_format }}</sac:SUNATNetTotalCashed>
            {% 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:SUNATPerceptionInformation>
		{% endif %}
	</sac:SUNATPerceptionDocumentReference>
	{% endfor %}
</Perception>
{% endapply %}