Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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