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