Subversion Repositories SmartDukaan

Rev

Rev 5030 | Go to most recent revision | Details | 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();
34
    private static final String ourAddress = "Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n";
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));
125
        salutationTable.addCell(new Paragraph(supplier
126
                .getCommunicationAddress(), helvetica8));
127
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
128
        salutationTable
129
                .addCell(new Phrase(
130
                        "Please supply the following stocks as per the details given below:",
131
                        helvetica8));
132
        salutationTable.addCell(new Phrase(" "));
133
        return salutationTable;
134
    }
135
 
136
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
137
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
138
                0.2f, 0.2f });
139
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
140
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
141
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
142
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
143
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
144
 
145
        int slNo = 0;
146
        double total = 0;
147
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
148
                .getLineitems()) {
149
            slNo++;
150
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
151
            detailsTable.addCell(getProductNameCell(lineitem));
152
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
153
                    helvetica8));
154
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
155
                    helvetica8));
156
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
157
            total += lineTotal;
158
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
159
        }
160
        detailsTable.addCell(getTotalCell(4));
161
        detailsTable.addCell(new Phrase("" + total, helvetica8));
162
        return detailsTable;
163
    }
164
 
165
    private static PdfPTable getBillToTable() {
166
        PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
167
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
168
 
169
        billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
170
        billToTable.addCell(new PdfPCell(new Paragraph(ourAddress
171
                + "\nTIN NO. " + tinNo, helvetica8)));
172
 
173
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
174
        billToTable.addCell(new Phrase(
175
                "Mr. Pramod Kumar, Contact No. +91 9971573026", helvetica8));
176
 
177
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
178
        billToTable.addCell(new Phrase("Prices inclusive of all taxes",
179
                helvetica8));
180
 
181
        return billToTable;
182
    }
183
 
184
    private static PdfPCell getProductNameCell(
185
            in.shop2020.purchase.LineItem lineitem) {
186
        String itemName = getItemDisplayName(lineitem);
187
        PdfPCell productNameCell = new PdfPCell(
188
                new Phrase(itemName, helvetica8));
189
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
190
        return productNameCell;
191
    }
192
 
193
    private static String getItemDisplayName(
194
            in.shop2020.purchase.LineItem lineitem) {
195
        StringBuffer itemName = new StringBuffer();
196
        if (lineitem.getBrand() != null)
197
            itemName.append(lineitem.getBrand() + " ");
198
        if (lineitem.getModelName() != null)
199
            itemName.append(lineitem.getModelName() + " ");
200
        if (lineitem.getModelNumber() != null)
201
            itemName.append(lineitem.getModelNumber() + " ");
202
        if (lineitem.getColor() != null
203
                && !lineitem.getColor().trim().equals("NA"))
204
            itemName.append("(" + lineitem.getColor() + ")");
205
 
206
        return itemName.toString();
207
    }
208
 
209
    private static PdfPCell getTotalCell(int colspan) {
210
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
211
        totalCell.setColspan(colspan);
212
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
213
        return totalCell;
214
    }
215
 
216
}