Subversion Repositories SmartDukaan

Rev

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