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