Subversion Repositories SmartDukaan

Rev

Rev 9925 | Rev 12543 | 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;
9416 amar.kumar 5
import in.shop2020.purchase.TaxType;
4687 mandeep.dh 6
 
7
import java.io.ByteArrayOutputStream;
8
import java.io.File;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.text.DateFormat;
12
import java.util.Date;
13
 
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
 
17
import com.itextpdf.text.Document;
18
import com.itextpdf.text.Element;
19
import com.itextpdf.text.Font;
20
import com.itextpdf.text.Font.FontFamily;
21
import com.itextpdf.text.FontFactory;
22
import com.itextpdf.text.Paragraph;
23
import com.itextpdf.text.Phrase;
24
import com.itextpdf.text.Rectangle;
25
import com.itextpdf.text.pdf.PdfPCell;
26
import com.itextpdf.text.pdf.PdfPTable;
27
import com.itextpdf.text.pdf.PdfWriter;
28
 
29
public class PdfPoSheetGenerator {
30
 
31
    private static Logger logger = LoggerFactory
32
            .getLogger(PdfPoSheetGenerator.class);
33
 
34
    // private static final Properties properties = readProperties();
7410 amar.kumar 35
    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 36
    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 37
    private static final String ourAddressGoregaon = "Spice Online Retail Pvt. Ltd.\n93/743, Motilal Nagar-1, Goregaon(WEST),\nMotilal Nagar, Mumbai, Maharashtra-400062\n";
10877 manish.sha 38
    private static final String ourAddressBangalore = "Spice Online Retail Pvt. Ltd.\n C/O.Drive India Enterprise Solutions Limited)\n Survey No. 86, Korulur Village, Kasab Hobli\n Hoskote Taluka\n Bangalore – 560067, Karnataka\n";
39
    private static final String ourAddressBangaloreSR = "Spice Online Retail Pvt. Ltd.\n Shop No. 320, Municipal 59th Cross,\n Next to Rammandir Ground,\n 3rd Block, Rajaji Nagar,\nBangalore - 560010, Karnataka\n";
40
 
9925 amar.kumar 41
    private static final String amazonAddress = "Spice Online Retail Pvt. Ltd. C/O Amazon Seller Services  Pvt. Ltd.,\nBuilding H Prathmesh Complex, Saravali Village,\nOpp Hotel Vatika Kalyan, Bhivandi Junction,\nBhiwandi, Maharashtra\n";
10877 manish.sha 42
    private static final String amazonAddreseBangalore = "Spice Online Retail Pvt. Ltd.\n C/O Amazon Seller Services Pvt. Ltd.\n 38 & 39, Soukya Road,\n kacherakanahalli,\n Hoskote Taluka,\n Bangalore  -560067, Karnataka\n";
7410 amar.kumar 43
    private static final String tinNoDelhi = "07250399732";
44
    private static final String tinNoMum = "27450984008";
10877 manish.sha 45
    private static final String tinNoBan = "29171183852";
4687 mandeep.dh 46
 
47
    private static final Font helvetica8 = FontFactory.getFont(
48
            FontFactory.HELVETICA, 8);
49
 
50
    private static final Font helveticaBold8 = FontFactory.getFont(
51
            FontFactory.HELVETICA_BOLD, 8);
52
    private static final Font helveticaBold12 = FontFactory.getFont(
53
            FontFactory.HELVETICA_BOLD, 12);
54
 
55
    public static String generatePdfSheet(PurchaseOrder purchaseOrder,
56
            Supplier supplier) throws IOException {
57
        ByteArrayOutputStream baosPDF = null;
58
        try {
59
            baosPDF = new ByteArrayOutputStream();
60
 
61
            Document document = new Document();
62
            PdfWriter.getInstance(document, baosPDF);
63
            document.addAuthor("shop2020");
64
            document.addTitle("Purchase Order No: "
65
                    + purchaseOrder.getPoNumber());
66
            document.open();
67
 
68
            PdfPTable poTable = getPoTable(purchaseOrder, supplier);
69
            poTable.setSpacingAfter(10.0f);
70
            poTable.setWidthPercentage(90.0f);
71
 
72
            document.add(poTable);
73
            document.close();
74
            baosPDF.close();
75
        } catch (Exception e) {
76
            logger.error("Error while generating Invoice: ", e);
77
        }
78
 
79
        String tmpDir = System.getProperty("java.io.tmpdir");
80
        String filename = tmpDir + "/po-" + purchaseOrder.getId() + ".pdf";
81
        File f = new File(filename);
82
        FileOutputStream fos = new FileOutputStream(f);
83
        baosPDF.writeTo(fos);
84
        return filename;
85
    }
86
 
87
    private static PdfPTable getPoTable(PurchaseOrder purchaseOrder,
88
            Supplier supplier) throws Exception {
89
        PdfPTable poTable = new PdfPTable(1);
90
        poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
91
        poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
92
 
93
        PdfPCell poTitleCell = new PdfPCell(new Phrase("Purchase Order",
94
                helveticaBold12));
95
        poTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
96
        poTitleCell.setBorder(Rectangle.NO_BORDER);
97
 
98
        Date poDate = new Date(purchaseOrder.getCreatedAt());
99
        PdfPTable poSummaryTable = new PdfPTable(new float[] { 0.5f, 0.5f });
100
        poSummaryTable.addCell(new PdfPCell(new Phrase("PO No: "
101
                + purchaseOrder.getPoNumber())));
9416 amar.kumar 102
        poSummaryTable.addCell(new PdfPCell(new Phrase("Date: "
103
        		+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(
104
        				poDate))));
4687 mandeep.dh 105
        poSummaryTable.setSpacingBefore(10.0f);
106
 
107
        poTable.addCell(poTitleCell);
9925 amar.kumar 108
        poTable.addCell(getAddressCell(purchaseOrder.getWarehouseId(), purchaseOrder.getShippingWarehouseId()));
4687 mandeep.dh 109
        poTable.addCell(poSummaryTable);
110
        poTable.addCell(getSalutationTable(supplier));
111
        poTable.addCell(getPoDetailsTable(purchaseOrder));
9591 amar.kumar 112
        poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId(), purchaseOrder.getTaxType()));
113
        //poTable.addCell(getCFormCell(purchaseOrder.getTaxType()));
4687 mandeep.dh 114
 
115
        return poTable;
116
    }
117
 
9416 amar.kumar 118
    private static PdfPCell getCFormCell(TaxType taxType) {
119
    	PdfPCell cFormCell = null;
120
		if(taxType == TaxType.CFORM) {
9586 amar.kumar 121
			cFormCell = new PdfPCell(new Paragraph("*To be billed on CST Against C-Form ", new Font(FontFamily.TIMES_ROMAN, 8f)));
9416 amar.kumar 122
		} else {
123
			cFormCell = new PdfPCell();
124
		}
125
		cFormCell.setBorder(Rectangle.NO_BORDER);
126
		cFormCell.setHorizontalAlignment(Element.ALIGN_LEFT);
127
		return cFormCell;
128
	}
129
 
9925 amar.kumar 130
	private static PdfPTable getAddressCell(long warehouseId, long shippingWarehouseId) {
7410 amar.kumar 131
    	//TODO Write this code in a proper configurable way
132
    	String address = "";
133
    	String tinNo = "";
9925 amar.kumar 134
    	String shippingAddress = "";
135
    	String shippingTinNo = "";
7410 amar.kumar 136
    	if(warehouseId ==7) {
137
    		address = ourAddressDelhi;
138
    		tinNo = tinNoDelhi;
139
    	} else if(warehouseId == 12) {
140
    		address = ourAddressGoregaon;
141
    		tinNo = tinNoMum;
142
    	} else if(warehouseId == 13) {
143
    		address = ourAddressBhiwandi;
144
    		tinNo = tinNoMum;
7466 amar.kumar 145
    	} else if(warehouseId ==16) {
7464 amar.kumar 146
    		address = amazonAddress;
147
    		tinNo = tinNoMum;
10877 manish.sha 148
    	} else if(warehouseId ==1765){
149
    		address = ourAddressBangalore;
150
    		tinNo = tinNoBan;
151
    	} else if(warehouseId ==1768){
152
    		address = ourAddressBangaloreSR;
153
    		tinNo = tinNoBan;
154
    	} else if(warehouseId ==1771){
155
    		address = amazonAddreseBangalore;
156
    		tinNo = tinNoBan;
7410 amar.kumar 157
    	}
9925 amar.kumar 158
    	PdfPTable billToShipToTable  = new PdfPTable(2);
7410 amar.kumar 159
        Paragraph addressParagraph = new Paragraph(address + "\nTIN NO. "
4687 mandeep.dh 160
                + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));
161
        PdfPCell addressCell = new PdfPCell();
9925 amar.kumar 162
        if(warehouseId != shippingWarehouseId) {
163
           	addressParagraph = new Paragraph("Bill To :\n" + address + "\n\nTIN NO. "
164
                    + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));
165
        }
4687 mandeep.dh 166
        addressCell.addElement(addressParagraph);
9925 amar.kumar 167
        //addressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
4687 mandeep.dh 168
        addressCell.setBorder(Rectangle.NO_BORDER);
9925 amar.kumar 169
 
170
        if(warehouseId != shippingWarehouseId) {
171
			if(shippingWarehouseId ==7) {
172
				shippingAddress = ourAddressDelhi;
173
				shippingTinNo = tinNoDelhi;
174
	    	} else if(shippingWarehouseId == 12) {
175
	    		shippingAddress = ourAddressGoregaon;
176
	    		shippingTinNo = tinNoMum;
177
	    	} else if(shippingWarehouseId == 13) {
178
	    		shippingAddress = ourAddressBhiwandi;
179
	    		shippingTinNo = tinNoMum;
180
	    	} else if(shippingWarehouseId ==16) {
181
	    		shippingAddress = amazonAddress;
182
	    		shippingTinNo = tinNoMum;
10877 manish.sha 183
	    	} else if(shippingWarehouseId ==1765){
184
	    		shippingAddress = ourAddressBangalore;
185
	    		shippingTinNo = tinNoBan;
186
	    	} else if(shippingWarehouseId ==1768){
187
	    		shippingAddress = ourAddressBangaloreSR;
188
	    		shippingTinNo = tinNoBan;
189
	    	} else if(shippingWarehouseId ==1771){
190
	    		shippingAddress = amazonAddreseBangalore;
191
	    		shippingTinNo = tinNoBan;
9925 amar.kumar 192
	    	}
193
 
194
			PdfPCell billToShipToCell = new PdfPCell();
195
			billToShipToCell.setHorizontalAlignment(Element.ALIGN_LEFT);
196
			billToShipToCell.setBorder(Rectangle.NO_BORDER);
197
 
198
			PdfPCell shippingAddressCell = new PdfPCell();
199
			shippingAddressCell.addElement(new Paragraph("Ship To :\n" +shippingAddress + "\nTIN NO. "
200
	                + shippingTinNo, new Font(FontFamily.TIMES_ROMAN, 8f)));
201
			shippingAddressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
202
	        shippingAddressCell.setBorder(Rectangle.LEFT);
203
	        billToShipToTable.addCell(addressCell);
204
	        billToShipToTable.addCell(shippingAddressCell);
205
	        billToShipToCell.addElement(billToShipToTable);
206
	        return billToShipToTable;
207
 
208
    	}
209
        billToShipToTable.addCell(addressCell);
210
        PdfPCell placeHolderCell = new PdfPCell();
211
        placeHolderCell.setBorder(Rectangle.NO_BORDER);
212
        billToShipToTable.addCell(placeHolderCell);
213
        return billToShipToTable;
4687 mandeep.dh 214
    }
215
 
216
    private static PdfPTable getSalutationTable(Supplier supplier)
217
            throws Exception {
218
        PdfPTable salutationTable = new PdfPTable(1);
219
        salutationTable.getDefaultCell().setHorizontalAlignment(
220
                Element.ALIGN_LEFT);
221
        salutationTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
222
        salutationTable.addCell(new Phrase("To", helvetica8));
8712 amar.kumar 223
        if(supplier.getName().equals("Smobility Ltd.")) {
224
        	salutationTable.addCell(new Phrase("Spice Retail Ltd.", helvetica8));
8713 amar.kumar 225
        } else if(supplier.getName().equals("Smobility Ltd. - Mumbai")) {
226
        	salutationTable.addCell(new Phrase("Spice Retail Ltd. Mumbai", helvetica8));
8712 amar.kumar 227
        } else {
228
        	salutationTable.addCell(new Phrase(supplier
229
                    .getName(), helvetica8));
230
        }
231
 
4687 mandeep.dh 232
        salutationTable.addCell(new Paragraph(supplier
233
                .getCommunicationAddress(), helvetica8));
234
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
235
        salutationTable
236
                .addCell(new Phrase(
237
                        "Please supply the following stocks as per the details given below:",
238
                        helvetica8));
239
        salutationTable.addCell(new Phrase(" "));
240
        return salutationTable;
241
    }
242
 
243
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
244
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
9591 amar.kumar 245
                0.1f, 0.15f, 0.15f });
4687 mandeep.dh 246
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
247
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
248
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
9591 amar.kumar 249
        detailsTable.addCell(new Phrase("Mrp", helveticaBold8));
4687 mandeep.dh 250
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
251
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
252
 
253
        int slNo = 0;
254
        double total = 0;
255
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
256
                .getLineitems()) {
257
            slNo++;
258
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
259
            detailsTable.addCell(getProductNameCell(lineitem));
260
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
261
                    helvetica8));
9591 amar.kumar 262
            detailsTable.addCell(new Phrase(lineitem.getMrp() + "",
263
                    helvetica8));
4687 mandeep.dh 264
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
265
                    helvetica8));
266
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
267
            total += lineTotal;
268
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
269
        }
9634 amar.kumar 270
        detailsTable.addCell(getTotalCell(5));
4687 mandeep.dh 271
        detailsTable.addCell(new Phrase("" + total, helvetica8));
272
        return detailsTable;
273
    }
274
 
9591 amar.kumar 275
    private static PdfPTable getBillToTable(long warehouseId, TaxType taxType) {
7410 amar.kumar 276
    	//TODO Write this code in a proper configurable way
277
    	String address = "";
278
    	String tinNo = "";
279
    	String contactPerson = "";
280
    	if(warehouseId ==7) {
281
    		address = ourAddressDelhi;
282
    		tinNo = tinNoDelhi;
283
    		contactPerson = "Mr. Shiv Kumar, Contact No. +91 9911232226";
284
    	} else if(warehouseId == 12) {
285
    		address = ourAddressGoregaon;
286
    		tinNo = tinNoMum;
7421 amar.kumar 287
    		contactPerson = "Mr. Avinash Sambhaji Lavange, Contact No. +91 9004049589";
7410 amar.kumar 288
    	} else if(warehouseId == 13) {
289
    		address = ourAddressBhiwandi;
290
    		tinNo = tinNoMum;
291
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
7466 amar.kumar 292
    	} else if(warehouseId ==16) {
293
    		address = amazonAddress;
294
    		tinNo = tinNoMum;
295
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
10877 manish.sha 296
    	} else if(warehouseId ==16) {
297
    		address = amazonAddress;
298
    		tinNo = tinNoMum;
299
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
300
    	} else if(warehouseId ==16) {
301
    		address = amazonAddress;
302
    		tinNo = tinNoMum;
303
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
7410 amar.kumar 304
    	}
10877 manish.sha 305
    	else if(warehouseId ==1765) {
306
    		address = ourAddressBangalore;
307
    		tinNo = tinNoBan;
308
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
309
    	}
310
    	else if(warehouseId ==1768) {
311
    		address = ourAddressBangaloreSR;
312
    		tinNo = tinNoBan;
313
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
314
    	}
315
    	else if(warehouseId ==1771) {
316
    		address = amazonAddreseBangalore;
317
    		tinNo = tinNoBan;
318
    		contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
319
    	}
7410 amar.kumar 320
 
4687 mandeep.dh 321
        PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
322
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
323
 
9925 amar.kumar 324
        billToTable.addCell(new Phrase("Bill To :", helvetica8));
7410 amar.kumar 325
        billToTable.addCell(new PdfPCell(new Paragraph(address
4687 mandeep.dh 326
                + "\nTIN NO. " + tinNo, helvetica8)));
327
 
328
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
7410 amar.kumar 329
        billToTable.addCell(new Phrase(contactPerson, helvetica8));
4687 mandeep.dh 330
 
331
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
9591 amar.kumar 332
        if(taxType == TaxType.CFORM) {
333
        	billToTable.addCell(new Phrase("Prices inclusive of all taxes (To be billed on CST Against C-Form)",
334
        			helvetica8));
335
        } else {
336
        	billToTable.addCell(new Phrase("Prices inclusive of all taxes",
337
        			helvetica8));
338
        }
4687 mandeep.dh 339
 
340
        return billToTable;
341
    }
342
 
343
    private static PdfPCell getProductNameCell(
344
            in.shop2020.purchase.LineItem lineitem) {
345
        String itemName = getItemDisplayName(lineitem);
346
        PdfPCell productNameCell = new PdfPCell(
347
                new Phrase(itemName, helvetica8));
348
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
349
        return productNameCell;
350
    }
351
 
352
    private static String getItemDisplayName(
353
            in.shop2020.purchase.LineItem lineitem) {
354
        StringBuffer itemName = new StringBuffer();
355
        if (lineitem.getBrand() != null)
356
            itemName.append(lineitem.getBrand() + " ");
357
        if (lineitem.getModelName() != null)
358
            itemName.append(lineitem.getModelName() + " ");
359
        if (lineitem.getModelNumber() != null)
360
            itemName.append(lineitem.getModelNumber() + " ");
361
        if (lineitem.getColor() != null
362
                && !lineitem.getColor().trim().equals("NA"))
363
            itemName.append("(" + lineitem.getColor() + ")");
364
 
365
        return itemName.toString();
366
    }
367
 
368
    private static PdfPCell getTotalCell(int colspan) {
369
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
370
        totalCell.setColspan(colspan);
371
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
372
        return totalCell;
373
    }
374
 
375
}