Populating the BasicProfile and ComfortProfile implementations: an example

This is a code example of iText PDF, discover more.

30th August 2015
admin-marketing

Switch code for this example

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