Subversion Repositories SmartDukaan

Rev

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

Rev 4715 Rev 4734
Line 12... Line 12...
12
import java.util.Date;
12
import java.util.Date;
13
import java.util.HashMap;
13
import java.util.HashMap;
14
import java.util.List;
14
import java.util.List;
15
import java.util.Map;
15
import java.util.Map;
16
 
16
 
-
 
17
import in.shop2020.logistics.LogisticsServiceException;
-
 
18
import in.shop2020.logistics.Provider;
17
import in.shop2020.model.v1.catalog.InventoryService.Client;
19
import in.shop2020.model.v1.catalog.InventoryService.Client;
18
import in.shop2020.model.v1.catalog.Vendor;
20
import in.shop2020.model.v1.catalog.Vendor;
19
import in.shop2020.model.v1.order.LineItem;
21
import in.shop2020.model.v1.order.LineItem;
20
import in.shop2020.model.v1.order.Order;
22
import in.shop2020.model.v1.order.Order;
-
 
23
import in.shop2020.model.v1.order.OrderStatus;
21
import in.shop2020.model.v1.order.PaymentSettlement;
24
import in.shop2020.model.v1.order.PaymentSettlement;
22
import in.shop2020.model.v1.order.Transaction;
25
import in.shop2020.model.v1.order.Transaction;
23
import in.shop2020.model.v1.order.TransactionServiceException;
26
import in.shop2020.model.v1.order.TransactionServiceException;
24
import in.shop2020.payments.Payment;
27
import in.shop2020.payments.Payment;
25
import in.shop2020.payments.PaymentException;
28
import in.shop2020.payments.PaymentException;
26
import in.shop2020.payments.PaymentGateway;
29
import in.shop2020.payments.PaymentGateway;
27
import in.shop2020.thrift.clients.CatalogClient;
30
import in.shop2020.thrift.clients.CatalogClient;
-
 
31
import in.shop2020.thrift.clients.LogisticsClient;
28
import in.shop2020.thrift.clients.PaymentClient;
32
import in.shop2020.thrift.clients.PaymentClient;
29
import in.shop2020.thrift.clients.TransactionClient;
33
import in.shop2020.thrift.clients.TransactionClient;
30
 
34
 
31
import javax.servlet.ServletContext;
35
import javax.servlet.ServletContext;
32
import javax.servlet.ServletOutputStream;
36
import javax.servlet.ServletOutputStream;
Line 151... Line 155...
151
	private File aramexSettlementReport;
155
	private File aramexSettlementReport;
152
	
156
	
153
	private Map<Long, Map<String, Double>> misMatches = new HashMap<Long, Map<String, Double>>();
157
	private Map<Long, Map<String, Double>> misMatches = new HashMap<Long, Map<String, Double>>();
154
	private Map<Long, String> ebsSettlementSummaries;
158
	private Map<Long, String> ebsSettlementSummaries;
155
	
159
	
-
 
160
	private List<OrderStatus> refundOrderStatuses = new ArrayList<OrderStatus>();
-
 
161
	
156
	private final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
162
	private final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
157
	
163
	
158
	public VendorReconciliationController()	{
164
	public VendorReconciliationController()	{
159
		try	{
165
		try	{
160
			CatalogClient csc = new CatalogClient();
166
			CatalogClient csc = new CatalogClient();
Line 206... Line 212...
206
		return new DefaultHttpHeaders("report");
212
		return new DefaultHttpHeaders("report");
207
	}
213
	}
208
	
214
	
209
	public HttpHeaders generateReconciliationReport()	{
215
	public HttpHeaders generateReconciliationReport()	{
210
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
216
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
-
 
217
 
-
 
218
		refundOrderStatuses.add(OrderStatus.CANCELLED_ON_CUSTOMER_REQUEST);
-
 
219
		refundOrderStatuses.add(OrderStatus.RTO_REFUNDED);
-
 
220
		refundOrderStatuses.add(OrderStatus.DOA_VALID_REFUNDED);
-
 
221
		refundOrderStatuses.add(OrderStatus.DOA_INVALID_REFUNDED);
-
 
222
		refundOrderStatuses.add(OrderStatus.CANCELLED_DUE_TO_LOW_INVENTORY);
-
 
223
		refundOrderStatuses.add(OrderStatus.DOA_REFUNDED_RCVD_DAMAGED);
-
 
224
		refundOrderStatuses.add(OrderStatus.DOA_REFUNDED_LOST_IN_TRANSIT);
-
 
225
		refundOrderStatuses.add(OrderStatus.RTO_DAMAGED_REFUNDED);
-
 
226
		refundOrderStatuses.add(OrderStatus.RTO_LOST_IN_TRANSIT_REFUNDED);
-
 
227
		refundOrderStatuses.add(OrderStatus.RET_PRODUCT_USABLE_REFUNDED);
-
 
228
		refundOrderStatuses.add(OrderStatus.RET_PRODUCT_UNUSABLE_REFUNDED);
-
 
229
		refundOrderStatuses.add(OrderStatus.RET_REFUNDED_LOST_IN_TRANSIT);
-
 
230
		refundOrderStatuses.add(OrderStatus.RET_REFUNDED_RCVD_DAMAGED);
-
 
231
		refundOrderStatuses.add(OrderStatus.LOST_IN_TRANSIT_REFUNDED);
211
		
232
		
212
		try	{
233
		try	{
213
			//Formatting Form input parameters
234
			//Formatting Form input parameters
214
			
235
			
215
		    Date startDate = dateFormat.parse(request.getParameter("start"));
236
		    Date startDate = dateFormat.parse(request.getParameter("start"));
Line 316... Line 337...
316
		
337
		
317
		Workbook wb = null;
338
		Workbook wb = null;
318
		try {
339
		try {
319
			wb = new HSSFWorkbook(new FileInputStream(this.hdfcSettlementReport));
340
			wb = new HSSFWorkbook(new FileInputStream(this.hdfcSettlementReport));
320
		} catch (FileNotFoundException e) {
341
		} catch (FileNotFoundException e) {
321
			logger.error("Unable to open the Settlement report", e);
342
			String errMsg = "Unable to open the HDFC Payout report";
-
 
343
			logger.error(errMsg, e);
-
 
344
			addActionError(errMsg);
-
 
345
			return;
322
		} catch (IOException e) {
346
		} catch (IOException e) {
323
			logger.error("Unable to open the Settlement report", e);
347
			String errMsg = "Unable to open the HDFC Payout report";
-
 
348
			logger.error(errMsg, e);
-
 
349
			addActionError(errMsg);
-
 
350
			return;
324
		}
351
		}
325
		Sheet sheet = wb.getSheetAt(0);
352
		Sheet sheet = wb.getSheetAt(0);
326
		
353
		
327
		Row firstRow = sheet.getRow(0);
354
		Row firstRow = sheet.getRow(0);
328
 
355
 
Line 335... Line 362...
335
		int indexNetCollection = 17;
362
		int indexNetCollection = 17;
336
		
363
		
337
		TransactionClient tsc;
364
		TransactionClient tsc;
338
		String paymentIdStr = "";
365
		String paymentIdStr = "";
339
		long paymentId = 0;
366
		long paymentId = 0;
-
 
367
 
-
 
368
		int countSuccessfulInserts = 0;
-
 
369
		int countRefunds = 0;
340
		
370
		
341
		try {
371
		try {
342
			tsc = new TransactionClient();
372
			tsc = new TransactionClient();
343
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
373
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
344
			
374
			
Line 355... Line 385...
355
                	logger.info("paymentId: " + paymentId + ", recfmt: " + recfmt + ", settlementDate: " + settlementDate + ", netCollection: " + netCollection);
385
                	logger.info("paymentId: " + paymentId + ", recfmt: " + recfmt + ", settlementDate: " + settlementDate + ", netCollection: " + netCollection);
356
                	
386
                	
357
                    if (recfmt.trim().equalsIgnoreCase("CVD"))	{
387
                    if (recfmt.trim().equalsIgnoreCase("CVD"))	{
358
                    	if (netCollection > 0.0)	netCollection *= -1.0;
388
                    	if (netCollection > 0.0)	netCollection *= -1.0;
359
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, 0.0, 0.0, netCollection);
389
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, 0.0, 0.0, netCollection);
-
 
390
                    	countRefunds += 1;
360
                    	
391
                    	
361
                    } else	{
392
                    } else	{
362
                    	double msf = row.getCell(indexMSF).getNumericCellValue();
393
                    	double msf = row.getCell(indexMSF).getNumericCellValue();
363
                    	double serviceTax = row.getCell(indexServiceTax).getNumericCellValue();
394
                    	double serviceTax = row.getCell(indexServiceTax).getNumericCellValue();
364
                    	double eduCess = row.getCell(indexEduCess).getNumericCellValue();
395
                    	double eduCess = row.getCell(indexEduCess).getNumericCellValue();
365
                    	logger.info("msf: " + msf + ", serviceTax: " + serviceTax + ", eduCess: " + eduCess);
396
                    	logger.info("msf: " + msf + ", serviceTax: " + serviceTax + ", eduCess: " + eduCess);
366
                    	
397
                    	
367
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, serviceTax, (eduCess + msf), netCollection);
398
                    	transactionClient.savePaymentSettlements(settlementDate.getTime(), 1, paymentId, serviceTax, (eduCess + msf), netCollection);
-
 
399
                    	countSuccessfulInserts += 1;
368
                 	}
400
                 	}
369
                } catch (NumberFormatException e) {
401
                } catch (NumberFormatException e) {
370
        			logger.error("NumberFormatException recieved for payment Id," + paymentIdStr + " "+ e);
402
                	String errMsg = "NumberFormatException recieved for payment Id, " + paymentIdStr;
-
 
403
        			logger.error(errMsg + e);
-
 
404
        			addActionError(errMsg);
371
        			
405
        			
372
        		} catch (TransactionServiceException e) {
406
        		} catch (TransactionServiceException e) {
373
        			logger.error("TransactionServiceException recieved for payment Id," + Long.toString(paymentId) + " " + e);
407
                	String errMsg = "TransactionServiceException recieved for payment Id, " + paymentId;
-
 
408
        			logger.error(errMsg + e);
-
 
409
        			addActionError(errMsg);
374
        		}
410
        		}
375
			}
411
			}
376
		} catch (TTransportException e) {
412
		} catch (TTransportException e) {
377
			logger.error("TTransportException recieved for payment Id," + Long.toString(paymentId) + " " + e);
413
			logger.error("TTransportException recieved for payment Id," + Long.toString(paymentId) + " " + e);
378
		
414
		
379
		} catch (TException e) {
415
		} catch (TException e) {
380
			logger.error("TException recieved for payment Id," + Long.toString(paymentId) + " " + e);
416
			logger.error("TException recieved for payment Id," + Long.toString(paymentId) + " " + e);
381
		}
417
		}
-
 
418
		addActionMessage("Added settlements for " + countSuccessfulInserts + " payments");
-
 
419
		addActionMessage("Added settlements for " + countRefunds + " refund payments");
382
	}
420
	}
383
	
421
	
384
	public void uploadEBSSettlements()	{
422
	public void uploadEBSSettlements()	{
385
		Workbook wb = null;
423
		Workbook wb = null;
386
        try {
424
        try {
387
            wb = new HSSFWorkbook(new FileInputStream(this.ebsSettlementReport));
425
            wb = new HSSFWorkbook(new FileInputStream(this.ebsSettlementReport));
388
        } catch (FileNotFoundException e) {
426
        } catch (FileNotFoundException e) {
389
            logger.error("Unable to open the Settlement report", e);
427
            logger.error("Unable to open the EBS Settlement detail report", e);
390
        } catch (IOException e) {
428
        } catch (IOException e) {
391
            logger.error("Unable to open the Settlement report", e);
429
            logger.error("Unable to open the EBS Settlement detail report", e);
392
        }
430
        }
393
        
431
        
394
        Sheet sheet = wb.getSheetAt(0);
432
        Sheet sheet = wb.getSheetAt(0);
395
        
433
        
396
		Row firstRow = sheet.getRow(0);
434
		Row firstRow = sheet.getRow(0);
Line 400... Line 438...
400
        int indexTxnType = 5;
438
        int indexTxnType = 5;
401
        int indexAmount = 6;
439
        int indexAmount = 6;
402
        
440
        
403
        Map<Long, Map<String, Double>> map = new HashMap<Long, Map<String,Double>>();
441
        Map<Long, Map<String, Double>> map = new HashMap<Long, Map<String,Double>>();
404
        Map <String, Double> record;
442
        Map <String, Double> record;
-
 
443
        int countPaymentIds = 0;
405
        
444
        
406
        try	{
445
        try	{
407
        	for (Row row: sheet)	{
446
        	for (Row row: sheet)	{
408
                if(row.equals(firstRow) || row.equals(secondRow))	continue;
447
                if(row.equals(firstRow) || row.equals(secondRow))	continue;
409
                
448
                
Line 416... Line 455...
416
                	
455
                	
417
                } else	{
456
                } else	{
418
                	record = new HashMap<String, Double>();
457
                	record = new HashMap<String, Double>();
419
                	record.put(transactionType, amount);
458
                	record.put(transactionType, amount);
420
                	map.put(paymentId, record);
459
                	map.put(paymentId, record);
-
 
460
                	countPaymentIds += 1;
421
                }
461
                }
422
        	}
462
        	}
423
        } catch	(NullPointerException e)	{
463
        } catch	(NullPointerException e)	{
424
        	logger.error("" + e);
464
        	logger.error("" + e);
425
        }
465
        }
-
 
466
        addActionMessage("Total Payment Ids added: " + countPaymentIds);
426
        
467
        
427
		TransactionClient tsc;
468
		TransactionClient tsc;
428
        
-
 
429
        long settlementId = Long.parseLong(request.getParameter("settlementId"));
469
        long settlementId = Long.parseLong(request.getParameter("settlementId"));
430
        
470
        
-
 
471
        int countPaymentsRecieved = 0;
-
 
472
        int countPaymentsRefunded = 0;
-
 
473
        
431
        try	{
474
        try	{
432
			tsc = new TransactionClient();
475
			tsc = new TransactionClient();
433
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
476
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
-
 
477
			long settlementDate;
-
 
478
			
-
 
479
			try	{
434
			long settlementDate = transactionClient.getEBSSettlementDate(settlementId);
480
				settlementDate = transactionClient.getEBSSettlementDate(settlementId);
-
 
481
			} catch (TransactionServiceException e) {
-
 
482
				String errMsg = "Could not retrieve EBS Settlement date for settlementId, " + settlementId;
-
 
483
	        	logger.error(errMsg + e);
-
 
484
	        	addActionError(errMsg);
-
 
485
	        	return;
-
 
486
			}
435
			
487
			
436
        	for (long paymentId: map.keySet())	{
488
        	for (long paymentId: map.keySet())	{
437
            	record = map.get(paymentId);
489
            	record = map.get(paymentId);
438
            	
490
            	
439
            	if(record.containsKey("Captured"))	{
491
            	if(record.containsKey("Captured"))	{
Line 443... Line 495...
443
            		
495
            		
444
            		if(tdr < 0)	tdr *= -1;
496
            		if(tdr < 0)	tdr *= -1;
445
            		if(serviceTax < 0)	serviceTax *= -1;
497
            		if(serviceTax < 0)	serviceTax *= -1;
446
            		
498
            		
447
            		double amount = capturedAmount - tdr - serviceTax;
499
            		double amount = capturedAmount - tdr - serviceTax;
-
 
500
            		
-
 
501
            		try	{
448
            		transactionClient.savePaymentSettlements(settlementDate, 2, paymentId, serviceTax, tdr, amount);
502
            			transactionClient.savePaymentSettlements(settlementDate, 2, paymentId, serviceTax, tdr, amount);
-
 
503
        			} catch (TransactionServiceException e) {
-
 
504
        				String errMsg = "Could not save settlement for paymentId, " + paymentId;
-
 
505
        	        	logger.error(errMsg + e);
-
 
506
        	        	addActionError(errMsg);
-
 
507
        			}
-
 
508
            		countPaymentsRecieved += 1;
449
            	} else	{
509
            	}
-
 
510
            	
-
 
511
            	if(record.containsKey("Refunded"))	{
450
            		double refundedAmount = record.get("Refunded");
512
            		double refundedAmount = record.get("Refunded");
451
            		
513
            		
452
            		if (refundedAmount > 0)	refundedAmount *= -1;
514
            		if (refundedAmount > 0)	refundedAmount *= -1;
453
            		
515
            		
-
 
516
            		try	{
454
            		transactionClient.savePaymentSettlements(settlementDate, 2, paymentId, 0.0, 0.0, refundedAmount);
517
            			transactionClient.savePaymentSettlements(settlementDate, 2, paymentId, 0.0, 0.0, refundedAmount);
-
 
518
        			} catch (TransactionServiceException e) {
-
 
519
        				String errMsg = "Could not save refund settlement for paymentId, " + paymentId;
-
 
520
        	        	logger.error(errMsg + e);
-
 
521
        	        	addActionError(errMsg);
-
 
522
        			}
-
 
523
            		countPaymentsRefunded += 1;
455
            	}
524
            	}
456
            }
525
            }
-
 
526
        	addActionMessage("Total count of payments recieved: " + countPaymentsRecieved);
457
        	transactionClient.markEBSSettlementUploaded(settlementId);
527
        	addActionMessage("Total count of payments refunded: " + countPaymentsRefunded);
458
        	
528
        	
-
 
529
        	try	{
-
 
530
        		transactionClient.markEBSSettlementUploaded(settlementId);
-
 
531
			} catch (TransactionServiceException e) {
-
 
532
				String errMsg = "Could not mark settlement for details Uploaded. Settlement ID: " + settlementId;
-
 
533
	        	logger.error(errMsg + e);
-
 
534
	        	addActionError(errMsg);
-
 
535
			}
459
        } catch (TTransportException e) {
536
        } catch (TTransportException e) {
460
        	logger.error("TTransportException " + e);
537
        	logger.error("TTransportException " + e);
461
		} catch (TException e) {
538
		} catch (TException e) {
462
        	logger.error("TException " + e);
539
        	logger.error("TException " + e);
463
		} catch (TransactionServiceException e) {
-
 
464
        	logger.error("TransactionServiceException " + e);
-
 
465
		}
540
		}
466
	}
541
	}
467
	
542
	
468
	public void uploadBluedartSettlements()	{
543
	public void uploadBluedartSettlements()	{
469
		
544
		
Line 479... Line 554...
479
        Sheet sheet = wb.getSheetAt(0);
554
        Sheet sheet = wb.getSheetAt(0);
480
        
555
        
481
        Row firstRow = sheet.getRow(0);
556
        Row firstRow = sheet.getRow(0);
482
        logger.info("Cell count:" + firstRow.getPhysicalNumberOfCells());
557
        logger.info("Cell count:" + firstRow.getPhysicalNumberOfCells());
483
        
558
        
484
        int indexAWBno = 4;		//FIXME: add look up string to determine exact cell
559
        int indexOrderId = 0;
485
        int indexAmount = 19;
560
        int indexAmount = 1;
486
        
561
        
487
        TransactionClient tsc;
562
        TransactionClient tsc;
488
        PaymentClient psc;
563
        PaymentClient psc;
-
 
564
    	int countSuccessfulSaves = 0;
489
        
565
        
490
        try	{
566
        try	{
491
			long settlementDate = DATE_FORMAT.parse(bluedartSettlementDate).getTime();
567
			long settlementDate = DATE_FORMAT.parse(bluedartSettlementDate).getTime();
492
        	tsc = new TransactionClient();
568
        	tsc = new TransactionClient();
493
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
569
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
Line 496... Line 572...
496
        	
572
        	
497
        	for (Row row : sheet) {
573
        	for (Row row : sheet) {
498
        		Order order = null;
574
        		Order order = null;
499
        		try	{
575
        		try	{
500
        			if(row.equals(firstRow))	continue;
576
        			if(row.equals(firstRow))	continue;
501
        			long awbNumber = (long) row.getCell(indexAWBno).getNumericCellValue();
577
        			long orderId = (long) row.getCell(indexOrderId).getNumericCellValue();
502
        			
578
        			
503
        			double amount = row.getCell(indexAmount).getNumericCellValue();
579
        			double amount = row.getCell(indexAmount).getNumericCellValue();
504
        			order = transactionClient.getOrderForAwb(Long.toString(awbNumber));
580
        			order = transactionClient.getOrder(orderId);
505
        			Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
581
        			Payment payment = paymentClient.getPaymentForTxnId(order.getTransactionId()).get(0);
506
        			logger.info("settlementDate: " + settlementDate + ", PaymentId: " + payment.getPaymentId() + ", Amount: " + amount);
582
        			logger.info("settlementDate: " + settlementDate + ", PaymentId: " + payment.getPaymentId() + ", Amount: " + amount);
507
        			
583
        			
508
        			transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0, amount);
584
        			transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0, amount);
-
 
585
        			countSuccessfulSaves += 1;
509
        		} catch (NullPointerException e) {
586
        		} catch (NullPointerException e) {
510
        			logger.error("" + e);
587
        			logger.error("" + e);
511
        			
588
        			
512
        		} catch (PaymentException e) {
589
        		} catch (PaymentException e) {
513
        			logger.error("" + e);
590
        			logger.error("" + e);
Line 527... Line 604...
527
		} catch (TTransportException e) {
604
		} catch (TTransportException e) {
528
			logger.error("" + e);
605
			logger.error("" + e);
529
        } catch (TException e) {
606
        } catch (TException e) {
530
        	logger.error("" + e);
607
        	logger.error("" + e);
531
        }
608
        }
-
 
609
        addActionMessage("Successfully added Bluedart settlement details for " + countSuccessfulSaves + " orders");
532
	}
610
	}
533
	
611
	
534
	public void uploadAramexSettlements()	{
612
	public void uploadAramexSettlements()	{
535
		logger.info("Uploading Aramex Settlements" + this.aramexSettlementDate);
613
		logger.info("Uploading Aramex Settlements" + this.aramexSettlementDate);
536
		
614
		
Line 553... Line 631...
553
		}
631
		}
554
        
632
        
555
        Sheet sheet = wb.getSheetAt(0);
633
        Sheet sheet = wb.getSheetAt(0);
556
        
634
        
557
        int indexOrderId = 0;
635
        int indexOrderId = 0;
558
        int indexAmount = 4;
636
        int indexAmount = 1;
559
        
637
        
560
        Row firstRow = sheet.getRow(0);
638
        Row firstRow = sheet.getRow(0);
561
        logger.info("Cell count:" + firstRow.getPhysicalNumberOfCells());
639
        logger.info("Cell count:" + firstRow.getPhysicalNumberOfCells());
562
        
640
        
563
        TransactionClient tsc;
641
        TransactionClient tsc;
564
        PaymentClient psc;
642
        PaymentClient psc;
565
        
643
        int countSuccessfulSaves = 0;
566
        try	{
644
        try	{
567
			long settlementDate = DATE_FORMAT.parse(aramexSettlementDate).getTime();
645
			long settlementDate = DATE_FORMAT.parse(aramexSettlementDate).getTime();
568
			
646
			
569
        	tsc = new TransactionClient();
647
        	tsc = new TransactionClient();
570
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
648
        	in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
Line 578... Line 656...
578
	    		double amount = row.getCell(indexAmount).getNumericCellValue();
656
	    		double amount = row.getCell(indexAmount).getNumericCellValue();
579
	    		Order order = null;
657
	    		Order order = null;
580
	    		
658
	    		
581
	    		try {
659
	    		try {
582
					order = transactionClient.getOrder(orderId);
660
					order = transactionClient.getOrder(orderId);
583
					Payment payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
661
					Payment payment = paymentClient.getPaymentForTxnId(order.getTransactionId()).get(0);
584
					transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0.0, amount);
662
					transactionClient.savePaymentSettlements(settlementDate, 4, payment.getPaymentId(), 0.0, 0.0, amount);
-
 
663
					countSuccessfulSaves += 1;
585
					
664
					
586
				} catch (TransactionServiceException e) {
665
				} catch (TransactionServiceException e) {
587
					logger.error("Transaction Service Exception while getting order for id, " + orderId, e);
666
					logger.error("Transaction Service Exception while getting order for id, " + orderId, e);
588
					addActionError("Transaction Service Exception while getting order for id, " + orderId);
667
					addActionError("Transaction Service Exception while getting order for id, " + orderId);
589
					
668
					
Line 604... Line 683...
604
        } catch (TTransportException e) {
683
        } catch (TTransportException e) {
605
        	logger.error("" + e);
684
        	logger.error("" + e);
606
        } catch (TException e) {
685
        } catch (TException e) {
607
        	logger.error("" + e);
686
        	logger.error("" + e);
608
        }
687
        }
-
 
688
        addActionMessage("Successfully added Aramex settlement details for " + countSuccessfulSaves + " orders");
609
	}
689
	}
610
	
690
	
611
	// Prepares the XLS worksheet object and fills in the data with proper formatting
691
	// Prepares the XLS worksheet object and fills in the data with proper formatting
612
	private ByteArrayOutputStream getReconciliationReport(List<Order> orders, Date startDate, Date endDate)	{
692
	private ByteArrayOutputStream getReconciliationReport(List<Order> orders, Date startDate, Date endDate)	{
613
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
693
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
Line 706... Line 786...
706
	    
786
	    
707
	    sheet.createRow(serialNo ++);
787
	    sheet.createRow(serialNo ++);
708
	    
788
	    
709
		TransactionClient tsc;
789
		TransactionClient tsc;
710
    	PaymentClient psc;
790
    	PaymentClient psc;
-
 
791
    	LogisticsClient lsc;
711
 
792
 
712
		try {
793
		try {
713
			tsc = new TransactionClient();
794
			tsc = new TransactionClient();
714
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
795
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
715
			
796
			
716
			psc = new PaymentClient();
797
			psc = new PaymentClient();
717
	    	in.shop2020.payments.PaymentService.Client paymentClient = psc.getClient();
798
	    	in.shop2020.payments.PaymentService.Client paymentClient = psc.getClient();
718
	    	
799
	    	
-
 
800
	    	lsc = new LogisticsClient();
-
 
801
	    	in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
-
 
802
	    	
-
 
803
	    	Map<Long, String> codProviders = new HashMap<Long, String>();
-
 
804
	    	
-
 
805
	    	for (Provider provider: logisticsClient.getAllProviders())	{
-
 
806
	    		codProviders.put(provider.getId(), provider.getName());
-
 
807
	    	}
-
 
808
	    	
719
	    	List<PaymentGateway> tPaymentGateways = paymentClient.getActivePaymentGateways();
809
	    	List<PaymentGateway> tPaymentGateways = paymentClient.getActivePaymentGateways();
720
	    	Map<Long, String> paymentGateways = new HashMap<Long, String>();
810
	    	Map<Long, String> paymentGateways = new HashMap<Long, String>();
721
	    	
811
	    	
722
	    	for (PaymentGateway tPaymentGateway: tPaymentGateways)	{
812
	    	for (PaymentGateway tPaymentGateway: tPaymentGateways)	{
723
	    		paymentGateways.put(tPaymentGateway.getId(), tPaymentGateway.getName());
813
	    		paymentGateways.put(tPaymentGateway.getId(), tPaymentGateway.getName());
Line 732... Line 822...
732
			for (PaymentSettlement settlement: settlements)	{
822
			for (PaymentSettlement settlement: settlements)	{
733
				refunds.put(settlement.getPaymentId(), settlement);
823
				refunds.put(settlement.getPaymentId(), settlement);
734
			}
824
			}
735
			
825
			
736
			List <Long> orderIds = new ArrayList<Long>();
826
			List <Long> orderIds = new ArrayList<Long>();
-
 
827
			Map<Long, List<Order>> orderMap = new HashMap<Long, List<Order>>();
737
			
828
			
738
			for(Order order : orders)	{
829
			for(Order order : orders)	{
739
				orderIds.add(order.getId());
830
				orderIds.add(order.getId());
-
 
831
				
-
 
832
				if (orderMap.containsKey(order.getTransactionId()))	{
-
 
833
					orderMap.get(order.getTransactionId()).add(order);
-
 
834
					
-
 
835
				} else	{
-
 
836
					List<Order> o = new ArrayList<Order>();
-
 
837
					o.add(order);
-
 
838
					orderMap.put(order.getTransactionId(), o);
-
 
839
				}
740
			}
840
			}
741
			
841
			
742
			List<Long> reshippedOrderIds = null;
842
			List<Long> reshippedOrderIds = null;
743
			
843
			
744
			reshippedOrderIds = transactionClient.getReshippedOrderIds(orderIds);
844
			reshippedOrderIds = transactionClient.getReshippedOrderIds(orderIds);
745
			
845
			
746
			for(Order order : orders)	{
846
			for(long txnId: orderMap.keySet())	{
747
			    logger.info("For order: " + order.getId());
847
				List<Order> orderList = orderMap.get(txnId);
748
		    	
-
 
749
			    LineItem lineItem = order.getLineitems().get(0);
-
 
750
			    double transferPrice = lineItem.getTransfer_price();
-
 
751
			    Payment payment = null;
-
 
752
			    
-
 
753
			    try	{
-
 
754
			    	payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
-
 
755
				    logger.info("getSuccessfulPaymentForTxnId: " + payment);
-
 
756
				    
848
				
757
			    } catch (TApplicationException e)	{
849
				for (Order order: orderList)	{
758
			    	orderIdsWithoutSuccess.add(order.getId());
850
				    logger.info("For order: " + order.getId());
759
			    	
851
			    	
-
 
852
				    LineItem lineItem = order.getLineitems().get(0);
-
 
853
				    double transferPrice = lineItem.getTransfer_price();
-
 
854
				    Payment payment = null;
-
 
855
				    
-
 
856
				    try	{
-
 
857
				    	payment = paymentClient.getSuccessfulPaymentForTxnId(order.getTransactionId());
-
 
858
					    logger.info("getSuccessfulPaymentForTxnId: " + payment);
-
 
859
					    
-
 
860
				    } catch (TApplicationException e)	{
-
 
861
				    	orderIdsWithoutSuccess.add(order.getId());
-
 
862
				    	
760
					logger.error("Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
863
						logger.error("Exception while getting successfull payment for transaction Id, " + order.getTransactionId(), e);
761
					addActionError("Exception while getting payment for transaction Id, " + order.getTransactionId());
864
						addActionError("Exception while getting payment for transaction Id, " + order.getTransactionId());
762
				}
865
					}
763
			    
866
				    
764
			    Transaction txn = transactionClient.getTransaction(order.getTransactionId());
867
				    Transaction txn = transactionClient.getTransaction(order.getTransactionId());
765
			    PaymentSettlement paymentSettlement = null;
868
				    PaymentSettlement paymentSettlement = null;
766
			    String paymentType = null;
869
				    String paymentType = null;
767
			    
870
				    
768
			    if(payment != null)	{
871
				    if(payment != null)	{
769
			    	paymentSettlement = transactionClient.getSettlementForPaymentId(payment.getPaymentId());
872
				    	paymentSettlement = transactionClient.getSettlementForPaymentId(payment.getPaymentId());
770
			    	paymentType = paymentGateways.get(payment.getGatewayId());
873
				    	paymentType = paymentGateways.get(payment.getGatewayId());
771
			    }
874
				    }
772
			    
875
				    
773
	            double totalSellingPrice = lineItem.getTotal_price();
876
		            double totalSellingPrice = lineItem.getTotal_price();
774
	            double serviceTax = 0.0;
877
		            double serviceTax = 0.0;
775
	            double otherCollectionCharges = 0.0;
878
		            double otherCollectionCharges = 0.0;
776
	            double netCollection = 0.0;
879
		            double netCollection = 0.0;
777
	            
880
		            
778
	            if(paymentSettlement != null)	{
881
		            if(paymentSettlement != null)	{
779
		            serviceTax = paymentSettlement.getServiceTax();
882
			            serviceTax = paymentSettlement.getServiceTax();
780
		            otherCollectionCharges = paymentSettlement.getOtherCharges();
883
			            otherCollectionCharges = paymentSettlement.getOtherCharges();
781
		            netCollection = paymentSettlement.getNetCollection();
884
			            netCollection = paymentSettlement.getNetCollection();
782
	            }
885
		            }
783
	            
886
		            
784
	            if (netCollection < 0.0)	{
887
		            if (netCollection < 0.0)	{
785
	            	paymentType = paymentType + "-REFUND";
888
		            	paymentType = paymentType + "-REFUND";
786
	            }
889
		            }
787
	            double expectedCollection = totalSellingPrice - otherCollectionCharges - serviceTax;
890
		            double expectedCollection = totalSellingPrice - otherCollectionCharges - serviceTax;
788
	            
891
		            
789
	            //FIXME ignore differences of upto 50 paisa
892
		            //FIXME ignore differences of upto 50 paisa
790
	            if (netCollection > 0.0 && expectedCollection != netCollection)	{
893
		            if (netCollection > 0.0 && expectedCollection != netCollection)	{
791
	            	Map <String, Double> mismatch = new HashMap<String, Double>();
894
		            	Map <String, Double> mismatch = new HashMap<String, Double>();
792
	            	mismatch.put("expected", expectedCollection);
895
		            	mismatch.put("expected", expectedCollection);
793
	            	mismatch.put("actual", netCollection);
896
		            	mismatch.put("actual", netCollection);
794
	            	misMatches.put(order.getId(), mismatch);
897
		            	misMatches.put(order.getId(), mismatch);
795
	            }
898
		            }
796
 
-
 
797
		    	Row contentRow = sheet.createRow(serialNo++);
-
 
798
	            contentRow.createCell(OrderReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
-
 
799
	            contentRow.createCell(OrderReportColumn.ORDER_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getCreated_timestamp())));
-
 
800
			    contentRow.createCell(OrderReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
-
 
801
			    contentRow.createCell(OrderReportColumn.BILLING_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getBilling_timestamp())));
-
 
802
			    contentRow.createCell(OrderReportColumn.DELIVERY_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getDelivery_timestamp())));
-
 
803
			    contentRow.createCell(OrderReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
-
 
804
			    contentRow.createCell(OrderReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
-
 
805
			    contentRow.createCell(OrderReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
-
 
806
			    contentRow.createCell(OrderReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
-
 
807
			    contentRow.createCell(OrderReportColumn.QUANTITY.getValue()).setCellValue(lineItem.getQuantity());
-
 
808
			    contentRow.createCell(OrderReportColumn.UNIT_TRANSFER_PRICE.getValue()).setCellValue(transferPrice);
-
 
809
			    contentRow.createCell(OrderReportColumn.UNIT_SELLING_PRICE.getValue()).setCellValue(lineItem.getUnit_price());
-
 
810
			    contentRow.createCell(OrderReportColumn.TOTAL_TRANSFER_PRICE.getValue()).setCellValue(transferPrice * lineItem.getQuantity());
-
 
811
			    contentRow.createCell(OrderReportColumn.TOTAL_SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
-
 
812
			    contentRow.createCell(OrderReportColumn.CURRENT_STATUS.getValue()).setCellValue(order.getStatusDescription());
-
 
813
 
899
 
814
		    	double refund = 0.0;
900
			    	Row contentRow = sheet.createRow(serialNo++);
815
		    	
-
 
816
			    if (payment != null)	{
901
		            contentRow.createCell(OrderReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
-
 
902
		            contentRow.createCell(OrderReportColumn.ORDER_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getCreated_timestamp())));
-
 
903
				    contentRow.createCell(OrderReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
-
 
904
				    contentRow.createCell(OrderReportColumn.BILLING_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getBilling_timestamp())));
817
				    contentRow.createCell(OrderReportColumn.SETTLEMENT_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(paymentSettlement.getSettlementDate())));
905
				    contentRow.createCell(OrderReportColumn.DELIVERY_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(order.getDelivery_timestamp())));
-
 
906
				    contentRow.createCell(OrderReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
-
 
907
				    contentRow.createCell(OrderReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
-
 
908
				    contentRow.createCell(OrderReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
-
 
909
				    contentRow.createCell(OrderReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
818
			    	contentRow.createCell(OrderReportColumn.GATEWAY_TRANSACTION_ID.getValue()).setCellValue(payment.getGatewayTxnId());
910
				    contentRow.createCell(OrderReportColumn.QUANTITY.getValue()).setCellValue(lineItem.getQuantity());
819
			    	contentRow.createCell(OrderReportColumn.PAYMENT_TYPE.getValue()).setCellValue(paymentType);
911
				    contentRow.createCell(OrderReportColumn.UNIT_TRANSFER_PRICE.getValue()).setCellValue(transferPrice);
-
 
912
				    contentRow.createCell(OrderReportColumn.UNIT_SELLING_PRICE.getValue()).setCellValue(lineItem.getUnit_price());
-
 
913
				    contentRow.createCell(OrderReportColumn.TOTAL_TRANSFER_PRICE.getValue()).setCellValue(transferPrice * lineItem.getQuantity());
-
 
914
				    contentRow.createCell(OrderReportColumn.TOTAL_SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
820
			    	contentRow.createCell(OrderReportColumn.PAYMENT_ID.getValue()).setCellValue(payment.getPaymentId());
915
				    contentRow.createCell(OrderReportColumn.CURRENT_STATUS.getValue()).setCellValue(order.getStatusDescription());
-
 
916
 
-
 
917
			    	double refund = 0.0;
821
			    	
918
			    	
-
 
919
				    if (payment != null)	{
-
 
920
					    contentRow.createCell(OrderReportColumn.SETTLEMENT_DATE.getValue()).setCellValue(DATE_FORMAT.format(new Date(paymentSettlement.getSettlementDate())));
-
 
921
				    	contentRow.createCell(OrderReportColumn.GATEWAY_TRANSACTION_ID.getValue()).setCellValue(payment.getGatewayTxnId());
-
 
922
				    	contentRow.createCell(OrderReportColumn.PAYMENT_TYPE.getValue()).setCellValue(paymentType);
-
 
923
				    	contentRow.createCell(OrderReportColumn.PAYMENT_ID.getValue()).setCellValue(payment.getPaymentId());
-
 
924
				    	
822
			    	if(refunds.containsKey(payment.getPaymentId()))	{
925
				    	if(refunds.containsKey(payment.getPaymentId()))	{
823
			    		refund = refunds.get(payment.getPaymentId()).getNetCollection();
926
				    		refund = refunds.get(payment.getPaymentId()).getNetCollection();
824
			    		refunds.remove(payment.getPaymentId());
927
				    		refunds.remove(payment.getPaymentId());
825
			    	}
928
				    	}
826
			    	contentRow.createCell(OrderReportColumn.REFUND.getValue()).setCellValue(refund);
929
				    	contentRow.createCell(OrderReportColumn.REFUND.getValue()).setCellValue(refund);
827
			    }
930
				    }
828
			    contentRow.createCell(OrderReportColumn.COUPON_CODE.getValue()).setCellValue(txn.getCoupon_code());
931
				    contentRow.createCell(OrderReportColumn.COUPON_CODE.getValue()).setCellValue(txn.getCoupon_code());
829
			    contentRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue(serviceTax);
932
				    contentRow.createCell(OrderReportColumn.SERVICE_TAX.getValue()).setCellValue(serviceTax);
830
			    contentRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue(otherCollectionCharges);
933
				    contentRow.createCell(OrderReportColumn.OTHER_CHARGES.getValue()).setCellValue(otherCollectionCharges);
831
			    contentRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue(netCollection);
934
				    contentRow.createCell(OrderReportColumn.NET_COLLECTION.getValue()).setCellValue(netCollection);
832
			    contentRow.createCell(OrderReportColumn.FINAL_AMOUNT.getValue()).setCellValue(netCollection - refund);
935
				    contentRow.createCell(OrderReportColumn.FINAL_AMOUNT.getValue()).setCellValue(netCollection - refund);
833
			    
936
				    
834
			    boolean isReshippedOrder = reshippedOrderIds.contains(order.getId());
937
				    boolean isReshippedOrder = reshippedOrderIds.contains(order.getId());
835
			    contentRow.createCell(OrderReportColumn.IS_RESHIPED_ORDER.getValue()).setCellValue(isReshippedOrder ? 1 : 0);
938
				    contentRow.createCell(OrderReportColumn.IS_RESHIPED_ORDER.getValue()).setCellValue(isReshippedOrder ? 1 : 0);
836
		    }
939
				}
-
 
940
			}
-
 
941
			
837
		    logger.info("Order Ids where no successful payment was found: " + orderIdsWithoutSuccess.size());
942
		    logger.info("Order Ids where no successful payment was found: " + orderIdsWithoutSuccess.size());
838
		    logger.info(orderIdsWithoutSuccess.toString());
943
		    logger.info(orderIdsWithoutSuccess.toString());
839
		    logger.info("Pending Refunds: " + refunds);
944
		    logger.info("Pending Refunds: " + refunds);
840
		    logger.info("Reshipped Order Ids" + reshippedOrderIds);
945
		    logger.info("Reshipped Order Ids" + reshippedOrderIds);
841
 
946
 
Line 846... Line 951...
846
		    	Payment payment = paymentClient.getPayment(paymentId);
951
		    	Payment payment = paymentClient.getPayment(paymentId);
847
		    	Transaction txn = transactionClient.getTransaction(payment.getMerchantTxnId());
952
		    	Transaction txn = transactionClient.getTransaction(payment.getMerchantTxnId());
848
		    	PaymentSettlement paymentSettlement = refunds.get(paymentId);
953
		    	PaymentSettlement paymentSettlement = refunds.get(paymentId);
849
		    	
954
		    	
850
		    	for (Order order: txn.getOrders())	{
955
		    	for (Order order: txn.getOrders())	{
-
 
956
		    		if(!refundOrderStatuses.contains(order.getStatus()))	continue;
-
 
957
		    		
851
			    	Row contentRow = sheet.createRow(serialNo++);
958
			    	Row contentRow = sheet.createRow(serialNo++);
852
			    	
959
			    	
853
			    	LineItem lineItem = order.getLineitems().get(0);
960
			    	LineItem lineItem = order.getLineitems().get(0);
854
				    double transferPrice = lineItem.getTransfer_price();
961
				    double transferPrice = lineItem.getTransfer_price();
855
		            double totalSellingPrice = lineItem.getTotal_price();
962
		            double totalSellingPrice = lineItem.getTotal_price();
Line 889... Line 996...
889
		} catch (PaymentException e) {
996
		} catch (PaymentException e) {
890
			logger.error("PaymentException " + e);
997
			logger.error("PaymentException " + e);
891
		} catch (TException e) {
998
		} catch (TException e) {
892
			logger.error("TException " + e);
999
			logger.error("TException " + e);
893
		} catch (TransactionServiceException e) {
1000
		} catch (TransactionServiceException e) {
894
			logger.error("TransactionServiceException " + e);
1001
			logger.error("Transaction Service Exception " + e);
-
 
1002
		} catch (LogisticsServiceException e) {
-
 
1003
			addActionError("Error loading list of COD providers");
-
 
1004
			logger.error("Logistics Service Exception " + e);
895
		}
1005
		}
896
	}
1006
	}
897
	
1007
	
898
	public List<Vendor> getAllVendors()	{
1008
	public List<Vendor> getAllVendors()	{
899
		return this.vendors;
1009
		return this.vendors;