Subversion Repositories SmartDukaan

Rev

Rev 5554 | Rev 5714 | Go to most recent revision | Details | Compare with Previous | 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;
4410 rajveer 10
import java.util.ArrayList;
676 chandransh 11
import java.util.Date;
12
import java.util.List;
13
 
14
import org.apache.thrift.TException;
3062 chandransh 15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
676 chandransh 17
 
18
import com.itextpdf.text.Document;
19
import com.itextpdf.text.DocumentException;
20
import com.itextpdf.text.Element;
21
import com.itextpdf.text.Font;
22
import com.itextpdf.text.FontFactory;
23
import com.itextpdf.text.Image;
24
import com.itextpdf.text.Paragraph;
25
import com.itextpdf.text.Phrase;
26
import com.itextpdf.text.Rectangle;
27
import com.itextpdf.text.Font.FontFamily;
28
import com.itextpdf.text.pdf.PdfPCell;
29
import com.itextpdf.text.pdf.PdfPTable;
30
import com.itextpdf.text.pdf.PdfWriter;
31
 
32
import in.shop2020.logistics.LogisticsServiceException;
5678 rajveer 33
import in.shop2020.logistics.PickupStore;
676 chandransh 34
import in.shop2020.logistics.Provider;
35
import in.shop2020.model.v1.catalog.InventoryServiceException;
36
import in.shop2020.model.v1.catalog.Warehouse;
37
import in.shop2020.model.v1.order.LineItem;
38
import in.shop2020.model.v1.order.Order;
39
import in.shop2020.model.v1.order.OrderStatus;
40
import in.shop2020.model.v1.order.TransactionServiceException;
3125 rajveer 41
import in.shop2020.thrift.clients.CatalogClient;
42
import in.shop2020.thrift.clients.LogisticsClient;
43
import in.shop2020.thrift.clients.TransactionClient;
676 chandransh 44
 
45
public class ManifestGenerator {
3062 chandransh 46
 
47
    private static Logger logger = LoggerFactory.getLogger(ManifestGenerator.class);
48
 
3125 rajveer 49
	private TransactionClient tsc = null;
50
	private CatalogClient csc = null;
51
	private LogisticsClient lsc = null;
744 chandransh 52
	private DecimalFormat weightFormat = new DecimalFormat("0.000");
676 chandransh 53
	public ManifestGenerator() {
54
		try {
3125 rajveer 55
			tsc = new TransactionClient();
56
			csc = new CatalogClient();
57
			lsc = new LogisticsClient();
676 chandransh 58
		} catch (Exception e) {
3062 chandransh 59
		    logger.error("Error while initializing one of the thrift clients", e);
676 chandransh 60
		}
61
	}
62
 
4788 rajveer 63
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId, boolean isCod, List<Long> orderIds) {
676 chandransh 64
		ByteArrayOutputStream baosPDF = null;
65
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
66
		in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();
67
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
68
 
4410 rajveer 69
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
70
		statuses.add(OrderStatus.BILLED);
71
 
676 chandransh 72
		List<Order> orders = null;
73
		Warehouse warehouse = null;
74
		Provider provider = null;
75
		try {
4410 rajveer 76
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId);
676 chandransh 77
			warehouse = inventoryClient.getWarehouse(warehouseId);
78
			provider = logisticsClient.getProvider(providerId);
3062 chandransh 79
		} catch (TException e) {
80
		    logger.error("Error getting information from one of the Thrift Services: ", e);
676 chandransh 81
			return baosPDF;
82
		} catch (InventoryServiceException e) {
3062 chandransh 83
		    logger.error("Error getting warehouse info from the catalog service: ", e);
676 chandransh 84
			return baosPDF;
85
		} catch (LogisticsServiceException e) {
3062 chandransh 86
		    logger.error("Error getting provider info from the logistics service: ", e);
676 chandransh 87
			return baosPDF;
88
		} catch (TransactionServiceException e) {
3062 chandransh 89
		    logger.error("Error getting orders from the transaction service: ", e);
676 chandransh 90
			return baosPDF;
91
		}
92
 
93
		try {
94
			baosPDF = new ByteArrayOutputStream();
95
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
96
			Document document = new Document();
97
			PdfWriter.getInstance(document, baosPDF);
98
			document.addAuthor("shop2020");
99
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
100
			document.open();
101
			PdfPTable table = new PdfPTable(1);
102
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
103
			table.getDefaultCell().setPaddingBottom(10.0f);
104
 
744 chandransh 105
			String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();
676 chandransh 106
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
107
			logoCell.setBorder(Rectangle.NO_BORDER);
108
 
109
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
110
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
111
			PdfPCell addressCell = new PdfPCell();
112
			addressCell.addElement(addressParagraph);
113
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
114
			addressCell.setBorder(Rectangle.NO_BORDER);
115
 
116
			PdfPTable ordersTable = new PdfPTable(8);
117
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
118
			ordersTable.addCell(new Phrase("Order No", helvetica8));
119
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
120
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
121
			ordersTable.addCell(new Phrase("Name", helvetica8));
122
			ordersTable.addCell(new Phrase("Destination City", helvetica8));
123
			ordersTable.addCell(new Phrase("Pincode", helvetica8));
124
			ordersTable.addCell(new Phrase("State", helvetica8));
125
 
126
			int serialNo = 0;
127
			for(int i=0; i < orders.size();i++){
128
				Order order = orders.get(i);
4788 rajveer 129
				if(!orderIds.contains(order.getId()))
130
					continue;
676 chandransh 131
				if(order.getLogistics_provider_id()!=providerId)
132
					continue;
5554 rajveer 133
				if(order.isLogisticsCod() != isCod)
3062 chandransh 134
	                continue;
676 chandransh 135
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
136
				serialNo++;
137
				List<LineItem> lineItems = order.getLineitems();
138
				double weight = 0;
139
				for(LineItem lineItem: lineItems)
140
					weight += lineItem.getTotal_weight();
141
 
142
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
143
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
144
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 145
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
676 chandransh 146
				ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
147
				ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
148
				ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
149
				ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));				
150
			}
151
 
152
			table.addCell(logoCell);
153
			table.addCell(addressCell);
3062 chandransh 154
			if(isCod)
155
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
156
			else
157
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
676 chandransh 158
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
159
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
160
			table.addCell(ordersTable);
161
			table.setWidthPercentage(90.0f);
162
 
163
			document.add(table);  
164
			document.close();
165
			baosPDF.close();
166
		} catch (DocumentException e) {
3062 chandransh 167
		    logger.error("Error while creating the manifest file", e);
676 chandransh 168
		} catch (MalformedURLException e) {
3062 chandransh 169
		    logger.error("Error while creating the manifest file", e);
676 chandransh 170
		} catch (IOException e) {
3062 chandransh 171
			logger.error("Error while creating the manifest file", e);
676 chandransh 172
		}
173
 
174
		return baosPDF;
175
	}
5678 rajveer 176
 
744 chandransh 177
 
5678 rajveer 178
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds) {
179
		ByteArrayOutputStream baosPDF = null;
180
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
181
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
182
 
183
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
184
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
185
 
186
		List<Order> orders = null;
187
		PickupStore store = null;
188
		Provider provider = null;
189
		try {
190
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0);
191
			store = logisticsClient.getPickupStore(storeId);
192
			provider = logisticsClient.getProvider(providerId);
193
		} catch (TException e) {
194
		    logger.error("Error getting information from one of the Thrift Services: ", e);
195
			return baosPDF;
196
		} catch (LogisticsServiceException e) {
197
		    logger.error("Error getting provider info from the logistics service: ", e);
198
			return baosPDF;
199
		} catch (TransactionServiceException e) {
200
		    logger.error("Error getting orders from the transaction service: ", e);
201
			return baosPDF;
202
		}
203
 
204
		try {
205
			baosPDF = new ByteArrayOutputStream();
206
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
207
			Document document = new Document();
208
			PdfWriter.getInstance(document, baosPDF);
209
			document.addAuthor("shop2020");
210
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
211
			document.open();
212
			PdfPTable table = new PdfPTable(1);
213
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
214
			table.getDefaultCell().setPaddingBottom(10.0f);
215
 
216
			String addressString =  store.getName() + "\nPIN " + store.getPin()+ "\n\n";
217
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
218
			PdfPCell addressCell = new PdfPCell();
219
			addressCell.addElement(addressParagraph);
220
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
221
			addressCell.setBorder(Rectangle.NO_BORDER);
222
 
223
			PdfPTable ordersTable = new PdfPTable(4);
224
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
225
			ordersTable.addCell(new Phrase("Order No", helvetica8));
226
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
227
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
228
 
229
			int serialNo = 0;
230
			for(int i=0; i < orders.size();i++){
231
				Order order = orders.get(i);
232
				if(!orderIds.contains(order.getId()))
233
					continue;
234
				if(order.getLogistics_provider_id()!=providerId)
235
					continue;
236
				if(order.getPickupStoreId()!=storeId)
237
					continue;
238
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
239
				serialNo++;
240
				List<LineItem> lineItems = order.getLineitems();
241
				double weight = 0;
242
				for(LineItem lineItem: lineItems)
243
					weight += lineItem.getTotal_weight();
244
 
245
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
246
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
247
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
248
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
249
			}
250
 
251
			table.addCell(addressCell);
252
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
253
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
254
			table.addCell(ordersTable);
255
			table.setWidthPercentage(90.0f);
256
 
257
			document.add(table);  
258
			document.close();
259
			baosPDF.close();
260
		} catch (DocumentException e) {
261
		    logger.error("Error while creating the manifest file", e);
262
		} catch (MalformedURLException e) {
263
		    logger.error("Error while creating the manifest file", e);
264
		} catch (IOException e) {
265
			logger.error("Error while creating the manifest file", e);
266
		}
267
 
268
		return baosPDF;
269
	}
270
 
744 chandransh 271
	public static void main(String[] args) throws IOException {
272
		ManifestGenerator manifestGenerator = new ManifestGenerator();
4788 rajveer 273
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null);
744 chandransh 274
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
275
		FileOutputStream fos = new FileOutputStream(f);
276
		baos.writeTo(fos);
277
	}
676 chandransh 278
}