Subversion Repositories SmartDukaan

Rev

Rev 7208 | Rev 7421 | 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();
7410 amar.kumar 34
    private static final String ourAddressDelhi = "Spice Online Retail Pvt. Ltd.\nC/O,PIBCO LIMITED, Basement,Punjsons\n2,Kalkaji Industrial Area, New Delhi-110019\n";
35
    private static final String ourAddressBhiwandi = "Spice Online Retail Pvt. Ltd.\nBhivandi Junction, Bhiwandi, Thane,\nMaharashtra -421302\n";
36
    private static final String ourAddressGoregaon = "Spice Online Retail Pvt. Ltd.\n93/743, Motilal Nagar-1, Goregaon(WEST),\nMotilal Nagar, Mumbai, Maharashtra-400062\n";
37
    private static final String tinNoDelhi = "07250399732";
38
    private static final String tinNoMum = "27450984008";
4687 mandeep.dh 39
 
40
    private static final Font helvetica8 = FontFactory.getFont(
41
            FontFactory.HELVETICA, 8);
42
 
43
    private static final Font helveticaBold8 = FontFactory.getFont(
44
            FontFactory.HELVETICA_BOLD, 8);
45
    private static final Font helveticaBold12 = FontFactory.getFont(
46
            FontFactory.HELVETICA_BOLD, 12);
47
 
48
    public static String generatePdfSheet(PurchaseOrder purchaseOrder,
49
            Supplier supplier) throws IOException {
50
        ByteArrayOutputStream baosPDF = null;
51
        try {
52
            baosPDF = new ByteArrayOutputStream();
53
 
54
            Document document = new Document();
55
            PdfWriter.getInstance(document, baosPDF);
56
            document.addAuthor("shop2020");
57
            document.addTitle("Purchase Order No: "
58
                    + purchaseOrder.getPoNumber());
59
            document.open();
60
 
61
            PdfPTable poTable = getPoTable(purchaseOrder, supplier);
62
            poTable.setSpacingAfter(10.0f);
63
            poTable.setWidthPercentage(90.0f);
64
 
65
            document.add(poTable);
66
            document.close();
67
            baosPDF.close();
68
        } catch (Exception e) {
69
            logger.error("Error while generating Invoice: ", e);
70
        }
71
 
72
        String tmpDir = System.getProperty("java.io.tmpdir");
73
        String filename = tmpDir + "/po-" + purchaseOrder.getId() + ".pdf";
74
        File f = new File(filename);
75
        FileOutputStream fos = new FileOutputStream(f);
76
        baosPDF.writeTo(fos);
77
        return filename;
78
    }
79
 
80
    private static PdfPTable getPoTable(PurchaseOrder purchaseOrder,
81
            Supplier supplier) throws Exception {
82
        PdfPTable poTable = new PdfPTable(1);
83
        poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
84
        poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
85
 
86
        PdfPCell poTitleCell = new PdfPCell(new Phrase("Purchase Order",
87
                helveticaBold12));
88
        poTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
89
        poTitleCell.setBorder(Rectangle.NO_BORDER);
90
 
91
        Date poDate = new Date(purchaseOrder.getCreatedAt());
92
        PdfPTable poSummaryTable = new PdfPTable(new float[] { 0.5f, 0.5f });
93
        poSummaryTable.addCell(new PdfPCell(new Phrase("PO No: "
94
                + purchaseOrder.getPoNumber())));
95
        poSummaryTable
96
                .addCell(new PdfPCell(new Phrase("Date: "
97
                        + DateFormat.getDateInstance(DateFormat.MEDIUM).format(
98
                                poDate))));
99
        poSummaryTable.setSpacingBefore(10.0f);
100
 
101
        poTable.addCell(poTitleCell);
7410 amar.kumar 102
        poTable.addCell(getAddressCell(purchaseOrder.getWarehouseId()));
4687 mandeep.dh 103
        poTable.addCell(poSummaryTable);
104
        poTable.addCell(getSalutationTable(supplier));
105
        poTable.addCell(getPoDetailsTable(purchaseOrder));
7410 amar.kumar 106
        poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId()));
4687 mandeep.dh 107
 
108
        return poTable;
109
    }
110
 
7410 amar.kumar 111
    private static PdfPCell getAddressCell(long warehouseId) {
112
    	//TODO Write this code in a proper configurable way
113
    	String address = "";
114
    	String tinNo = "";
115
    	if(warehouseId ==7) {
116
    		address = ourAddressDelhi;
117
    		tinNo = tinNoDelhi;
118
    	} else if(warehouseId == 12) {
119
    		address = ourAddressGoregaon;
120
    		tinNo = tinNoMum;
121
    	} else if(warehouseId == 13) {
122
    		address = ourAddressBhiwandi;
123
    		tinNo = tinNoMum;
124
    	}
125
        Paragraph addressParagraph = new Paragraph(address + "\nTIN NO. "
4687 mandeep.dh 126
                + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));
127
        PdfPCell addressCell = new PdfPCell();
128
        addressCell.addElement(addressParagraph);
129
        addressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
130
        addressCell.setBorder(Rectangle.NO_BORDER);
131
        return addressCell;
132
    }
133
 
134
    private static PdfPTable getSalutationTable(Supplier supplier)
135
            throws Exception {
136
        PdfPTable salutationTable = new PdfPTable(1);
137
        salutationTable.getDefaultCell().setHorizontalAlignment(
138
                Element.ALIGN_LEFT);
139
        salutationTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
140
        salutationTable.addCell(new Phrase("To", helvetica8));
5030 mandeep.dh 141
        salutationTable.addCell(new Phrase(supplier
142
                .getName(), helvetica8));
4687 mandeep.dh 143
        salutationTable.addCell(new Paragraph(supplier
144
                .getCommunicationAddress(), helvetica8));
145
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
146
        salutationTable
147
                .addCell(new Phrase(
148
                        "Please supply the following stocks as per the details given below:",
149
                        helvetica8));
150
        salutationTable.addCell(new Phrase(" "));
151
        return salutationTable;
152
    }
153
 
154
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
155
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
156
                0.2f, 0.2f });
157
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
158
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
159
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
160
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
161
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
162
 
163
        int slNo = 0;
164
        double total = 0;
165
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
166
                .getLineitems()) {
167
            slNo++;
168
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
169
            detailsTable.addCell(getProductNameCell(lineitem));
170
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
171
                    helvetica8));
172
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
173
                    helvetica8));
174
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
175
            total += lineTotal;
176
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
177
        }
178
        detailsTable.addCell(getTotalCell(4));
179
        detailsTable.addCell(new Phrase("" + total, helvetica8));
180
        return detailsTable;
181
    }
182
 
7410 amar.kumar 183
    private static PdfPTable getBillToTable(long warehouseId) {
184
    	//TODO Write this code in a proper configurable way
185
    	String address = "";
186
    	String tinNo = "";
187
    	String contactPerson = "";
188
    	if(warehouseId ==7) {
189
    		address = ourAddressDelhi;
190
    		tinNo = tinNoDelhi;
191
    		contactPerson = "Mr. Shiv Kumar, Contact No. +91 9911232226";
192
    	} else if(warehouseId == 12) {
193
    		address = ourAddressGoregaon;
194
    		tinNo = tinNoMum;
195
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
196
    	} else if(warehouseId == 13) {
197
    		address = ourAddressBhiwandi;
198
    		tinNo = tinNoMum;
199
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
200
    	}
201
 
4687 mandeep.dh 202
        PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
203
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
204
 
205
        billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
7410 amar.kumar 206
        billToTable.addCell(new PdfPCell(new Paragraph(address
4687 mandeep.dh 207
                + "\nTIN NO. " + tinNo, helvetica8)));
208
 
209
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
7410 amar.kumar 210
        billToTable.addCell(new Phrase(contactPerson, helvetica8));
4687 mandeep.dh 211
 
212
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
213
        billToTable.addCell(new Phrase("Prices inclusive of all taxes",
214
                helvetica8));
215
 
216
        return billToTable;
217
    }
218
 
219
    private static PdfPCell getProductNameCell(
220
            in.shop2020.purchase.LineItem lineitem) {
221
        String itemName = getItemDisplayName(lineitem);
222
        PdfPCell productNameCell = new PdfPCell(
223
                new Phrase(itemName, helvetica8));
224
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
225
        return productNameCell;
226
    }
227
 
228
    private static String getItemDisplayName(
229
            in.shop2020.purchase.LineItem lineitem) {
230
        StringBuffer itemName = new StringBuffer();
231
        if (lineitem.getBrand() != null)
232
            itemName.append(lineitem.getBrand() + " ");
233
        if (lineitem.getModelName() != null)
234
            itemName.append(lineitem.getModelName() + " ");
235
        if (lineitem.getModelNumber() != null)
236
            itemName.append(lineitem.getModelNumber() + " ");
237
        if (lineitem.getColor() != null
238
                && !lineitem.getColor().trim().equals("NA"))
239
            itemName.append("(" + lineitem.getColor() + ")");
240
 
241
        return itemName.toString();
242
    }
243
 
244
    private static PdfPCell getTotalCell(int colspan) {
245
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
246
        totalCell.setColspan(colspan);
247
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
248
        return totalCell;
249
    }
250
 
251
}