org.mustangproject.ZUGFeRD.OXPullProvider Maven / Gradle / Ivy
Show all versions of library Show documentation
/**
* *********************************************************************
*
* Copyright 2018 Jochen Staerk
*
* Use is subject to license terms.
*
* 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 org.mustangproject.ZUGFeRD;
import static org.mustangproject.ZUGFeRD.ZUGFeRDDateFormat.DATE;
import static org.mustangproject.ZUGFeRD.model.DocumentCodeTypeConstants.CORRECTEDINVOICE;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import org.mustangproject.EStandard;
import org.mustangproject.FileAttachment;
import org.mustangproject.XMLTools;
public class OXPullProvider extends ZUGFeRD2PullProvider {
protected IExportableTransaction trans;
protected TransactionCalculator calc;
private String paymentTermsDescription;
protected Profile profile = Profiles.getByName(EStandard.orderx,"basic", 1);
@Override
public void generateXML(IExportableTransaction trans) {
this.trans = trans;
this.calc = new TransactionCalculator(trans);
boolean hasDueDate = false;
final SimpleDateFormat germanDateFormat = new SimpleDateFormat("dd.MM.yyyy");
final String exemptionReason = "";
if (trans.getPaymentTermDescription() != null) {
paymentTermsDescription = XMLTools.encodeXML(trans.getPaymentTermDescription());
}
if ((paymentTermsDescription == null) && (trans.getDocumentCode() != CORRECTEDINVOICE)/* && (trans.getDocumentCode() != DocumentCodeTypeConstants.CREDITNOTE)*/) {
paymentTermsDescription = "Zahlbar ohne Abzug bis " + germanDateFormat.format(trans.getDueDate());
}
final String typecode = "220";
/*if (trans.getDocumentCode() != null) {
typecode = trans.getDocumentCode();
}*/
String xml = ""
+ ""
// + "
// xsi:schemaLocation=\"urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100
// ../Schema/ZUGFeRD1p0.xsd\""
+ ""
// + "
// "+testBooleanStr+" \n"
//
+ ""
+ "A1 "
+ " "
+ ""
+ "" + getProfile().getID() + " "
+ " "
+ " "
+ ""
+ "" + XMLTools.encodeXML(trans.getNumber()) + " "
// + " RECHNUNG "
// + "380 "
+ "" + typecode + " "
+ ""
+ DATE.udtFormat(trans.getIssueDate()) + " " // date
+ buildNotes(trans)
+ " "
+ "";
int lineID = 0;
for (final IZUGFeRDExportableItem currentItem : trans.getZFItems()) {
lineID++;
if (currentItem.getProduct().getTaxExemptionReason() != null) {
// exemptionReason = "" + XMLTools.encodeXML(currentItem.getProduct().getTaxExemptionReason()) + " ";
}
final LineCalculator lc = new LineCalculator(currentItem);
xml += "" +
""
+ "" + lineID + " "
+ buildItemNotes(currentItem)
+ " "
+ "";
// + " 4012345001235 "
if (currentItem.getProduct().getSellerAssignedID() != null) {
xml += ""
+ XMLTools.encodeXML(currentItem.getProduct().getSellerAssignedID()) + " ";
}
if (currentItem.getProduct().getBuyerAssignedID() != null) {
xml += ""
+ XMLTools.encodeXML(currentItem.getProduct().getBuyerAssignedID()) + " ";
}
String allowanceChargeStr = "";
if (currentItem.getItemAllowances() != null && currentItem.getItemAllowances().length > 0) {
for (final IZUGFeRDAllowanceCharge allowance : currentItem.getItemAllowances()) {
allowanceChargeStr += getAllowanceChargeStr(allowance, currentItem);
}
}
if (currentItem.getItemCharges() != null && currentItem.getItemCharges().length > 0) {
for (final IZUGFeRDAllowanceCharge charge : currentItem.getItemCharges()) {
allowanceChargeStr += getAllowanceChargeStr(charge, currentItem);
}
}
xml += "" + XMLTools.encodeXML(currentItem.getProduct().getName()) + " "
+ "" + XMLTools.encodeXML(currentItem.getProduct().getDescription())
+ " "
+ " "
+ "";
/* if (currentItem.getReferencedDocuments() != null) {
for (IReferencedDocument currentReferencedDocument : currentItem.getReferencedDocuments()) {
xml += "\n" +
"" + XMLTools.encodeXML(currentReferencedDocument.getIssuerAssignedID()) + " \n" +
"" + XMLTools.encodeXML(currentReferencedDocument.getTypeCode()) + " \n" +
"" + XMLTools.encodeXML(currentReferencedDocument.getReferenceTypeCode()) + " \n" +
" \n";
}
}*/
if (currentItem.getBuyerOrderReferencedDocumentLineID() != null) {
xml += " \n"
+ "" + XMLTools.encodeXML(currentItem.getBuyerOrderReferencedDocumentLineID()) + " "
+ " ";
}
xml += ""
+ "" + priceFormat(lc.getPriceGross())
+ " " //currencyID=\"EUR\"
+ "" + quantityFormat(currentItem.getBasisQuantity()) + " "
+ allowanceChargeStr
// + " \n"
// + " false \n"
// + " 0.6667 \n"
// + " Rabatt \n"
// + " \n"
+ " "
+ ""
+ "" + priceFormat(lc.getPrice())
+ " " // currencyID=\"EUR\"
+ "" + quantityFormat(currentItem.getBasisQuantity()) + " "
+ " "
+ " "
+ ""
+ ""
+ quantityFormat(currentItem.getQuantity()) + " "
+ " "
+ ""
+ ""
+ "VAT "
+ exemptionReason
+ "" + currentItem.getProduct().getTaxCategoryCode() + " "
+ ""
+ vatFormat(currentItem.getProduct().getVATPercent()) + " "
+ " ";
if ((currentItem.getDetailedDeliveryPeriodFrom() != null) || (currentItem.getDetailedDeliveryPeriodTo() != null)) {
xml += "";
if (currentItem.getDetailedDeliveryPeriodFrom() != null) {
xml += "" + DATE.udtFormat(currentItem.getDetailedDeliveryPeriodFrom()) + " ";
}
if (currentItem.getDetailedDeliveryPeriodTo() != null) {
xml += "" + DATE.udtFormat(currentItem.getDetailedDeliveryPeriodTo()) + " ";
}
xml += " ";
}
xml += ""
+ "" + currencyFormat(lc.getItemTotalNetAmount())
+ " " // currencyID=\"EUR\"
+ " ";
/* if (currentItem.getAdditionalReferencedDocumentID() != null) {
xml += "" + currentItem.getAdditionalReferencedDocumentID() + " 130 ";
}*/
xml += " "
+ " ";
}
xml += "";
if (trans.getReferenceNumber() != null) {
xml += "" + XMLTools.encodeXML(trans.getReferenceNumber()) + " ";
}
xml += ""
+ getTradePartyAsXML(trans.getSender(), true, false)
+ " "
+ "";
// + " GE2020211 "
// + " 4000001987658 "
xml += getTradePartyAsXML(trans.getRecipient(), false, false);
xml += " ";
if (trans.getSellerOrderReferencedDocumentID() != null) {
xml += ""
+ ""
+ XMLTools.encodeXML(trans.getSellerOrderReferencedDocumentID()) + " "
+ " ";
}
if (trans.getBuyerOrderReferencedDocumentID() != null) {
xml += ""
+ ""
+ XMLTools.encodeXML(trans.getBuyerOrderReferencedDocumentID()) + " "
+ " ";
}
if (trans.getContractReferencedDocument() != null) {
xml += ""
+ ""
+ XMLTools.encodeXML(trans.getContractReferencedDocument()) + " "
+ " ";
}
// Additional Documents of XRechnung (Rechnungsbegruendende Unterlagen - BG-24 XRechnung)
if (trans.getAdditionalReferencedDocuments() != null) {
for (final FileAttachment f : trans.getAdditionalReferencedDocuments()) {
final String documentContent = new String(Base64.getEncoder().encodeToString(f.getData()));
xml += ""
+ "" + f.getFilename() + " "
+ "916 "
+ "" + f.getDescription() + " "
+ "" + documentContent + " "
+ " ";
}
}
if (trans.getSpecifiedProcuringProjectID() != null) {
xml += ""
+ ""
+ XMLTools.encodeXML(trans.getSpecifiedProcuringProjectID()) + " ";
if (trans.getSpecifiedProcuringProjectName() != null) {
xml += "" + XMLTools.encodeXML(trans.getSpecifiedProcuringProjectName()) + " ";
}
xml += " ";
}
xml += " "
+ "";
IZUGFeRDExportableTradeParty deliveryAddress=this.trans.getDeliveryAddress();
if (deliveryAddress == null) {
deliveryAddress = this.trans.getRecipient();
}
xml += "" +
getTradePartyAsXML(deliveryAddress, false, true) +
" ";
/*
xml += ""
+ "";
if (trans.getDeliveryDate() != null) {
xml += DATE.udtFormat(trans.getDeliveryDate());
} else {
throw new IllegalStateException("No delivery date provided");
}
xml += " \n";
xml += " \n"
*/
/*
* + "\n" +
* "20130603 \n" +
* "2013-51112 \n" +
* " \n"
*/
xml+= " \n"
// + "" + XMLTools.encodeXML(trans.getNumber()) + " "
+ "" + trans.getCurrency() + " ";
if (trans.getTradeSettlementPayment() != null) {
for (final IZUGFeRDTradeSettlementPayment payment : trans.getTradeSettlementPayment()) {
if (payment != null) {
hasDueDate = true;
// xml += payment.getSettlementXML();
}
}
}
if (trans.getTradeSettlement() != null) {
for (final IZUGFeRDTradeSettlement payment : trans.getTradeSettlement()) {
if (payment != null) {
if (payment instanceof IZUGFeRDTradeSettlementPayment) {
hasDueDate = true;
}
// xml += payment.getSettlementXML();
}
}
}
if (trans.getDocumentCode() != null) {
if ((trans.getDocumentCode().equals(CORRECTEDINVOICE))/*||(trans.getDocumentCode().equals (DocumentCodeTypeConstants.CREDITNOTE))*/) {
hasDueDate = false;
}
}
final Map VATPercentAmountMap = calc.getVATPercentAmountMap();
for (final BigDecimal currentTaxPercent : VATPercentAmountMap.keySet()) {
final VATAmount amount = VATPercentAmountMap.get(currentTaxPercent);
if (amount != null) {
/*final String amountCategoryCode = amount.getCategoryCode();
final boolean displayExemptionReason = CATEGORY_CODES_WITH_EXEMPTION_REASON.contains(amountCategoryCode);
xml += "\n"
+ "" + currencyFormat(amount.getCalculated())
+ " \n" //currencyID=\"EUR\"
+ "VAT \n"
+ (displayExemptionReason ? exemptionReason : "")
+ "" + currencyFormat(amount.getBasis()) + " \n" // currencyID=\"EUR\"
+ "" + amountCategoryCode + " \n"
+ ""
+ vatFormat(currentTaxPercent) + " \n" + " \n";
*/
}
}
if ((trans.getDetailedDeliveryPeriodFrom() != null) || (trans.getDetailedDeliveryPeriodTo() != null)) {
xml += "";
if (trans.getDetailedDeliveryPeriodFrom() != null) {
xml += "" + DATE.udtFormat(trans.getDetailedDeliveryPeriodFrom()) + " ";
}
if (trans.getDetailedDeliveryPeriodTo() != null) {
xml += "" + DATE.udtFormat(trans.getDetailedDeliveryPeriodTo()) + " ";
}
xml += " ";
}
if ((trans.getZFCharges() != null) && (trans.getZFCharges().length > 0)) {
for (final BigDecimal currentTaxPercent : VATPercentAmountMap.keySet()) {
if (calc.getChargesForPercent(currentTaxPercent).compareTo(BigDecimal.ZERO) != 0) {
xml += " " +
"" +
"true " +
" " +
"" + currencyFormat(calc.getChargesForPercent(currentTaxPercent)) + " " +
"" + XMLTools.encodeXML(calc.getChargeReasonForPercent(currentTaxPercent)) + " " +
"" +
"VAT " +
"" + VATPercentAmountMap.get(currentTaxPercent).getCategoryCode() + " " +
"" + vatFormat(currentTaxPercent) + " " +
" " +
" ";
}
}
}
if ((trans.getZFAllowances() != null) && (trans.getZFAllowances().length > 0)) {
for (final BigDecimal currentTaxPercent : VATPercentAmountMap.keySet()) {
if (calc.getAllowancesForPercent(currentTaxPercent).compareTo(BigDecimal.ZERO) != 0) {
xml += "" +
"" +
"false " +
" " +
"" + currencyFormat(calc.getAllowancesForPercent(currentTaxPercent)) + " " +
"" + XMLTools.encodeXML(calc.getAllowanceReasonForPercent(currentTaxPercent)) + " " +
"" +
"VAT " +
"" + VATPercentAmountMap.get(currentTaxPercent).getCategoryCode() + " " +
"" + vatFormat(currentTaxPercent) + " " +
" " +
" \n";
}
}
}
if ((trans.getPaymentTerms() == null) && ((paymentTermsDescription != null) || (trans.getTradeSettlement() != null) || (hasDueDate))) {
xml += "";
if (paymentTermsDescription != null) {
xml += "" + paymentTermsDescription + " ";
}
if (trans.getTradeSettlement() != null) {
for (final IZUGFeRDTradeSettlement payment : trans.getTradeSettlement()) {
if ((payment != null) && (payment instanceof IZUGFeRDTradeSettlementDebit)) {
//not in order-x xml += payment.getPaymentXML();
}
}
}
xml += " ";
} else {
xml += buildPaymentTermsXml();
}
final String allowanceTotalLine = "" + currencyFormat(calc.getAllowancesForPercent(null)) + " ";
final String chargesTotalLine = "" + currencyFormat(calc.getChargesForPercent(null)) + " ";
xml += ""
+ "" + currencyFormat(calc.getTotal()) + " "
+ chargesTotalLine
+ allowanceTotalLine
+ "" + currencyFormat(calc.getTaxBasis()) + " "
// //
// currencyID=\"EUR\"
+ ""
+ currencyFormat(calc.getGrandTotal().subtract(calc.getTaxBasis())) + " "
+ "" + currencyFormat(calc.getGrandTotal()) + " "
// //
// currencyID=\"EUR\"
//+ "" + currencyFormat(calc.getTotalPrepaid()) + " "
//+ "" + currencyFormat(calc.getGrandTotal().subtract(calc.getTotalPrepaid())) + " "
+ " ";
if (trans.getInvoiceReferencedDocumentID() != null) {
xml += ""
+ ""
+ XMLTools.encodeXML(trans.getInvoiceReferencedDocumentID()) + " ";
if (trans.getInvoiceReferencedIssueDate() != null) {
xml += ""
+ DATE.qdtFormat(trans.getInvoiceReferencedIssueDate())
+ " ";
}
xml += " ";
}
xml += " ";
// + " "
// + " "
// + " "
// + " Wir erlauben uns Ihnen folgende Positionen aus der Lieferung Nr.
// 2013-51112 in Rechnung zu stellen: \n"
// + " \n"
// + " \n"
// + " \n";
xml += " "
+ " ";
final byte[] zugferdRaw;
zugferdRaw = xml.getBytes(StandardCharsets.UTF_8);
zugferdData = XMLTools.removeBOM(zugferdRaw);
}
@Override
public void setProfile(Profile p) {
profile = p;
}
@Override
public Profile getProfile() {
return profile;
}
private String buildPaymentTermsXml() {
final IZUGFeRDPaymentTerms paymentTerms = trans.getPaymentTerms();
if (paymentTerms == null) {
return "";
}
String paymentTermsXml = "";
final IZUGFeRDPaymentDiscountTerms discountTerms = paymentTerms.getDiscountTerms();
paymentTermsXml += "" + paymentTerms.getDescription() + " ";
if (discountTerms != null) {
paymentTermsXml += "";
final String currency = trans.getCurrency();
final String basisAmount = currencyFormat(calc.getGrandTotal());
paymentTermsXml += "" + basisAmount + " ";
paymentTermsXml += "" + discountTerms.getCalculationPercentage().toString()
+ " ";
if (discountTerms.getBaseDate() != null) {
final Date baseDate = discountTerms.getBaseDate();
paymentTermsXml += "";
paymentTermsXml += DATE.udtFormat(baseDate);
paymentTermsXml += " ";
paymentTermsXml += ""
+ discountTerms.getBasePeriodMeasure() + " ";
}
paymentTermsXml += " ";
}
paymentTermsXml += " ";
return paymentTermsXml;
}
}