Skip to content

Commit

Permalink
Merge pull request #8 from digipost/reporting-information
Browse files Browse the repository at this point in the history
Methods for PeppolBillingApi to find country identifier of supplier and customer party
  • Loading branch information
hermanwh authored Jan 18, 2024
2 parents 0b9b98d + 0d03b22 commit bad3057
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api/src/main/java/peppol/bis/invoice3/api/PeppolBillingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package peppol.bis.invoice3.api;

import org.eaxy.Document;
import org.eaxy.Element;
import peppol.bis.invoice3.domain.BillingCommon;
import peppol.bis.invoice3.domain.CreditNote;
import peppol.bis.invoice3.domain.Invoice;
Expand Down Expand Up @@ -71,6 +72,28 @@ public boolean isInvoice() {
return false;
}

public String getSupplierCountryIdentifier() {
if (this.object instanceof Document) {
return ((Document) this.object).find("AccountingSupplierParty", "Party", "PostalAddress", "Country", "IdentificationCode").single().text().trim();
}
throw new RuntimeException("Mandatory property missing in document: AccountingSupplierParty -> Party -> PostalAddress -> Country -> IdentificationCode");
}

public String getCustomerCountryIdentifier() {
if (this.object instanceof Document) {
return ((Document) this.object).find("AccountingCustomerParty", "Party", "PostalAddress", "Country", "IdentificationCode").single().text().trim();
}
throw new RuntimeException("Mandatory property missing in document: AccountingCustomerParty -> Party -> PostalAddress -> Country -> IdentificationCode");
}

public String getSupplierEndpointID() {
if (this.object instanceof Document) {
Element element = ((Document) this.object).find("AccountingSupplierParty", "Party", "EndpointID").single();
return element.attr("schemeID").trim() + ":" + element.text().trim();
}
throw new RuntimeException("Mandatory property missing in document: AccountingSupplierParty -> Party -> EndpointID");
}

public String prettyPrint() {
return XML_FIRST_LINE + (this.object instanceof BillingCommon ? ((BillingCommon) this.object).xmlRoot() : ((Document) this.object).getRootElement()).toIndentedXML();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) Posten Norge AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package peppol.bis.invoice3.api;

import org.eaxy.Document;
import org.eaxy.Xml;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class PeppolBillingApiTest {

@Test
public void test() throws IOException {
try (InputStream inputStream = PeppolBillingApiTest.class.getResourceAsStream("/norwegian-example.xml")) {
assertNotNull(inputStream);
Document document = Xml.xml(new String(inputStream.readAllBytes()));
PeppolBillingApi<Document> peppolBillingApi = new PeppolBillingApi<>(document);
assertEquals("NO", peppolBillingApi.getCustomerCountryIdentifier());
assertEquals("NO", peppolBillingApi.getSupplierCountryIdentifier());
assertEquals("0192:123456785", peppolBillingApi.getSupplierEndpointID());
}
}

}

0 comments on commit bad3057

Please sign in to comment.