Subversion Repositories SmartDukaan

Rev

Rev 7792 | Rev 13407 | Go to most recent revision | 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());
154
					double orderQuantity = orderItemQuantityMap.get(lineItem.getItem_id())+ lineItem.getQuantity();
155
					orderItemQuantityMap.put(lineItem.getItem_id(),orderQuantity);
156
					logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);
157
				}else{
158
					Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();
159
					orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());
160
					logisticsTxnIdOrderQuantityMap.put(order.getLogisticsTransactionId(), orderItemQuantityMap);
161
				}
162
			}else{
163
				List<Order> orderList = new ArrayList<Order>();
164
				orderList.add(order);
165
				logisticsTxnIdOrdersMap.put(order.getId()+"", orderList);
166
				Map<Long, Double> orderItemQuantityMap = new HashMap<Long, Double>();
167
				orderItemQuantityMap.put(lineItem.getItem_id(), lineItem.getQuantity());
168
				logisticsTxnIdOrderQuantityMap.put(order.getId()+"", orderItemQuantityMap);
169
			}
170
			if(!itemNamesMap.containsKey(lineItem.getItem_id())){
171
				itemNamesMap.put(lineItem.getItem_id(), getItemDisplayName(lineItem, false));
172
			}
173
	    }
174
 
679 chandransh 175
	    int serialNo = 0;
13276 manish.sha 176
	    for(String logisticsTxnId : logisticsTxnIdOrdersMap.keySet()){
177
	    	serialNo++;
178
	    	Row contentRow = sheet.createRow((short)serialNo);
179
	    	contentRow.createCell(0).setCellValue(serialNo);
180
	    	List<Order> ordersList = logisticsTxnIdOrdersMap.get(logisticsTxnId);
181
	    	double totalAmount = 0.0;
182
	    	double totalWeight = 0.0;
183
	    	for(Order o:ordersList){
184
	    		totalAmount = totalAmount + (o.getTotal_amount()-o.getGvAmount());
185
	    		totalWeight = totalWeight + o.getTotal_weight();
186
	    	}
187
	    	Order order = logisticsTxnIdOrdersMap.get(logisticsTxnId).get(0);
188
	    	contentRow.createCell(1).setCellValue(order.getAirwaybill_no());
189
	    	Cell awbDateCell = contentRow.createCell(2);
190
		    awbDateCell.setCellValue(awbDate);
191
		    awbDateCell.setCellStyle(dateCellStyle);
192
		    contentRow.createCell(3).setCellValue(logisticsTxnId);
193
		    if(order.getPickupStoreId() != 0){
194
		    	PickupStore store = null;
195
				try {
196
					store = logisticsClient.getPickupStore(order.getPickupStoreId());
197
				} catch (TException e) {
198
					// TODO Auto-generated catch block
199
					e.printStackTrace();
200
				}
201
		    	contentRow.createCell(4).setCellValue(getValueForEmptyString(store.getName()));
202
			    contentRow.createCell(5).setCellValue(getValueForEmptyString(store.getLine1()));
203
			    contentRow.createCell(6).setCellValue(getValueForEmptyString(store.getLine2()));
204
			    contentRow.createCell(7).setCellValue(getValueForEmptyString(store.getCity()));
205
			    contentRow.createCell(8).setCellValue(getValueForEmptyString(store.getState()));
206
			    contentRow.createCell(9).setCellValue(getValueForEmptyString(store.getPin()));
207
			    contentRow.createCell(10).setCellValue(getValueForEmptyString(store.getPhone()));
208
		    }else{
209
		    	contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));
210
			    contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));
211
			    contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));
212
			    contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));
213
			    contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));
214
			    contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));
215
			    contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));	
216
		    }
217
		    contentRow.createCell(11).setCellValue("-");
218
		    if(isCod){
219
		        contentRow.createCell(12).setCellValue("COD");
220
                contentRow.createCell(13).setCellValue(totalAmount);
221
		    } else {
222
		        contentRow.createCell(12).setCellValue("Prepaid");
223
                contentRow.createCell(13).setCellValue(0);
224
		    }
225
		    contentRow.createCell(14).setCellValue(totalAmount);
226
		    Cell weightCell = contentRow.createCell(15);
227
		    weightCell.setCellValue(totalWeight);
228
		    weightCell.setCellStyle(weightStyle);
229
		    StringBuffer productNameBuffer = new StringBuffer();
230
		    int skuSizeTxn = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet().size();
231
		    int count = 1;
232
		    for(Long itemId : logisticsTxnIdOrderQuantityMap.get(logisticsTxnId).keySet()){
233
		    	Map<Long, Double> quantityMap = logisticsTxnIdOrderQuantityMap.get(logisticsTxnId);
234
		    	double quantity = quantityMap.get(itemId);
235
		    	productNameBuffer.append(itemNamesMap.get(itemId)+"("+quantity+")");
236
		    	if(count<skuSizeTxn){
237
		    		productNameBuffer.append(",");
238
		    	}
239
		    	count++;
240
		    }
241
		    contentRow.createCell(16).setCellValue(productNameBuffer.toString());
242
		    contentRow.createCell(17).setCellValue(location);
243
		    contentRow.createCell(18).setCellValue(accountNo);
244
 
245
	    }
246
	    /*for(int i = 0; i<orders.size(); i++){
679 chandransh 247
	    	Order order = orders.get(i);
248
			if(order.getLogistics_provider_id()!=providerId)
249
				continue;
5554 rajveer 250
			if(order.isLogisticsCod() != isCod)
3062 chandransh 251
			    continue;
679 chandransh 252
			serialNo++;
253
	    	Row contentRow = sheet.createRow((short)serialNo);
254
		    contentRow.createCell(0).setCellValue(serialNo);
255
		    contentRow.createCell(1).setCellValue(order.getAirwaybill_no());
256
		    Cell awbDateCell = contentRow.createCell(2);
257
		    awbDateCell.setCellValue(awbDate);
258
		    awbDateCell.setCellStyle(dateCellStyle);		    
259
		    contentRow.createCell(3).setCellValue(order.getId());
744 chandransh 260
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));
261
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));
262
		    contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));
263
		    contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));
264
		    contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));
265
		    contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));
266
		    contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));
734 chandransh 267
		    contentRow.createCell(11).setCellValue("-");
3062 chandransh 268
		    if(isCod){
269
		        contentRow.createCell(12).setCellValue("COD");
6318 rajveer 270
                contentRow.createCell(13).setCellValue(order.getTotal_amount()-order.getGvAmount());
3062 chandransh 271
		    } else {
272
		        contentRow.createCell(12).setCellValue("Prepaid");
273
                contentRow.createCell(13).setCellValue(0);
274
		    }
275
 
679 chandransh 276
		    List<LineItem> lineItems = order.getLineitems();
277
		    LineItem lineItem = lineItems.get(0);
6318 rajveer 278
		    contentRow.createCell(14).setCellValue(order.getTotal_amount()-order.getGvAmount());
679 chandransh 279
		    contentRow.createCell(15).setCellValue(lineItem.getId());
280
		    Cell weightCell = contentRow.createCell(16);
919 rajveer 281
		    weightCell.setCellValue(lineItem.getTotal_weight());
679 chandransh 282
		    weightCell.setCellStyle(weightStyle);
283
		    contentRow.createCell(17).setCellValue(lineItem.getBrand() + " " + lineItem.getModel_number() + " " + lineItem.getModel_name() + " " + lineItem.getColor());
3107 chandransh 284
		    contentRow.createCell(18).setCellValue(location);
3044 chandransh 285
		    contentRow.createCell(19).setCellValue(accountNo);
13276 manish.sha 286
	    }*/
679 chandransh 287
 
288
		// Write the workbook to the output stream
289
		try {
290
			wb.write(baosXLS);
291
			baosXLS.close();
292
		} catch (IOException e) {
3044 chandransh 293
			logger.error("Exception while creating the Courier Details report", e);
679 chandransh 294
		}
295
 
296
		return baosXLS;
297
	}
744 chandransh 298
 
299
	private String getValueForEmptyString(String s){
300
		if(s==null || s.equals(""))
301
			return "-";
302
		else
303
			return s; 
304
	}
305
 
3107 chandransh 306
	private String removeNewLines(String str){
307
	    return str.replace('\n', ' ');
308
	}
309
 
13276 manish.sha 310
	private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
311
		StringBuffer itemName = new StringBuffer();
312
		if(lineitem.getBrand()!= null)
313
			itemName.append(lineitem.getBrand() + " ");
314
		if(lineitem.getModel_name() != null)
315
			itemName.append(lineitem.getModel_name() + " ");
316
		if(lineitem.getModel_number() != null )
317
			itemName.append(lineitem.getModel_number() + " ");
318
		if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
319
			itemName.append("("+lineitem.getColor()+")");
320
		if(appendIMEI && lineitem.isSetSerial_number()){
321
			itemName.append("\nIMEI No. " + lineitem.getSerial_number());
322
		}
323
 
324
		return itemName.toString();
325
	}
326
 
679 chandransh 327
	/**
328
	 * @param args
329
	 */
330
	public static void main(String[] args) {
331
		System.out.println("Hey There");
332
		CourierDetailsGenerator g = new CourierDetailsGenerator();
333
		try {
744 chandransh 334
			FileOutputStream f = new FileOutputStream("/home/ashish/Downloads/courier-details.xls");
3062 chandransh 335
			ByteArrayOutputStream baosXLS = g.generateCourierDetails(1, 1, true);
679 chandransh 336
			baosXLS.writeTo(f);
337
			f.close();
338
		} catch (FileNotFoundException e) {
3107 chandransh 339
			logger.error("Error while creating the Courier Details report", e);
679 chandransh 340
		} catch (IOException e) {
3107 chandransh 341
			logger.error("IO error while writing the Courier Details report", e);
679 chandransh 342
		}
744 chandransh 343
		System.out.println("Successfully generated the detailed courier report");
679 chandransh 344
	}
345
 
346
}