peppolautoriteit-nl / validation

Contains all files related to validation of the simplerinvoicing xml files
MIT License
23 stars 12 forks source link

Error in rule BR-S-08 for SI UBL 2.0 RC1 XSL #13

Closed ojundt closed 5 years ago

ojundt commented 6 years ago

I just came across a small bug in the XSL file for SI UBL 2.0 RC1.

Rule BR-S-08 runs the following XPath query as test:

every $rate in round(cbc:Percent) satisfies (
  (
    exists(//cac:InvoiceLine) and (
      ../cbc:TaxableAmount = (
        sum(../../../cac:InvoiceLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='S'][cac:Item/cac:ClassifiedTaxCategory/round(cbc:Percent) =$rate]/xs:decimal(cbc:LineExtensionAmount)) +
        sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='S'][cac:TaxCategory/round(cbc:Percent) = $rate]/xs:decimal(cbc:Amount)) -
        sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='S'][cac:TaxCategory/round(cbc:Percent) = $rate]/xs:decimal(cbc:Amount))
      )
    )
  ) or (
    exists(//cac:CreditNoteLine) and (
      ../cbc:TaxableAmount = (
        sum(../../../cac:CreditNoteLine[normalize-space(cac:Item/cac:ClassifiedTaxCategory/cbc:ID)='S'][cac:Item/cac:ClassifiedTaxCategory/round(cbc:Percent) =$rate]/xs:decimal(cbc:LineExtensionAmount)) +
        sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='true'][normalize-space(cac:TaxCategory/cbc:ID)='S'][cac:TaxCategory/round(cbc:Percent) = $rate]/xs:decimal(cbc:Amount)) -
        sum(../../../cac:AllowanceCharge[cbc:ChargeIndicator='false'][normalize-space(cac:TaxCategory/cbc:ID)='S'][cac:TaxCategory/round(cbc:Percent) = $rate]/xs:decimal(cbc:Amount))
      )
    )
  )
)

in the context of /xmlns:Invoice/cac:InvoiceLine/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory.

The path traversal in the sum parts does not go high enough. For example the line sum(../../../cac:InvoiceLine[ ... is translated to sum(/xmlns:Invoice/cac:InvoiceLine/cac:InvoiceLine[ ... which ofcourse does not give the expected results, causing the rule to always fail.

tjeb commented 6 years ago

Heh, nice catch. I will deflect blame upstream where I got the CEN rules from :) (and will report, but let's first see if we can fix it).

I think it is not so much the test itself, but the context that is wrong.

From the looks of it, this rule is only meant to be fired from within /cac:[Invoice|CreditNote]/cac:TaxTotal, and not from the InvoiceLines and CreditNoteLines; not only do the relative paths point to the wrong level then, the TaxableAmount will also be the wrong one (and if all are corrected to use an absolute path, the test is performed multiple times for all the values unless the context is fixed).

I have created a branch with what I think fixes it (by fixing the context), fix in commit 26d5d6a3b773f5ffec763a78ac4934e731998761

And I have created two test documents that shows the problem: https://github.com/SimplerInvoicing/testset/blob/master/SI-UBL-2.0/SI-UBL-2.0_BR-S-08_error.xml (should give error)

https://github.com/SimplerInvoicing/testset/blob/master/SI-UBL-2.0/SI-UBL-2.0_UBL-CR-561_warning.xml (should only warn, but errors as well due to this issue)

ojundt commented 6 years ago

Thanks for the quick fix. I can confirm that the update XSL works.

yvonnenieuwerth commented 5 years ago

Is it possible that this fix is being undone with commit https://github.com/SimplerInvoicing/validation/commit/e85addeb30bdffeafda101aa70df0c5cc5c17c61#diff-6dd04cdf48683997bf2d75f64a10c097?

We have updated the xsl file, and seem to be getting this error again.

HugoOlthof commented 4 years ago

Is it possible to release version 2.0.2 with this fix included?