Skip to content

Commit

Permalink
added methods to set the tradeParties (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Dec 26, 2024
1 parent 8492380 commit 01985de
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ public Set<TradeParty> getTradeParties() {
return tradeParties;
}

/**
* Adds a new Trade party. If a party with this type already exists, the method
* removes first the existing party.
*
* @param party
*/
public void setTradeParty(TradeParty party) {

if (party == null) {
return;
}

// Remove existing party of same type (if exists)
TradeParty existingParty = findTradeParty(party.getType());
if (existingParty != null) {
tradeParties.remove(existingParty);
}

// Add new party
tradeParties.add(party);
}

/**
* Finds a Trade Party by its type. Method can return null if not trade party of
* the type is defined in the invoice
Expand All @@ -151,7 +173,7 @@ public TradeParty findTradeParty(String type) {
if (type == null || type.isEmpty()) {
return null;
}
Iterator<TradeParty> iterParties = tradeParties.iterator();
Iterator<TradeParty> iterParties = getTradeParties().iterator();
while (iterParties.hasNext()) {
TradeParty party = iterParties.next();
if (type.equals(party.getType())) {
Expand Down Expand Up @@ -216,6 +238,21 @@ public Element findChildNodeByName(Element parent, EInvoiceNS ns, String nodeNam
}
}

/**
* Helper method to update or create an element with a given value
*/
public void updateElementValue(Element parent, String elementName, String value) {
if (value == null) {
return;
}
Element element = findChildNodeByName(parent, EInvoiceNS.RAM, elementName);
if (element == null) {
element = getDoc().createElement(getPrefix(EInvoiceNS.RAM) + elementName);
parent.appendChild(element);
}
element.setTextContent(value);
}

/**
* Returns the central logger instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ private void loadDocumentCoreData() {
}
}

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

}

public TradeParty parseTradeParty(Element tradePartyElement, String type) {
Expand Down Expand Up @@ -274,4 +283,74 @@ public void setGrandTotalAmount(BigDecimal value) {
}
}
}

/**
* Updates or creates a trade party in the model and XML structure
*
* @param newParty the trade party to be set
*/
/**
* Updates or creates a trade party in the model and XML structure
*
* @param newParty the trade party to be set
*/
public void setTradeParty(TradeParty newParty) {
if (newParty == null) {
return;
}

// First update the model
super.setTradeParty(newParty);

Element parentElement;
String elementName;

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

if (parentElement != null) {
Element tradePartyElement = findChildNodeByName(parentElement, EInvoiceNS.RAM, elementName);

// Create element if it doesn't exist
if (tradePartyElement == null) {
tradePartyElement = getDoc().createElement(getPrefix(EInvoiceNS.RAM) + elementName);
parentElement.appendChild(tradePartyElement);
}

// Update Name
updateElementValue(tradePartyElement, "Name", newParty.getName());

// Update PostalTradeAddress
Element postalAddress = findChildNodeByName(tradePartyElement, EInvoiceNS.RAM, "PostalTradeAddress");
if (postalAddress == null) {
postalAddress = getDoc().createElement(getPrefix(EInvoiceNS.RAM) + "PostalTradeAddress");
tradePartyElement.appendChild(postalAddress);
}

// Update address details
updateElementValue(postalAddress, "PostcodeCode", newParty.getPostcodeCode());
updateElementValue(postalAddress, "CityName", newParty.getCityName());
updateElementValue(postalAddress, "CountryID", newParty.getCountryId());
updateElementValue(postalAddress, "LineOne", newParty.getStreetAddress());

// Update VAT registration if available
if (newParty.getVatNumber() != null && !newParty.getVatNumber().isEmpty()) {
Element taxRegistration = findChildNodeByName(tradePartyElement, EInvoiceNS.RAM,
"SpecifiedTaxRegistration");
if (taxRegistration == null) {
taxRegistration = getDoc().createElement(getPrefix(EInvoiceNS.RAM) + "SpecifiedTaxRegistration");
tradePartyElement.appendChild(taxRegistration);
}
updateElementValue(taxRegistration, "ID", newParty.getVatNumber());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,6 @@ public class TradeParty {
public TradeParty(String type) {
this.type = type;
}
// public TradeParty(Element sellerElement, String type) {
// this.type = type;
// // Parse name
// Element element=sellerElement.getElementsByTagNameNS( EInvoiceNS.RAM.name(),
// "name");
// this.name =
// .getElementsByTagName("ram:Name")
// .item(0)
// .getTextContent();

// // Get PostalTradeAddress element
// Element postalAddress = (Element) sellerElement
// .getElementsByTagName("ram:PostalTradeAddress")
// .item(0);

// // Parse address details
// this.postcodeCode = postalAddress
// .getElementsByTagName("ram:PostcodeCode")
// .item(0)
// .getTextContent();

// this.streetAddress = postalAddress
// .getElementsByTagName("ram:LineOne")
// .item(0)
// .getTextContent();

// this.cityName = postalAddress
// .getElementsByTagName("ram:CityName")
// .item(0)
// .getTextContent();

// this.countryId = postalAddress
// .getElementsByTagName("ram:CountryID")
// .item(0)
// .getTextContent();

// // // Parse VAT number
// // Element taxRegistration = (Element) sellerElement
// // .getElementsByTagName("ram:SpecifiedTaxRegistration")
// // .item(0);

// // this.vatNumber = taxRegistration
// // .getElementsByTagName("ram:ID")
// // .item(0)
// // .getTextContent();
// }

// Getters
public String getType() {
Expand Down

0 comments on commit 01985de

Please sign in to comment.