| 24783 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import java.text.Normalizer.Form;
|
|
|
4 |
import java.time.LocalDate;
|
|
|
5 |
import java.util.ArrayList;
|
|
|
6 |
import java.util.Arrays;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
import java.util.logging.SimpleFormatter;
|
|
|
10 |
import java.util.stream.Collectors;
|
|
|
11 |
|
|
|
12 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.core.io.ByteArrayResource;
|
|
|
15 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
16 |
import org.springframework.stereotype.Component;
|
|
|
17 |
|
|
|
18 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
19 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
|
|
20 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
22 |
import com.spice.profitmandi.dao.entity.transaction.ReturnOrder;
|
|
|
23 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
24 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
|
|
|
28 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
29 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
30 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
31 |
|
|
|
32 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
33 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
34 |
|
|
|
35 |
@Component
|
|
|
36 |
public class Reconciliation {
|
|
|
37 |
@Autowired
|
|
|
38 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
39 |
@Autowired
|
|
|
40 |
private UserWalletRepository userWalletRepository;
|
|
|
41 |
@Autowired
|
|
|
42 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
43 |
@Autowired
|
|
|
44 |
private RetailerService retailerService;
|
|
|
45 |
@Autowired
|
|
|
46 |
private ReturnOrderRepository returnOrderRepository;
|
|
|
47 |
@Autowired
|
|
|
48 |
private OrderRepository orderRepository;
|
|
|
49 |
@Autowired
|
|
|
50 |
private JavaMailSender mailSender;
|
|
|
51 |
|
|
|
52 |
public void dailyReconciliation() throws Exception {
|
|
|
53 |
boolean reconciled = true;
|
|
|
54 |
Map<Integer, String> stores = fofoStoreRepository.getStoresMap();
|
|
|
55 |
List<List<?>> rows = new ArrayList<>();
|
|
|
56 |
LocalDate yesterday = LocalDate.now().minusDays(1);
|
|
|
57 |
Map<Integer, String> retailerNameMap = retailerService
|
|
|
58 |
.getAllFofoRetailerIdNameMap(new ArrayList<>(stores.keySet()));
|
|
|
59 |
for (int partnerId : stores.keySet()) {
|
|
|
60 |
UserWallet uw = userWalletRepository.selectByRetailerId(partnerId);
|
|
|
61 |
List<UserWalletHistory> walletHistory = userWalletHistoryRepository.selectByWalletIdAndDate(uw.getId(),
|
|
|
62 |
yesterday);
|
|
|
63 |
Map<LocalDate, List<UserWalletHistory>> dateWiseWalletHistory = walletHistory.stream()
|
|
|
64 |
.collect(Collectors.groupingBy(x -> x.getTimestamp().toLocalDate(), Collectors.toList()));
|
|
|
65 |
for (Map.Entry<LocalDate, List<UserWalletHistory>> entry : dateWiseWalletHistory.entrySet()) {
|
|
|
66 |
LocalDate dateToReconcile = entry.getKey();
|
|
|
67 |
List<UserWalletHistory> history = entry.getValue();
|
|
|
68 |
List<?> reconciliation = reconcileOrdersAndWallet(uw.getUserId(), retailerNameMap.get(partnerId), dateToReconcile,history);
|
| 24785 |
amit.gupta |
69 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.remove(0));
|
| 24783 |
amit.gupta |
70 |
rows.add(reconciliation);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Code", "Date",
|
|
|
74 |
"Wallet amount consumed", "Ordered Total", "Cancelled Total", "Refunded Total"), rows);
|
|
|
75 |
|
|
|
76 |
Utils.sendMailWithAttachment(mailSender, new String[] { "amit.gupta@shop2020.in" }, new String[] {},
|
|
|
77 |
reconciled ? "Reconciled Successfully": "Reconciliation failed",
|
|
|
78 |
"Report attached", String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())), new ByteArrayResource(baos.toByteArray()));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private List<?> reconcileOrdersAndWallet(int fofoId, String storeName, LocalDate localDate, List<UserWalletHistory> history)
|
|
|
82 |
throws Exception {
|
|
|
83 |
Map<Integer, Integer> transactionsOnThatDate = history.stream()
|
|
|
84 |
.filter(x -> x.getReferenceType().equals(WalletReferenceType.PURCHASE))
|
|
|
85 |
.collect(Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
|
|
86 |
|
|
|
87 |
int totalWalletConsumed = 0;
|
|
|
88 |
float cancelledAmount = 0;
|
|
|
89 |
float returnedAmount = 0;
|
|
|
90 |
float totalDeductedAmount = 0;
|
|
|
91 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
|
|
92 |
List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
|
|
|
93 |
for (Order o : orders) {
|
|
|
94 |
if (o.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
|
|
95 |
if (Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED)
|
|
|
96 |
.contains(o.getStatus())) {
|
|
|
97 |
cancelledAmount += o.getWalletAmount();
|
|
|
98 |
} else if (o.getRefundTimestamp() != null
|
|
|
99 |
&& o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
|
|
100 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
|
|
101 |
if (returnedOrder == null) {
|
|
|
102 |
cancelledAmount += o.getWalletAmount();
|
|
|
103 |
} else {
|
|
|
104 |
returnedAmount += returnedOrder.getTotalPrice();
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
totalDeductedAmount += o.getWalletAmount();
|
|
|
108 |
|
|
|
109 |
} else if (o.getRefundTimestamp() != null && o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
|
|
110 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
|
|
111 |
if (returnedOrder == null) {
|
|
|
112 |
cancelledAmount += o.getWalletAmount();
|
|
|
113 |
} else {
|
|
|
114 |
returnedAmount += returnedOrder.getTotalPrice();
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
totalWalletConsumed -= transactionsOnThatDate.get(transactionId);
|
|
|
119 |
|
|
|
120 |
}
|
|
|
121 |
boolean reconciled = Math.abs(totalWalletConsumed - (totalDeductedAmount-cancelledAmount - returnedAmount) ) < 2;
|
|
|
122 |
|
|
|
123 |
return Arrays.asList(reconciled, localDate, storeName, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount);
|
|
|
124 |
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
private void reconcileDailySchemeIn() {
|
|
|
128 |
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
private void reconcileDailySchemeOut() {
|
|
|
132 |
|
|
|
133 |
}
|
|
|
134 |
}
|