| 24783 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
| 24810 |
amit.gupta |
3 |
import java.io.Serializable;
|
| 24783 |
amit.gupta |
4 |
import java.time.LocalDate;
|
|
|
5 |
import java.util.ArrayList;
|
|
|
6 |
import java.util.Arrays;
|
| 24810 |
amit.gupta |
7 |
import java.util.HashSet;
|
| 24783 |
amit.gupta |
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
| 24810 |
amit.gupta |
10 |
import java.util.Set;
|
| 24783 |
amit.gupta |
11 |
import java.util.stream.Collectors;
|
|
|
12 |
|
|
|
13 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
15 |
import org.springframework.core.io.ByteArrayResource;
|
|
|
16 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
17 |
import org.springframework.stereotype.Component;
|
|
|
18 |
|
|
|
19 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
20 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
|
|
21 |
import com.spice.profitmandi.common.util.Utils;
|
| 24810 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
|
|
23 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
24 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
| 24783 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
26 |
import com.spice.profitmandi.dao.entity.transaction.ReturnOrder;
|
|
|
27 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
28 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 24810 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 24783 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 24810 |
amit.gupta |
32 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
|
|
33 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 24783 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
|
|
38 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
39 |
|
|
|
40 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
41 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
|
|
42 |
|
|
|
43 |
@Component
|
|
|
44 |
public class Reconciliation {
|
|
|
45 |
@Autowired
|
|
|
46 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
47 |
@Autowired
|
|
|
48 |
private UserWalletRepository userWalletRepository;
|
|
|
49 |
@Autowired
|
|
|
50 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
51 |
@Autowired
|
| 24810 |
amit.gupta |
52 |
private SchemeInOutRepository schemeInOutRepository;
|
|
|
53 |
@Autowired
|
|
|
54 |
private SchemeRepository schemeRepository;
|
|
|
55 |
@Autowired
|
|
|
56 |
private InventoryItemRepository inventoryRepository;
|
|
|
57 |
@Autowired
|
| 24783 |
amit.gupta |
58 |
private RetailerService retailerService;
|
|
|
59 |
@Autowired
|
|
|
60 |
private ReturnOrderRepository returnOrderRepository;
|
|
|
61 |
@Autowired
|
|
|
62 |
private OrderRepository orderRepository;
|
|
|
63 |
@Autowired
|
|
|
64 |
private JavaMailSender mailSender;
|
|
|
65 |
|
|
|
66 |
public void dailyReconciliation() throws Exception {
|
| 24810 |
amit.gupta |
67 |
Map<SchemeType, Set<Integer>> schemeTypeMap = schemeRepository.selectAll().stream()
|
|
|
68 |
.collect(Collectors.groupingBy(Scheme::getType, Collectors.mapping(Scheme::getId, Collectors.toSet())));
|
| 24783 |
amit.gupta |
69 |
boolean reconciled = true;
|
|
|
70 |
Map<Integer, String> stores = fofoStoreRepository.getStoresMap();
|
| 24810 |
amit.gupta |
71 |
List<List<? extends Serializable>> rows = new ArrayList<>();
|
| 24783 |
amit.gupta |
72 |
LocalDate yesterday = LocalDate.now().minusDays(1);
|
|
|
73 |
Map<Integer, String> retailerNameMap = retailerService
|
|
|
74 |
.getAllFofoRetailerIdNameMap(new ArrayList<>(stores.keySet()));
|
|
|
75 |
for (int partnerId : stores.keySet()) {
|
|
|
76 |
UserWallet uw = userWalletRepository.selectByRetailerId(partnerId);
|
|
|
77 |
List<UserWalletHistory> walletHistory = userWalletHistoryRepository.selectByWalletIdAndDate(uw.getId(),
|
|
|
78 |
yesterday);
|
| 24810 |
amit.gupta |
79 |
Map<WalletReferenceType, List<UserWalletHistory>> referenceWiseWalletHistory = walletHistory.stream()
|
|
|
80 |
.collect(Collectors.groupingBy(x -> x.getReferenceType(), Collectors.toList()));
|
|
|
81 |
|
|
|
82 |
for (Map.Entry<WalletReferenceType, List<UserWalletHistory>> entry : referenceWiseWalletHistory
|
|
|
83 |
.entrySet()) {
|
|
|
84 |
LocalDate dateToReconcile = yesterday;
|
| 24783 |
amit.gupta |
85 |
List<UserWalletHistory> history = entry.getValue();
|
| 24810 |
amit.gupta |
86 |
List<Serializable> reconciliation = Arrays.asList(retailerNameMap.get(partnerId));
|
|
|
87 |
Map<Integer, Integer> referenceWalletMap = entry.getValue().stream().collect(
|
|
|
88 |
Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
|
|
89 |
switch (entry.getKey()) {
|
|
|
90 |
case PURCHASE:
|
|
|
91 |
reconciliation.addAll(
|
|
|
92 |
reconcileOrdersAndWallet(uw.getUserId(), dateToReconcile, referenceWalletMap, history));
|
|
|
93 |
break;
|
|
|
94 |
case SCHEME_IN:
|
|
|
95 |
reconciliation.addAll(reconcileSchemeInAndWallet(uw.getUserId(), dateToReconcile,
|
|
|
96 |
referenceWalletMap, history, schemeTypeMap.get(SchemeType.IN)));
|
|
|
97 |
break;
|
|
|
98 |
case SCHEME_OUT:
|
|
|
99 |
break;
|
|
|
100 |
default:
|
|
|
101 |
break;
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.get(0));
|
| 24783 |
amit.gupta |
105 |
rows.add(reconciliation);
|
|
|
106 |
}
|
|
|
107 |
}
|
| 24810 |
amit.gupta |
108 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Name", "Wallet amount consumed",
|
|
|
109 |
"Ordered Total", "Cancelled Total", "Refunded Total", "Scheme In to Wallet", "Scheme In disbursed", "Scheme In rolledback",
|
|
|
110 |
"Scheme Out to Wallet", "Scheme Out Disbursed", "Scheme Out Rolledback"), rows);
|
| 24783 |
amit.gupta |
111 |
|
| 24810 |
amit.gupta |
112 |
Utils.sendMailWithAttachment(mailSender, new String[] { "amit.gupta@shop2020.in" }, new String[] {},
|
|
|
113 |
reconciled ? "Reconciled Successfully" : "Reconciliation failed", "Report attached",
|
|
|
114 |
String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())),
|
|
|
115 |
new ByteArrayResource(baos.toByteArray()));
|
| 24783 |
amit.gupta |
116 |
}
|
|
|
117 |
|
| 24810 |
amit.gupta |
118 |
private List<? extends Serializable> reconcileOrdersAndWallet(int fofoId, LocalDate localDate,
|
|
|
119 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history) throws Exception {
|
| 24783 |
amit.gupta |
120 |
|
|
|
121 |
int totalWalletConsumed = 0;
|
|
|
122 |
float cancelledAmount = 0;
|
|
|
123 |
float returnedAmount = 0;
|
|
|
124 |
float totalDeductedAmount = 0;
|
|
|
125 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
|
|
126 |
List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
|
|
|
127 |
for (Order o : orders) {
|
|
|
128 |
if (o.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
|
|
129 |
if (Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED)
|
|
|
130 |
.contains(o.getStatus())) {
|
|
|
131 |
cancelledAmount += o.getWalletAmount();
|
|
|
132 |
} else if (o.getRefundTimestamp() != null
|
|
|
133 |
&& o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
|
|
134 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
|
|
135 |
if (returnedOrder == null) {
|
|
|
136 |
cancelledAmount += o.getWalletAmount();
|
|
|
137 |
} else {
|
|
|
138 |
returnedAmount += returnedOrder.getTotalPrice();
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
totalDeductedAmount += o.getWalletAmount();
|
|
|
142 |
|
|
|
143 |
} else if (o.getRefundTimestamp() != null && o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
|
|
144 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
|
|
145 |
if (returnedOrder == null) {
|
|
|
146 |
cancelledAmount += o.getWalletAmount();
|
|
|
147 |
} else {
|
|
|
148 |
returnedAmount += returnedOrder.getTotalPrice();
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
totalWalletConsumed -= transactionsOnThatDate.get(transactionId);
|
| 24810 |
amit.gupta |
153 |
|
| 24783 |
amit.gupta |
154 |
}
|
| 24810 |
amit.gupta |
155 |
boolean reconciled = Math
|
|
|
156 |
.abs(totalWalletConsumed - (totalDeductedAmount - cancelledAmount - returnedAmount)) < 2;
|
| 24783 |
amit.gupta |
157 |
|
| 24810 |
amit.gupta |
158 |
return Arrays.asList(reconciled, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount);
|
| 24783 |
amit.gupta |
159 |
|
|
|
160 |
}
|
|
|
161 |
|
| 24810 |
amit.gupta |
162 |
private List<? extends Serializable> reconcileSchemeInAndWallet(int fofoId, LocalDate localDate,
|
|
|
163 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
| 24783 |
amit.gupta |
164 |
|
| 24810 |
amit.gupta |
165 |
int totalSchemeInWalletCredited = 0;
|
|
|
166 |
float schemeInAmountAdded = 0;
|
|
|
167 |
float schemeInAmountRolledBack = 0;
|
|
|
168 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
|
|
169 |
List<InventoryItem> inventoryItems = inventoryRepository.selectByPurchaseId(transactionId).stream()
|
|
|
170 |
.filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
|
|
171 |
Set<Integer> inventoryItemIds = inventoryItems.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
172 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
|
|
173 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
|
|
174 |
totalSchemeInWalletCredited += transactionsOnThatDate.get(transactionId);
|
|
|
175 |
for (SchemeInOut sio : sios) {
|
|
|
176 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
|
|
177 |
schemeInAmountAdded += sio.getAmount();
|
|
|
178 |
}
|
|
|
179 |
if (sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
|
|
180 |
schemeInAmountAdded += sio.getAmount();
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
}
|
|
|
185 |
boolean reconciled = Math.abs(totalSchemeInWalletCredited - (schemeInAmountAdded - schemeInAmountRolledBack)) < 5;
|
|
|
186 |
|
|
|
187 |
return Arrays.asList(reconciled, totalSchemeInWalletCredited, schemeInAmountAdded, schemeInAmountRolledBack);
|
|
|
188 |
|
| 24783 |
amit.gupta |
189 |
}
|
|
|
190 |
|
| 24810 |
amit.gupta |
191 |
private void reconcileSchemeOutAndWallet() {
|
| 24783 |
amit.gupta |
192 |
|
|
|
193 |
}
|
|
|
194 |
}
|