Subversion Repositories SmartDukaan

Rev

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