Subversion Repositories SmartDukaan

Rev

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