Subversion Repositories SmartDukaan

Rev

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