Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
676 chandransh 1
package in.shop2020.support.services;
2
 
3
import java.io.ByteArrayOutputStream;
4
import java.io.IOException;
5
import java.net.MalformedURLException;
6
import java.text.DateFormat;
7
import java.util.Date;
8
import java.util.List;
9
 
10
import org.apache.thrift.TException;
11
 
12
import com.itextpdf.text.Document;
13
import com.itextpdf.text.DocumentException;
14
import com.itextpdf.text.Element;
15
import com.itextpdf.text.Font;
16
import com.itextpdf.text.FontFactory;
17
import com.itextpdf.text.Image;
18
import com.itextpdf.text.Paragraph;
19
import com.itextpdf.text.Phrase;
20
import com.itextpdf.text.Rectangle;
21
import com.itextpdf.text.Font.FontFamily;
22
import com.itextpdf.text.pdf.PdfPCell;
23
import com.itextpdf.text.pdf.PdfPTable;
24
import com.itextpdf.text.pdf.PdfWriter;
25
 
26
import in.shop2020.logistics.LogisticsServiceException;
27
import in.shop2020.logistics.Provider;
28
import in.shop2020.model.v1.catalog.InventoryServiceException;
29
import in.shop2020.model.v1.catalog.Warehouse;
30
import in.shop2020.model.v1.order.LineItem;
31
import in.shop2020.model.v1.order.Order;
32
import in.shop2020.model.v1.order.OrderStatus;
33
import in.shop2020.model.v1.order.TransactionServiceException;
34
import in.shop2020.thrift.clients.CatalogServiceClient;
35
import in.shop2020.thrift.clients.LogisticsServiceClient;
36
import in.shop2020.thrift.clients.TransactionServiceClient;
37
 
38
public class ManifestGenerator {
39
	private TransactionServiceClient tsc = null;
40
	private CatalogServiceClient csc = null;
41
	private LogisticsServiceClient lsc = null;
42
 
43
	public ManifestGenerator() {
44
		try {
45
			tsc = new TransactionServiceClient();
46
			csc = new CatalogServiceClient();
47
			lsc = new LogisticsServiceClient();
48
		} catch (Exception e) {
49
			// TODO Auto-generated catch block
50
			e.printStackTrace();
51
		}
52
	}
53
 
54
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId) {
55
		ByteArrayOutputStream baosPDF = null;
56
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
57
		in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();
58
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
59
 
60
		List<Order> orders = null;
61
		Warehouse warehouse = null;
62
		Provider provider = null;
63
		try {
64
			orders = txnClient.getAllOrders(OrderStatus.BILLED, 0L, new Date().getTime(), warehouseId);
65
			warehouse = inventoryClient.getWarehouse(warehouseId);
66
			provider = logisticsClient.getProvider(providerId);
67
		} catch (TException e1) {
68
			e1.printStackTrace();
69
			return baosPDF;
70
		} catch (InventoryServiceException e) {
71
			e.printStackTrace();
72
			return baosPDF;
73
		} catch (LogisticsServiceException e) {
74
			e.printStackTrace();
75
			return baosPDF;
76
		} catch (TransactionServiceException e) {
77
			e.printStackTrace();
78
			return baosPDF;
79
		}
80
 
81
		try {
82
			baosPDF = new ByteArrayOutputStream();
83
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
84
			Document document = new Document();
85
			PdfWriter.getInstance(document, baosPDF);
86
			document.addAuthor("shop2020");
87
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
88
			document.open();
89
			PdfPTable table = new PdfPTable(1);
90
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
91
			table.getDefaultCell().setPaddingBottom(10.0f);
92
 
93
			String logoPath = ManifestGenerator.class.getResource("/logo.png").getPath();
94
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
95
			logoCell.setBorder(Rectangle.NO_BORDER);
96
 
97
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
98
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
99
			PdfPCell addressCell = new PdfPCell();
100
			addressCell.addElement(addressParagraph);
101
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
102
			addressCell.setBorder(Rectangle.NO_BORDER);
103
 
104
			PdfPTable ordersTable = new PdfPTable(8);
105
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
106
			ordersTable.addCell(new Phrase("Order No", helvetica8));
107
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
108
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
109
			ordersTable.addCell(new Phrase("Name", helvetica8));
110
			ordersTable.addCell(new Phrase("Destination City", helvetica8));
111
			ordersTable.addCell(new Phrase("Pincode", helvetica8));
112
			ordersTable.addCell(new Phrase("State", helvetica8));
113
 
114
			int serialNo = 0;
115
			for(int i=0; i < orders.size();i++){
116
				Order order = orders.get(i);
117
				if(order.getLogistics_provider_id()!=providerId)
118
					continue;
119
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
120
				serialNo++;
121
				List<LineItem> lineItems = order.getLineitems();
122
				double weight = 0;
123
				for(LineItem lineItem: lineItems)
124
					weight += lineItem.getTotal_weight();
125
 
126
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
127
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
128
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
129
				ordersTable.addCell(new Phrase(weight+"", helvetica8));
130
				ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
131
				ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
132
				ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
133
				ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));				
134
			}
135
 
136
			table.addCell(logoCell);
137
			table.addCell(addressCell);
138
			table.addCell(new Phrase("PAYMODE: EXP", helvetica8));
139
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
140
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
141
			table.addCell(ordersTable);
142
			table.setWidthPercentage(90.0f);
143
 
144
			document.add(table);  
145
			document.close();
146
			baosPDF.close();
147
		} catch (DocumentException e) {
148
			e.printStackTrace();
149
		} catch (MalformedURLException e) {
150
			e.printStackTrace();
151
		} catch (IOException e) {
152
			e.printStackTrace();
153
		}
154
 
155
		return baosPDF;
156
	}
157
}