Subversion Repositories SmartDukaan

Rev

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
			e.printStackTrace();
50
		}
51
	}
52
 
53
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId) {
54
		ByteArrayOutputStream baosPDF = null;
55
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
56
		in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();
57
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
58
 
59
		List<Order> orders = null;
60
		Warehouse warehouse = null;
61
		Provider provider = null;
62
		try {
63
			orders = txnClient.getAllOrders(OrderStatus.BILLED, 0L, new Date().getTime(), warehouseId);
64
			warehouse = inventoryClient.getWarehouse(warehouseId);
65
			provider = logisticsClient.getProvider(providerId);
66
		} catch (TException e1) {
67
			e1.printStackTrace();
68
			return baosPDF;
69
		} catch (InventoryServiceException e) {
70
			e.printStackTrace();
71
			return baosPDF;
72
		} catch (LogisticsServiceException e) {
73
			e.printStackTrace();
74
			return baosPDF;
75
		} catch (TransactionServiceException e) {
76
			e.printStackTrace();
77
			return baosPDF;
78
		}
79
 
80
		try {
81
			baosPDF = new ByteArrayOutputStream();
82
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
83
			Document document = new Document();
84
			PdfWriter.getInstance(document, baosPDF);
85
			document.addAuthor("shop2020");
86
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
87
			document.open();
88
			PdfPTable table = new PdfPTable(1);
89
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
90
			table.getDefaultCell().setPaddingBottom(10.0f);
91
 
92
			String logoPath = ManifestGenerator.class.getResource("/logo.png").getPath();
93
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
94
			logoCell.setBorder(Rectangle.NO_BORDER);
95
 
96
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
97
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
98
			PdfPCell addressCell = new PdfPCell();
99
			addressCell.addElement(addressParagraph);
100
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
101
			addressCell.setBorder(Rectangle.NO_BORDER);
102
 
103
			PdfPTable ordersTable = new PdfPTable(8);
104
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
105
			ordersTable.addCell(new Phrase("Order No", helvetica8));
106
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
107
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
108
			ordersTable.addCell(new Phrase("Name", helvetica8));
109
			ordersTable.addCell(new Phrase("Destination City", helvetica8));
110
			ordersTable.addCell(new Phrase("Pincode", helvetica8));
111
			ordersTable.addCell(new Phrase("State", helvetica8));
112
 
113
			int serialNo = 0;
114
			for(int i=0; i < orders.size();i++){
115
				Order order = orders.get(i);
116
				if(order.getLogistics_provider_id()!=providerId)
117
					continue;
118
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
119
				serialNo++;
120
				List<LineItem> lineItems = order.getLineitems();
121
				double weight = 0;
122
				for(LineItem lineItem: lineItems)
123
					weight += lineItem.getTotal_weight();
124
 
125
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
126
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
127
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
128
				ordersTable.addCell(new Phrase(weight+"", helvetica8));
129
				ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
130
				ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
131
				ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
132
				ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));				
133
			}
134
 
135
			table.addCell(logoCell);
136
			table.addCell(addressCell);
137
			table.addCell(new Phrase("PAYMODE: EXP", helvetica8));
138
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
139
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
140
			table.addCell(ordersTable);
141
			table.setWidthPercentage(90.0f);
142
 
143
			document.add(table);  
144
			document.close();
145
			baosPDF.close();
146
		} catch (DocumentException e) {
147
			e.printStackTrace();
148
		} catch (MalformedURLException e) {
149
			e.printStackTrace();
150
		} catch (IOException e) {
151
			e.printStackTrace();
152
		}
153
 
154
		return baosPDF;
155
	}
156
}