new functions may now return null for default values (empty strings or EUR in case of currency), renamed getInvoiceCurrency() to getCurrency()

This commit is contained in:
Jochen Staerk
2015-12-28 17:54:46 +01:00
parent d274c5b098
commit af90f09255
2 changed files with 59 additions and 27 deletions

View File

@@ -127,7 +127,7 @@ public interface IZUGFeRDExportableTransaction {
* get main invoice currency used on the invoice * get main invoice currency used on the invoice
* @return * @return
*/ */
String getInvoiceCurrency(); String getCurrency();
/** /**
* get payment information text. e.g. Bank transfer * get payment information text. e.g. Bank transfer

View File

@@ -40,6 +40,7 @@ import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.apache.pdfbox.pdmodel.PDDocumentInformation;
@@ -51,7 +52,6 @@ import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecifica
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.preflight.PreflightDocument; import org.apache.pdfbox.preflight.PreflightDocument;
import org.apache.pdfbox.preflight.ValidationResult; import org.apache.pdfbox.preflight.ValidationResult;
import org.apache.pdfbox.preflight.exception.SyntaxValidationException;
import org.apache.pdfbox.preflight.exception.ValidationException; import org.apache.pdfbox.preflight.exception.ValidationException;
import org.apache.pdfbox.preflight.parser.PreflightParser; import org.apache.pdfbox.preflight.parser.PreflightParser;
import org.mustangproject.ZUGFeRD.model.*; import org.mustangproject.ZUGFeRD.model.*;
@@ -213,6 +213,7 @@ public class ZUGFeRDExporter {
IZUGFeRDExportableTransaction trans = null; IZUGFeRDExportableTransaction trans = null;
private boolean ignoreA1Errors; private boolean ignoreA1Errors;
private PDDocument doc; private PDDocument doc;
private String currency="EUR";
private BigDecimal nDigitFormat(BigDecimal value, int scale) { private BigDecimal nDigitFormat(BigDecimal value, int scale) {
/* /*
@@ -340,6 +341,18 @@ public class ZUGFeRDExporter {
return result.isValid(); return result.isValid();
} }
public void loadPDFA3(String filename) {
try {
doc = PDDocument
.load(filename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** /**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on
* the metadata level, this will not e.g. convert graphics to JPG-2000) * the metadata level, this will not e.g. convert graphics to JPG-2000)
@@ -352,9 +365,8 @@ public class ZUGFeRDExporter {
if (!ignoreA1Errors&&!isValidA1(filename)) { if (!ignoreA1Errors&&!isValidA1(filename)) {
throw new IOException("File is not a valid PDF/A-1 input file"); throw new IOException("File is not a valid PDF/A-1 input file");
} }
doc = PDDocument loadPDFA3(filename);
.load(filename);
String fullProducer = producer + " (via mustangproject.org " + versionStr + ")"; String fullProducer = producer + " (via mustangproject.org " + versionStr + ")";
PDDocumentCatalog cat = doc.getDocumentCatalog(); PDDocumentCatalog cat = doc.getDocumentCatalog();
@@ -428,8 +440,8 @@ public class ZUGFeRDExporter {
private String getZugferdXMLForTransaction(IZUGFeRDExportableTransaction trans) throws JAXBException { private String getZugferdXMLForTransaction(IZUGFeRDExportableTransaction trans) throws JAXBException {
this.trans = trans; this.trans = trans;
this.totals = new Totals(); this.totals = new Totals();
currency=trans.getCurrency();
CrossIndustryDocumentType invoice = xmlFactory.createCrossIndustryDocumentType(); CrossIndustryDocumentType invoice = xmlFactory.createCrossIndustryDocumentType();
@@ -624,7 +636,7 @@ public class ZUGFeRDExporter {
tradeSettlement.getPaymentReference().add(paymentReference); tradeSettlement.getPaymentReference().add(paymentReference);
CodeType currencyCode = xmlFactory.createCodeType(); CodeType currencyCode = xmlFactory.createCodeType();
currencyCode.setValue(trans.getInvoiceCurrency()); currencyCode.setValue(currency);
tradeSettlement.setInvoiceCurrencyCode(currencyCode); tradeSettlement.setInvoiceCurrencyCode(currencyCode);
tradeSettlement.getSpecifiedTradeSettlementPaymentMeans().add(this.getPaymentData()); tradeSettlement.getSpecifiedTradeSettlementPaymentMeans().add(this.getPaymentData());
@@ -652,7 +664,11 @@ public class ZUGFeRDExporter {
paymentData.setTypeCode(paymentDataType); paymentData.setTypeCode(paymentDataType);
TextType paymentInfo = xmlFactory.createTextType(); TextType paymentInfo = xmlFactory.createTextType();
paymentInfo.setValue(trans.getOwnPaymentInfoText()); String paymentInfoText=trans.getOwnPaymentInfoText();
if (paymentInfoText==null) {
paymentInfoText="";
}
paymentInfo.setValue(paymentInfoText);
paymentData.getInformation().add(paymentInfo); paymentData.getInformation().add(paymentInfo);
CreditorFinancialAccountType bankAccount = xmlFactory.createCreditorFinancialAccountType(); CreditorFinancialAccountType bankAccount = xmlFactory.createCreditorFinancialAccountType();
@@ -694,12 +710,12 @@ public class ZUGFeRDExporter {
tradeTax.setApplicablePercent(taxPercent); tradeTax.setApplicablePercent(taxPercent);
AmountType calculatedTaxAmount = xmlFactory.createAmountType(); AmountType calculatedTaxAmount = xmlFactory.createAmountType();
calculatedTaxAmount.setCurrencyID(trans.getInvoiceCurrency()); calculatedTaxAmount.setCurrencyID(currency);
calculatedTaxAmount.setValue(currencyFormat(amount.getCalculated())); calculatedTaxAmount.setValue(currencyFormat(amount.getCalculated()));
tradeTax.getCalculatedAmount().add(calculatedTaxAmount); tradeTax.getCalculatedAmount().add(calculatedTaxAmount);
AmountType basisTaxAmount = xmlFactory.createAmountType(); AmountType basisTaxAmount = xmlFactory.createAmountType();
basisTaxAmount.setCurrencyID(trans.getInvoiceCurrency()); basisTaxAmount.setCurrencyID(currency);
basisTaxAmount.setValue(currencyFormat(amount.getBasis())); basisTaxAmount.setValue(currencyFormat(amount.getBasis()));
tradeTax.getBasisAmount().add(basisTaxAmount); tradeTax.getBasisAmount().add(basisTaxAmount);
@@ -721,7 +737,7 @@ public class ZUGFeRDExporter {
allowance.setChargeIndicator(chargeIndicator); allowance.setChargeIndicator(chargeIndicator);
AmountType actualAmount = xmlFactory.createAmountType(); AmountType actualAmount = xmlFactory.createAmountType();
actualAmount.setCurrencyID(trans.getInvoiceCurrency()); actualAmount.setCurrencyID(currency);
actualAmount.setValue(currencyFormat(iAllowance.getTotalAmount())); actualAmount.setValue(currencyFormat(iAllowance.getTotalAmount()));
allowance.getActualAmount().add(actualAmount); allowance.getActualAmount().add(actualAmount);
@@ -769,7 +785,7 @@ public class ZUGFeRDExporter {
charge.setChargeIndicator(chargeIndicator); charge.setChargeIndicator(chargeIndicator);
AmountType actualAmount = xmlFactory.createAmountType(); AmountType actualAmount = xmlFactory.createAmountType();
actualAmount.setCurrencyID(trans.getInvoiceCurrency()); actualAmount.setCurrencyID(currency);
actualAmount.setValue(currencyFormat(iCharge.getTotalAmount())); actualAmount.setValue(currencyFormat(iCharge.getTotalAmount()));
charge.getActualAmount().add(actualAmount); charge.getActualAmount().add(actualAmount);
@@ -813,7 +829,7 @@ public class ZUGFeRDExporter {
LogisticsServiceChargeType serviceCharge = xmlFactory.createLogisticsServiceChargeType(); LogisticsServiceChargeType serviceCharge = xmlFactory.createLogisticsServiceChargeType();
AmountType actualAmount = xmlFactory.createAmountType(); AmountType actualAmount = xmlFactory.createAmountType();
actualAmount.setCurrencyID(trans.getInvoiceCurrency()); actualAmount.setCurrencyID(currency);
actualAmount.setValue(currencyFormat(iServiceCharge.getTotalAmount())); actualAmount.setValue(currencyFormat(iServiceCharge.getTotalAmount()));
serviceCharge.getAppliedAmount().add(actualAmount); serviceCharge.getAppliedAmount().add(actualAmount);
@@ -864,7 +880,11 @@ public class ZUGFeRDExporter {
TextType paymentTermDescr = xmlFactory.createTextType(); TextType paymentTermDescr = xmlFactory.createTextType();
paymentTermDescr.setValue(trans.getPaymentTermDescription()); String paymentTermDescription=trans.getPaymentTermDescription();
if (paymentTermDescription==null) {
paymentTermDescription="";
}
paymentTermDescr.setValue(paymentTermDescription);
paymentTerm.getDescription().add(paymentTermDescr); paymentTerm.getDescription().add(paymentTermDescr);
paymentTerms.add(paymentTerm); paymentTerms.add(paymentTerm);
@@ -877,7 +897,7 @@ public class ZUGFeRDExporter {
// AllowanceTotalAmount = sum of all allowances // AllowanceTotalAmount = sum of all allowances
AmountType allowanceTotalAmount = xmlFactory.createAmountType(); AmountType allowanceTotalAmount = xmlFactory.createAmountType();
allowanceTotalAmount.setCurrencyID(trans.getInvoiceCurrency()); allowanceTotalAmount.setCurrencyID(currency);
if(trans.getZFAllowances() != null){ if(trans.getZFAllowances() != null){
BigDecimal totalHeaderAllowance = BigDecimal.ZERO; BigDecimal totalHeaderAllowance = BigDecimal.ZERO;
for(IZUGFeRDAllowanceCharge headerAllowance : trans.getZFAllowances()){ for(IZUGFeRDAllowanceCharge headerAllowance : trans.getZFAllowances()){
@@ -892,7 +912,7 @@ public class ZUGFeRDExporter {
// ChargeTotalAmount = sum of all Logistic service charges + normal charges // ChargeTotalAmount = sum of all Logistic service charges + normal charges
BigDecimal totalCharge = BigDecimal.ZERO; BigDecimal totalCharge = BigDecimal.ZERO;
AmountType totalChargeAmount = xmlFactory.createAmountType(); AmountType totalChargeAmount = xmlFactory.createAmountType();
totalChargeAmount.setCurrencyID(trans.getInvoiceCurrency()); totalChargeAmount.setCurrencyID(currency);
if(trans.getZFLogisticsServiceCharges()!= null){ if(trans.getZFLogisticsServiceCharges()!= null){
for(IZUGFeRDAllowanceCharge logisticsServiceCharge : trans.getZFLogisticsServiceCharges()){ for(IZUGFeRDAllowanceCharge logisticsServiceCharge : trans.getZFLogisticsServiceCharges()){
totalCharge = logisticsServiceCharge.getTotalAmount().add(totalCharge); totalCharge = logisticsServiceCharge.getTotalAmount().add(totalCharge);
@@ -914,27 +934,27 @@ public class ZUGFeRDExporter {
monetarySummation.getChargeTotalAmount().add(chargeTotalAmount);*/ monetarySummation.getChargeTotalAmount().add(chargeTotalAmount);*/
AmountType lineTotalAmount = xmlFactory.createAmountType(); AmountType lineTotalAmount = xmlFactory.createAmountType();
lineTotalAmount.setCurrencyID(trans.getInvoiceCurrency()); lineTotalAmount.setCurrencyID(currency);
lineTotalAmount.setValue(currencyFormat(totals.getLineTotal())); lineTotalAmount.setValue(currencyFormat(totals.getLineTotal()));
monetarySummation.getLineTotalAmount().add(lineTotalAmount); monetarySummation.getLineTotalAmount().add(lineTotalAmount);
AmountType taxBasisTotalAmount = xmlFactory.createAmountType(); AmountType taxBasisTotalAmount = xmlFactory.createAmountType();
taxBasisTotalAmount.setCurrencyID(trans.getInvoiceCurrency()); taxBasisTotalAmount.setCurrencyID(currency);
taxBasisTotalAmount.setValue(currencyFormat(totals.getTotalNet())); taxBasisTotalAmount.setValue(currencyFormat(totals.getTotalNet()));
monetarySummation.getTaxBasisTotalAmount().add(taxBasisTotalAmount); monetarySummation.getTaxBasisTotalAmount().add(taxBasisTotalAmount);
AmountType taxTotalAmount = xmlFactory.createAmountType(); AmountType taxTotalAmount = xmlFactory.createAmountType();
taxTotalAmount.setCurrencyID(trans.getInvoiceCurrency()); taxTotalAmount.setCurrencyID(currency);
taxTotalAmount.setValue(currencyFormat(totals.getTaxTotal())); taxTotalAmount.setValue(currencyFormat(totals.getTaxTotal()));
monetarySummation.getTaxTotalAmount().add(taxTotalAmount); monetarySummation.getTaxTotalAmount().add(taxTotalAmount);
AmountType grandTotalAmount = xmlFactory.createAmountType(); AmountType grandTotalAmount = xmlFactory.createAmountType();
grandTotalAmount.setCurrencyID(trans.getInvoiceCurrency()); grandTotalAmount.setCurrencyID(currency);
grandTotalAmount.setValue(currencyFormat(totals.getTotalGross())); grandTotalAmount.setValue(currencyFormat(totals.getTotalGross()));
monetarySummation.getGrandTotalAmount().add(grandTotalAmount); monetarySummation.getGrandTotalAmount().add(grandTotalAmount);
AmountType duePayableAmount = xmlFactory.createAmountType(); AmountType duePayableAmount = xmlFactory.createAmountType();
duePayableAmount.setCurrencyID(trans.getInvoiceCurrency()); duePayableAmount.setCurrencyID(currency);
duePayableAmount.setValue(currencyFormat(totals.getTotalGross())); duePayableAmount.setValue(currencyFormat(totals.getTotalGross()));
monetarySummation.getDuePayableAmount().add(duePayableAmount); monetarySummation.getDuePayableAmount().add(duePayableAmount);
@@ -964,7 +984,7 @@ public class ZUGFeRDExporter {
grossTradePrice.setBasisQuantity(grossQuantity); grossTradePrice.setBasisQuantity(grossQuantity);
AmountType grossChargeAmount = xmlFactory.createAmountType(); AmountType grossChargeAmount = xmlFactory.createAmountType();
grossChargeAmount.setCurrencyID(trans.getInvoiceCurrency()); grossChargeAmount.setCurrencyID(currency);
grossChargeAmount.setValue(priceFormat(currentItem.getPrice())); grossChargeAmount.setValue(priceFormat(currentItem.getPrice()));
grossTradePrice.getChargeAmount().add(grossChargeAmount); grossTradePrice.getChargeAmount().add(grossChargeAmount);
tradeAgreement.getGrossPriceProductTradePrice().add(grossTradePrice); tradeAgreement.getGrossPriceProductTradePrice().add(grossTradePrice);
@@ -976,7 +996,7 @@ public class ZUGFeRDExporter {
chargeIndicator.setIndicator(false); chargeIndicator.setIndicator(false);
eItemAllowance.setChargeIndicator(chargeIndicator); eItemAllowance.setChargeIndicator(chargeIndicator);
AmountType actualAmount = xmlFactory.createAmountType(); AmountType actualAmount = xmlFactory.createAmountType();
actualAmount.setCurrencyID(trans.getInvoiceCurrency()); actualAmount.setCurrencyID(currency);
actualAmount.setValue(priceFormat(itemAllowance.getTotalAmount().divide(currentItem.getQuantity(), 4, BigDecimal.ROUND_HALF_UP))); actualAmount.setValue(priceFormat(itemAllowance.getTotalAmount().divide(currentItem.getQuantity(), 4, BigDecimal.ROUND_HALF_UP)));
eItemAllowance.getActualAmount().add(actualAmount); eItemAllowance.getActualAmount().add(actualAmount);
TextType reason = xmlFactory.createTextType(); TextType reason = xmlFactory.createTextType();
@@ -990,7 +1010,7 @@ public class ZUGFeRDExporter {
for(IZUGFeRDAllowanceCharge itemCharge : currentItem.getItemCharges()){ for(IZUGFeRDAllowanceCharge itemCharge : currentItem.getItemCharges()){
TradeAllowanceChargeType eItemCharge = xmlFactory.createTradeAllowanceChargeType(); TradeAllowanceChargeType eItemCharge = xmlFactory.createTradeAllowanceChargeType();
AmountType actualAmount = xmlFactory.createAmountType(); AmountType actualAmount = xmlFactory.createAmountType();
actualAmount.setCurrencyID(trans.getInvoiceCurrency()); actualAmount.setCurrencyID(currency);
actualAmount.setValue(priceFormat(itemCharge.getTotalAmount().divide(currentItem.getQuantity(), 4, BigDecimal.ROUND_HALF_UP))); actualAmount.setValue(priceFormat(itemCharge.getTotalAmount().divide(currentItem.getQuantity(), 4, BigDecimal.ROUND_HALF_UP)));
eItemCharge.getActualAmount().add(actualAmount); eItemCharge.getActualAmount().add(actualAmount);
TextType reason = xmlFactory.createTextType(); TextType reason = xmlFactory.createTextType();
@@ -1010,7 +1030,7 @@ public class ZUGFeRDExporter {
netTradePrice.setBasisQuantity(netQuantity); netTradePrice.setBasisQuantity(netQuantity);
AmountType netChargeAmount = xmlFactory.createAmountType(); AmountType netChargeAmount = xmlFactory.createAmountType();
netChargeAmount.setCurrencyID(trans.getInvoiceCurrency()); netChargeAmount.setCurrencyID(currency);
netChargeAmount.setValue(priceFormat(lc.getItemNetAmount())); netChargeAmount.setValue(priceFormat(lc.getItemNetAmount()));
netTradePrice.getChargeAmount().add(netChargeAmount); netTradePrice.getChargeAmount().add(netChargeAmount);
tradeAgreement.getNetPriceProductTradePrice().add(netTradePrice); tradeAgreement.getNetPriceProductTradePrice().add(netTradePrice);
@@ -1042,7 +1062,7 @@ public class ZUGFeRDExporter {
TradeSettlementMonetarySummationType monetarySummation = xmlFactory.createTradeSettlementMonetarySummationType(); TradeSettlementMonetarySummationType monetarySummation = xmlFactory.createTradeSettlementMonetarySummationType();
AmountType itemAmount = xmlFactory.createAmountType(); AmountType itemAmount = xmlFactory.createAmountType();
itemAmount.setCurrencyID(trans.getInvoiceCurrency()); itemAmount.setCurrencyID(currency);
itemAmount.setValue(currencyFormat(lc.getItemTotalNetAmount())); itemAmount.setValue(currencyFormat(lc.getItemTotalNetAmount()));
monetarySummation.getLineTotalAmount().add(itemAmount); monetarySummation.getLineTotalAmount().add(itemAmount);
tradeSettlement.setSpecifiedTradeSettlementMonetarySummation(monetarySummation); tradeSettlement.setSpecifiedTradeSettlementMonetarySummation(monetarySummation);
@@ -1147,7 +1167,7 @@ public class ZUGFeRDExporter {
* should hav ebeen set via * should hav ebeen set via
* <code>setZUGFeRDXMLData(byte[] zugferdData)</code> * <code>setZUGFeRDXMLData(byte[] zugferdData)</code>
*/ */
public void PDFattachZugferdFile(PDDocument doc, IZUGFeRDExportableTransaction trans) throws IOException, JAXBException { public void PDFattachZugferdFile(IZUGFeRDExportableTransaction trans) throws IOException, JAXBException {
if (zugferdData == null) // XML ZUGFeRD data not set externally, needs to be built if (zugferdData == null) // XML ZUGFeRD data not set externally, needs to be built
{ {
@@ -1167,6 +1187,18 @@ public class ZUGFeRDExporter {
PDFAttachGenericFile(doc, "ZUGFeRD-invoice.xml", "Alternative", "Invoice metadata conforming to ZUGFeRD standard (http://www.ferd-net.de/front_content.php?idcat=231&lang=4)", "text/xml", zugferdData); PDFAttachGenericFile(doc, "ZUGFeRD-invoice.xml", "Alternative", "Invoice metadata conforming to ZUGFeRD standard (http://www.ferd-net.de/front_content.php?idcat=231&lang=4)", "text/xml", zugferdData);
} }
public void export(String ZUGFeRDfilename) {
try {
doc.save(ZUGFeRDfilename);
} catch (COSVisitorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** /**
* Embeds an external file (generic - any type allowed) in the PDF. * Embeds an external file (generic - any type allowed) in the PDF.