make invoiceimporters namespace aware to be able to properly ignore namespaces

This commit is contained in:
jstaerk
2021-10-18 15:27:50 +02:00
parent 5bac833f2a
commit 8094504fa5
4 changed files with 145 additions and 89 deletions

View File

@@ -1,3 +1,6 @@
- ZugferdInvoideImporter to be able to read XML, not only PDF
- ignore whitespace around numbers in invoiceimporter
2.3.0 2.3.0
======= =======
2021-10-04 2021-10-04

View File

@@ -180,7 +180,7 @@ public class ZUGFeRDImporter {
private void setDocument() throws ParserConfigurationException, IOException, SAXException { private void setDocument() throws ParserConfigurationException, IOException, SAXException {
final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
xmlFact.setNamespaceAware(false); xmlFact.setNamespaceAware(true);
final DocumentBuilder builder = xmlFact.newDocumentBuilder(); final DocumentBuilder builder = xmlFact.newDocumentBuilder();
final ByteArrayInputStream is = new ByteArrayInputStream(rawXML); final ByteArrayInputStream is = new ByteArrayInputStream(rawXML);
/// is.skip(guessBOMSize(is)); /// is.skip(guessBOMSize(is));
@@ -232,9 +232,9 @@ public class ZUGFeRDImporter {
* @return the reference (purpose) the sender specified for this invoice * @return the reference (purpose) the sender specified for this invoice
*/ */
public String getForeignReference() { public String getForeignReference() {
String result = extractString("//ApplicableHeaderTradeSettlement/PaymentReference"); String result = extractString("//*[local-name() = 'ApplicableHeaderTradeSettlement']/*[local-name() = 'PaymentReference']");
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
result = extractString("//ApplicableSupplyChainTradeSettlement/PaymentReference"); result = extractString("//*[local-name() = 'ApplicableSupplyChainTradeSettlement']/*[local-name() = 'PaymentReference']");
} }
return result; return result;
} }
@@ -243,7 +243,7 @@ public class ZUGFeRDImporter {
* @return the ZUGFeRD Profile * @return the ZUGFeRD Profile
*/ */
public String getZUGFeRDProfil() { public String getZUGFeRDProfil() {
switch (extractString("//GuidelineSpecifiedDocumentContextParameter//ID")) { switch (extractString("//*[local-name() = 'GuidelineSpecifiedDocumentContextParameter']//*[local-name() = 'ID']")) {
case "urn:cen.eu:en16931:2017": case "urn:cen.eu:en16931:2017":
case "urn:ferd:CrossIndustryDocument:invoice:1p0:comfort": case "urn:ferd:CrossIndustryDocument:invoice:1p0:comfort":
return "COMFORT"; return "COMFORT";
@@ -267,9 +267,9 @@ public class ZUGFeRDImporter {
public String getInvoiceCurrencyCode() { public String getInvoiceCurrencyCode() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//ApplicableSupplyChainTradeSettlement//InvoiceCurrencyCode"); return extractString("//*[local-name() = 'ApplicableSupplyChainTradeSettlement']//*[local-name() = 'InvoiceCurrencyCode']");
} else { } else {
return extractString("//ApplicableHeaderTradeSettlement//InvoiceCurrencyCode"); return extractString("//*[local-name() = 'ApplicableHeaderTradeSettlement']//*[local-name() = 'InvoiceCurrencyCode']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -302,9 +302,9 @@ public class ZUGFeRDImporter {
private String extractIssuerAssignedID(String propertyName) { private String extractIssuerAssignedID(String propertyName) {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//BuyerOrderReferencedDocument//ID"); return extractString("//*[local-name() = 'BuyerOrderReferencedDocument']//*[local-name() = 'ID']");
} else { } else {
return extractString("//BuyerOrderReferencedDocument//IssuerAssignedID"); return extractString("//*[local-name() = 'BuyerOrderReferencedDocument']//*[local-name() = 'IssuerAssignedID']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -316,7 +316,7 @@ public class ZUGFeRDImporter {
* @return the BuyerTradeParty ID * @return the BuyerTradeParty ID
*/ */
public String getBuyerTradePartyID() { public String getBuyerTradePartyID() {
return extractString("//BuyerTradeParty//ID"); return extractString("//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'ID']");
} }
/** /**
@@ -325,9 +325,9 @@ public class ZUGFeRDImporter {
public String getIssueDate() { public String getIssueDate() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//HeaderExchangedDocument//IssueDateTime//DateTimeString"); return extractString("//*[local-name() = 'HeaderExchangedDocument']//*[local-name() = 'IssueDateTime']//*[local-name() = 'DateTimeString']");
} else { } else {
return extractString("//ExchangedDocument//IssueDateTime//DateTimeString"); return extractString("//*[local-name() = 'ExchangedDocument']//*[local-name() = 'IssueDateTime']//*[local-name() = 'DateTimeString']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -341,9 +341,9 @@ public class ZUGFeRDImporter {
public String getTaxBasisTotalAmount() { public String getTaxBasisTotalAmount() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//SpecifiedTradeSettlementMonetarySummation//TaxBasisTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']//*[local-name() = 'TaxBasisTotalAmount']");
} else { } else {
return extractString("//SpecifiedTradeSettlementHeaderMonetarySummation//TaxBasisTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']//*[local-name() = 'TaxBasisTotalAmount']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -357,9 +357,9 @@ public class ZUGFeRDImporter {
public String getTaxTotalAmount() { public String getTaxTotalAmount() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//SpecifiedTradeSettlementMonetarySummation//TaxTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']//*[local-name() = 'TaxTotalAmount']");
} else { } else {
return extractString("//SpecifiedTradeSettlementHeaderMonetarySummation//TaxTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']//*[local-name() = 'TaxTotalAmount']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -373,9 +373,9 @@ public class ZUGFeRDImporter {
public String getRoundingAmount() { public String getRoundingAmount() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//SpecifiedTradeSettlementMonetarySummation//RoundingAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']//*[local-name() = 'RoundingAmount']");
} else { } else {
return extractString("//SpecifiedTradeSettlementHeaderMonetarySummation//RoundingAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']//*[local-name() = 'RoundingAmount']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -389,9 +389,9 @@ public class ZUGFeRDImporter {
public String getPaidAmount() { public String getPaidAmount() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//SpecifiedTradeSettlementMonetarySummation//TotalPrepaidAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']//*[local-name() = 'TotalPrepaidAmount']");
} else { } else {
return extractString("//SpecifiedTradeSettlementHeaderMonetarySummation//TotalPrepaidAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']//*[local-name() = 'TotalPrepaidAmount']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -403,21 +403,21 @@ public class ZUGFeRDImporter {
* @return SellerTradeParty GlobalID * @return SellerTradeParty GlobalID
*/ */
public String getSellerTradePartyGlobalID() { public String getSellerTradePartyGlobalID() {
return extractString("//SellerTradeParty//GlobalID"); return extractString("//*[local-name() = 'SellerTradeParty']//*[local-name() = 'GlobalID']");
} }
/** /**
* @return the BuyerTradeParty GlobalID * @return the BuyerTradeParty GlobalID
*/ */
public String getBuyerTradePartyGlobalID() { public String getBuyerTradePartyGlobalID() {
return extractString("//BuyerTradeParty//GlobalID"); return extractString("//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'GlobalID']");
} }
/** /**
* @return the BuyerTradeParty SpecifiedTaxRegistration ID * @return the BuyerTradeParty SpecifiedTaxRegistration ID
*/ */
public String getBuyertradePartySpecifiedTaxRegistrationID() { public String getBuyertradePartySpecifiedTaxRegistrationID() {
return extractString("//BuyerTradeParty//SpecifiedTaxRegistration//ID"); return extractString("//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'SpecifiedTaxRegistration']//*[local-name() = 'ID']");
} }
@@ -427,9 +427,9 @@ public class ZUGFeRDImporter {
public String getIncludedNote() { public String getIncludedNote() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//HeaderExchangedDocument//IncludedNote"); return extractString("//*[local-name() = 'HeaderExchangedDocument']//*[local-name() = 'IncludedNote']");
} else { } else {
return extractString("//ExchangedDocument//IncludedNote"); return extractString("//*[local-name() = 'ExchangedDocument']//*[local-name() = 'IncludedNote']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -441,7 +441,7 @@ public class ZUGFeRDImporter {
* @return the BuyerTradeParty Name * @return the BuyerTradeParty Name
*/ */
public String getBuyerTradePartyName() { public String getBuyerTradePartyName() {
return extractString("//BuyerTradeParty//Name"); return extractString("//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'Name']");
} }
@@ -452,9 +452,9 @@ public class ZUGFeRDImporter {
public String getLineTotalAmount() { public String getLineTotalAmount() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//SpecifiedTradeSettlementMonetarySummation//LineTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']//*[local-name() = 'LineTotalAmount']");
} else { } else {
return extractString("//SpecifiedTradeSettlementHeaderMonetarySummation//LineTotalAmount"); return extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']//*[local-name() = 'LineTotalAmount']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -466,7 +466,7 @@ public class ZUGFeRDImporter {
* @return the Payment Terms * @return the Payment Terms
*/ */
public String getPaymentTerms() { public String getPaymentTerms() {
return extractString("//SpecifiedTradePaymentTerms//Description"); return extractString("//*[local-name() = 'SpecifiedTradePaymentTerms']//*[local-name() = 'Description']");
} }
/** /**
@@ -475,9 +475,9 @@ public class ZUGFeRDImporter {
public String getTaxPointDate() { public String getTaxPointDate() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//ActualDeliverySupplyChainEvent//OccurrenceDateTime//DateTimeString"); return extractString("//*[local-name() = 'ActualDeliverySupplyChainEvent']//*[local-name() = 'OccurrenceDateTime']//*[local-name() = 'DateTimeString']");
} else { } else {
return extractString("//ActualDeliverySupplyChainEvent//OccurrenceDateTime//DateTimeString"); return extractString("//*[local-name() = 'ActualDeliverySupplyChainEvent']//*[local-name() = 'OccurrenceDateTime']//*[local-name() = 'DateTimeString']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -491,9 +491,9 @@ public class ZUGFeRDImporter {
public String getInvoiceID() { public String getInvoiceID() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//HeaderExchangedDocument//ID"); return extractString("//*[local-name() = 'HeaderExchangedDocument']//*[local-name() = 'ID']");
} else { } else {
return extractString("//ExchangedDocument//ID"); return extractString("//*[local-name() = 'ExchangedDocument']//*[local-name() = 'ID']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -509,9 +509,9 @@ public class ZUGFeRDImporter {
public String getDocumentCode() { public String getDocumentCode() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//HeaderExchangedDocument/TypeCode"); return extractString("//*[local-name() = 'HeaderExchangedDocument']/*[local-name() = 'TypeCode']");
} else { } else {
return extractString("//ExchangedDocument/TypeCode"); return extractString("//*[local-name() = 'ExchangedDocument']/*[local-name() = 'TypeCode']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -526,9 +526,9 @@ public class ZUGFeRDImporter {
public String getReference() { public String getReference() {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
return extractString("//ApplicableSupplyChainTradeAgreement/BuyerReference"); return extractString("//*[local-name() = 'ApplicableSupplyChainTradeAgreement']/*[local-name() = 'BuyerReference']");
} else { } else {
return extractString("//ApplicableHeaderTradeAgreement/BuyerReference"); return extractString("//*[local-name() = 'ApplicableHeaderTradeAgreement']/*[local-name() = 'BuyerReference']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -541,7 +541,7 @@ public class ZUGFeRDImporter {
* @return the sender's bank's BIC code * @return the sender's bank's BIC code
*/ */
public String getBIC() { public String getBIC() {
return extractString("//PayeeSpecifiedCreditorFinancialInstitution/BICID"); return extractString("//*[local-name() = 'PayeeSpecifiedCreditorFinancialInstitution']/*[local-name() = 'BICID']");
} }
@@ -549,7 +549,7 @@ public class ZUGFeRDImporter {
* @return the sender's bank name * @return the sender's bank name
*/ */
public String getBankName() { public String getBankName() {
return extractString("//PayeeSpecifiedCreditorFinancialInstitution/Name"); return extractString("//*[local-name() = 'PayeeSpecifiedCreditorFinancialInstitution']/*[local-name() = 'Name']");
} }
@@ -557,12 +557,12 @@ public class ZUGFeRDImporter {
* @return the sender's account IBAN code * @return the sender's account IBAN code
*/ */
public String getIBAN() { public String getIBAN() {
return extractString("//PayeePartyCreditorFinancialAccount/IBANID"); return extractString("//*[local-name() = 'PayeePartyCreditorFinancialAccount']/*[local-name() = 'IBANID']");
} }
public String getHolder() { public String getHolder() {
return extractString("//SellerTradeParty/Name"); return extractString("//*[local-name() = 'SellerTradeParty']/*[local-name() = 'Name']");
} }
@@ -570,9 +570,9 @@ public class ZUGFeRDImporter {
* @return the total payable amount * @return the total payable amount
*/ */
public String getAmount() { public String getAmount() {
String result = extractString("//SpecifiedTradeSettlementHeaderMonetarySummation/DuePayableAmount"); String result = extractString("//*[local-name() = 'SpecifiedTradeSettlementHeaderMonetarySummation']/*[local-name() = 'DuePayableAmount']");
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
result = extractString("//SpecifiedTradeSettlementMonetarySummation/GrandTotalAmount"); result = extractString("//*[local-name() = 'SpecifiedTradeSettlementMonetarySummation']/*[local-name() = 'GrandTotalAmount']");
} }
return result; return result;
} }
@@ -582,7 +582,7 @@ public class ZUGFeRDImporter {
* @return when the payment is due * @return when the payment is due
*/ */
public String getDueDate() { public String getDueDate() {
return extractString("//SpecifiedTradePaymentTerms/DueDateDateTime/DateTimeString"); return extractString("//*[local-name() = 'SpecifiedTradePaymentTerms']/*[local-name() = 'DueDateDateTime']/*[local-name() = 'DateTimeString']");
} }
@@ -714,9 +714,9 @@ public class ZUGFeRDImporter {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
nl = getNodeListByPath("//CrossIndustryDocument//SpecifiedSupplyChainTradeTransaction//ApplicableSupplyChainTradeAgreement//BuyerTradeParty//PostalTradeAddress"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryDocument']//*[local-name() = 'SpecifiedSupplyChainTradeTransaction/']/*[local-name() = 'ApplicableSupplyChainTradeAgreement']//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'PostalTradeAddress']");
} else { } else {
nl = getNodeListByPath("//CrossIndustryInvoice//SupplyChainTradeTransaction//ApplicableHeaderTradeAgreement//BuyerTradeParty//PostalTradeAddress"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryInvoice']//*[local-name() = 'SupplyChainTradeTransaction']//*[local-name() = 'ApplicableHeaderTradeAgreement']//*[local-name() = 'BuyerTradeParty']//*[local-name() = 'PostalTradeAddress']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -737,9 +737,9 @@ public class ZUGFeRDImporter {
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
nl = getNodeListByPath("//CrossIndustryDocument//SpecifiedSupplyChainTradeTransaction//ApplicableSupplyChainTradeAgreement//SellerTradeParty//PostalTradeAddress"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryDocument']//*[local-name() = 'SpecifiedSupplyChainTradeTransaction']//*[local-name() = 'ApplicableSupplyChainTradeAgreement']//*[local-name() = 'SellerTradeParty']//*[local-name() = 'PostalTradeAddress']");
} else { } else {
nl = getNodeListByPath("//CrossIndustryInvoice//SupplyChainTradeTransaction//ApplicableHeaderTradeAgreement//SellerTradeParty//PostalTradeAddress"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryInvoice']//*[local-name() = 'SupplyChainTradeTransaction']//*[local-name() = 'ApplicableHeaderTradeAgreement']//*[local-name() = 'SellerTradeParty']//*[local-name() = 'PostalTradeAddress']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
@@ -936,9 +936,9 @@ public class ZUGFeRDImporter {
NodeList nl = null; NodeList nl = null;
try { try {
if (getVersion() == 1) { if (getVersion() == 1) {
nl = getNodeListByPath("//CrossIndustryDocument//SpecifiedSupplyChainTradeTransaction//IncludedSupplyChainTradeLineItem"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryDocument']//*[local-name() = 'SpecifiedSupplyChainTradeTransaction']//*[local-name() = 'IncludedSupplyChainTradeLineItem']");
} else { } else {
nl = getNodeListByPath("//CrossIndustryInvoice//SupplyChainTradeTransaction//IncludedSupplyChainTradeLineItem"); nl = getNodeListByPath("//*[local-name() = 'CrossIndustryInvoice']//*[local-name() = 'SupplyChainTradeTransaction']//*[local-name() = 'IncludedSupplyChainTradeLineItem']");
} }
} catch (final Exception e) { } catch (final Exception e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);

View File

@@ -1,21 +1,41 @@
package org.mustangproject.ZUGFeRD; package org.mustangproject.ZUGFeRD;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.mustangproject.*; import org.mustangproject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import javax.xml.xpath.*; import javax.xml.xpath.*;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter { public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
private static final Logger LOGGER = LoggerFactory.getLogger(ZUGFeRDInvoiceImporter.class.getCanonicalName()); // log
public ZUGFeRDInvoiceImporter() {
super();
}
public ZUGFeRDInvoiceImporter(String filename) { public ZUGFeRDInvoiceImporter(String filename) {
super(filename); super(filename);
} }
public void fromXML(String XML) {
try {
containsMeta = true;
setRawXML(XML.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
/*** /***
* This will parse a XML into a invoice object * This will parse a XML into a invoice object
* @return the parsed invoice object * @return the parsed invoice object
@@ -58,13 +78,13 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
Node exchangedDocumentNode = ExchangedDocumentNodes.item(i); Node exchangedDocumentNode = ExchangedDocumentNodes.item(i);
NodeList exchangedDocumentChilds = exchangedDocumentNode.getChildNodes(); NodeList exchangedDocumentChilds = exchangedDocumentNode.getChildNodes();
for (int documentChildIndex = 0; documentChildIndex < exchangedDocumentChilds.getLength(); documentChildIndex++) { for (int documentChildIndex = 0; documentChildIndex < exchangedDocumentChilds.getLength(); documentChildIndex++) {
if (exchangedDocumentChilds.item(documentChildIndex).getNodeName().equals("ram:ID")) { if ((exchangedDocumentChilds.item(documentChildIndex).getLocalName() != null) && (exchangedDocumentChilds.item(documentChildIndex).getLocalName().equals("ID"))) {
number = exchangedDocumentChilds.item(documentChildIndex).getTextContent(); number = exchangedDocumentChilds.item(documentChildIndex).getTextContent();
} }
if (exchangedDocumentChilds.item(documentChildIndex).getNodeName().equals("ram:IssueDateTime")) { if ((exchangedDocumentChilds.item(documentChildIndex).getLocalName() != null) && (exchangedDocumentChilds.item(documentChildIndex).getLocalName().equals("IssueDateTime"))) {
NodeList issueDateTimeChilds = exchangedDocumentChilds.item(documentChildIndex).getChildNodes(); NodeList issueDateTimeChilds = exchangedDocumentChilds.item(documentChildIndex).getChildNodes();
for (int issueDateChildIndex = 0; issueDateChildIndex < issueDateTimeChilds.getLength(); issueDateChildIndex++) { for (int issueDateChildIndex = 0; issueDateChildIndex < issueDateTimeChilds.getLength(); issueDateChildIndex++) {
if (issueDateTimeChilds.item(issueDateChildIndex).getNodeName().equals("udt:DateTimeString")) { if ((issueDateTimeChilds.item(issueDateChildIndex).getLocalName()!=null)&&(issueDateTimeChilds.item(issueDateChildIndex).getLocalName().equals("DateTimeString"))) {
issueDate = new SimpleDateFormat("yyyyMMdd").parse(issueDateTimeChilds.item(issueDateChildIndex).getTextContent()); issueDate = new SimpleDateFormat("yyyyMMdd").parse(issueDateTimeChilds.item(issueDateChildIndex).getTextContent());
} }
} }
@@ -82,15 +102,14 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
Node headerTradeDeliveryNode = headerTradeDeliveryNodes.item(i); Node headerTradeDeliveryNode = headerTradeDeliveryNodes.item(i);
NodeList headerTradeDeliveryChilds = headerTradeDeliveryNode.getChildNodes(); NodeList headerTradeDeliveryChilds = headerTradeDeliveryNode.getChildNodes();
for (int deliveryChildIndex = 0; deliveryChildIndex < headerTradeDeliveryChilds.getLength(); deliveryChildIndex++) { for (int deliveryChildIndex = 0; deliveryChildIndex < headerTradeDeliveryChilds.getLength(); deliveryChildIndex++) {
if (headerTradeDeliveryChilds.item(deliveryChildIndex).getNodeName().equals("ram:ActualDeliverySupplyChainEvent")) { if ((headerTradeDeliveryChilds.item(deliveryChildIndex).getLocalName() != null) && (headerTradeDeliveryChilds.item(deliveryChildIndex).getLocalName().equals("ActualDeliverySupplyChainEvent"))) {
NodeList actualDeliveryChilds = headerTradeDeliveryChilds.item(deliveryChildIndex).getChildNodes(); NodeList actualDeliveryChilds = headerTradeDeliveryChilds.item(deliveryChildIndex).getChildNodes();
for (int actualDeliveryChildIndex = 0; actualDeliveryChildIndex < actualDeliveryChilds.getLength(); actualDeliveryChildIndex++) { for (int actualDeliveryChildIndex = 0; actualDeliveryChildIndex < actualDeliveryChilds.getLength(); actualDeliveryChildIndex++) {
if (actualDeliveryChilds.item(actualDeliveryChildIndex).getNodeName().equals("ram:OccurrenceDateTime")) { if ((actualDeliveryChilds.item(actualDeliveryChildIndex).getLocalName() != null) && (actualDeliveryChilds.item(actualDeliveryChildIndex).getLocalName().equals("OccurrenceDateTime"))) {
NodeList occurenceChilds = actualDeliveryChilds.item(actualDeliveryChildIndex).getChildNodes(); NodeList occurenceChilds = actualDeliveryChilds.item(actualDeliveryChildIndex).getChildNodes();
for (int occurenceChildIndex = 0; occurenceChildIndex < occurenceChilds.getLength(); occurenceChildIndex++) { for (int occurenceChildIndex = 0; occurenceChildIndex < occurenceChilds.getLength(); occurenceChildIndex++) {
if (occurenceChilds.item(occurenceChildIndex).getNodeName().equals("udt:DateTimeString")) { if ((occurenceChilds.item(occurenceChildIndex).getLocalName() != null) && (occurenceChilds.item(occurenceChildIndex).getLocalName().equals("DateTimeString"))) {
deliveryDate = new SimpleDateFormat("yyyyMMdd").parse(occurenceChilds.item(occurenceChildIndex).getTextContent()); deliveryDate = new SimpleDateFormat("yyyyMMdd").parse(occurenceChilds.item(occurenceChildIndex).getTextContent());
} }
} }
} }
@@ -109,15 +128,14 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
Node headerTradeSettlementNode = headerTradeSettlementNodes.item(i); Node headerTradeSettlementNode = headerTradeSettlementNodes.item(i);
NodeList headerTradeSettlementChilds = headerTradeSettlementNode.getChildNodes(); NodeList headerTradeSettlementChilds = headerTradeSettlementNode.getChildNodes();
for (int settlementChildIndex = 0; settlementChildIndex < headerTradeSettlementChilds.getLength(); settlementChildIndex++) { for (int settlementChildIndex = 0; settlementChildIndex < headerTradeSettlementChilds.getLength(); settlementChildIndex++) {
if (headerTradeSettlementChilds.item(settlementChildIndex).getNodeName().equals("ram:SpecifiedTradePaymentTerms")) { if ((headerTradeSettlementChilds.item(settlementChildIndex).getLocalName() != null) && (headerTradeSettlementChilds.item(settlementChildIndex).getLocalName().equals("SpecifiedTradePaymentTerms"))) {
NodeList paymentTermChilds = headerTradeSettlementChilds.item(settlementChildIndex).getChildNodes(); NodeList paymentTermChilds = headerTradeSettlementChilds.item(settlementChildIndex).getChildNodes();
for (int paymentTermChildIndex = 0; paymentTermChildIndex < paymentTermChilds.getLength(); paymentTermChildIndex++) { for (int paymentTermChildIndex = 0; paymentTermChildIndex < paymentTermChilds.getLength(); paymentTermChildIndex++) {
if (paymentTermChilds.item(paymentTermChildIndex).getNodeName().equals("ram:DueDateDateTime")) { if ((paymentTermChilds.item(paymentTermChildIndex).getLocalName() != null) && (paymentTermChilds.item(paymentTermChildIndex).getLocalName().equals("DueDateDateTime"))) {
NodeList dueDateChilds = paymentTermChilds.item(paymentTermChildIndex).getChildNodes(); NodeList dueDateChilds = paymentTermChilds.item(paymentTermChildIndex).getChildNodes();
for (int dueDateChildIndex = 0; dueDateChildIndex < dueDateChilds.getLength(); dueDateChildIndex++) { for (int dueDateChildIndex = 0; dueDateChildIndex < dueDateChilds.getLength(); dueDateChildIndex++) {
if (dueDateChilds.item(dueDateChildIndex).getNodeName().equals("udt:DateTimeString")) { if ((dueDateChilds.item(dueDateChildIndex).getLocalName() != null) && (dueDateChilds.item(dueDateChildIndex).getLocalName().equals("DateTimeString"))) {
dueDate = new SimpleDateFormat("yyyyMMdd").parse(dueDateChilds.item(dueDateChildIndex).getTextContent()); dueDate = new SimpleDateFormat("yyyyMMdd").parse(dueDateChilds.item(dueDateChildIndex).getTextContent());
} }
} }
} }
@@ -128,7 +146,7 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
zpp = new Invoice().setDueDate(dueDate).setDeliveryDate(deliveryDate).setIssueDate(issueDate).setSender(new TradeParty(SellerNodes)).setRecipient(new TradeParty(BuyerNodes)).setNumber(number); zpp = new Invoice().setDueDate(dueDate).setDeliveryDate(deliveryDate).setIssueDate(issueDate).setSender(new TradeParty(SellerNodes)).setRecipient(new TradeParty(BuyerNodes)).setNumber(number);
//.addItem(new Item(new Product("Testprodukt","","C62",BigDecimal.ZERO),amount,new BigDecimal(1.0))) //.addItem(new Item(new Product("Testprodukt","","C62",BigDecimal.ZERO),amount,new BigDecimal(1.0)))
zpp.setOwnOrganisationName(extractString("//SellerTradeParty/Name")); zpp.setOwnOrganisationName(extractString("//*[local-name()=\"SellerTradeParty\"]/*[local-name()=\"Name\"]"));
xpr = xpath.compile( xpr = xpath.compile(
"//*[local-name()=\"IncludedSupplyChainTradeLineItem\"]"); "//*[local-name()=\"IncludedSupplyChainTradeLineItem\"]");
NodeList nodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET); NodeList nodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET);
@@ -148,44 +166,45 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
Node currentItemNode = nodes.item(i); Node currentItemNode = nodes.item(i);
NodeList itemChilds = currentItemNode.getChildNodes(); NodeList itemChilds = currentItemNode.getChildNodes();
for (int itemChildIndex = 0; itemChildIndex < itemChilds.getLength(); itemChildIndex++) { for (int itemChildIndex = 0; itemChildIndex < itemChilds.getLength(); itemChildIndex++) {
if (itemChilds.item(itemChildIndex).getNodeName().equals("ram:SpecifiedLineTradeAgreement")) { if ((itemChilds.item(itemChildIndex).getLocalName() != null) && (itemChilds.item(itemChildIndex).getLocalName().equals("SpecifiedLineTradeAgreement"))) {
NodeList tradeLineChilds = itemChilds.item(itemChildIndex).getChildNodes(); NodeList tradeLineChilds = itemChilds.item(itemChildIndex).getChildNodes();
for (int tradeLineChildIndex = 0; tradeLineChildIndex < tradeLineChilds.getLength(); tradeLineChildIndex++) { for (int tradeLineChildIndex = 0; tradeLineChildIndex < tradeLineChilds.getLength(); tradeLineChildIndex++) {
if (tradeLineChilds.item(tradeLineChildIndex).getNodeName().equals("ram:NetPriceProductTradePrice")) { if ((tradeLineChilds.item(tradeLineChildIndex).getLocalName()!=null)&&tradeLineChilds.item(tradeLineChildIndex).getLocalName().equals("NetPriceProductTradePrice")) {
NodeList netChilds = tradeLineChilds.item(tradeLineChildIndex).getChildNodes(); NodeList netChilds = tradeLineChilds.item(tradeLineChildIndex).getChildNodes();
for (int netIndex = 0; netIndex < netChilds.getLength(); netIndex++) { for (int netIndex = 0; netIndex < netChilds.getLength(); netIndex++) {
if (netChilds.item(netIndex).getNodeName().equals("ram:ChargeAmount")) { if ((netChilds.item(netIndex).getLocalName() != null) && (netChilds.item(netIndex).getLocalName().equals("ChargeAmount"))) {
price = netChilds.item(netIndex).getTextContent();//ram:ChargeAmount price = netChilds.item(netIndex).getTextContent();//ChargeAmount
} }
} }
} }
} }
} }
if (itemChilds.item(itemChildIndex).getNodeName().equals("ram:SpecifiedLineTradeDelivery")) { if ((itemChilds.item(itemChildIndex).getLocalName() != null) && (itemChilds.item(itemChildIndex).getLocalName().equals("SpecifiedLineTradeDelivery"))) {
NodeList tradeLineChilds = itemChilds.item(itemChildIndex).getChildNodes(); NodeList tradeLineChilds = itemChilds.item(itemChildIndex).getChildNodes();
for (int tradeLineChildIndex = 0; tradeLineChildIndex < tradeLineChilds.getLength(); tradeLineChildIndex++) { for (int tradeLineChildIndex = 0; tradeLineChildIndex < tradeLineChilds.getLength(); tradeLineChildIndex++) {
if (tradeLineChilds.item(tradeLineChildIndex).getNodeName().equals("ram:BilledQuantity")) { if ((tradeLineChilds.item(tradeLineChildIndex).getLocalName() != null) && (tradeLineChilds.item(tradeLineChildIndex).getLocalName().equals("BilledQuantity"))) {
quantity = tradeLineChilds.item(tradeLineChildIndex).getTextContent(); quantity = tradeLineChilds.item(tradeLineChildIndex).getTextContent();
unitCode = tradeLineChilds.item(tradeLineChildIndex).getAttributes().getNamedItem("unitCode").getNodeValue(); unitCode = tradeLineChilds.item(tradeLineChildIndex).getAttributes().getNamedItem("unitCode").getNodeValue();
} }
} }
} }
if (itemChilds.item(itemChildIndex).getNodeName().equals("ram:SpecifiedTradeProduct")) { if ((itemChilds.item(itemChildIndex).getLocalName() != null) && (itemChilds.item(itemChildIndex).getLocalName().equals("SpecifiedTradeProduct"))) {
NodeList tradeProductChilds = itemChilds.item(itemChildIndex).getChildNodes(); NodeList tradeProductChilds = itemChilds.item(itemChildIndex).getChildNodes();
for (int tradeProductChildIndex = 0; tradeProductChildIndex < tradeProductChilds.getLength(); tradeProductChildIndex++) { for (int tradeProductChildIndex = 0; tradeProductChildIndex < tradeProductChilds.getLength(); tradeProductChildIndex++) {
if (tradeProductChilds.item(tradeProductChildIndex).getNodeName().equals("ram:Name")) { if ((tradeProductChilds.item(tradeProductChildIndex).getLocalName() != null) && (tradeProductChilds.item(tradeProductChildIndex).getLocalName().equals("Name")))
{
name = tradeProductChilds.item(tradeProductChildIndex).getTextContent(); name = tradeProductChilds.item(tradeProductChildIndex).getTextContent();
} }
} }
} }
if (itemChilds.item(itemChildIndex).getNodeName().equals("ram:SpecifiedLineTradeSettlement")) { if ((itemChilds.item(itemChildIndex).getLocalName() != null) && (itemChilds.item(itemChildIndex).getLocalName().equals("SpecifiedLineTradeSettlement"))) {
NodeList tradeSettlementChilds = itemChilds.item(itemChildIndex).getChildNodes(); NodeList tradeSettlementChilds = itemChilds.item(itemChildIndex).getChildNodes();
for (int tradeSettlementChildIndex = 0; tradeSettlementChildIndex < tradeSettlementChilds.getLength(); tradeSettlementChildIndex++) { for (int tradeSettlementChildIndex = 0; tradeSettlementChildIndex < tradeSettlementChilds.getLength(); tradeSettlementChildIndex++) {
if (tradeSettlementChilds.item(tradeSettlementChildIndex).getNodeName().equals("ram:ApplicableTradeTax")) { if ((tradeSettlementChilds.item(tradeSettlementChildIndex).getLocalName() != null) && (tradeSettlementChilds.item(tradeSettlementChildIndex).getLocalName().equals("ApplicableTradeTax"))) {
NodeList taxChilds = tradeSettlementChilds.item(tradeSettlementChildIndex).getChildNodes(); NodeList taxChilds = tradeSettlementChilds.item(tradeSettlementChildIndex).getChildNodes();
for (int taxChildIndex = 0; taxChildIndex < taxChilds.getLength(); taxChildIndex++) { for (int taxChildIndex = 0; taxChildIndex < taxChilds.getLength(); taxChildIndex++) {
if (taxChilds.item(taxChildIndex).getNodeName().equals("ram:RateApplicablePercent")) { if ((taxChilds.item(taxChildIndex).getLocalName() != null) && (taxChilds.item(taxChildIndex).getLocalName().equals("RateApplicablePercent"))) {
vatPercent = taxChilds.item(taxChildIndex).getTextContent(); vatPercent = taxChilds.item(taxChildIndex).getTextContent();
} }
} }
@@ -195,7 +214,7 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
} }
zpp.addItem(new Item(new Product(name, description, unitCode, new BigDecimal(vatPercent)), new BigDecimal(price), new BigDecimal(quantity))); zpp.addItem(new Item(new Product(name, description, unitCode, new BigDecimal(vatPercent.trim())), new BigDecimal(price.trim()), new BigDecimal(quantity.trim())));
} }
} }
@@ -213,26 +232,31 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
String reason = null; String reason = null;
String taxPercent = null; String taxPercent = null;
for (int chargeChildIndex = 0; chargeChildIndex < chargeNodeChilds.getLength(); chargeChildIndex++) { for (int chargeChildIndex = 0; chargeChildIndex < chargeNodeChilds.getLength(); chargeChildIndex++) {
if (chargeNodeChilds.item(chargeChildIndex).getNodeName().equals("ram:ChargeIndicator")) { if (chargeNodeChilds.item(chargeChildIndex).getLocalName() != null) {
NodeList indicatorChilds = chargeNodeChilds.item(chargeChildIndex).getChildNodes();
for (int indicatorChildIndex = 0; indicatorChildIndex < indicatorChilds.getLength(); indicatorChildIndex++) { if (chargeNodeChilds.item(chargeChildIndex).getLocalName().equals("ChargeIndicator")) {
if (indicatorChilds.item(indicatorChildIndex).getNodeName().equals("udt:Indicator")) { NodeList indicatorChilds = chargeNodeChilds.item(chargeChildIndex).getChildNodes();
isCharge = indicatorChilds.item(indicatorChildIndex).getTextContent().equalsIgnoreCase("true"); for (int indicatorChildIndex = 0; indicatorChildIndex < indicatorChilds.getLength(); indicatorChildIndex++) {
if (indicatorChilds.item(indicatorChildIndex).getLocalName().equals("Indicator")) {
isCharge = indicatorChilds.item(indicatorChildIndex).getTextContent().equalsIgnoreCase("true");
}
} }
} } else if (chargeNodeChilds.item(chargeChildIndex).getLocalName().equals("ActualAmount")) {
} else if (chargeNodeChilds.item(chargeChildIndex).getNodeName().equals("ram:ActualAmount")) { chargeAmount = chargeNodeChilds.item(chargeChildIndex).getTextContent();
chargeAmount = chargeNodeChilds.item(chargeChildIndex).getTextContent(); } else if (chargeNodeChilds.item(chargeChildIndex).getLocalName().equals("Reason")) {
} else if (chargeNodeChilds.item(chargeChildIndex).getNodeName().equals("ram:Reason")) { reason = chargeNodeChilds.item(chargeChildIndex).getTextContent();
reason = chargeNodeChilds.item(chargeChildIndex).getTextContent(); } else if (chargeNodeChilds.item(chargeChildIndex).getLocalName().equals("CategoryTradeTax")) {
} else if (chargeNodeChilds.item(chargeChildIndex).getNodeName().equals("ram:CategoryTradeTax")) { NodeList taxChilds = chargeNodeChilds.item(chargeChildIndex).getChildNodes();
NodeList taxChilds = chargeNodeChilds.item(chargeChildIndex).getChildNodes(); for (int taxChildIndex = 0; taxChildIndex < taxChilds.getLength(); taxChildIndex++) {
for (int taxChildIndex = 0; taxChildIndex < taxChilds.getLength(); taxChildIndex++) { if ((taxChilds.item(taxChildIndex).getLocalName() != null) && (taxChilds.item(taxChildIndex).getLocalName().equals("RateApplicablePercent")))
if (taxChilds.item(taxChildIndex).getNodeName().equals("ram:RateApplicablePercent")) { {
taxPercent = taxChilds.item(taxChildIndex).getTextContent(); taxPercent = taxChilds.item(taxChildIndex).getTextContent();
}
} }
} }
} }
} }

View File

@@ -19,12 +19,23 @@
*********************************************************************** */ *********************************************************************** */
package org.mustangproject.ZUGFeRD; package org.mustangproject.ZUGFeRD;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.mustangproject.Invoice; import org.mustangproject.Invoice;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
import java.io.StringReader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -33,7 +44,7 @@ import java.text.SimpleDateFormat;
* Classname ZF2ZInvoiceImporterTest is alphabetical behind the tests which will create the file * Classname ZF2ZInvoiceImporterTest is alphabetical behind the tests which will create the file
* used for this import, testout-ZF2New.pdf * used for this import, testout-ZF2New.pdf
*/ */
public class ZF2ZInvoiceImporterTest extends TestCase { public class ZF2ZInvoiceImporterTest extends ResourceCase {
public void testInvoiceImport() { public void testInvoiceImport() {
@@ -119,6 +130,24 @@ public class ZF2ZInvoiceImporterTest extends TestCase {
} }
public void testItemReferencedDocumentsImport() {
ZUGFeRDInvoiceImporter zii=new ZUGFeRDInvoiceImporter();
DocumentBuilderFactory db = DocumentBuilderFactory.newInstance();
boolean hasExceptions=false;
Invoice invoice=null;
try {
zii.fromXML(new String(Files.readAllBytes(Paths.get(getResourceAsFile("factur-x-testImport.xml").getAbsolutePath())), StandardCharsets.UTF_8));
invoice=zii.extractInvoice();
} catch (XPathExpressionException | ParseException | IOException e) {
hasExceptions=true;
}
assertFalse(hasExceptions);
TransactionCalculator tc=new TransactionCalculator(invoice);
assertEquals(new BigDecimal("1284.66"),tc.getGrandTotal());
}
} }