DT-CIUS-PT-171

For each different value of VAT category rate (BT-119) where the VAT category code (BT-118) is "Lower rate", the VAT category taxable amount (BT-116) in a VATBReakdown (BG-23) shall equal the sum of Invoice line net amounts (BT-131) plus the sum of document level charge amounts (BT-99) minus the sum of document level allowance amounts (BT-92) where the VAT category code (BT-151, BT-102, BT-95) is “Lower rate” and the VAT rate (BT-152, BT-103, BT-96) equals the VAT category rate (BT-119), with an acceptance range of 1.00 € (it does not mean that this tolerance is accepted by the customer).

error: the document is invalid

What the rule checks

As the rule set's publisher wrote it. The rule runs on every element the context matches, and fails where the test is false; the validator reports the line of that element.

Context
cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[normalize-space(cbc:ID) = 'AA' or normalize-space(cbc:ID) = 'RED' or normalize-space(cbc:ID) = 'INT']
Test
every $rate in round(cbc:Percent) satisfies ((exists(//cac:InvoiceLine) and ((round((sum(../../../cac:InvoiceLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='AA' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='RED' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='INT'][cac:Item/cac:ClassifiedTaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:LineExtensionAmount)) + sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount)) - sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount))) * 100) div 100) <= (xs:decimal(../cbc:TaxableAmount) + 1.00) and (round((sum(../../../cac:InvoiceLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='AA' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='RED' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='INT'][cac:Item/cac:ClassifiedTaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:LineExtensionAmount)) + sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount)) - sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount))) * 100) div 100) >=(xs:decimal(../cbc:TaxableAmount) - 1.00))) or (exists(//cac:CreditNoteLine) and ( ((round((sum(../../../cac:CreditNoteLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='AA' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='RED' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='INT'][cac:Item/cac:ClassifiedTaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:LineExtensionAmount)) + sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount)) - sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount))) * 100) div 100) <= (xs:decimal(../cbc:TaxableAmount) + 1.00) and (round((sum(../../../cac:CreditNoteLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='AA' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='RED' or normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='INT'][cac:Item/cac:ClassifiedTaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:LineExtensionAmount)) + sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount)) - sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='AA' or normalize-space(cac:TaxCategory/cbc:ID)='RED' or normalize-space(cac:TaxCategory/cbc:ID)='INT'][cac:TaxCategory/cbc:Percent=$rate]/xs:decimal(cbc:Amount))) * 100) div 100) >=(xs:decimal(../cbc:TaxableAmount) - 1.00)))))

Checked with Portugal CIUS-PT Invoice (UBL), Portugal CIUS-PT Credit Note (UBL)