Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.customerrelationship.customer.report;
import java.awt.Color;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.ibm.icu.text.SimpleDateFormat;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class ReportingCommercialPDF {
private final List<ReportingCommercial> items;
public ReportingCommercialPDF(List<ReportingCommercial> items) {
this.items = new ArrayList<>(items);
Collections.sort(this.items, new Comparator<ReportingCommercial>() {
@Override
public int compare(ReportingCommercial o1, ReportingCommercial o2) {
return o2.getTotal().compareTo(o1.getTotal());
}
});
}
public static void main(String[] args) throws IOException {
final List<ReportingCommercial> list = new ArrayList<>();
final Calendar c = Calendar.getInstance();
final Date d1 = c.getTime();
c.add(Calendar.MONTH, 50);
final Date d2 = c.getTime();
for (int i = 0; i < 5; i++) {
ReportingCommercial r = new ReportingCommercial("commercial " + i, d1, d2);
for (int j = 0; j < i * 5; j++) {
final ReportingCommercialItem item = new ReportingCommercialItem("client " + j);
item.addCA(BigDecimal.valueOf(j * 12345.78f));
r.add(item);
}
list.add(r);
}
final File file = new File("out.pdf");
final ReportingCommercialPDF pdf = new ReportingCommercialPDF(list);
pdf.export(file);
Desktop.getDesktop().open(file);
}
public void export(File file) throws IOException {
try {
final Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
// Formats
final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
final DecimalFormat decimalFormat = new DecimalFormat("###,###,##0.00");
final DecimalFormatSymbols formatSymbols = decimalFormat.getDecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
formatSymbols.setDecimalSeparator(',');
decimalFormat.setDecimalFormatSymbols(formatSymbols);
// Fonts
final Font fontHeader = new Font(Font.HELVETICA, 11f, Font.BOLD);
fontHeader.setColor(Color.WHITE);
final Font normal = new Font(Font.HELVETICA, 10f, Font.NORMAL);
final Font fontTitle = new Font(Font.HELVETICA, 18f, Font.BOLD);
final Font fontInfo = new Font(Font.HELVETICA, 11f, Font.NORMAL);
// Colors
final Color backgroundColor = new Color(57, 115, 157);
final Color cellBackgroundColor = new Color(225, 236, 244);
for (ReportingCommercial reporting : this.items) {
final List<ReportingCommercialItem> rItems = new ArrayList<ReportingCommercialItem>(reporting.getItems());
Collections.sort(rItems, new Comparator<ReportingCommercialItem>() {
@Override
public int compare(ReportingCommercialItem o1, ReportingCommercialItem o2) {
return o2.getCa().compareTo(o1.getCa());
}
});
document.add(new Paragraph("Chiffre d'affaires", fontTitle));
document.add(new Paragraph("Commercial : " + reporting.getCommercial(), fontInfo));
document.add(new Paragraph("Période : du " + df.format(reporting.getDebut()) + " au " + df.format(reporting.getFin()), fontInfo));
if (rItems.isEmpty()) {
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
final Font fontInfo2 = new Font(Font.HELVETICA, 13f, Font.ITALIC);
fontInfo2.setColor(Color.GRAY);
document.add(new Paragraph("Aucune donnée.", fontInfo2));
} else {
document.add(new Paragraph("Total : " + decimalFormat.format(reporting.getTotal()) + " € HT", fontInfo));
document.add(new Paragraph("\n"));
final PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.setWidths(new float[] { 800f, 160f });
final PdfPCell c1 = new PdfPCell(new Phrase("Clients", fontHeader));
c1.setBackgroundColor(backgroundColor);
c1.setBorder(Rectangle.NO_BORDER);
c1.setHorizontalAlignment(Element.ALIGN_LEFT);
c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
c1.setPaddingBottom(7);
c1.setPaddingLeft(6);
table.addCell(c1);
final PdfPCell c2 = new PdfPCell(new Phrase("C.A. H.T.", fontHeader));
c2.setBackgroundColor(backgroundColor);
c2.setBorder(Rectangle.NO_BORDER);
c2.setHorizontalAlignment(Element.ALIGN_CENTER);
c2.setVerticalAlignment(Element.ALIGN_MIDDLE);
c2.setPaddingBottom(7);
c2.setPaddingLeft(6);
table.addCell(c2);
table.setHeaderRows(1);
boolean pair = true;
for (ReportingCommercialItem rItem : rItems) {
final PdfPCell col1 = new PdfPCell(new Phrase(rItem.getClient(), normal));
col1.setBorder(Rectangle.NO_BORDER);
col1.setHorizontalAlignment(Element.ALIGN_LEFT);
col1.setVerticalAlignment(Element.ALIGN_CENTER);
col1.setPaddingBottom(5);
col1.setPaddingLeft(6);
final PdfPCell col2 = new PdfPCell(new Phrase(decimalFormat.format(rItem.getCa()), normal));
col2.setHorizontalAlignment(Element.ALIGN_RIGHT);
col2.setBorder(Rectangle.NO_BORDER);
// Alternance couleur
if (pair) {
col1.setBackgroundColor(cellBackgroundColor);
col2.setBackgroundColor(cellBackgroundColor);
}
table.addCell(col1);
table.addCell(col2);
pair = !pair;
}
document.add(table);
}
document.newPage();
}
document.close();
} catch (Exception e) {
throw new IOException(e);
}
}
}