| 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));
|
| 7410 |
amar.kumar |
107 |
poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId()));
|
| 9416 |
amar.kumar |
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,
|
|
|
180 |
0.2f, 0.2f });
|
|
|
181 |
detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
|
|
|
182 |
detailsTable.addCell(new Phrase("Description", helveticaBold8));
|
|
|
183 |
detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
|
|
|
184 |
detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
|
|
|
185 |
detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
|
|
|
186 |
|
|
|
187 |
int slNo = 0;
|
|
|
188 |
double total = 0;
|
|
|
189 |
for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
|
|
|
190 |
.getLineitems()) {
|
|
|
191 |
slNo++;
|
|
|
192 |
detailsTable.addCell(new Phrase(slNo + "", helvetica8));
|
|
|
193 |
detailsTable.addCell(getProductNameCell(lineitem));
|
|
|
194 |
detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
|
|
|
195 |
helvetica8));
|
|
|
196 |
detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
|
|
|
197 |
helvetica8));
|
|
|
198 |
double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
|
|
|
199 |
total += lineTotal;
|
|
|
200 |
detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
|
|
|
201 |
}
|
|
|
202 |
detailsTable.addCell(getTotalCell(4));
|
|
|
203 |
detailsTable.addCell(new Phrase("" + total, helvetica8));
|
|
|
204 |
return detailsTable;
|
|
|
205 |
}
|
|
|
206 |
|
| 7410 |
amar.kumar |
207 |
private static PdfPTable getBillToTable(long warehouseId) {
|
|
|
208 |
//TODO Write this code in a proper configurable way
|
|
|
209 |
String address = "";
|
|
|
210 |
String tinNo = "";
|
|
|
211 |
String contactPerson = "";
|
|
|
212 |
if(warehouseId ==7) {
|
|
|
213 |
address = ourAddressDelhi;
|
|
|
214 |
tinNo = tinNoDelhi;
|
|
|
215 |
contactPerson = "Mr. Shiv Kumar, Contact No. +91 9911232226";
|
|
|
216 |
} else if(warehouseId == 12) {
|
|
|
217 |
address = ourAddressGoregaon;
|
|
|
218 |
tinNo = tinNoMum;
|
| 7421 |
amar.kumar |
219 |
contactPerson = "Mr. Avinash Sambhaji Lavange, Contact No. +91 9004049589";
|
| 7410 |
amar.kumar |
220 |
} else if(warehouseId == 13) {
|
|
|
221 |
address = ourAddressBhiwandi;
|
|
|
222 |
tinNo = tinNoMum;
|
|
|
223 |
contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
|
| 7466 |
amar.kumar |
224 |
} else if(warehouseId ==16) {
|
|
|
225 |
address = amazonAddress;
|
|
|
226 |
tinNo = tinNoMum;
|
|
|
227 |
contactPerson = "Mr. Sandeep Sachdeva, Contact No. +91 9716691287";
|
| 7410 |
amar.kumar |
228 |
}
|
|
|
229 |
|
| 4687 |
mandeep.dh |
230 |
PdfPTable billToTable = new PdfPTable(new float[] { 0.2f, 0.8f });
|
|
|
231 |
billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
232 |
|
|
|
233 |
billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
|
| 7410 |
amar.kumar |
234 |
billToTable.addCell(new PdfPCell(new Paragraph(address
|
| 4687 |
mandeep.dh |
235 |
+ "\nTIN NO. " + tinNo, helvetica8)));
|
|
|
236 |
|
|
|
237 |
billToTable.addCell(new Phrase("Contact Person:", helvetica8));
|
| 7410 |
amar.kumar |
238 |
billToTable.addCell(new Phrase(contactPerson, helvetica8));
|
| 4687 |
mandeep.dh |
239 |
|
|
|
240 |
billToTable.addCell(new Phrase("Taxes:", helvetica8));
|
|
|
241 |
billToTable.addCell(new Phrase("Prices inclusive of all taxes",
|
|
|
242 |
helvetica8));
|
|
|
243 |
|
|
|
244 |
return billToTable;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
private static PdfPCell getProductNameCell(
|
|
|
248 |
in.shop2020.purchase.LineItem lineitem) {
|
|
|
249 |
String itemName = getItemDisplayName(lineitem);
|
|
|
250 |
PdfPCell productNameCell = new PdfPCell(
|
|
|
251 |
new Phrase(itemName, helvetica8));
|
|
|
252 |
productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
253 |
return productNameCell;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
private static String getItemDisplayName(
|
|
|
257 |
in.shop2020.purchase.LineItem lineitem) {
|
|
|
258 |
StringBuffer itemName = new StringBuffer();
|
|
|
259 |
if (lineitem.getBrand() != null)
|
|
|
260 |
itemName.append(lineitem.getBrand() + " ");
|
|
|
261 |
if (lineitem.getModelName() != null)
|
|
|
262 |
itemName.append(lineitem.getModelName() + " ");
|
|
|
263 |
if (lineitem.getModelNumber() != null)
|
|
|
264 |
itemName.append(lineitem.getModelNumber() + " ");
|
|
|
265 |
if (lineitem.getColor() != null
|
|
|
266 |
&& !lineitem.getColor().trim().equals("NA"))
|
|
|
267 |
itemName.append("(" + lineitem.getColor() + ")");
|
|
|
268 |
|
|
|
269 |
return itemName.toString();
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
private static PdfPCell getTotalCell(int colspan) {
|
|
|
273 |
PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
|
|
|
274 |
totalCell.setColspan(colspan);
|
|
|
275 |
totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
276 |
return totalCell;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
}
|