From da17627a11dd589b463a1b1b6f30938aaacf24c9 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Sun, 15 Dec 2024 17:12:01 +0100 Subject: [PATCH] added setter methods to update the xml tree (#215) --- .../documents/einvoice/EInvoiceModel.java | 49 ++++++++++++++ .../documents/einvoice/EInvoiceModelCII.java | 32 +++++++-- .../documents/einvoice/EInvoiceModelUBL.java | 66 ++----------------- 3 files changed, 82 insertions(+), 65 deletions(-) diff --git a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModel.java b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModel.java index 1efe880..614a182 100644 --- a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModel.java +++ b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModel.java @@ -1,5 +1,7 @@ package org.imixs.archive.documents.einvoice; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.math.BigDecimal; import java.security.SecureRandom; import java.time.LocalDate; @@ -11,6 +13,13 @@ import java.util.Set; import java.util.logging.Logger; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -107,6 +116,12 @@ public BigDecimal getNetTotalAmount() { return netTotalAmount; } + public abstract void setNetTotalAmount(BigDecimal value); + + public void setNetTotalAmount(Double value) { + setNetTotalAmount(BigDecimal.valueOf(value)); + } + /** * Returns all trade parties * @@ -268,4 +283,38 @@ public void setPrefix(EInvoiceNS ns, String prefix) { } + /** + * Returns the XML representation of the current document as a byte array + * + * @return byte array containing the XML data + * @throws TransformerException + */ + public byte[] getContent() throws TransformerException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try { + // Setup transformer + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + + // Configure output properties + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); + + // Transform DOM to byte array + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(outputStream); + transformer.transform(source, result); + + return outputStream.toByteArray(); + + } finally { + try { + outputStream.close(); + } catch (IOException e) { + logger.warning("Failed to close output stream: " + e.getMessage()); + } + } + } + } diff --git a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelCII.java b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelCII.java index 296b5ae..edad41c 100644 --- a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelCII.java +++ b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelCII.java @@ -54,11 +54,6 @@ public void setNameSpaces() { for (int j = 0; j < defAttributes.getLength(); j++) { Node node = defAttributes.item(j); - // if (getPrefix(EInvoiceNS.A).equals(node.getLocalName()) - // && !getUri(EInvoiceNS.A).equals(node.getNodeValue())) { - // logger.fine("...set A namespace URI: " + node.getNodeValue()); - // setUri(EInvoiceNS.A, node.getNodeValue()); - // } if (getPrefix(EInvoiceNS.RSM).equals(node.getLocalName()) && !getUri(EInvoiceNS.RSM).equals(node.getNodeValue())) { logger.fine("...set RSM namespace URI: " + node.getNodeValue()); @@ -227,4 +222,31 @@ public TradeParty parseTradeParty(Element tradePartyElement, String type) { } return tradeParty; } + + @Override + public void setNetTotalAmount(BigDecimal value) { + // Finde das SpecifiedTradeSettlementHeaderMonetarySummation Element + Element element = findChildNodeByName(supplyChainTradeTransaction, EInvoiceNS.RAM, + "ApplicableHeaderTradeSettlement"); + if (element != null) { + Element tradeSettlementElement = findChildNodeByName(element, EInvoiceNS.RAM, + "SpecifiedTradeSettlementHeaderMonetarySummation"); + if (tradeSettlementElement != null) { + // Update LineTotalAmount + Element lineTotalElement = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM, + "LineTotalAmount"); + if (lineTotalElement != null) { + lineTotalElement.setTextContent(value.toPlainString()); + // Update auch den internen Wert + netTotalAmount = value; + } + // Update TaxBasisTotalAmount + Element taxBasisElement = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM, + "TaxBasisTotalAmount"); + if (taxBasisElement != null) { + taxBasisElement.setTextContent(value.toPlainString()); + } + } + } + } } diff --git a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelUBL.java b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelUBL.java index 1329074..62f2130 100644 --- a/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelUBL.java +++ b/imixs-archive-documents/src/main/java/org/imixs/archive/documents/einvoice/EInvoiceModelUBL.java @@ -102,30 +102,6 @@ private void loadDocumentCoreData() { parseTotal(); - // // read Total amount - // element = findChildNodeByName(supplyChainTradeTransaction, EInvoiceNS.RAM, - // "ApplicableHeaderTradeSettlement"); - // if (element != null) { - // Element tradeSettlementElement = findChildNodeByName(element, EInvoiceNS.RAM, - // "SpecifiedTradeSettlementHeaderMonetarySummation"); - // if (tradeSettlementElement != null) { - // Element child = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM, - // "GrandTotalAmount"); - // if (child != null) { - // grandTotalAmount = new BigDecimal(child.getTextContent()); - // } - // child = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM, - // "TaxTotalAmount"); - // if (child != null) { - // taxTotalAmount = new BigDecimal(child.getTextContent()); - // } - // netTotalAmount = grandTotalAmount.subtract(taxTotalAmount).setScale(2, - // RoundingMode.HALF_UP); - - // } - - // } - } /** @@ -184,43 +160,13 @@ public TradeParty parseTradeParty(Element tradePartyElement, String type) { } - // Element postalAddress = findChildNodeByName(tradePartyElement, - // EInvoiceNS.RAM, - // "PostalTradeAddress"); - // if (postalAddress != null) { - // partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM, - // "PostcodeCode"); - // if (partyElement != null) { - // tradeParty.setPostcodeCode(partyElement.getTextContent()); - // } - // partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM, - // "CityName"); - // if (partyElement != null) { - // tradeParty.setCityName(partyElement.getTextContent()); - // } - // partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM, - // "CountryID"); - // if (partyElement != null) { - // tradeParty.setCountryId(partyElement.getTextContent()); - // } - // partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM, - // "LineOne"); - // if (partyElement != null) { - // tradeParty.setStreetAddress(partyElement.getTextContent()); - // } - // } - - // Element specifiedTaxRegistration = findChildNodeByName(tradePartyElement, - // EInvoiceNS.RAM, - // "SpecifiedTaxRegistration"); - // if (specifiedTaxRegistration != null) { - // partyElement = findChildNodeByName(specifiedTaxRegistration, EInvoiceNS.RAM, - // "ID"); - // if (partyElement != null) { - // tradeParty.setVatNumber(partyElement.getTextContent()); - // } - // } return tradeParty; } + @Override + public void setNetTotalAmount(BigDecimal value) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'setNetTotalAmount'"); + } + }