Subversion Repositories SmartDukaan

Rev

Rev 6790 | Rev 7410 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4687 mandeep.dh 1
package in.shop2020.inventory.service;
2
 
3
import in.shop2020.purchase.PurchaseOrder;
4
import in.shop2020.purchase.Supplier;
5
 
6
import java.io.ByteArrayOutputStream;
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.text.DateFormat;
11
import java.util.Date;
12
 
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
 
16
import com.itextpdf.text.Document;
17
import com.itextpdf.text.Element;
18
import com.itextpdf.text.Font;
19
import com.itextpdf.text.Font.FontFamily;
20
import com.itextpdf.text.FontFactory;
21
import com.itextpdf.text.Paragraph;
22
import com.itextpdf.text.Phrase;
23
import com.itextpdf.text.Rectangle;
24
import com.itextpdf.text.pdf.PdfPCell;
25
import com.itextpdf.text.pdf.PdfPTable;
26
import com.itextpdf.text.pdf.PdfWriter;
27
 
28
public class PdfPoSheetGenerator {
29
 
30
    private static Logger logger = LoggerFactory
31
            .getLogger(PdfPoSheetGenerator.class);
32
 
33
    // private static final Properties properties = readProperties();
6315 amar.kumar 34
    private static final String ourAddress = "Spice Online Retail Pvt. Ltd.\nC/O,PIBCO LIMITED, Basement,Punjsons\n2,Kalkaji Industrial Area, New Delhi-110019\n";
4687 mandeep.dh 35
    private static final String tinNo = "07250399732";
36
 
37
    private static final Font helvetica8 = FontFactory.getFont(
38
            FontFactory.HELVETICA, 8);
39
 
40
    private static final Font helveticaBold8 = FontFactory.getFont(
41
            FontFactory.HELVETICA_BOLD, 8);
42
    private static final Font helveticaBold12 = FontFactory.getFont(
43
            FontFactory.HELVETICA_BOLD, 12);
44
 
45
    public static String generatePdfSheet(PurchaseOrder purchaseOrder,
46
            Supplier supplier) throws IOException {
47
        ByteArrayOutputStream baosPDF = null;
48
        try {
49
            baosPDF = new ByteArrayOutputStream();
50
 
51
            Document document = new Document();
52
            PdfWriter.getInstance(document, baosPDF);
53
            document.addAuthor("shop2020");
54
            document.addTitle("Purchase Order No: "
55
                    + purchaseOrder.getPoNumber());
56
            document.open();
57
 
58
            PdfPTable poTable = getPoTable(purchaseOrder, supplier);
59
            poTable.setSpacingAfter(10.0f);
60
            poTable.setWidthPercentage(90.0f);
61
 
62
            document.add(poTable);
63
            document.close();
64
            baosPDF.close();
65
        } catch (Exception e) {
66
            logger.error("Error while generating Invoice: ", e);
67
        }
68
 
69
        String tmpDir = System.getProperty("java.io.tmpdir");
70
        String filename = tmpDir + "/po-" + purchaseOrder.getId() + ".pdf";
71
        File f = new File(filename);
72
        FileOutputStream fos = new FileOutputStream(f);
73
        baosPDF.writeTo(fos);
74
        return filename;
75
    }
76
 
77
    private static PdfPTable getPoTable(PurchaseOrder purchaseOrder,
78
            Supplier supplier) throws Exception {
79
        PdfPTable poTable = new PdfPTable(1);
80
        poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
81
        poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
82
 
83
        PdfPCell poTitleCell = new PdfPCell(new Phrase("Purchase Order",
84
                helveticaBold12));
85
        poTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
86
        poTitleCell.setBorder(Rectangle.NO_BORDER);
87
 
88
        Date poDate = new Date(purchaseOrder.getCreatedAt());
89
        PdfPTable poSummaryTable = new PdfPTable(new float[] { 0.5f, 0.5f });
90
        poSummaryTable.addCell(new PdfPCell(new Phrase("PO No: "
91
                + purchaseOrder.getPoNumber())));
92
        poSummaryTable
93
                .addCell(new PdfPCell(new Phrase("Date: "
94
                        + DateFormat.getDateInstance(DateFormat.MEDIUM).format(
95
                                poDate))));
96
        poSummaryTable.setSpacingBefore(10.0f);
97
 
98
        poTable.addCell(poTitleCell);
99
        poTable.addCell(getAddressCell());
100
        poTable.addCell(poSummaryTable);
101
        poTable.addCell(getSalutationTable(supplier));
102
        poTable.addCell(getPoDetailsTable(purchaseOrder));
103
        poTable.addCell(getBillToTable());
104
 
105
        return poTable;
106
    }
107
 
108
    private static PdfPCell getAddressCell() {
109
        Paragraph addressParagraph = new Paragraph(ourAddress + "\nTIN NO. "
110
                + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));
111
        PdfPCell addressCell = new PdfPCell();
112
        addressCell.addElement(addressParagraph);
113
        addressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
114
        addressCell.setBorder(Rectangle.NO_BORDER);
115
        return addressCell;
116
    }
117
 
118
    private static PdfPTable getSalutationTable(Supplier supplier)
119
            throws Exception {
120
        PdfPTable salutationTable = new PdfPTable(1);
121
        salutationTable.getDefaultCell().setHorizontalAlignment(
122
                Element.ALIGN_LEFT);
123
        salutationTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
124
        salutationTable.addCell(new Phrase("To", helvetica8));
5030 mandeep.dh 125
        salutationTable.addCell(new Phrase(supplier
126
                .getName(), helvetica8));
4687 mandeep.dh 127
        salutationTable.addCell(new Paragraph(supplier
128
                .getCommunicationAddress(), helvetica8));
129
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
130
        salutationTable
131
                .addCell(new Phrase(
132
                        "Please supply the following stocks as per the details given below:",
133
                        helvetica8));
134
        salutationTable.addCell(new Phrase(" "));
135
        return salutationTable;
136
    }
137
 
138
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
139
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
140
                0.2f, 0.2f });
141
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
142
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
143
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
144
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
145
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
146
 
147
        int slNo = 0;
148
        double total = 0;
149
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
150
                .getLineitems()) {
151
            slNo++;
152
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
153
            detailsTable.addCell(getProductNameCell(lineitem));
154
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
155
                    helvetica8));
156
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
157
                    helvetica8));
158
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
159
            total += lineTotal;
160
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
161
        }
162
        detailsTable.addCell(getTotalCell(4));
163
        detailsTable.addCell(new Phrase("" + total, helvetica8));
164
        return detailsTable;
165
    }
166
 
167
    private static PdfPTable getBillToTable() {
168
        PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
169
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
170
 
171
        billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
172
        billToTable.addCell(new PdfPCell(new Paragraph(ourAddress
173
                + "\nTIN NO. " + tinNo, helvetica8)));
174
 
175
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
176
        billToTable.addCell(new Phrase(
7208 amar.kumar 177
                "Mr. Shiv Kumar, Contact No. +91 9911232226", helvetica8));
4687 mandeep.dh 178
 
179
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
180
        billToTable.addCell(new Phrase("Prices inclusive of all taxes",
181
                helvetica8));
182
 
183
        return billToTable;
184
    }
185
 
186
    private static PdfPCell getProductNameCell(
187
            in.shop2020.purchase.LineItem lineitem) {
188
        String itemName = getItemDisplayName(lineitem);
189
        PdfPCell productNameCell = new PdfPCell(
190
                new Phrase(itemName, helvetica8));
191
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
192
        return productNameCell;
193
    }
194
 
195
    private static String getItemDisplayName(
196
            in.shop2020.purchase.LineItem lineitem) {
197
        StringBuffer itemName = new StringBuffer();
198
        if (lineitem.getBrand() != null)
199
            itemName.append(lineitem.getBrand() + " ");
200
        if (lineitem.getModelName() != null)
201
            itemName.append(lineitem.getModelName() + " ");
202
        if (lineitem.getModelNumber() != null)
203
            itemName.append(lineitem.getModelNumber() + " ");
204
        if (lineitem.getColor() != null
205
                && !lineitem.getColor().trim().equals("NA"))
206
            itemName.append("(" + lineitem.getColor() + ")");
207
 
208
        return itemName.toString();
209
    }
210
 
211
    private static PdfPCell getTotalCell(int colspan) {
212
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
213
        totalCell.setColspan(colspan);
214
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
215
        return totalCell;
216
    }
217
 
218
}