ITADN

Wrong total Amount extracted

#1070Openosthues-labs 创建于 2026-03-20
O
osthues-labscommented
I experienced this issue on a pdf that was shared with my by an external party, so I cannot provide an example unfortunately. Also I created the fix and the issue description with the help of claude since I am not an expert of this matter. But the explanation does make sense to me and all our own tests using the extraction still pass. It might be that this is only an issue with invalid pdfs but it seems it this fix would still not hurt. Let me know if you need any other information from me. ## `ZUGFeRDInvoiceImporter` extracts wrong `GrandTotalAmount` and `TaxBasisTotalAmount` when multiple elements exist ### Description When importing invoices using `ZUGFeRDInvoiceImporter`, the `grandTotal` and `taxBasis` values extracted into `CalculatedInvoice` can be incorrect. This happens because the XPath expressions used to extract these values match **any** element with the given local name, rather than specifically targeting the header-level monetary summation. ### Root Cause In `ZUGFeRDInvoiceImporter.java`, the XPath expressions at lines 475 and 488 are too broad: ```java // Line 475 - GrandTotalAmount extraction xpr = xpath.compile("//*[local-name()=\"GrandTotalAmount\"]|//*[local-name()=\"TaxInclusiveAmount\"]"); // Line 488 - TaxBasisTotalAmount extraction xpr = xpath.compile("//*[local-name()=\"TaxBasisTotalAmount\"]|//*[local-name()=\"TaxExclusiveAmount\"]"); ``` The `//*` selector matches **any** element with that local name anywhere in the document. If there are multiple elements (e.g., at line-item level or in other contexts), `totalNodes.item(0)` returns the **first one found in document order**, which may not be the document-level total. ### Expected Behavior The extraction should specifically target the header-level monetary summation: - For CII format: `SpecifiedTradeSettlementHeaderMonetarySummation/GrandTotalAmount` - For UBL format: `LegalMonetaryTotal/TaxInclusiveAmount` This is already done correctly for other fields like `LineTotalAmount` at line 508: ```java xpr = xpath.compile("//*[local-name()=\"SpecifiedTradeSettlementHeaderMonetarySummation\"]/*[local-name()=\"LineTotalAmount\"]|//*[local-name()=\"LegalMonetaryTotal\"]/*[local-name()=\"LineExtensionAmount\"]"); ``` ### Proposed Fix Change the XPath expressions to be more specific: ```diff - xpr = xpath.compile("//*[local-name()=\"GrandTotalAmount\"]|//*[local-name()=\"TaxInclusiveAmount\"]"); + xpr = xpath.compile("//*[local-name()=\"SpecifiedTradeSettlementHeaderMonetarySummation\"]/*[local-name()=\"GrandTotalAmount\"]|//*[local-name()=\"LegalMonetaryTotal\"]/*[local-name()=\"TaxInclusiveAmount\"]"); ``` ```diff - xpr = xpath.compile("//*[local-name()=\"TaxBasisTotalAmount\"]|//*[local-name()=\"TaxExclusiveAmount\"]"); + xpr = xpath.compile("//*[local-name()=\"SpecifiedTradeSettlementHeaderMonetarySummation\"]/*[local-name()=\"TaxBasisTotalAmount\"]|//*[local-name()=\"LegalMonetaryTotal\"]/*[local-name()=\"TaxExclusiveAmount\"]"); ``` ### Impact Invoices with certain structures (e.g., where line-item or other non-header elements appear before the header summation in document order) will have incorrect `grandTotal` and `taxBasis` values extracted. ### Version Observed in version 2.22.0
0 条评论