Skip to content

Commit

Permalink
fixed CII Model (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Dec 27, 2024
1 parent bfed18a commit 5ba6662
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,39 @@ public Element findOrCreateChildNode(Element parent, EInvoiceNS ns, String nodeN
/**
* This helper method creates a new child node by name from a given parent
* node.
* <p>
* If the param insertBefore is given, the new element is inserted before this
* element.
*
* @param parent
* @param nodeName
* @param insertBefore
* @return - Child Element matching the given node name. If no nodes were found,
* the method returns null
*/
public Element createChildNode(Element parent, EInvoiceNS ns, String nodeName) {
public Element createChildNode(Element parent, EInvoiceNS ns, String nodeName, Element insertBefore) {
Element element = getDoc().createElement(getPrefix(ns) + nodeName);
parent.appendChild(element);
if (insertBefore != null) {
parent.insertBefore(element, insertBefore);
} else {
parent.appendChild(element);
}
return element;
}

/**
* This helper method creates a new child node by name from a given parent
* node.
*
* @param parent
* @param nodeName
* @return - Child Element matching the given node name. If no nodes were found,
* the method returns null
*/
public Element createChildNode(Element parent, EInvoiceNS ns, String nodeName) {
return createChildNode(parent, ns, nodeName, null);
}

/**
* Helper method to update or create an element with a given value
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class EInvoiceModelCII extends EInvoiceModel {
protected Element supplyChainTradeTransaction;
protected Element applicableHeaderTradeSettlement;
protected Element specifiedTradeSettlementHeaderMonetarySummation;
protected Element applicableHeaderTradeDelivery;
protected Element applicableHeaderTradeAgreement;

public EInvoiceModelCII(Document doc) {
super(doc);
Expand Down Expand Up @@ -98,6 +100,11 @@ public void parseContent() {
supplyChainTradeTransaction = findOrCreateChildNode(getRoot(), EInvoiceNS.RSM,
"SupplyChainTradeTransaction");

applicableHeaderTradeAgreement = findOrCreateChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeAgreement");
applicableHeaderTradeDelivery = findOrCreateChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeDelivery");

applicableHeaderTradeSettlement = findOrCreateChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeSettlement");
specifiedTradeSettlementHeaderMonetarySummation = findChildNode(applicableHeaderTradeSettlement,
Expand Down Expand Up @@ -168,32 +175,26 @@ private void loadDocumentCoreData() {
}

// read ApplicableHeaderTradeAgreement - buyerReference
element = findChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM, "ApplicableHeaderTradeAgreement");
if (element != null) {
Element buyerReferenceElement = findChildNode(element, EInvoiceNS.RAM,
"BuyerReference");
if (buyerReferenceElement != null) {
buyerReference = buyerReferenceElement.getTextContent();
}
Element tradePartyElement = findChildNode(element, EInvoiceNS.RAM,
"SellerTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "seller"));
}
tradePartyElement = findChildNode(element, EInvoiceNS.RAM,
"BuyerTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "buyer"));
}
Element buyerReferenceElement = findChildNode(applicableHeaderTradeAgreement, EInvoiceNS.RAM,
"BuyerReference");
if (buyerReferenceElement != null) {
buyerReference = buyerReferenceElement.getTextContent();
}
Element tradePartyElement = findChildNode(applicableHeaderTradeAgreement, EInvoiceNS.RAM,
"SellerTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "seller"));
}
tradePartyElement = findChildNode(applicableHeaderTradeAgreement, EInvoiceNS.RAM,
"BuyerTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "buyer"));
}

// read ShipToTradeParty from ApplicableHeaderTradeDelivery
element = findChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM, "ApplicableHeaderTradeDelivery");
if (element != null) {
Element tradePartyElement = findChildNode(element, EInvoiceNS.RAM, "ShipToTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "ship_to"));
}
tradePartyElement = findChildNode(applicableHeaderTradeDelivery, EInvoiceNS.RAM, "ShipToTradeParty");
if (tradePartyElement != null) {
tradeParties.add(parseTradeParty(tradePartyElement, "ship_to"));
}

// read line items...
Expand Down Expand Up @@ -473,12 +474,10 @@ public void setTradeParty(TradeParty newParty) {

// Determine parent element and party element name based on type
if ("ship_to".equals(newParty.getType())) {
parentElement = findChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeDelivery");
parentElement = applicableHeaderTradeDelivery;
elementName = "ShipToTradeParty";
} else {
parentElement = findChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeAgreement");
parentElement = applicableHeaderTradeAgreement;
elementName = newParty.getType().equals("seller") ? "SellerTradeParty" : "BuyerTradeParty";
}

Expand Down Expand Up @@ -541,8 +540,9 @@ public void setTradeLineItem(TradeLineItem item) {
super.setTradeLineItem(item);

// create main tags...
// Insert before ApplicableHeaderTradeAgreement !!
Element lineItem = createChildNode(supplyChainTradeTransaction, EInvoiceNS.RAM,
"IncludedSupplyChainTradeLineItem");
"IncludedSupplyChainTradeLineItem", applicableHeaderTradeAgreement);
Element associatedDocumentLineDocument = createChildNode(lineItem, EInvoiceNS.RAM,
"AssociatedDocumentLineDocument");
Element product = createChildNode(lineItem, EInvoiceNS.RAM, "SpecifiedTradeProduct");
Expand Down

0 comments on commit 5ba6662

Please sign in to comment.