iText pdf library
Website search

Populating the BasicProfile and ComfortProfile implementations: an example

This example was written in the context of the book ZUGFeRD: The Future of Invoicing. It is referred to from different chapters.

InvoiceData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Code written by Bruno Lowagie in the context of an example.
 */
package zugferd.data;
 
import com.itextpdf.text.zugferd.checkers.basic.DateFormatCode;
import com.itextpdf.text.zugferd.checkers.basic.DocumentTypeCode;
import com.itextpdf.text.zugferd.checkers.basic.TaxIDTypeCode;
import com.itextpdf.text.zugferd.checkers.basic.TaxTypeCode;
import com.itextpdf.text.zugferd.checkers.comfort.FreeTextSubjectCode;
import com.itextpdf.text.zugferd.checkers.comfort.PaymentMeansCode;
import com.itextpdf.text.zugferd.checkers.comfort.TaxCategoryCode;
import com.itextpdf.text.zugferd.profiles.BasicProfile;
import com.itextpdf.text.zugferd.profiles.BasicProfileImp;
import com.itextpdf.text.zugferd.profiles.ComfortProfile;
import com.itextpdf.text.zugferd.profiles.ComfortProfileImp;
 
import java.util.Map;
import java.util.TreeMap;
 
import zugferd.pojo.Customer;
import zugferd.pojo.Invoice;
import zugferd.pojo.Item;
 
/**
 *
 * @author Bruno Lowagie (iText Software)
 */
public class InvoiceData {
    
    public InvoiceData() {
    }
    
    public BasicProfile createBasicProfileData(Invoice invoice) {
        BasicProfileImp profileImp = new BasicProfileImp();
        importData(profileImp, invoice);
        importBasicData(profileImp, invoice);
        return profileImp;
    }
    
    public ComfortProfile createComfortProfileData(Invoice invoice) {
        ComfortProfileImp profileImp = new ComfortProfileImp();
        importData(profileImp, invoice);
        importComfortData(profileImp, invoice);
        return profileImp;
    }
   
    public void importData(BasicProfileImp profileImp, Invoice invoice) {
        profileImp.setTest(true);
        profileImp.setId(String.format("I/%05d", invoice.getId()));
        profileImp.setName("INVOICE");
        profileImp.setTypeCode(DocumentTypeCode.COMMERCIAL_INVOICE);
        profileImp.setDate(invoice.getInvoiceDate(), DateFormatCode.YYYYMMDD);
        profileImp.setSellerName("Das Company");
        profileImp.setSellerLineOne("ZUG Business Center");
        profileImp.setSellerLineTwo("Highway 1");
        profileImp.setSellerPostcode("9000");
        profileImp.setSellerCityName("Ghent");
        profileImp.setSellerCountryID("BE");
        profileImp.addSellerTaxRegistration(TaxIDTypeCode.FISCAL_NUMBER, "201/113/40209");
        profileImp.addSellerTaxRegistration(TaxIDTypeCode.VAT, "BE123456789");
        Customer customer = invoice.getCustomer();
        profileImp.setBuyerName(String.format("%s, %s", customer.getLastName(), customer.getFirstName()));
        profileImp.setBuyerPostcode(customer.getPostalcode());
        profileImp.setBuyerLineOne(customer.getStreet());
        profileImp.setBuyerCityName(customer.getCity());
        profileImp.setBuyerCountryID(customer.getCountryId());
        profileImp.setPaymentReference(String.format("%09d", invoice.getId()));
        profileImp.setInvoiceCurrencyCode("EUR");
    }
    
    public void importBasicData(BasicProfileImp profileImp, Invoice invoice) {
        profileImp.addNote(
            new String[]{"This is a test invoice.\nNothing on this invoice is real.\nThis invoice is part of a tutorial."});
        profileImp.addPaymentMeans("", "", "BE 41 7360 0661 9710", "", "", "KREDBEBB", "", "KBC");
        profileImp.addPaymentMeans("", "", "BE 56 0015 4298 7888", "", "", "GEBABEBB", "", "BNP Paribas");
        Map<Double,Double> taxes = new TreeMap<Double, Double>();
        double tax;
        for (Item item : invoice.getItems()) {
            tax = item.getProduct().getVat();
            if (taxes.containsKey(tax)) {
                taxes.put(tax, taxes.get(tax) + item.getCost());
            }
            else {
                taxes.put(tax, item.getCost());
            }
            profileImp.addIncludedSupplyChainTradeLineItem(format4dec(item.getQuantity()), "C62", item.getProduct().getName());
        }
        double total, tA;
        double ltN = 0;
        double ttA = 0;
        double gtA = 0;
        for (Map.Entry<Double, Double> t : taxes.entrySet()) {
            tax = t.getKey();
            total = round(t.getValue());
            gtA += total;
            tA = round((100 * total) / (100 + tax));
            ttA += (total - tA);
            ltN += tA;
            profileImp.addApplicableTradeTax(format2dec(total - tA), "EUR", TaxTypeCode.VALUE_ADDED_TAX, format2dec(tA), "EUR", format2dec(tax));
        }
        profileImp.setMonetarySummation(format2dec(ltN), "EUR",
            format2dec(0), "EUR",
            format2dec(0), "EUR",
            format2dec(ltN), "EUR",
            format2dec(ttA), "EUR",
            format2dec(gtA), "EUR");
    }
   
    public void importComfortData(ComfortProfileImp profileImp, Invoice invoice) {
        profileImp.addNote(
            new String[]{"This is a test invoice.\nNothing on this invoice is real.\nThis invoice is part of a tutorial."},
            FreeTextSubjectCode.REGULATORY_INFORMATION);
        profileImp.addPaymentMeans(
                PaymentMeansCode.PAYMENT_TO_BANK_ACCOUNT,
                new String[]{"This is the preferred bank account."},
                "", "",
                "", "",
                "BE 41 7360 0661 9710", "", "",
                "", "", "",
                "KREDBEBB", "", "KBC");
        profileImp.addPaymentMeans(
                PaymentMeansCode.PAYMENT_TO_BANK_ACCOUNT,
                new String[]{"Use this as an alternative account."},
                "", "",
                "", "",
                "BE 56 0015 4298 7888", "", "",
                "", "", "",
                "GEBABEBB", "", "BNP Paribas");
        Map<Double,Double> taxes = new TreeMap<Double, Double>();
        double tax;
        int counter = 0;
        for (Item item : invoice.getItems()) {
            counter++;
            tax = item.getProduct().getVat();
            if (taxes.containsKey(tax)) {
                taxes.put(tax, taxes.get(tax) + item.getCost());
            }
            else {
                taxes.put(tax, item.getCost());
            }
            profileImp.addIncludedSupplyChainTradeLineItem(
                    String.valueOf(counter),
                    null,
                    format4dec(item.getProduct().getPrice()), "EUR", null, null,
                    null, null, null, null,
                    null, null, null, null,
                    format4dec(item.getQuantity()), "C62",
                    new String[]{TaxTypeCode.VALUE_ADDED_TAX},
                    new String[1],
                    new String[]{TaxCategoryCode.STANDARD_RATE},
                    new String[]{format2dec(item.getProduct().getVat())},
                    format2dec(item.getCost()), "EUR",
                    null, null,
                    String.valueOf(item.getProduct().getId()), null,
                    item.getProduct().getName(), null
            );
        }
        double total, tA;
        double ltN = 0;
        double ttA = 0;
        double gtA = 0;
        for (Map.Entry<Double, Double> t : taxes.entrySet()) {
            tax = t.getKey();
            total = round(t.getValue());
            gtA += total;
            tA = round((100 * total) / (100 + tax));
            ttA += (total - tA);
            ltN += tA;
            profileImp.addApplicableTradeTax(
                    format2dec(total - tA), "EUR", TaxTypeCode.VALUE_ADDED_TAX,
                    null, format2dec(tA), "EUR",
                    TaxCategoryCode.STANDARD_RATE, format2dec(tax));
        }
        profileImp.setMonetarySummation(format2dec(ltN), "EUR",
            format2dec(0), "EUR",
            format2dec(0), "EUR",
            format2dec(ltN), "EUR",
            format2dec(ttA), "EUR",
            format2dec(gtA), "EUR");
    }
    
    public static double round(double d) {
        d = d * 100;
        long tmp = Math.round(d);
        return (double) tmp / 100;
    }
    
    public static String format2dec(double d) {
        return String.format("%.2f", d);
    }
    
    public static String format4dec(double d) {
        return String.format("%.4f", d);
    }
    
}


Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now