Subversion Repositories SmartDukaan

Rev

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