Subversion Repositories SmartDukaan

Rev

Rev 8712 | Rev 9416 | 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));
8713 amar.kumar 147
        } else if(supplier.getName().equals("Smobility Ltd. - Mumbai")) {
148
        	salutationTable.addCell(new Phrase("Spice Retail Ltd. Mumbai", helvetica8));
8712 amar.kumar 149
        } else {
150
        	salutationTable.addCell(new Phrase(supplier
151
                    .getName(), helvetica8));
152
        }
153
 
4687 mandeep.dh 154
        salutationTable.addCell(new Paragraph(supplier
155
                .getCommunicationAddress(), helvetica8));
156
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
157
        salutationTable
158
                .addCell(new Phrase(
159
                        "Please supply the following stocks as per the details given below:",
160
                        helvetica8));
161
        salutationTable.addCell(new Phrase(" "));
162
        return salutationTable;
163
    }
164
 
165
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
166
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
167
                0.2f, 0.2f });
168
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
169
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
170
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
171
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
172
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
173
 
174
        int slNo = 0;
175
        double total = 0;
176
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
177
                .getLineitems()) {
178
            slNo++;
179
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
180
            detailsTable.addCell(getProductNameCell(lineitem));
181
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
182
                    helvetica8));
183
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
184
                    helvetica8));
185
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
186
            total += lineTotal;
187
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
188
        }
189
        detailsTable.addCell(getTotalCell(4));
190
        detailsTable.addCell(new Phrase("" + total, helvetica8));
191
        return detailsTable;
192
    }
193
 
7410 amar.kumar 194
    private static PdfPTable getBillToTable(long warehouseId) {
195
    	//TODO Write this code in a proper configurable way
196
    	String address = "";
197
    	String tinNo = "";
198
    	String contactPerson = "";
199
    	if(warehouseId ==7) {
200
    		address = ourAddressDelhi;
201
    		tinNo = tinNoDelhi;
202
    		contactPerson = "Mr. Shiv Kumar, Contact No. +91 9911232226";
203
    	} else if(warehouseId == 12) {
204
    		address = ourAddressGoregaon;
205
    		tinNo = tinNoMum;
7421 amar.kumar 206
    		contactPerson = "Mr. Avinash Sambhaji Lavange, Contact No. +91 9004049589";
7410 amar.kumar 207
    	} else if(warehouseId == 13) {
208
    		address = ourAddressBhiwandi;
209
    		tinNo = tinNoMum;
210
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
7466 amar.kumar 211
    	} else if(warehouseId ==16) {
212
    		address = amazonAddress;
213
    		tinNo = tinNoMum;
214
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
7410 amar.kumar 215
    	}
216
 
4687 mandeep.dh 217
        PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
218
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
219
 
220
        billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
7410 amar.kumar 221
        billToTable.addCell(new PdfPCell(new Paragraph(address
4687 mandeep.dh 222
                + "\nTIN NO. " + tinNo, helvetica8)));
223
 
224
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
7410 amar.kumar 225
        billToTable.addCell(new Phrase(contactPerson, helvetica8));
4687 mandeep.dh 226
 
227
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
228
        billToTable.addCell(new Phrase("Prices inclusive of all taxes",
229
                helvetica8));
230
 
231
        return billToTable;
232
    }
233
 
234
    private static PdfPCell getProductNameCell(
235
            in.shop2020.purchase.LineItem lineitem) {
236
        String itemName = getItemDisplayName(lineitem);
237
        PdfPCell productNameCell = new PdfPCell(
238
                new Phrase(itemName, helvetica8));
239
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
240
        return productNameCell;
241
    }
242
 
243
    private static String getItemDisplayName(
244
            in.shop2020.purchase.LineItem lineitem) {
245
        StringBuffer itemName = new StringBuffer();
246
        if (lineitem.getBrand() != null)
247
            itemName.append(lineitem.getBrand() + " ");
248
        if (lineitem.getModelName() != null)
249
            itemName.append(lineitem.getModelName() + " ");
250
        if (lineitem.getModelNumber() != null)
251
            itemName.append(lineitem.getModelNumber() + " ");
252
        if (lineitem.getColor() != null
253
                && !lineitem.getColor().trim().equals("NA"))
254
            itemName.append("(" + lineitem.getColor() + ")");
255
 
256
        return itemName.toString();
257
    }
258
 
259
    private static PdfPCell getTotalCell(int colspan) {
260
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
261
        totalCell.setColspan(colspan);
262
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
263
        return totalCell;
264
    }
265
 
266
}