| 490 |
rajveer |
1 |
package in.shop2020.support.services;
|
|
|
2 |
|
| 1517 |
ankur.sing |
3 |
import in.shop2020.config.ConfigException;
|
| 670 |
chandransh |
4 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
5 |
import in.shop2020.logistics.Provider;
|
| 490 |
rajveer |
6 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
|
|
7 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
8 |
import in.shop2020.model.v1.catalog.Warehouse;
|
| 505 |
rajveer |
9 |
import in.shop2020.model.v1.order.LineItem;
|
| 490 |
rajveer |
10 |
import in.shop2020.model.v1.order.Order;
|
|
|
11 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
12 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
| 670 |
chandransh |
13 |
import in.shop2020.thrift.clients.LogisticsServiceClient;
|
| 490 |
rajveer |
14 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
| 1517 |
ankur.sing |
15 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 490 |
rajveer |
16 |
|
|
|
17 |
import java.io.ByteArrayOutputStream;
|
|
|
18 |
import java.io.File;
|
|
|
19 |
import java.io.FileOutputStream;
|
| 505 |
rajveer |
20 |
import java.io.IOException;
|
| 670 |
chandransh |
21 |
import java.text.DateFormat;
|
| 734 |
chandransh |
22 |
import java.text.DecimalFormat;
|
| 490 |
rajveer |
23 |
import java.util.Date;
|
| 505 |
rajveer |
24 |
import java.util.List;
|
| 490 |
rajveer |
25 |
|
|
|
26 |
import org.apache.thrift.TException;
|
|
|
27 |
|
|
|
28 |
import com.itextpdf.text.Document;
|
|
|
29 |
import com.itextpdf.text.Element;
|
|
|
30 |
import com.itextpdf.text.Font;
|
|
|
31 |
import com.itextpdf.text.Font.FontFamily;
|
|
|
32 |
import com.itextpdf.text.FontFactory;
|
|
|
33 |
import com.itextpdf.text.FontFactoryImp;
|
| 670 |
chandransh |
34 |
import com.itextpdf.text.Image;
|
| 490 |
rajveer |
35 |
import com.itextpdf.text.Paragraph;
|
| 505 |
rajveer |
36 |
import com.itextpdf.text.Phrase;
|
| 670 |
chandransh |
37 |
import com.itextpdf.text.Rectangle;
|
| 490 |
rajveer |
38 |
import com.itextpdf.text.pdf.BaseFont;
|
|
|
39 |
import com.itextpdf.text.pdf.PdfPCell;
|
|
|
40 |
import com.itextpdf.text.pdf.PdfPTable;
|
|
|
41 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
42 |
|
|
|
43 |
public class InvoiceGenerationService {
|
| 734 |
chandransh |
44 |
private TransactionServiceClient tsc = null;
|
|
|
45 |
private CatalogServiceClient csc = null;
|
|
|
46 |
private LogisticsServiceClient lsc = null;
|
|
|
47 |
private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");
|
| 490 |
rajveer |
48 |
|
| 734 |
chandransh |
49 |
public InvoiceGenerationService() {
|
|
|
50 |
try {
|
| 490 |
rajveer |
51 |
tsc = new TransactionServiceClient();
|
|
|
52 |
csc = new CatalogServiceClient();
|
| 670 |
chandransh |
53 |
lsc = new LogisticsServiceClient();
|
| 490 |
rajveer |
54 |
} catch (Exception e) {
|
|
|
55 |
e.printStackTrace();
|
|
|
56 |
}
|
|
|
57 |
}
|
| 734 |
chandransh |
58 |
|
|
|
59 |
public ByteArrayOutputStream generateInvoice(long orderId) {
|
| 670 |
chandransh |
60 |
ByteArrayOutputStream baosPDF = null;
|
|
|
61 |
in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
|
|
|
62 |
Client iclient = csc.getClient();
|
|
|
63 |
in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
|
| 734 |
chandransh |
64 |
|
| 670 |
chandransh |
65 |
Order order = null;
|
|
|
66 |
Warehouse warehouse = null;
|
|
|
67 |
Provider provider = null;
|
| 734 |
chandransh |
68 |
String destCode = null;
|
| 1517 |
ankur.sing |
69 |
int barcodeFontSize = 0;
|
| 670 |
chandransh |
70 |
try {
|
| 490 |
rajveer |
71 |
order = tclient.getOrder(orderId);
|
|
|
72 |
warehouse = iclient.getWarehouse(order.getWarehouse_id());
|
| 734 |
chandransh |
73 |
long providerId = order.getLogistics_provider_id();
|
|
|
74 |
provider = logisticsClient.getProvider(providerId);
|
|
|
75 |
destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
|
| 1517 |
ankur.sing |
76 |
barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
|
| 490 |
rajveer |
77 |
} catch (TransactionServiceException e1) {
|
|
|
78 |
e1.printStackTrace();
|
| 670 |
chandransh |
79 |
return baosPDF;
|
| 490 |
rajveer |
80 |
} catch (TException e1) {
|
|
|
81 |
e1.printStackTrace();
|
| 670 |
chandransh |
82 |
return baosPDF;
|
| 490 |
rajveer |
83 |
} catch (InventoryServiceException e) {
|
|
|
84 |
e.printStackTrace();
|
| 670 |
chandransh |
85 |
return baosPDF;
|
|
|
86 |
} catch (LogisticsServiceException e) {
|
|
|
87 |
e.printStackTrace();
|
|
|
88 |
return baosPDF;
|
| 1517 |
ankur.sing |
89 |
} catch (ConfigException e) {
|
|
|
90 |
e.printStackTrace();
|
|
|
91 |
return baosPDF;
|
| 490 |
rajveer |
92 |
}
|
| 734 |
chandransh |
93 |
|
|
|
94 |
try {
|
|
|
95 |
baosPDF = new ByteArrayOutputStream();
|
|
|
96 |
|
|
|
97 |
String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();
|
|
|
98 |
FontFactoryImp ttfFontFactory = new FontFactoryImp();
|
|
|
99 |
ttfFontFactory.register(fontPath, "barcode");
|
| 1517 |
ankur.sing |
100 |
Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
|
| 734 |
chandransh |
101 |
Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
|
|
|
102 |
Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);
|
|
|
103 |
Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);
|
|
|
104 |
Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);
|
|
|
105 |
|
|
|
106 |
Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);
|
|
|
107 |
Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
|
|
|
108 |
Document document = new Document();
|
|
|
109 |
PdfWriter.getInstance(document, baosPDF);
|
|
|
110 |
document.addAuthor("shop2020");
|
|
|
111 |
document.addTitle("Invoice No: " + order.getInvoice_number());
|
|
|
112 |
document.open();
|
|
|
113 |
PdfPTable table = new PdfPTable(1);
|
|
|
114 |
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
115 |
|
|
|
116 |
String logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();
|
|
|
117 |
PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
|
|
|
118 |
logoCell.setBorder(Rectangle.NO_BORDER);
|
|
|
119 |
|
|
|
120 |
String addressString = warehouse.getLocation() + "\nPIN " + warehouse.getPincode() + "\n\n";
|
|
|
121 |
Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN, 8f));
|
|
|
122 |
PdfPCell addressCell = new PdfPCell();
|
|
|
123 |
addressCell.addElement(addressParagraph);
|
|
|
124 |
addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
125 |
addressCell.setBorder(Rectangle.NO_BORDER);
|
|
|
126 |
|
|
|
127 |
PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));
|
|
|
128 |
titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
129 |
titleCell.setBorder(Rectangle.NO_BORDER);
|
|
|
130 |
|
| 1322 |
chandransh |
131 |
PdfPTable providerInfoTable = new PdfPTable(1);
|
| 734 |
chandransh |
132 |
providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
133 |
PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));
|
|
|
134 |
providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
| 1322 |
chandransh |
135 |
//providerNameCell.setColspan(2);
|
| 734 |
chandransh |
136 |
providerNameCell.setBorder(Rectangle.NO_BORDER);
|
|
|
137 |
|
|
|
138 |
PdfPCell awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));
|
|
|
139 |
awbNumberCell.setPaddingTop(20.0f);
|
| 1322 |
chandransh |
140 |
//awbNumberCell.setPaddingBottom(10.0f);
|
|
|
141 |
//awbNumberCell.setRowspan(3);
|
| 734 |
chandransh |
142 |
awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
143 |
awbNumberCell.setBorder(Rectangle.NO_BORDER);
|
|
|
144 |
|
|
|
145 |
providerInfoTable.addCell(providerNameCell);
|
|
|
146 |
providerInfoTable.addCell(awbNumberCell);
|
|
|
147 |
providerInfoTable.addCell(new Phrase("Account No : " + provider.getAccountNo(), helvetica8));
|
|
|
148 |
providerInfoTable.addCell(new Phrase("AWB Date : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
|
| 919 |
rajveer |
149 |
providerInfoTable.addCell(new Phrase("Weight : " + order.getTotal_weight() + " Kg", helvetica8));
|
| 734 |
chandransh |
150 |
|
|
|
151 |
PdfPTable customerTable = new PdfPTable(1);
|
|
|
152 |
customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
| 1322 |
chandransh |
153 |
customerTable.addCell(new Phrase(order.getCustomer_name(), helvetica12));
|
| 734 |
chandransh |
154 |
customerTable.addCell(new Phrase(order.getCustomer_address1(), helvetica12));
|
|
|
155 |
customerTable.addCell(new Phrase(order.getCustomer_address2(), helvetica12));
|
|
|
156 |
customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), helvetica12));
|
|
|
157 |
customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
|
| 1322 |
chandransh |
158 |
customerTable.addCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), helvetica12));
|
| 734 |
chandransh |
159 |
|
| 1322 |
chandransh |
160 |
PdfPTable dispatchTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
|
|
|
161 |
dispatchTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
|
|
162 |
dispatchTable.addCell(customerTable);
|
|
|
163 |
dispatchTable.addCell(new Phrase(" "));
|
|
|
164 |
dispatchTable.addCell(providerInfoTable);
|
|
|
165 |
|
|
|
166 |
PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
|
| 734 |
chandransh |
167 |
invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
168 |
//invoiceTable.setSpacingAfter(20);
|
|
|
169 |
//invoiceTable.setSpacingBefore(20);
|
|
|
170 |
|
|
|
171 |
PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
|
|
|
172 |
invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
|
|
|
173 |
invoiceTableHeader.setColspan(6);
|
|
|
174 |
invoiceTableHeader.setPaddingTop(10);
|
|
|
175 |
invoiceTable.addCell(invoiceTableHeader);
|
|
|
176 |
|
|
|
177 |
invoiceTable.addCell(new Phrase("Order No", helvetica8));
|
|
|
178 |
invoiceTable.addCell(new Phrase("Paymode", helvetica8));
|
|
|
179 |
invoiceTable.addCell(new Phrase("Product Name", helvetica8));
|
|
|
180 |
invoiceTable.addCell(new Phrase("Quantity", helvetica8));
|
|
|
181 |
invoiceTable.addCell(new Phrase("Rate", helvetica8));
|
|
|
182 |
invoiceTable.addCell(new Phrase("Amount", helvetica8));
|
|
|
183 |
List<LineItem> lineitems = order.getLineitems();
|
|
|
184 |
double totalitems = 0;
|
|
|
185 |
for (LineItem lineitem : lineitems) {
|
|
|
186 |
invoiceTable.addCell(new Phrase(orderId + "", helvetica8));
|
|
|
187 |
invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
|
|
|
188 |
|
| 919 |
rajveer |
189 |
String itemName = ((lineitem.getBrand()!= null) ? lineitem.getBrand() + " " : "")
|
|
|
190 |
+ ((lineitem.getModel_name() != null) ? lineitem.getModel_name() + " " : "")
|
|
|
191 |
+ (( lineitem.getModel_number() != null ) ? lineitem.getModel_number() + " " : "" )
|
|
|
192 |
+ (( (lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))) ? "("+lineitem.getColor()+")" : "" );
|
|
|
193 |
|
|
|
194 |
PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
|
| 734 |
chandransh |
195 |
productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
|
|
196 |
invoiceTable.addCell(productNameCell);
|
|
|
197 |
|
|
|
198 |
invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
|
|
|
199 |
|
|
|
200 |
PdfPCell unitPriceCell = new PdfPCell(new Phrase(amountFormat.format(lineitem.getUnit_price()), helvetica8));
|
|
|
201 |
unitPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
202 |
invoiceTable.addCell(unitPriceCell);
|
|
|
203 |
|
|
|
204 |
PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(lineitem.getTotal_price()), helvetica8));
|
|
|
205 |
totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
206 |
invoiceTable.addCell(totalPriceCell);
|
|
|
207 |
totalitems = totalitems + lineitem.getQuantity();
|
|
|
208 |
}
|
|
|
209 |
PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
|
|
|
210 |
totalCell.setColspan(4);
|
|
|
211 |
invoiceTable.addCell(totalCell);
|
|
|
212 |
|
|
|
213 |
PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
|
|
|
214 |
rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
215 |
invoiceTable.addCell(rupeesCell);
|
|
|
216 |
|
|
|
217 |
PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(order.getTotal_amount()) + "", helveticaBold8));
|
|
|
218 |
totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
219 |
invoiceTable.addCell(totalAmountCell);
|
|
|
220 |
|
|
|
221 |
PdfPCell tinCell = new PdfPCell(new Phrase("TIN No: " + warehouse.getTinNumber(), helvetica8));
|
|
|
222 |
tinCell.setColspan(6);
|
|
|
223 |
tinCell.setPadding(2);
|
|
|
224 |
invoiceTable.addCell(tinCell);
|
|
|
225 |
|
|
|
226 |
table.addCell(logoCell);
|
|
|
227 |
table.addCell(titleCell);
|
| 1322 |
chandransh |
228 |
table.addCell(dispatchTable);
|
| 734 |
chandransh |
229 |
table.addCell(invoiceTable);
|
|
|
230 |
table.addCell(new Phrase("If undelivered, return to:", helvetica10));
|
|
|
231 |
table.addCell(addressCell);
|
|
|
232 |
|
|
|
233 |
table.addCell(new Phrase("Do not pay any extra charges to the Courier."));
|
|
|
234 |
|
|
|
235 |
table.setWidthPercentage(90.0f);
|
|
|
236 |
|
|
|
237 |
document.add(table);
|
|
|
238 |
document.close();
|
|
|
239 |
baosPDF.close();
|
|
|
240 |
} catch (Exception e) {
|
|
|
241 |
e.printStackTrace();
|
|
|
242 |
}
|
|
|
243 |
return baosPDF;
|
|
|
244 |
}
|
|
|
245 |
|
| 505 |
rajveer |
246 |
public static void main(String[] args) throws IOException {
|
| 734 |
chandransh |
247 |
InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
|
| 1476 |
ankur.sing |
248 |
long orderId = 13;
|
|
|
249 |
ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId);
|
|
|
250 |
String userHome = System.getProperty("user.home");
|
|
|
251 |
File f = new File(userHome + "/invoice-" + orderId + ".pdf");
|
| 505 |
rajveer |
252 |
FileOutputStream fos = new FileOutputStream(f);
|
|
|
253 |
baos.writeTo(fos);
|
| 1476 |
ankur.sing |
254 |
System.out.println("Invoice generated.");
|
| 490 |
rajveer |
255 |
}
|
|
|
256 |
}
|