Subversion Repositories SmartDukaan

Rev

Rev 13407 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
679 chandransh 1
package in.shop2020.support.services;
2
 
3044 chandransh 3
import in.shop2020.logistics.DeliveryType;
679 chandransh 4
import in.shop2020.logistics.LogisticsServiceException;
13276 manish.sha 5
import in.shop2020.logistics.PickupStore;
679 chandransh 6
import in.shop2020.logistics.Provider;
7792 anupam.sin 7
import in.shop2020.logistics.ProviderDetails;
5945 mandeep.dh 8
import in.shop2020.model.v1.inventory.InventoryServiceException;
9
import in.shop2020.model.v1.inventory.Warehouse;
679 chandransh 10
import in.shop2020.model.v1.order.LineItem;
11
import in.shop2020.model.v1.order.Order;
12
import in.shop2020.model.v1.order.OrderStatus;
13
import in.shop2020.model.v1.order.TransactionServiceException;
5945 mandeep.dh 14
import in.shop2020.thrift.clients.InventoryClient;
3125 rajveer 15
import in.shop2020.thrift.clients.LogisticsClient;
16
import in.shop2020.thrift.clients.TransactionClient;
679 chandransh 17
 
18
import java.io.ByteArrayOutputStream;
19
import java.io.FileNotFoundException;
20
import java.io.FileOutputStream;
21
import java.io.IOException;
4801 anupam.sin 22
import java.util.ArrayList;
679 chandransh 23
import java.util.Date;
13276 manish.sha 24
import java.util.HashMap;
679 chandransh 25
import java.util.List;
13276 manish.sha 26
import java.util.Map;
679 chandransh 27
 
28
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
29
import org.apache.poi.ss.usermodel.Cell;
30
import org.apache.poi.ss.usermodel.CellStyle;
31
import org.apache.poi.ss.usermodel.CreationHelper;
32
import org.apache.poi.ss.usermodel.Row;
33
import org.apache.poi.ss.usermodel.Sheet;
34
import org.apache.poi.ss.usermodel.Workbook;
35
import org.apache.thrift.TException;
3044 chandransh 36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
679 chandransh 38
 
39
public class CourierDetailsGenerator {
3044 chandransh 40
    private static Logger logger = LoggerFactory.getLogger(CourierDetailsGenerator.class);
41
 
3125 rajveer 42
	private TransactionClient tsc = null;
5945 mandeep.dh 43
	private InventoryClient isc = null;
3125 rajveer 44
	private LogisticsClient lsc = null;
679 chandransh 45
 
46
	public CourierDetailsGenerator(){
47
		try {
3125 rajveer 48
			tsc = new TransactionClient();
5945 mandeep.dh 49
			isc = new InventoryClient();
3125 rajveer 50
			lsc = new LogisticsClient();
679 chandransh 51
		} catch (Exception e) {
3044 chandransh 52
			logger.error("Error while initializing one of the thrift clients", e);
679 chandransh 53
		}
54
	}
55
 
3062 chandransh 56
	public ByteArrayOutputStream generateCourierDetails(long warehouseId, long providerId, boolean isCod){
679 chandransh 57
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
58
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
5945 mandeep.dh 59
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = isc.getClient();
679 chandransh 60
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
61
 
62
		List<Order> orders = null;
63
		Warehouse warehouse = null;
64
		Provider provider = null;
65
 
66
		try {
4801 anupam.sin 67
		    List<OrderStatus> statuses = new ArrayList<OrderStatus>();
68
            statuses.add(OrderStatus.BILLED);
69
			orders = txnClient.getAllOrders(statuses, 0L, new Date().getTime(), warehouseId);
679 chandransh 70
			warehouse = inventoryClient.getWarehouse(warehouseId);
71
			provider = logisticsClient.getProvider(providerId);
3044 chandransh 72
		} catch (TException e) {
73
		    logger.error("Error getting information from one of the Thrift Services: ", e);
679 chandransh 74
			return baosXLS;
75
		} catch (InventoryServiceException e) {
3044 chandransh 76
		    logger.error("Error getting warehouse info from the catalog service: ", e);
679 chandransh 77
			return baosXLS;
78
		} catch (LogisticsServiceException e) {
3044 chandransh 79
		    logger.error("Error getting provider info from the logistics service: ", e);
679 chandransh 80
			return baosXLS;
81
		} catch (TransactionServiceException e) {
3044 chandransh 82
		    logger.error("Error getting orders from the transaction service: ", e);
679 chandransh 83
			return baosXLS;
84
		}
85
 
86
	    Workbook wb = new HSSFWorkbook();
87
	    CreationHelper createHelper = wb.getCreationHelper();
88
	    Sheet sheet = wb.createSheet("new sheet");
89
 
90
	    CellStyle dateCellStyle = wb.createCellStyle();
91
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));
92
 
93
	    CellStyle weightStyle = wb.createCellStyle();
94
	    weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));
95
 
96
	    // Create the header row and put all the titles in it. Rows are 0 based.
97
	    Row headerRow = sheet.createRow((short)0);
98
	    headerRow.createCell(0).setCellValue("Sl No");
99
	    headerRow.createCell(1).setCellValue("AWB No");
100
	    headerRow.createCell(2).setCellValue("AWB Date");
13276 manish.sha 101
	    headerRow.createCell(3).setCellValue("Saholic Order Id");
679 chandransh 102
	    headerRow.createCell(4).setCellValue("Name");
103
	    headerRow.createCell(5).setCellValue("Address 1");
104
	    headerRow.createCell(6).setCellValue("Address 2");
105
	    headerRow.createCell(7).setCellValue("City");
106
	    headerRow.createCell(8).setCellValue("State");
107
	    headerRow.createCell(9).setCellValue("Pin Code");
108
	    headerRow.createCell(10).setCellValue("Telephone No 1");
109
	    headerRow.createCell(11).setCellValue("Telephone No 2");
110
	    headerRow.createCell(12).setCellValue("Paymode");
111
	    headerRow.createCell(13).setCellValue("Amount to be Collected");
112
	    headerRow.createCell(14).setCellValue("Shipment Value");
13276 manish.sha 113
	    headerRow.createCell(15).setCellValue("Packet Weight(in Kg)");
114
	    headerRow.createCell(16).setCellValue("Product Name");
115
	    headerRow.createCell(17).setCellValue("Pickup Location");
116
	    headerRow.createCell(18).setCellValue("Customer A/C Code");
679 chandransh 117
 
7792 anupam.sin 118
	    String accountNo = "";
119
	    DeliveryType dt =  DeliveryType.PREPAID;
120
        if (isCod) {
121
            dt = DeliveryType.COD;
122
        }
123
 
124
        for (ProviderDetails detail : provider.getDetails()) {
125
            if(in.shop2020.model.v1.inventory.WarehouseLocation.findByValue((int) detail.getLogisticLocation()) == warehouse.getLogisticsLocation() && detail.getDeliveryType() == dt) {
126
                accountNo = detail.getAccountNo();
127
            }
128
        }
679 chandransh 129
	    Date awbDate = new Date();
3107 chandransh 130
	    String location = removeNewLines(warehouse.getLocation());
131
 
13276 manish.sha 132
	    Map<String, List<Order>> logisticsTxnIdOrdersMap = new HashMap<String, List<Order>>(); 
133
	    Map<Long, String> itemNamesMap = new HashMap<Long, String>();
134
	    Map<String, Map<Long, Double>> logisticsTxnIdOrderQuantityMap = new HashMap<String, Map<Long, Double>>();
135
 
136
	    for(Order order:orders){
137
			if(order.getLogistics_provider_id()!=providerId)
138
				continue;
139
			if(order.isLogisticsCod() != isCod)
140
			    continue;
141
			LineItem lineItem = order.getLineitems().get(0);
142
			if(order.isSetLogisticsTransactionId()){
143
				if(logisticsTxnIdOrdersMap.containsKey(order.getLogisticsTransactionId())){
144
					List<Order> orderList = logisticsTxnIdOrdersMap.get(order.getLogisticsTransactionId());
145
					orderList.add(order);
146
					logisticsTxnIdOrdersMap.put(order.getLogisticsTransactionId(), orderList);
147
				} else{
148
					List<Order> orderList = new ArrayList<Order>();
149
					orderList.add(order);
150
					logisticsTxnIdOrdersMap.put(order.getLogisticsTransactionId(), orderList);
151
				}
152
				if(logisticsTxnIdOrderQuantityMap.containsKey(order.getLogisticsTransactionId())){
153
					Map<Long, Double> orderItemQuantityMap = logisticsTxnIdOrderQuantityMap.get(order.getLogisticsTransactionId());
13407 manish.sha 154
					if(orderItemQuantityMap.containsKey(lineItem.getItem_id())){
155
						double orderQuantity = orderItemQuantityMap.get(lineItem.getItem_id())+ lineItem.getQuantity();
156
						orderItemQuantityMap.put(lineItem.getItem_id(),orderQuantity);
157
						logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);
158
					}else{
159
						double orderQuantity = lineItem.getQuantity();
160
						orderItemQuantityMap.put(lineItem.getItem_id(),orderQuantity);
161
						logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);
162
					}
13276 manish.sha 163
				}else{
164
					Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();
165
					orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());
166
					logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);
167
				}
168
			}else{
169
				List<Order> orderList = new ArrayList<Order>();
170
				orderList.add(order);
171
				logisticsTxnIdOrdersMap.put(order.getId()+"", orderList);
172
				Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();
173
				orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());
174
				logisticsTxnIdOrderQuantityMap.put(order.getId()+"", orderItemQuantityMap);
175
			}
176
			if(!itemNamesMap.containsKey(lineItem.getItem_id())){
177
				itemNamesMap.put(lineItem.getItem_id(), getItemDisplayName(lineItem, false));
178
			}
179
	    }
180
 
679 chandransh 181
	    int serialNo = 0;
13276 manish.sha 182
	    for(String logisticsTxnId : logisticsTxnIdOrdersMap.keySet()){
183
	    	serialNo++;
184
	    	Row contentRow = sheet.createRow((short)serialNo);
185
	    	contentRow.createCell(0).setCellValue(serialNo);
186
	    	List<Order> ordersList = logisticsTxnIdOrdersMap.get(logisticsTxnId);
187
	    	double totalAmount = 0.0;
188
	    	double totalWeight = 0.0;
189
	    	for(Order o:ordersList){
17470 manish.sha 190
	    		totalAmount = totalAmount + (o.getTotal_amount()+o.getShippingCost()+o.getCodCharges()-o.getGvAmount());
13276 manish.sha 191
	    		totalWeight = totalWeight + o.getTotal_weight();
192
	    	}
193
	    	Order order = logisticsTxnIdOrdersMap.get(logisticsTxnId).get(0);
194
	    	contentRow.createCell(1).setCellValue(order.getAirwaybill_no());
195
	    	Cell awbDateCell = contentRow.createCell(2);
196
		    awbDateCell.setCellValue(awbDate);
197
		    awbDateCell.setCellStyle(dateCellStyle);
198
		    contentRow.createCell(3).setCellValue(logisticsTxnId);
199
		    if(order.getPickupStoreId() != 0){
200
		    	PickupStore store = null;
201
				try {
202
					store = logisticsClient.getPickupStore(order.getPickupStoreId());
203
				} catch (TException e) {
204
					// TODO Auto-generated catch block
205
					e.printStackTrace();
206
				}
207
		    	contentRow.createCell(4).setCellValue(getValueForEmptyString(store.getName()));
208
			    contentRow.createCell(5).setCellValue(getValueForEmptyString(store.getLine1()));
209
			    contentRow.createCell(6).setCellValue(getValueForEmptyString(store.getLine2()));
210
			    contentRow.createCell(7).setCellValue(getValueForEmptyString(store.getCity()));
211
			    contentRow.createCell(8).setCellValue(getValueForEmptyString(store.getState()));
212
			    contentRow.createCell(9).setCellValue(getValueForEmptyString(store.getPin()));
213
			    contentRow.createCell(10).setCellValue(getValueForEmptyString(store.getPhone()));
214
		    }else{
215
		    	contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));
216
			    contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));
217
			    contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));
218
			    contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));
219
			    contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));
220
			    contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));
221
			    contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));	
222
		    }
223
		    contentRow.createCell(11).setCellValue("-");
224
		    if(isCod){
225
		        contentRow.createCell(12).setCellValue("COD");
226
                contentRow.createCell(13).setCellValue(totalAmount);
227
		    } else {
228
		        contentRow.createCell(12).setCellValue("Prepaid");
229
                contentRow.createCell(13).setCellValue(0);
230
		    }
231
		    contentRow.createCell(14).setCellValue(totalAmount);
232
		    Cell weightCell = contentRow.createCell(15);
233
		    weightCell.setCellValue(totalWeight);
234
		    weightCell.setCellStyle(weightStyle);
235
		    StringBuffer productNameBuffer = new StringBuffer();
236
		    int skuSizeTxn = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet().size();
237
		    int count = 1;
238
		    for(Long itemId : logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet()){
239
		    	Map<Long, Double> quantityMap = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId);
240
		    	double quantity = quantityMap.get(itemId);
241
		    	productNameBuffer.append(itemNamesMap.get(itemId)+"("+quantity+")");
242
		    	if(count<skuSizeTxn){
243
		    		productNameBuffer.append(",");
244
		    	}
245
		    	count++;
246
		    }
247
		    contentRow.createCell(16).setCellValue(productNameBuffer.toString());
248
		    contentRow.createCell(17).setCellValue(location);
249
		    contentRow.createCell(18).setCellValue(accountNo);
250
 
251
	    }
252
	    /*for(int i = 0; i<orders.size(); i++){
679 chandransh 253
	    	Order order = orders.get(i);
254
			if(order.getLogistics_provider_id()!=providerId)
255
				continue;
5554 rajveer 256
			if(order.isLogisticsCod() != isCod)
3062 chandransh 257
			    continue;
679 chandransh 258
			serialNo++;
259
	    	Row contentRow = sheet.createRow((short)serialNo);
260
		    contentRow.createCell(0).setCellValue(serialNo);
261
		    contentRow.createCell(1).setCellValue(order.getAirwaybill_no());
262
		    Cell awbDateCell = contentRow.createCell(2);
263
		    awbDateCell.setCellValue(awbDate);
264
		    awbDateCell.setCellStyle(dateCellStyle);		    
265
		    contentRow.createCell(3).setCellValue(order.getId());
744 chandransh 266
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));
267
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));
268
		    contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));
269
		    contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));
270
		    contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));
271
		    contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));
272
		    contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));
734 chandransh 273
		    contentRow.createCell(11).setCellValue("-");
3062 chandransh 274
		    if(isCod){
275
		        contentRow.createCell(12).setCellValue("COD");
6318 rajveer 276
                contentRow.createCell(13).setCellValue(order.getTotal_amount()-order.getGvAmount());
3062 chandransh 277
		    } else {
278
		        contentRow.createCell(12).setCellValue("Prepaid");
279
                contentRow.createCell(13).setCellValue(0);
280
		    }
281
 
679 chandransh 282
		    List<LineItem> lineItems = order.getLineitems();
283
		    LineItem lineItem = lineItems.get(0);
6318 rajveer 284
		    contentRow.createCell(14).setCellValue(order.getTotal_amount()-order.getGvAmount());
679 chandransh 285
		    contentRow.createCell(15).setCellValue(lineItem.getId());
286
		    Cell weightCell = contentRow.createCell(16);
919 rajveer 287
		    weightCell.setCellValue(lineItem.getTotal_weight());
679 chandransh 288
		    weightCell.setCellStyle(weightStyle);
289
		    contentRow.createCell(17).setCellValue(lineItem.getBrand() + " " + lineItem.getModel_number() + " " + lineItem.getModel_name() + " " + lineItem.getColor());
3107 chandransh 290
		    contentRow.createCell(18).setCellValue(location);
3044 chandransh 291
		    contentRow.createCell(19).setCellValue(accountNo);
13276 manish.sha 292
	    }*/
679 chandransh 293
 
294
		// Write the workbook to the output stream
295
		try {
296
			wb.write(baosXLS);
297
			baosXLS.close();
298
		} catch (IOException e) {
3044 chandransh 299
			logger.error("Exception while creating the Courier Details report", e);
679 chandransh 300
		}
301
 
302
		return baosXLS;
303
	}
744 chandransh 304
 
305
	private String getValueForEmptyString(String s){
306
		if(s==null || s.equals(""))
307
			return "-";
308
		else
309
			return s; 
310
	}
311
 
3107 chandransh 312
	private String removeNewLines(String str){
313
	    return str.replace('\n', ' ');
314
	}
315
 
13276 manish.sha 316
	private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
317
		StringBuffer itemName = new StringBuffer();
318
		if(lineitem.getBrand()!= null)
319
			itemName.append(lineitem.getBrand() + " ");
320
		if(lineitem.getModel_name() != null)
321
			itemName.append(lineitem.getModel_name() + " ");
322
		if(lineitem.getModel_number() != null )
323
			itemName.append(lineitem.getModel_number() + " ");
324
		if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
325
			itemName.append("("+lineitem.getColor()+")");
326
		if(appendIMEI && lineitem.isSetSerial_number()){
327
			itemName.append("\nIMEI No. " + lineitem.getSerial_number());
328
		}
329
 
330
		return itemName.toString();
331
	}
332
 
679 chandransh 333
	/**
334
	 * @param args
335
	 */
336
	public static void main(String[] args) {
337
		System.out.println("Hey There");
338
		CourierDetailsGenerator g = new CourierDetailsGenerator();
339
		try {
744 chandransh 340
			FileOutputStream f = new FileOutputStream("/home/ashish/Downloads/courier-details.xls");
3062 chandransh 341
			ByteArrayOutputStream baosXLS = g.generateCourierDetails(1, 1, true);
679 chandransh 342
			baosXLS.writeTo(f);
343
			f.close();
344
		} catch (FileNotFoundException e) {
3107 chandransh 345
			logger.error("Error while creating the Courier Details report", e);
679 chandransh 346
		} catch (IOException e) {
3107 chandransh 347
			logger.error("IO error while writing the Courier Details report", e);
679 chandransh 348
		}
744 chandransh 349
		System.out.println("Successfully generated the detailed courier report");
679 chandransh 350
	}
351
 
352
}