Subversion Repositories SmartDukaan

Rev

Rev 4616 | Rev 4734 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4616 Rev 4715
Line 6... Line 6...
6
import java.io.FileNotFoundException;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
7
import java.io.IOException;
8
import java.text.DateFormat;
8
import java.text.DateFormat;
9
import java.text.ParseException;
9
import java.text.ParseException;
10
import java.text.SimpleDateFormat;
10
import java.text.SimpleDateFormat;
-
 
11
import java.util.ArrayList;
11
import java.util.Date;
12
import java.util.Date;
12
import java.util.HashMap;
13
import java.util.HashMap;
13
import java.util.List;
14
import java.util.List;
14
import java.util.Map;
15
import java.util.Map;
15
 
16
 
Line 48... Line 49...
48
import org.apache.struts2.interceptor.ServletRequestAware;
49
import org.apache.struts2.interceptor.ServletRequestAware;
49
import org.apache.struts2.interceptor.ServletResponseAware;
50
import org.apache.struts2.interceptor.ServletResponseAware;
50
import org.apache.struts2.rest.DefaultHttpHeaders;
51
import org.apache.struts2.rest.DefaultHttpHeaders;
51
import org.apache.struts2.rest.HttpHeaders;
52
import org.apache.struts2.rest.HttpHeaders;
52
import org.apache.struts2.util.ServletContextAware;
53
import org.apache.struts2.util.ServletContextAware;
-
 
54
import org.apache.thrift.TApplicationException;
53
import org.apache.thrift.TException;
55
import org.apache.thrift.TException;
54
import org.apache.thrift.transport.TTransportException;
56
import org.apache.thrift.transport.TTransportException;
55
import org.slf4j.Logger;
57
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
58
import org.slf4j.LoggerFactory;
57
 
59
 
-
 
60
import com.opensymphony.xwork2.ValidationAwareSupport;
-
 
61
 
58
/**
62
/**
59
 * @author Varun Gupta
63
 * @author Varun Gupta
60
 * @description: This class handles web requests to generate vendor specific reconciliation reports
64
 * @description: This class handles web requests to generate vendor specific reconciliation reports
61
 *               for given vendor for a given date range
65
 *               for given vendor for a given date range
62
 */
66
 */
63
 
67
 
64
@InterceptorRefs({
68
//@InterceptorRefs({
65
    @InterceptorRef("defaultStack"),
69
//    @InterceptorRef("defaultStack"),
66
    @InterceptorRef("login")
70
//    @InterceptorRef("login")
67
})
71
//})
68
@Results({
72
//@Results({
69
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
73
//    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
70
})
74
//})
71
public class VendorReconciliationController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
75
public class VendorReconciliationController extends ValidationAwareSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {
72
 
76
 
73
    private static Logger logger = LoggerFactory.getLogger(VendorReconciliationController.class);
77
    private static Logger logger = LoggerFactory.getLogger(VendorReconciliationController.class);
74
    
78
    
75
	private enum OrderReportColumn {
79
	private enum OrderReportColumn {
76
	    ORDER_ID(0),
80
	    ORDER_ID(0),
Line 93... Line 97...
93
		PAYMENT_TYPE(17),
97
		PAYMENT_TYPE(17),
94
		PAYMENT_ID(18),
98
		PAYMENT_ID(18),
95
		COUPON_CODE(19),
99
		COUPON_CODE(19),
96
		SERVICE_TAX(20),
100
		SERVICE_TAX(20),
97
		OTHER_CHARGES(21),
101
		OTHER_CHARGES(21),
98
		NET_COLLECTION(22);
102
		NET_COLLECTION(22),
99
 
103
	    REFUND(23),
-
 
104
	    FINAL_AMOUNT(24),
-
 
105
	    IS_RESHIPED_ORDER(25);
100
//		COUPON_CATEGORY(19),
106
//		COUPON_CATEGORY(19),
101
//		DISCOUNT(20),
107
//		DISCOUNT(20),
102
	    
108
	    
103
		private int value;
109
		private int value;
104
		
110
		
Line 129... Line 135...
129
	private HttpServletResponse response;
135
	private HttpServletResponse response;
130
	private ServletContext context;
136
	private ServletContext context;
131
    private HttpSession session;
137
    private HttpSession session;
132
	private List<Vendor> vendors;
138
	private List<Vendor> vendors;
133
	private String reportSource;
139
	private String reportSource;
-
 
140
	private List<Long> orderIdsWithoutSuccess;
134
	
141
	
135
	private File hdfcSettlementReport;
142
	private File hdfcSettlementReport;
136
	
143
	
137
	private File ebsSettlementReport;
144
	private File ebsSettlementReport;
138
	private File ebsSettlementSummary;
145
	private File ebsSettlementSummary;
139
	
146
	
140
	private String bluedartSettlementDate;
147
	private String bluedartSettlementDate;
141
	private File bluedartSettlementReport;
148
	private File bluedartSettlementReport;
142
	
149
	
-
 
150
	private String aramexSettlementDate;
-
 
151
	private File aramexSettlementReport;
-
 
152
	
143
	private Map<Long, Map<String, Double>> misMatches = new HashMap<Long, Map<String, Double>>();
153
	private Map<Long, Map<String, Double>> misMatches = new HashMap<Long, Map<String, Double>>();
144
	private Map<Long, String> ebsSettlementSummaries;
154
	private Map<Long, String> ebsSettlementSummaries;
145
	
155
	
146
	private final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
156
	private final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
147
	
157
	
Line 149... Line 159...
149
		try	{
159
		try	{
150
			CatalogClient csc = new CatalogClient();
160
			CatalogClient csc = new CatalogClient();
151
			Client catalogClient = csc.getClient();
161
			Client catalogClient = csc.getClient();
152
			
162
			
153
			vendors = catalogClient.getAllVendors();
163
			vendors = catalogClient.getAllVendors();
-
 
164
			orderIdsWithoutSuccess = new ArrayList<Long>();
154
 
165
			
155
			TransactionClient tsc = new TransactionClient();
166
			TransactionClient tsc = new TransactionClient();
156
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
167
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
157
			
168
			
158
			ebsSettlementSummaries = transactionClient.getEBSSettlementSummaries();
169
			ebsSettlementSummaries = transactionClient.getEBSSettlementSummaries();
159
			System.out.println(ebsSettlementSummaries);
170
			logger.info("", ebsSettlementSummaries);
-
 
171
			
-
 
172
		} catch (TException e) {
-
 
173
			logger.error("TException", e);
160
			
174
			
161
		} catch (Exception e) {
175
		} catch (TransactionServiceException e) {
162
			// TODO: handle exception
176
			logger.error("TransactionServiceException", e);
163
		}
177
		}
164
	}
178
	}
165
	
179
	
166
	public String index()	{
180
	public String index()	{
167
		return "report";
181
		return "report";
168
	}
182
	}
169
	
183
	
170
	public HttpHeaders create()	{
184
	public HttpHeaders create()	{
171
		
185
		
172
		String formType = request.getParameter("formtype");
186
		String formType = request.getParameter("formtype");
173
		System.out.println(formType);
187
		logger.info("Form Type: " + formType);
174
		
188
		
175
		if (formType.equals("uploadEBSSettlementSummary")) {
189
		if (formType.equals("uploadEBSSettlementSummary")) {
176
			uploadEBSSettlementSummary();
190
			uploadEBSSettlementSummary();
177
			
191
			
178
		} else if (formType.equals("uploadEBSSettlements")) {
192
		} else if (formType.equals("uploadEBSSettlements")) {
Line 181... Line 195...
181
		} else if (formType.equals("uploadHDFCSettlements")) {
195
		} else if (formType.equals("uploadHDFCSettlements")) {
182
			uploadHDFCSettlements();
196
			uploadHDFCSettlements();
183
			
197
			
184
		} else if (formType.equals("uploadBluedartSettlements")) {
198
		} else if (formType.equals("uploadBluedartSettlements")) {
185
			uploadBluedartSettlements();
199
			uploadBluedartSettlements();
-
 
200
			
-
 
201
		} else if (formType.equals("uploadAramexSettlements")) {
-
 
202
			uploadAramexSettlements();
186
		}
203
		}
-
 
204
	    logger.info("Order Ids where no successful payment was found", orderIdsWithoutSuccess);
-
 
205
	    
187
		return new DefaultHttpHeaders("report");
206
		return new DefaultHttpHeaders("report");
188
	}
207
	}
189
	
208
	
190
	public HttpHeaders generateReconciliationReport()	{
209
	public HttpHeaders generateReconciliationReport()	{
191
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
210
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Line 197... Line 216...
197
			Date endDate = dateFormat.parse(request.getParameter("end"));
216
			Date endDate = dateFormat.parse(request.getParameter("end"));
198
		    long vendorId = Long.parseLong(request.getParameter("vendor"));
217
		    long vendorId = Long.parseLong(request.getParameter("vendor"));
199
			
218
			
200
		    logger.info(startDate + " " + endDate + " " + vendorId);
219
		    logger.info(startDate + " " + endDate + " " + vendorId);
201
			
220
			
202
			CatalogClient csc = new CatalogClient();
-
 
203
			Client catalogClient= csc.getClient();
-
 
204
			vendors = catalogClient.getAllVendors();
-
 
205
			
-
 
206
			TransactionClient tsc = new TransactionClient();
221
			TransactionClient tsc = new TransactionClient();
207
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
222
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
208
			
223
			
209
			List<Order> orders = transactionClient.getBilledOrdersForVendor(vendorId, startDate.getTime(), endDate.getTime());
224
			List<Order> orders = transactionClient.getBilledOrdersForVendor(vendorId, startDate.getTime(), endDate.getTime());
210
			logger.info("Order count: " + orders.size());
225
			logger.info("Order count: " + orders.size());
211
			DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
226
			DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
212
			String vendorName = getVendorName(vendorId);
227
			String vendorName = getVendorName(vendorId);
213
 
228
			
214
			response.setContentType("application/vnd.ms-excel");
229
			response.setContentType("application/vnd.ms-excel");
215
			response.setHeader("Content-disposition", "inline; filename=" + vendorName + "-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
230
			response.setHeader("Content-disposition", "inline; filename=" + vendorName + "-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
216
			
231
			
217
			ServletOutputStream sos;
232
			ServletOutputStream sos;
218
			try {
233
			try {
Line 337... Line 352...
337
                	paymentIdStr = row.getCell(indexPaymentId).getStringCellValue().replace("'", "");
352
                	paymentIdStr = row.getCell(indexPaymentId).getStringCellValue().replace("'", "");
338
                	paymentId = Long.parseLong(paymentIdStr);
353
                	paymentId = Long.parseLong(paymentIdStr);
339
                	
354
                	
340
                	logger.info("paymentId: " + paymentId + ", recfmt: " + recfmt + ", settlementDate: " + settlementDate + ", netCollection: " + netCollection);
355
                	logger.info("paymentId: " + paymentId + ", recfmt: " + recfmt + ", settlementDate: " + settlementDate + ", netCollection: " + netCollection);
341
                	
356
                	
342
                    if (recfmt.equalsIgnoreCase("CVD"))	{
357
                    if (recfmt.trim().equalsIgnoreCase("CVD"))	{
343
                    	if (netCollection > 0)	netCollection *= -1;
358
                    	if (netCollection > 0.0)	netCollection *= -1.0;
344
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, 0, 0, netCollection);
359
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, 0.0, 0.0, netCollection);
345
                    	
360
                    	
346
                    } else	{
361
                    } else	{
347
                    	double msf = row.getCell(indexMSF).getNumericCellValue();
362
                    	double msf = row.getCell(indexMSF).getNumericCellValue();
348
                    	double serviceTax = row.getCell(indexServiceTax).getNumericCellValue();
363
                    	double serviceTax = row.getCell(indexServiceTax).getNumericCellValue();
349
                    	double eduCess = row.getCell(indexEduCess).getNumericCellValue();
364
                    	double eduCess = row.getCell(indexEduCess).getNumericCellValue();
Line 478... Line 493...
478
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
493
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
479
			psc = new PaymentClient();
494
			psc = new PaymentClient();
480
	    	in.shop2020.payments.PaymentService.Client paymentClient = psc.getClient();
495
	    	in.shop2020.payments.PaymentService.Client paymentClient = psc.getClient();
481
        	
496
        	
482
        	for (Row row : sheet) {
497
        	for (Row row : sheet) {
-
 
498
        		Order order = null;
483
        		try	{
499
        		try	{
484
        			if(row.equals(firstRow))	continue;
500
        			if(row.equals(firstRow))	continue;
485
        			long awbNumber = (long) row.getCell(indexAWBno).getNumericCellValue();
501
        			long awbNumber = (long) row.getCell(indexAWBno).getNumericCellValue();
-
 
502
        			
486
        			double amount = row.getCell(indexAmount).getNumericCellValue();
503
        			double amount = row.getCell(indexAmount).getNumericCellValue();
487
//        			mapAWBAndAmount.put(awbNumber, amount);
-
 
488
        			Order order = transactionClient.getOrderForAwb(Long.toString(awbNumber));
504
        			order = transactionClient.getOrderForAwb(Long.toString(awbNumber));
489
        			Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
505
        			Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
490
        			logger.info("settlementDate: " + settlementDate + ", PaymentId: " + payment.getPaymentId() + ", Amount: " + amount);
506
        			logger.info("settlementDate: " + settlementDate + ", PaymentId: " + payment.getPaymentId() + ", Amount: " + amount);
491
        			
507
        			
492
        			transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0, amount);
508
        			transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0, amount);
493
        		} catch (NullPointerException e) {
509
        		} catch (NullPointerException e) {
Line 496... Line 512...
496
        		} catch (PaymentException e) {
512
        		} catch (PaymentException e) {
497
        			logger.error("" + e);
513
        			logger.error("" + e);
498
        			
514
        			
499
				} catch (TransactionServiceException e) {
515
				} catch (TransactionServiceException e) {
500
					logger.error("" + e);
516
					logger.error("" + e);
-
 
517
					
-
 
518
				} catch (TApplicationException e)	{
-
 
519
					orderIdsWithoutSuccess.add(order.getId());
-
 
520
					
-
 
521
					logger.error("Application Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
-
 
522
					addActionError("Application Exception while getting payment for transaction Id, " + order.getTransactionId());
501
				}
523
				}
502
        	}
524
        	}
503
        } catch (ParseException e) {
525
        } catch (ParseException e) {
504
        	logger.error("Could not parse " + bluedartSettlementDate + " " + e);
526
        	logger.error("Could not parse " + bluedartSettlementDate + " " + e);
505
		} catch (TTransportException e) {
527
		} catch (TTransportException e) {
506
			logger.error("" + e);
528
			logger.error("" + e);
507
        } catch (TException e) {
529
        } catch (TException e) {
508
        	logger.error("" + e);
530
        	logger.error("" + e);
509
        }
531
        }
-
 
532
	}
-
 
533
	
-
 
534
	public void uploadAramexSettlements()	{
510
//        Map<Long, Double> mapAWBAndAmount = new HashMap<Long, Double>();
535
		logger.info("Uploading Aramex Settlements" + this.aramexSettlementDate);
-
 
536
		
-
 
537
		if(this.aramexSettlementDate.equals(""))	{
-
 
538
			addActionError("Settlement date cannot be left blank");
511
//        
539
			return;
-
 
540
		}
-
 
541
		
512
//        for (Row row : sheet) {
542
		Workbook wb = null;
513
//        	try	{
543
        try {
-
 
544
            wb = new HSSFWorkbook(new FileInputStream(this.aramexSettlementReport));
514
//                if(row.equals(firstRow))	continue;
545
        } catch (FileNotFoundException e) {
515
//                long awbNumber = (long) row.getCell(indexAWBno).getNumericCellValue();
546
            logger.error("Unable to open the Settlement report", e);
-
 
547
        } catch (IOException e) {
516
//                double amount = row.getCell(indexAmount).getNumericCellValue();
548
            logger.error("Unable to open the Settlement report", e);
-
 
549
        } catch (NullPointerException e) {
517
//                mapAWBAndAmount.put(awbNumber, amount);
550
        	addActionError(e.getMessage());
-
 
551
        	logger.error("Unable to open the Settlement report", e);
-
 
552
        	return;
-
 
553
		}
518
//        	
554
        
519
//        	} catch (NullPointerException e) {
555
        Sheet sheet = wb.getSheetAt(0);
-
 
556
        
520
//        		logger.error("" + e);
557
        int indexOrderId = 0;
521
//        	}
558
        int indexAmount = 4;
522
//        }
559
        
-
 
560
        Row firstRow = sheet.getRow(0);
-
 
561
        logger.info("Cell count:" + firstRow.getPhysicalNumberOfCells());
523
//        
562
        
524
//		TransactionClient tsc;
563
        TransactionClient tsc;
-
 
564
        PaymentClient psc;
-
 
565
        
525
//		try {
566
        try	{
-
 
567
			long settlementDate = DATE_FORMAT.parse(aramexSettlementDate).getTime();
-
 
568
			
526
//			tsc = new TransactionClient();
569
        	tsc = new TransactionClient();
527
//			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
570
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
-
 
571
			psc = new PaymentClient();
-
 
572
	    	in.shop2020.payments.PaymentService.Client paymentClient = psc.getClient();
-
 
573
        	
-
 
574
	    	for (Row row: sheet)	{
-
 
575
	    		if(row.equals(firstRow))	continue;
-
 
576
	    		
-
 
577
	    		long orderId = (long) row.getCell(indexOrderId).getNumericCellValue();
-
 
578
	    		double amount = row.getCell(indexAmount).getNumericCellValue();
-
 
579
	    		Order order = null;
-
 
580
	    		
-
 
581
	    		try {
-
 
582
					order = transactionClient.getOrder(orderId);
-
 
583
					Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
-
 
584
					transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0.0, amount);
528
//			
585
					
-
 
586
				} catch (TransactionServiceException e) {
529
//	        transactionClient.saveBluedartSettlements(mapAWBAndAmount);
587
					logger.error("Transaction Service Exception while getting order for id, " + orderId, e);
-
 
588
					addActionError("Transaction Service Exception while getting order for id, " + orderId);
-
 
589
					
530
//		} catch (TTransportException e) {
590
				} catch (PaymentException e) {
-
 
591
					logger.error("Payment Service Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
-
 
592
					addActionError("Payment Service Exception while getting payment for transaction Id, " + order.getTransactionId());
-
 
593
					
-
 
594
				} catch (TApplicationException e)	{
531
//			logger.error("" + e);
595
					orderIdsWithoutSuccess.add(orderId);
-
 
596
					logger.error("Application Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
-
 
597
					addActionError("Application Exception while getting payment for transaction Id, " + order.getTransactionId());
-
 
598
				}
-
 
599
	    	}
-
 
600
        } catch (ParseException e) {
-
 
601
        	addActionError("Could not parse " + aramexSettlementDate);
-
 
602
        	logger.error("Could not parse " + aramexSettlementDate + " " + e);
-
 
603
        	return;
532
//		} catch (TransactionServiceException e) {
604
        } catch (TTransportException e) {
533
//			logger.error("" + e);
605
        	logger.error("" + e);
534
//		} catch (TException e) {
606
        } catch (TException e) {
535
//			logger.error("" + e);
607
        	logger.error("" + e);
536
//		}
608
        }
537
	}
609
	}
538
	
610
	
539
	// Prepares the XLS worksheet object and fills in the data with proper formatting
611
	// Prepares the XLS worksheet object and fills in the data with proper formatting
540
	private ByteArrayOutputStream getReconciliationReport(List<Order> orders, Date startDate, Date endDate)	{
612
	private ByteArrayOutputStream getReconciliationReport(List<Order> orders, Date startDate, Date endDate)	{
541
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
613
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
Line 626... Line 698...
626
//	    headerRow.createCell(OrderReportColumn.COUPON_CATEGORY.getValue()).setCellValue("Coupon Category");
698
//	    headerRow.createCell(OrderReportColumn.COUPON_CATEGORY.getValue()).setCellValue("Coupon Category");
627
//	    headerRow.createCell(OrderReportColumn.DISCOUNT.getValue()).setCellValue("Discount");
699
//	    headerRow.createCell(OrderReportColumn.DISCOUNT.getValue()).setCellValue("Discount");
628
	    headerRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue("Service Tax");
700
	    headerRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue("Service Tax");
629
	    headerRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue("Other Charges");
701
	    headerRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue("Other Charges");
630
	    headerRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue("Net Collection");
702
	    headerRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue("Net Collection");
-
 
703
	    headerRow.createCell(OrderReportColumn.REFUND.getValue()).setCellValue("Refund");
-
 
704
	    headerRow.createCell(OrderReportColumn.FINAL_AMOUNT.getValue()).setCellValue("Final Amount");
-
 
705
	    headerRow.createCell(OrderReportColumn.IS_RESHIPED_ORDER.getValue()).setCellValue("Is Reshipped Order");
631
	    
706
	    
632
	    sheet.createRow(serialNo ++);
707
	    sheet.createRow(serialNo ++);
633
	    
708
	    
634
		TransactionClient tsc;
709
		TransactionClient tsc;
635
    	PaymentClient psc;
710
    	PaymentClient psc;
Line 646... Line 721...
646
	    	
721
	    	
647
	    	for (PaymentGateway tPaymentGateway: tPaymentGateways)	{
722
	    	for (PaymentGateway tPaymentGateway: tPaymentGateways)	{
648
	    		paymentGateways.put(tPaymentGateway.getId(), tPaymentGateway.getName());
723
	    		paymentGateways.put(tPaymentGateway.getId(), tPaymentGateway.getName());
649
	    	}
724
	    	}
650
	    	
725
	    	
-
 
726
			List<PaymentSettlement> settlements = transactionClient.getSettlementsByDate(startDate.getTime(), endDate.getTime(), true);
-
 
727
			logger.info("Refunds: " + settlements.size());
-
 
728
			logger.info(settlements.toString());
-
 
729
			
-
 
730
			Map<Long, PaymentSettlement> refunds = new HashMap<Long, PaymentSettlement>();
-
 
731
			
-
 
732
			for (PaymentSettlement settlement: settlements)	{
-
 
733
				refunds.put(settlement.getPaymentId(), settlement);
-
 
734
			}
-
 
735
			
-
 
736
			List <Long> orderIds = new ArrayList<Long>();
-
 
737
			
651
			for(Order order : orders)	{
738
			for(Order order : orders)	{
-
 
739
				orderIds.add(order.getId());
-
 
740
			}
-
 
741
			
-
 
742
			List<Long> reshippedOrderIds = null;
-
 
743
			
-
 
744
			reshippedOrderIds = transactionClient.getReshippedOrderIds(orderIds);
-
 
745
			
-
 
746
			for(Order order : orders)	{
652
		    	Row contentRow = sheet.createRow(serialNo++);
747
			    logger.info("For order: " + order.getId());
653
		    	
748
		    	
654
			    LineItem lineItem = order.getLineitems().get(0);
749
			    LineItem lineItem = order.getLineitems().get(0);
655
			    double transferPrice = lineItem.getTransfer_price();
750
			    double transferPrice = lineItem.getTransfer_price();
-
 
751
			    Payment payment = null;
-
 
752
			    
-
 
753
			    try	{
656
			    Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
754
			    	payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
-
 
755
				    logger.info("getSuccessfulPaymentForTxnId: " + payment);
-
 
756
				    
-
 
757
			    } catch (TApplicationException e)	{
-
 
758
			    	orderIdsWithoutSuccess.add(order.getId());
-
 
759
			    	
-
 
760
					logger.error("Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
-
 
761
					addActionError("Exception while getting payment for transaction Id, " + order.getTransactionId());
-
 
762
				}
657
			    
763
			    
658
			    Transaction txn = transactionClient.getTransaction(order.getTransactionId());
764
			    Transaction txn = transactionClient.getTransaction(order.getTransactionId());
-
 
765
			    PaymentSettlement paymentSettlement = null;
-
 
766
			    String paymentType = null;
-
 
767
			    
-
 
768
			    if(payment != null)	{
659
			    PaymentSettlement paymentSettlement = transactionClient.getSettlementForPaymentId(payment.getPaymentId());
769
			    	paymentSettlement = transactionClient.getSettlementForPaymentId(payment.getPaymentId());
-
 
770
			    	paymentType = paymentGateways.get(payment.getGatewayId());
-
 
771
			    }
660
			    
772
			    
661
	            double totalSellingPrice = lineItem.getTotal_price();
773
	            double totalSellingPrice = lineItem.getTotal_price();
662
	            double serviceTax = paymentSettlement.getServiceTax();
774
	            double serviceTax = 0.0;
663
	            double otherCollectionCharges = paymentSettlement.getOtherCharges();
775
	            double otherCollectionCharges = 0.0;
664
	            double netCollection = paymentSettlement.getNetCollection();
776
	            double netCollection = 0.0;
665
	            
777
	            
-
 
778
	            if(paymentSettlement != null)	{
-
 
779
		            serviceTax = paymentSettlement.getServiceTax();
666
	            String paymentType = paymentGateways.get(payment.getGatewayId());
780
		            otherCollectionCharges = paymentSettlement.getOtherCharges();
-
 
781
		            netCollection = paymentSettlement.getNetCollection();
-
 
782
	            }
667
	            
783
	            
668
	            if (netCollection < 0.0)	{
784
	            if (netCollection < 0.0)	{
669
	            	paymentType = paymentType + "-REFUND";
785
	            	paymentType = paymentType + "-REFUND";
670
	            }
786
	            }
671
	            double expectedCollection = totalSellingPrice - otherCollectionCharges - serviceTax;
787
	            double expectedCollection = totalSellingPrice - otherCollectionCharges - serviceTax;
Line 676... Line 792...
676
	            	mismatch.put("expected", expectedCollection);
792
	            	mismatch.put("expected", expectedCollection);
677
	            	mismatch.put("actual", netCollection);
793
	            	mismatch.put("actual", netCollection);
678
	            	misMatches.put(order.getId(), mismatch);
794
	            	misMatches.put(order.getId(), mismatch);
679
	            }
795
	            }
680
 
796
 
-
 
797
		    	Row contentRow = sheet.createRow(serialNo++);
681
	            contentRow.createCell(OrderReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
798
	            contentRow.createCell(OrderReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
682
	            contentRow.createCell(OrderReportColumn.ORDER_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getCreated_timestamp())));
799
	            contentRow.createCell(OrderReportColumn.ORDER_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getCreated_timestamp())));
683
			    contentRow.createCell(OrderReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
800
			    contentRow.createCell(OrderReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
684
			    contentRow.createCell(OrderReportColumn.BILLING_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getBilling_timestamp())));
801
			    contentRow.createCell(OrderReportColumn.BILLING_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getBilling_timestamp())));
685
			    contentRow.createCell(OrderReportColumn.DELIVERY_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getDelivery_timestamp())));
802
			    contentRow.createCell(OrderReportColumn.DELIVERY_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getDelivery_timestamp())));
686
			    contentRow.createCell(OrderReportColumn.SETTLEMENT_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(paymentSettlement.getSettlementDate())));
-
 
687
			    contentRow.createCell(OrderReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
803
			    contentRow.createCell(OrderReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
688
			    contentRow.createCell(OrderReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
804
			    contentRow.createCell(OrderReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
689
			    contentRow.createCell(OrderReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
805
			    contentRow.createCell(OrderReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
690
			    contentRow.createCell(OrderReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
806
			    contentRow.createCell(OrderReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
691
			    contentRow.createCell(OrderReportColumn.QUANTITY.getValue()).setCellValue(lineItem.getQuantity());
807
			    contentRow.createCell(OrderReportColumn.QUANTITY.getValue()).setCellValue(lineItem.getQuantity());
692
			    contentRow.createCell(OrderReportColumn.UNIT_TRANSFER_PRICE.getValue()).setCellValue(transferPrice);
808
			    contentRow.createCell(OrderReportColumn.UNIT_TRANSFER_PRICE.getValue()).setCellValue(transferPrice);
693
			    contentRow.createCell(OrderReportColumn.UNIT_SELLING_PRICE.getValue()).setCellValue(lineItem.getUnit_price());
809
			    contentRow.createCell(OrderReportColumn.UNIT_SELLING_PRICE.getValue()).setCellValue(lineItem.getUnit_price());
694
			    contentRow.createCell(OrderReportColumn.TOTAL_TRANSFER_PRICE.getValue()).setCellValue(transferPrice * lineItem.getQuantity());
810
			    contentRow.createCell(OrderReportColumn.TOTAL_TRANSFER_PRICE.getValue()).setCellValue(transferPrice * lineItem.getQuantity());
695
			    contentRow.createCell(OrderReportColumn.TOTAL_SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
811
			    contentRow.createCell(OrderReportColumn.TOTAL_SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
696
			    contentRow.createCell(OrderReportColumn.CURRENT_STATUS.getValue()).setCellValue(order.getStatusDescription());
812
			    contentRow.createCell(OrderReportColumn.CURRENT_STATUS.getValue()).setCellValue(order.getStatusDescription());
-
 
813
 
-
 
814
		    	double refund = 0.0;
-
 
815
		    	
-
 
816
			    if (payment != null)	{
-
 
817
				    contentRow.createCell(OrderReportColumn.SETTLEMENT_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(paymentSettlement.getSettlementDate())));
697
			    contentRow.createCell(OrderReportColumn.GATEWAY_TRANSACTION_ID.getValue()).setCellValue(payment.getGatewayTxnId());
818
			    	contentRow.createCell(OrderReportColumn.GATEWAY_TRANSACTION_ID.getValue()).setCellValue(payment.getGatewayTxnId());
698
			    contentRow.createCell(OrderReportColumn.PAYMENT_TYPE.getValue()).setCellValue(paymentType);
819
			    	contentRow.createCell(OrderReportColumn.PAYMENT_TYPE.getValue()).setCellValue(paymentType);
699
			    contentRow.createCell(OrderReportColumn.PAYMENT_ID.getValue()).setCellValue(payment.getPaymentId());
820
			    	contentRow.createCell(OrderReportColumn.PAYMENT_ID.getValue()).setCellValue(payment.getPaymentId());
-
 
821
			    	
-
 
822
			    	if(refunds.containsKey(payment.getPaymentId()))	{
-
 
823
			    		refund = refunds.get(payment.getPaymentId()).getNetCollection();
-
 
824
			    		refunds.remove(payment.getPaymentId());
-
 
825
			    	}
-
 
826
			    	contentRow.createCell(OrderReportColumn.REFUND.getValue()).setCellValue(refund);
-
 
827
			    }
700
			    contentRow.createCell(OrderReportColumn.COUPON_CODE.getValue()).setCellValue(txn.getCoupon_code());
828
			    contentRow.createCell(OrderReportColumn.COUPON_CODE.getValue()).setCellValue(txn.getCoupon_code());
701
			    contentRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue(serviceTax);
829
			    contentRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue(serviceTax);
702
			    contentRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue(otherCollectionCharges);
830
			    contentRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue(otherCollectionCharges);
703
			    contentRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue(netCollection);
831
			    contentRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue(netCollection);
-
 
832
			    contentRow.createCell(OrderReportColumn.FINAL_AMOUNT.getValue()).setCellValue(netCollection - refund);
-
 
833
			    
-
 
834
			    boolean isReshippedOrder = reshippedOrderIds.contains(order.getId());
-
 
835
			    contentRow.createCell(OrderReportColumn.IS_RESHIPED_ORDER.getValue()).setCellValue(isReshippedOrder ? 1 : 0);
-
 
836
		    }
-
 
837
		    logger.info("Order Ids where no successful payment was found: " + orderIdsWithoutSuccess.size());
-
 
838
		    logger.info(orderIdsWithoutSuccess.toString());
-
 
839
		    logger.info("Pending Refunds: " + refunds);
-
 
840
		    logger.info("Reshipped Order Ids" + reshippedOrderIds);
-
 
841
 
-
 
842
	    	sheet.createRow(serialNo++);
-
 
843
	    	
-
 
844
		    // Refunds belonging to orders billed before the start date of supplied billing date range
-
 
845
		    for (long paymentId: refunds.keySet())	{
-
 
846
		    	Payment payment = paymentClient.getPayment(paymentId);
-
 
847
		    	Transaction txn = transactionClient.getTransaction(payment.getMerchantTxnId());
-
 
848
		    	PaymentSettlement paymentSettlement = refunds.get(paymentId);
-
 
849
		    	
-
 
850
		    	for (Order order: txn.getOrders())	{
-
 
851
			    	Row contentRow = sheet.createRow(serialNo++);
-
 
852
			    	
-
 
853
			    	LineItem lineItem = order.getLineitems().get(0);
-
 
854
				    double transferPrice = lineItem.getTransfer_price();
-
 
855
		            double totalSellingPrice = lineItem.getTotal_price();
-
 
856
		            String paymentType = paymentGateways.get(payment.getGatewayId()) + "-REFUND";
-
 
857
				    
-
 
858
			    	contentRow.createCell(OrderReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
-
 
859
		            contentRow.createCell(OrderReportColumn.ORDER_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getCreated_timestamp())));
-
 
860
				    contentRow.createCell(OrderReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
-
 
861
				    contentRow.createCell(OrderReportColumn.BILLING_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getBilling_timestamp())));
-
 
862
				    contentRow.createCell(OrderReportColumn.DELIVERY_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getDelivery_timestamp())));
-
 
863
				    contentRow.createCell(OrderReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
-
 
864
				    contentRow.createCell(OrderReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
-
 
865
				    contentRow.createCell(OrderReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
-
 
866
				    contentRow.createCell(OrderReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
-
 
867
				    contentRow.createCell(OrderReportColumn.QUANTITY.getValue()).setCellValue(lineItem.getQuantity());
-
 
868
				    contentRow.createCell(OrderReportColumn.UNIT_TRANSFER_PRICE.getValue()).setCellValue(transferPrice);
-
 
869
				    contentRow.createCell(OrderReportColumn.UNIT_SELLING_PRICE.getValue()).setCellValue(lineItem.getUnit_price());
-
 
870
				    contentRow.createCell(OrderReportColumn.TOTAL_TRANSFER_PRICE.getValue()).setCellValue(transferPrice * lineItem.getQuantity());
-
 
871
				    contentRow.createCell(OrderReportColumn.TOTAL_SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
-
 
872
				    contentRow.createCell(OrderReportColumn.CURRENT_STATUS.getValue()).setCellValue(order.getStatusDescription());
-
 
873
 
-
 
874
				    contentRow.createCell(OrderReportColumn.SETTLEMENT_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(paymentSettlement.getSettlementDate())));
-
 
875
			    	contentRow.createCell(OrderReportColumn.GATEWAY_TRANSACTION_ID.getValue()).setCellValue(payment.getGatewayTxnId());
-
 
876
			    	contentRow.createCell(OrderReportColumn.PAYMENT_TYPE.getValue()).setCellValue(paymentType);
-
 
877
			    	contentRow.createCell(OrderReportColumn.PAYMENT_ID.getValue()).setCellValue(payment.getPaymentId());
-
 
878
			    	contentRow.createCell(OrderReportColumn.REFUND.getValue()).setCellValue(paymentSettlement.getNetCollection());
-
 
879
				    
-
 
880
				    contentRow.createCell(OrderReportColumn.COUPON_CODE.getValue()).setCellValue(txn.getCoupon_code());
-
 
881
				    contentRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue(0.0);
-
 
882
				    contentRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue(0.0);
-
 
883
				    contentRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue(paymentSettlement.getNetCollection());
-
 
884
		    	}
704
		    }
885
		    }
-
 
886
		    
705
		} catch (TTransportException e) {
887
		} catch (TTransportException e) {
706
			logger.error("Transport exception", e);
888
			logger.error("TTransportException " + e);
707
		} catch (PaymentException e) {
889
		} catch (PaymentException e) {
708
			logger.error("Payment Service exception", e);
890
			logger.error("PaymentException " + e);
709
		} catch (TException e) {
891
		} catch (TException e) {
710
			logger.error("Thrift exception", e);
892
			logger.error("TException " + e);
711
		} catch (TransactionServiceException e) {
893
		} catch (TransactionServiceException e) {
712
			logger.error("Transaction Service exception", e);
894
			logger.error("TransactionServiceException " + e);
713
		}
895
		}
714
	}
896
	}
715
	
897
	
716
	public List<Vendor> getAllVendors()	{
898
	public List<Vendor> getAllVendors()	{
717
		return this.vendors;
899
		return this.vendors;
Line 777... Line 959...
777
    
959
    
778
    public void setBluedartSettlementReport(File bluedartSettlementReport)	{
960
    public void setBluedartSettlementReport(File bluedartSettlementReport)	{
779
    	this.bluedartSettlementReport = bluedartSettlementReport;
961
    	this.bluedartSettlementReport = bluedartSettlementReport;
780
    }
962
    }
781
    
963
    
-
 
964
    public String getAramexSettlementDate()	{
-
 
965
    	return this.aramexSettlementDate;
-
 
966
    }
-
 
967
    
-
 
968
    public void setAramexSettlementDate(String aramexSettlementDate)	{
-
 
969
    	this.aramexSettlementDate = aramexSettlementDate;
-
 
970
    }
-
 
971
    
-
 
972
    public void setAramexSettlementReport(File aramexSettlementReport) {
-
 
973
		this.aramexSettlementReport = aramexSettlementReport;
-
 
974
	}
-
 
975
    
-
 
976
	public File getAramexSettlementReport() {
-
 
977
		return aramexSettlementReport;
-
 
978
	}
-
 
979
	
782
	@Override
980
	@Override
783
    public void setServletContext(ServletContext context) {
981
     public void setServletContext(ServletContext context) {
784
        this.context = context;
982
        this.context = context;
785
    }
983
    }
786
 
984
 
787
    public String getServletContextPath() {
985
    public String getServletContextPath() {
788
        return context.getContextPath();
986
        return context.getContextPath();