| Line 9... |
Line 9... |
| 9 |
import java.util.List;
|
9 |
import java.util.List;
|
| 10 |
import java.util.Map;
|
10 |
import java.util.Map;
|
| 11 |
import java.util.Set;
|
11 |
import java.util.Set;
|
| 12 |
import java.util.stream.Collectors;
|
12 |
import java.util.stream.Collectors;
|
| 13 |
|
13 |
|
| - |
|
14 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 14 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
15 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 15 |
import org.apache.logging.log4j.LogManager;
|
16 |
import org.apache.logging.log4j.LogManager;
|
| 16 |
import org.apache.logging.log4j.Logger;
|
17 |
import org.apache.logging.log4j.Logger;
|
| 17 |
import org.springframework.beans.factory.annotation.Autowired;
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 18 |
import org.springframework.core.io.ByteArrayResource;
|
19 |
import org.springframework.core.io.ByteArrayResource;
|
| Line 59... |
Line 60... |
| 59 |
|
60 |
|
| 60 |
@Component
|
61 |
@Component
|
| 61 |
@Transactional(rollbackFor = Throwable.class)
|
62 |
@Transactional(rollbackFor = Throwable.class)
|
| 62 |
public class Reconciliation {
|
63 |
public class Reconciliation {
|
| 63 |
|
64 |
|
| 64 |
private static final Logger LOGGER = LogManager.getLogger(Application.class);
|
65 |
private static final Logger LOGGER = LogManager.getLogger(Application.class);
|
| 65 |
|
66 |
|
| 66 |
@Autowired
|
67 |
@Autowired
|
| 67 |
private FofoStoreRepository fofoStoreRepository;
|
68 |
private FofoStoreRepository fofoStoreRepository;
|
| 68 |
|
69 |
|
| 69 |
private static final List<WalletReferenceType> reconciliationReferenceTypes = Arrays
|
70 |
private static final List<WalletReferenceType> reconciliationReferenceTypes = Arrays
|
| 70 |
.asList(WalletReferenceType.PURCHASE, WalletReferenceType.SCHEME_IN, WalletReferenceType.SCHEME_OUT);
|
71 |
.asList(WalletReferenceType.PURCHASE, WalletReferenceType.SCHEME_IN, WalletReferenceType.SCHEME_OUT);
|
| 71 |
|
72 |
|
| 72 |
@Autowired
|
73 |
@Autowired
|
| 73 |
private PurchaseRepository purchaseRepository;
|
74 |
private PurchaseRepository purchaseRepository;
|
| 74 |
|
75 |
|
| 75 |
@Autowired
|
76 |
@Autowired
|
| 76 |
private OrderService orderService;
|
77 |
private OrderService orderService;
|
| 77 |
|
78 |
|
| 78 |
@Autowired
|
79 |
@Autowired
|
| 79 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
80 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| 80 |
@Autowired
|
81 |
@Autowired
|
| 81 |
private FofoLineItemRepository fofoLineItemRepository;
|
82 |
private FofoLineItemRepository fofoLineItemRepository;
|
| 82 |
@Autowired
|
83 |
@Autowired
|
| 83 |
private UserWalletRepository userWalletRepository;
|
84 |
private UserWalletRepository userWalletRepository;
|
| 84 |
|
85 |
|
| 85 |
@Autowired
|
86 |
@Autowired
|
| 86 |
private InventoryItemRepository inventoryItemRepository;
|
87 |
private InventoryItemRepository inventoryItemRepository;
|
| 87 |
|
88 |
|
| 88 |
@Autowired
|
89 |
@Autowired
|
| 89 |
private ScanRecordRepository scanRecordRepository;
|
90 |
private ScanRecordRepository scanRecordRepository;
|
| 90 |
@Autowired
|
91 |
@Autowired
|
| 91 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
92 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
| 92 |
@Autowired
|
93 |
@Autowired
|
| 93 |
private SchemeInOutRepository schemeInOutRepository;
|
94 |
private SchemeInOutRepository schemeInOutRepository;
|
| 94 |
@Autowired
|
95 |
@Autowired
|
| 95 |
private SchemeRepository schemeRepository;
|
96 |
private SchemeRepository schemeRepository;
|
| 96 |
@Autowired
|
97 |
|
| 97 |
private InventoryItemRepository inventoryRepository;
|
98 |
@Autowired
|
| 98 |
@Autowired
|
99 |
private RetailerService retailerService;
|
| 99 |
private RetailerService retailerService;
|
100 |
@Autowired
|
| 100 |
@Autowired
|
101 |
private ReturnOrderRepository returnOrderRepository;
|
| 101 |
private ReturnOrderRepository returnOrderRepository;
|
102 |
@Autowired
|
| 102 |
@Autowired
|
103 |
private OrderRepository orderRepository;
|
| 103 |
private OrderRepository orderRepository;
|
104 |
@Autowired
|
| 104 |
@Autowired
|
105 |
private FofoOrderRepository fofoOrderRepository;
|
| 105 |
private FofoOrderRepository fofoOrderRepository;
|
106 |
|
| 106 |
|
107 |
@Autowired
|
| 107 |
@Autowired
|
108 |
private JavaMailSender mailSender;
|
| 108 |
private JavaMailSender mailSender;
|
109 |
|
| 109 |
|
110 |
public void dailyReconciliation() throws Exception {
|
| 110 |
public void dailyReconciliation() throws Exception {
|
111 |
LocalDate date = LocalDate.now().minusDays(1);
|
| 111 |
LocalDate date = LocalDate.now().minusDays(1);
|
112 |
dailyReconciliation(date);
|
| 112 |
dailyReconciliation(date);
|
113 |
}
|
| 113 |
}
|
114 |
|
| 114 |
|
115 |
public void dailyReconciliation(LocalDate localDate) throws Exception {
|
| 115 |
public void dailyReconciliation(LocalDate localDate) throws Exception {
|
116 |
Map<SchemeType, Set<Integer>> schemeTypeMap = schemeRepository.selectAll().stream()
|
| 116 |
Map<SchemeType, Set<Integer>> schemeTypeMap = schemeRepository.selectAll().stream()
|
117 |
.collect(Collectors.groupingBy(Scheme::getType, Collectors.mapping(Scheme::getId, Collectors.toSet())));
|
| 117 |
.collect(Collectors.groupingBy(Scheme::getType, Collectors.mapping(Scheme::getId, Collectors.toSet())));
|
118 |
boolean reconciled = true;
|
| 118 |
boolean reconciled = true;
|
119 |
Map<Integer, String> stores = fofoStoreRepository.getStoresMap();
|
| 119 |
Map<Integer, String> stores = fofoStoreRepository.getStoresMap();
|
120 |
List<List<?>> rows = new ArrayList<>();
|
| 120 |
List<List<?>> rows = new ArrayList<>();
|
121 |
LocalDate yesterday = LocalDate.now().minusDays(1);
|
| 121 |
LocalDate yesterday = LocalDate.now().minusDays(1);
|
122 |
Map<Integer, String> retailerNameMap = retailerService
|
| 122 |
Map<Integer, String> retailerNameMap = retailerService
|
123 |
.getAllFofoRetailerIdNameMap(new ArrayList<>(stores.keySet()));
|
| 123 |
.getAllFofoRetailerIdNameMap(new ArrayList<>(stores.keySet()));
|
124 |
for (int partnerId : stores.keySet()) {
|
| 124 |
for (int partnerId : stores.keySet()) {
|
125 |
UserWallet uw = userWalletRepository.selectByRetailerId(partnerId);
|
| 125 |
UserWallet uw = userWalletRepository.selectByRetailerId(partnerId);
|
126 |
List<UserWalletHistory> walletHistory = userWalletHistoryRepository.selectByWalletIdAndDate(uw.getId(),
|
| 126 |
List<UserWalletHistory> walletHistory = userWalletHistoryRepository.selectByWalletIdAndDate(uw.getId(),
|
127 |
yesterday);
|
| 127 |
yesterday);
|
128 |
Map<WalletReferenceType, List<UserWalletHistory>> referenceWiseWalletHistory = walletHistory.stream()
|
| 128 |
Map<WalletReferenceType, List<UserWalletHistory>> referenceWiseWalletHistory = walletHistory.stream()
|
129 |
.collect(Collectors.groupingBy(x -> x.getReferenceType(), Collectors.toList()));
|
| 129 |
.collect(Collectors.groupingBy(x -> x.getReferenceType(), Collectors.toList()));
|
130 |
reconciliationReferenceTypes.forEach(x -> {
|
| 130 |
reconciliationReferenceTypes.forEach(x -> {
|
131 |
if (!referenceWiseWalletHistory.containsKey(x)) {
|
| 131 |
if (!referenceWiseWalletHistory.containsKey(x)) {
|
132 |
referenceWiseWalletHistory.put(x, new ArrayList<>());
|
| 132 |
referenceWiseWalletHistory.put(x, new ArrayList<>());
|
133 |
}
|
| 133 |
}
|
134 |
});
|
| 134 |
});
|
135 |
|
| 135 |
|
136 |
List<Serializable> reconciliation = new ArrayList<>();
|
| 136 |
List<Serializable> reconciliation = new ArrayList<>();
|
137 |
LocalDate dateToReconcile = yesterday;
|
| 137 |
LocalDate dateToReconcile = yesterday;
|
138 |
// "PartnerId", "Partner Name", "Reconciliation Date"
|
| 138 |
// "PartnerId", "Partner Name", "Reconciliation Date"
|
139 |
reconciliation.addAll(Arrays.asList(partnerId, retailerNameMap.get(partnerId), dateToReconcile));
|
| 139 |
reconciliation.addAll(Arrays.asList(partnerId, retailerNameMap.get(partnerId), dateToReconcile));
|
140 |
|
| 140 |
|
141 |
for (WalletReferenceType walletReferenceType : reconciliationReferenceTypes) {
|
| 141 |
for (WalletReferenceType walletReferenceType : reconciliationReferenceTypes) {
|
142 |
List<UserWalletHistory> history = referenceWiseWalletHistory.get(walletReferenceType);
|
| 142 |
List<UserWalletHistory> history = referenceWiseWalletHistory.get(walletReferenceType);
|
143 |
Map<Integer, Integer> referenceWalletMap = history.stream().collect(
|
| 143 |
Map<Integer, Integer> referenceWalletMap = history.stream().collect(
|
144 |
Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
| 144 |
Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
145 |
switch (walletReferenceType) {
|
| 145 |
switch (walletReferenceType) {
|
146 |
case PURCHASE:
|
| 146 |
case PURCHASE:
|
147 |
reconciliation.addAll(reconcileOrdersAndWallet(dateToReconcile, referenceWalletMap, history));
|
| 147 |
reconciliation.addAll(reconcileOrdersAndWallet(dateToReconcile, referenceWalletMap, history));
|
148 |
break;
|
| 148 |
break;
|
149 |
case SCHEME_IN:
|
| 149 |
case SCHEME_IN:
|
150 |
reconciliation.addAll(reconcileSchemeInAndWallet(dateToReconcile, referenceWalletMap, history,
|
| 150 |
reconciliation.addAll(reconcileSchemeInAndWallet(dateToReconcile, referenceWalletMap, history,
|
151 |
schemeTypeMap.get(SchemeType.IN)));
|
| 151 |
schemeTypeMap.get(SchemeType.IN)));
|
152 |
break;
|
| 152 |
break;
|
153 |
case SCHEME_OUT:
|
| 153 |
case SCHEME_OUT:
|
154 |
reconciliation.addAll(reconcileSchemeOutAndWallet(dateToReconcile, referenceWalletMap, history,
|
| 154 |
reconciliation.addAll(reconcileSchemeOutAndWallet(dateToReconcile, referenceWalletMap, history,
|
155 |
schemeTypeMap.get(SchemeType.OUT)));
|
| 155 |
schemeTypeMap.get(SchemeType.OUT)));
|
156 |
break;
|
| 156 |
break;
|
157 |
default:
|
| 157 |
default:
|
158 |
break;
|
| 158 |
break;
|
159 |
|
| 159 |
|
160 |
}
|
| 160 |
}
|
161 |
}
|
| 161 |
}
|
162 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.get(0));
|
| 162 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.get(0));
|
163 |
rows.add(reconciliation);
|
| 163 |
rows.add(reconciliation);
|
164 |
}
|
| 164 |
}
|
165 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("PartnerId", "Partner Name",
|
| 165 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("PartnerId", "Partner Name",
|
166 |
"Reconciliation Date", "Purchase Reconciled", "Wallet amount consumed", "Ordered Total",
|
| 166 |
"Reconciliation Date", "Purchase Reconciled", "Wallet amount consumed", "Ordered Total",
|
167 |
"Cancelled Total", "Refunded Total", "Scheme In Reconciled", "Scheme In to Wallet",
|
| 167 |
"Cancelled Total", "Refunded Total", "Scheme In Reconciled", "Scheme In to Wallet",
|
168 |
"Scheme In disbursed", "Scheme In rolledback", "SchemeOut Reconciled", "Scheme Out to Wallet",
|
| 168 |
"Scheme In disbursed", "Scheme In rolledback", "SchemeOut Reconciled", "Scheme Out to Wallet",
|
169 |
"Scheme Out Disbursed", "Scheme Out Rolledback"), rows);
|
| 169 |
"Scheme Out Disbursed", "Scheme Out Rolledback"), rows);
|
170 |
|
| 170 |
|
171 |
Utils.sendMailWithAttachment(mailSender,
|
| 171 |
Utils.sendMailWithAttachment(mailSender,
|
172 |
new String[]{"amit.gupta@shop2020.in", "neeraj.gupta@smartdukaan.com"}, new String[]{},
|
| 172 |
new String[] { "amit.gupta@shop2020.in", "neeraj.gupta@smartdukaan.com" }, new String[] {},
|
173 |
reconciled ? "Reconciled Successfully" : "Reconciliation failed", "Report attached",
|
| 173 |
reconciled ? "Reconciled Successfully" : "Reconciliation failed", "Report attached",
|
174 |
String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())),
|
| 174 |
String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())),
|
175 |
new ByteArrayResource(baos.toByteArray()));
|
| 175 |
new ByteArrayResource(baos.toByteArray()));
|
176 |
}
|
| 176 |
}
|
177 |
|
| 177 |
|
178 |
private List<? extends Serializable> reconcileOrdersAndWallet(LocalDate localDate,
|
| 178 |
private List<? extends Serializable> reconcileOrdersAndWallet(LocalDate localDate,
|
179 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history) throws Exception {
|
| 179 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history) throws Exception {
|
180 |
|
| 180 |
|
181 |
int totalWalletConsumed = 0;
|
| 181 |
int totalWalletConsumed = 0;
|
182 |
float cancelledAmount = 0;
|
| 182 |
float cancelledAmount = 0;
|
183 |
float returnedAmount = 0;
|
| 183 |
float returnedAmount = 0;
|
184 |
float totalDeductedAmount = 0;
|
| 184 |
float totalDeductedAmount = 0;
|
185 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
| 185 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
186 |
List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
|
| 186 |
List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
|
187 |
for (Order o : orders) {
|
| 187 |
for (Order o : orders) {
|
188 |
if (o.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
| 188 |
if (o.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
189 |
if (Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED)
|
| 189 |
if (Arrays.asList(OrderStatus.PAYMENT_PENDING, OrderStatus.PAYMENT_FAILED)
|
190 |
.contains(o.getStatus())) {
|
| 190 |
.contains(o.getStatus())) {
|
191 |
cancelledAmount += o.getWalletAmount();
|
| 191 |
cancelledAmount += o.getWalletAmount();
|
192 |
} else if (o.getRefundTimestamp() != null
|
| 192 |
} else if (o.getRefundTimestamp() != null
|
193 |
&& o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
| 193 |
&& o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
194 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
| 194 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
195 |
if (returnedOrder == null) {
|
| 195 |
if (returnedOrder == null) {
|
196 |
cancelledAmount += o.getWalletAmount();
|
| 196 |
cancelledAmount += o.getWalletAmount();
|
197 |
} else {
|
| 197 |
} else {
|
198 |
returnedAmount += returnedOrder.getTotalPrice();
|
| 198 |
returnedAmount += returnedOrder.getTotalPrice();
|
199 |
}
|
| 199 |
}
|
200 |
}
|
| 200 |
}
|
201 |
totalDeductedAmount += o.getWalletAmount();
|
| 201 |
totalDeductedAmount += o.getWalletAmount();
|
202 |
|
| 202 |
|
203 |
} else if (o.getRefundTimestamp() != null && o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
| 203 |
} else if (o.getRefundTimestamp() != null && o.getRefundTimestamp().toLocalDate().equals(localDate)) {
|
204 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
| 204 |
ReturnOrder returnedOrder = returnOrderRepository.selectByOrderId(o.getId());
|
205 |
if (returnedOrder == null) {
|
| 205 |
if (returnedOrder == null) {
|
206 |
cancelledAmount += o.getWalletAmount();
|
| 206 |
cancelledAmount += o.getWalletAmount();
|
207 |
} else {
|
| 207 |
} else {
|
208 |
returnedAmount += returnedOrder.getTotalPrice();
|
| 208 |
returnedAmount += returnedOrder.getTotalPrice();
|
209 |
}
|
| 209 |
}
|
210 |
}
|
| 210 |
}
|
211 |
}
|
| 211 |
}
|
212 |
totalWalletConsumed -= transactionsOnThatDate.get(transactionId);
|
| 212 |
totalWalletConsumed -= transactionsOnThatDate.get(transactionId);
|
213 |
|
| 213 |
|
214 |
}
|
| 214 |
}
|
215 |
boolean reconciled = Math
|
| 215 |
boolean reconciled = Math
|
216 |
.abs(totalWalletConsumed - (totalDeductedAmount - cancelledAmount - returnedAmount)) < 2;
|
| 216 |
.abs(totalWalletConsumed - (totalDeductedAmount - cancelledAmount - returnedAmount)) < 2;
|
217 |
|
| 217 |
|
218 |
return Arrays.asList(reconciled, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount, "");
|
| 218 |
return Arrays.asList(reconciled, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount, "");
|
219 |
|
| 219 |
|
220 |
}
|
| 220 |
}
|
221 |
|
| 221 |
|
222 |
private List<? extends Serializable> reconcileSchemeInAndWallet(LocalDate localDate,
|
| 222 |
private List<? extends Serializable> reconcileSchemeInAndWallet(LocalDate localDate,
|
223 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) throws ProfitMandiBusinessException {
|
| 223 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
224 |
|
| 224 |
|
225 |
int totalSchemeInWalletCredited = 0;
|
| 225 |
int totalSchemeInWalletCredited = 0;
|
226 |
float schemeInAmountAdded = 0;
|
| 226 |
float schemeInAmountAdded = 0;
|
227 |
float schemeInAmountRolledBack = 0;
|
| 227 |
float schemeInAmountRolledBack = 0;
|
228 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
| 228 |
for (int transactionId : transactionsOnThatDate.keySet()) {
|
229 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(transactionId).stream()
|
| 229 |
List<InventoryItem> inventoryItems = inventoryRepository.selectByPurchaseId(transactionId).stream()
|
230 |
.filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
| 230 |
.filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
231 |
Set<Integer> inventoryItemIds = inventoryItems.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
| 231 |
Set<Integer> inventoryItemIds = inventoryItems.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
232 |
if (inventoryItemIds.size() == 0) {
|
| 232 |
if (inventoryItemIds.size() == 0) {
|
233 |
continue;
|
| 233 |
continue;
|
234 |
}
|
| 234 |
}
|
235 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
| 235 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
236 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
| 236 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
237 |
totalSchemeInWalletCredited += transactionsOnThatDate.get(transactionId);
|
| 237 |
totalSchemeInWalletCredited += transactionsOnThatDate.get(transactionId);
|
238 |
for (SchemeInOut sio : sios) {
|
| 238 |
for (SchemeInOut sio : sios) {
|
239 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
| 239 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
240 |
schemeInAmountAdded += sio.getAmount();
|
| 240 |
schemeInAmountAdded += sio.getAmount();
|
241 |
}
|
| 241 |
}
|
242 |
if (sio.getRolledBackTimestamp() != null
|
| 242 |
if (sio.getRolledBackTimestamp() != null
|
243 |
&& sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
| 243 |
&& sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
244 |
schemeInAmountRolledBack += sio.getAmount();
|
| 244 |
schemeInAmountRolledBack += sio.getAmount();
|
245 |
}
|
| 245 |
}
|
246 |
}
|
| 246 |
}
|
247 |
|
| 247 |
|
248 |
}
|
| 248 |
}
|
249 |
boolean reconciled = Math
|
| 249 |
boolean reconciled = Math
|
250 |
.abs(totalSchemeInWalletCredited - (schemeInAmountAdded - schemeInAmountRolledBack)) < 5;
|
| 250 |
.abs(totalSchemeInWalletCredited - (schemeInAmountAdded - schemeInAmountRolledBack)) < 5;
|
251 |
|
| 251 |
|
252 |
return Arrays.asList(reconciled, totalSchemeInWalletCredited, schemeInAmountAdded, schemeInAmountRolledBack,
|
| 252 |
return Arrays.asList(reconciled, totalSchemeInWalletCredited, schemeInAmountAdded, schemeInAmountRolledBack,
|
253 |
"");
|
| 253 |
"");
|
254 |
|
| 254 |
|
255 |
}
|
| 255 |
}
|
256 |
|
| 256 |
|
257 |
private List<? extends Serializable> reconcileSchemeOutAndWallet(LocalDate localDate,
|
| 257 |
private List<? extends Serializable> reconcileSchemeOutAndWallet(LocalDate localDate,
|
258 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) throws ProfitMandiBusinessException {
|
| 258 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
259 |
int totalSchemeOutWalletCredited = 0;
|
| 259 |
int totalSchemeOutWalletCredited = 0;
|
260 |
float schemeOutAmountAdded = 0;
|
| 260 |
float schemeOutAmountAdded = 0;
|
261 |
float schemeOutAmountRolledBack = 0;
|
| 261 |
float schemeOutAmountRolledBack = 0;
|
262 |
for (int fofoOrderId : transactionsOnThatDate.keySet()) {
|
| 262 |
for (int fofoOrderId : transactionsOnThatDate.keySet()) {
|
263 |
Set<Integer> fofoOrderItemIds = fofoOrderItemRepository.selectByOrderId(fofoOrderId).stream()
|
| 263 |
Set<Integer> fofoOrderItemIds = fofoOrderItemRepository.selectByOrderId(fofoOrderId).stream()
|
264 |
.map(x -> x.getId()).collect(Collectors.toSet());
|
| 264 |
.map(x -> x.getId()).collect(Collectors.toSet());
|
265 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemIds(fofoOrderItemIds);
|
| 265 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemIds(fofoOrderItemIds);
|
266 |
Set<Integer> inventoryItemIds = fofoLineItems.stream().map(x -> x.getInventoryItemId())
|
| 266 |
Set<Integer> inventoryItemIds = fofoLineItems.stream().map(x -> x.getInventoryItemId())
|
267 |
.collect(Collectors.toSet());
|
| 267 |
.collect(Collectors.toSet());
|
268 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
| 268 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
269 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
| 269 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
270 |
totalSchemeOutWalletCredited += transactionsOnThatDate.get(fofoOrderId);
|
| 270 |
totalSchemeOutWalletCredited += transactionsOnThatDate.get(fofoOrderId);
|
271 |
for (SchemeInOut sio : sios) {
|
| 271 |
for (SchemeInOut sio : sios) {
|
272 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
| 272 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
273 |
schemeOutAmountAdded += sio.getAmount();
|
| 273 |
schemeOutAmountAdded += sio.getAmount();
|
274 |
}
|
| 274 |
}
|
275 |
if (sio.getRolledBackTimestamp() != null
|
| 275 |
if (sio.getRolledBackTimestamp() != null
|
276 |
&& sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
| 276 |
&& sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
277 |
schemeOutAmountRolledBack += sio.getAmount();
|
| 277 |
schemeOutAmountRolledBack += sio.getAmount();
|
278 |
}
|
| 278 |
}
|
279 |
}
|
| 279 |
}
|
280 |
}
|
| 280 |
}
|
281 |
boolean reconciled = Math
|
| 281 |
boolean reconciled = Math
|
282 |
.abs(totalSchemeOutWalletCredited - (schemeOutAmountAdded - schemeOutAmountRolledBack)) < 5;
|
| 282 |
.abs(totalSchemeOutWalletCredited - (schemeOutAmountAdded - schemeOutAmountRolledBack)) < 5;
|
283 |
return Arrays.asList(reconciled, totalSchemeOutWalletCredited, schemeOutAmountAdded, schemeOutAmountRolledBack,
|
| 283 |
return Arrays.asList(reconciled, totalSchemeOutWalletCredited, schemeOutAmountAdded, schemeOutAmountRolledBack,
|
284 |
"");
|
| 284 |
"");
|
285 |
}
|
| 285 |
}
|
286 |
|
| 286 |
|
287 |
public void reconcileExpiredFixedSchemes() throws Exception {
|
| 287 |
public void reconcileExpiredFixedSchemes() throws Exception {
|
288 |
List<Scheme> allSchemes = schemeRepository.selectAll();
|
| 288 |
List<Scheme> allSchemes = schemeRepository.selectAll();
|
289 |
// .stream().filter(x ->
|
| 289 |
// .stream().filter(x ->
|
290 |
// x.getAmountType().equals(AmountType.FIXED)).collect(Collectors.toList());
|
| 290 |
// x.getAmountType().equals(AmountType.FIXED)).collect(Collectors.toList());
|
291 |
allSchemes = allSchemes.stream()
|
| 291 |
allSchemes = allSchemes.stream()
|
292 |
.filter(x -> x.getEndDateTime().isAfter(LocalDate.of(2019, 3, 31).atStartOfDay()))
|
| 292 |
.filter(x -> x.getEndDateTime().isAfter(LocalDate.of(2019, 3, 31).atStartOfDay()))
|
293 |
.collect(Collectors.toList());
|
| 293 |
.collect(Collectors.toList());
|
294 |
System.out.println(
|
| 294 |
System.out.println(
|
295 |
"InventoryId\tSerialNumber\tItem Id\tScheme Id\tScheme Name\tScheme Type\tAmount Type\tScheme Amount\tAmount Paid\tReference\tTransaction Time\tScheme Start\tScheme End\tScheme Expiry");
|
| 295 |
"InventoryId\tSerialNumber\tItem Id\tScheme Id\tScheme Name\tScheme Type\tAmount Type\tScheme Amount\tAmount Paid\tReference\tTransaction Time\tScheme Start\tScheme End\tScheme Expiry");
|
296 |
for (Scheme scheme : allSchemes) {
|
| 296 |
for (Scheme scheme : allSchemes) {
|
297 |
if (scheme.getExpireTimestamp() != null) {
|
| 297 |
if (scheme.getExpireTimestamp() != null) {
|
298 |
if (scheme.getExpireTimestamp().isBefore(scheme.getEndDateTime())) {
|
| 298 |
if (scheme.getExpireTimestamp().isBefore(scheme.getEndDateTime())) {
|
299 |
List<SchemeInOut> inOuts = schemeInOutRepository.selectBySchemeIds(Sets.newHashSet(scheme.getId()));
|
| 299 |
List<SchemeInOut> inOuts = schemeInOutRepository.selectBySchemeIds(Sets.newHashSet(scheme.getId()));
|
300 |
if (scheme.getType().equals(SchemeType.IN)) {
|
| 300 |
if (scheme.getType().equals(SchemeType.IN)) {
|
301 |
for (SchemeInOut schemeInOut : inOuts) {
|
| 301 |
for (SchemeInOut schemeInOut : inOuts) {
|
302 |
ScanRecord inRecord = scanRecordRepository
|
| 302 |
ScanRecord inRecord = scanRecordRepository
|
303 |
.selectByInventoryItemId(schemeInOut.getInventoryItemId()).stream()
|
| 303 |
.selectByInventoryItemId(schemeInOut.getInventoryItemId()).stream()
|
304 |
.filter(x -> x.getType().equals(ScanType.PURCHASE)).collect(Collectors.toList())
|
| 304 |
.filter(x -> x.getType().equals(ScanType.PURCHASE)).collect(Collectors.toList())
|
305 |
.get(0);
|
| 305 |
.get(0);
|
306 |
if (inRecord.getCreateTimestamp().isAfter(scheme.getExpireTimestamp())) {
|
| 306 |
if (inRecord.getCreateTimestamp().isAfter(scheme.getExpireTimestamp())) {
|
307 |
InventoryItem ii = inventoryItemRepository.selectById(inRecord.getInventoryItemId());
|
| 307 |
InventoryItem ii = inventoryItemRepository.selectById(inRecord.getInventoryItemId());
|
308 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(ii.getPurchaseId(),
|
| 308 |
Purchase purchase = purchaseRepository.selectByIdAndFofoId(ii.getPurchaseId(),
|
309 |
ii.getFofoId());
|
| 309 |
ii.getFofoId());
|
310 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%f\t%f\t%d\t%s\t%s\t%s\t%s",
|
| 310 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%f\t%f\t%d\t%s\t%s\t%s\t%s",
|
311 |
ii.getId(), ii.getSerialNumber(), ii.getItemId(), schemeInOut.getSchemeId(),
|
| 311 |
ii.getId(), ii.getSerialNumber(), ii.getItemId(), schemeInOut.getSchemeId(),
|
312 |
scheme.getName(), scheme.getType(), scheme.getAmount(), schemeInOut.getAmount(),
|
| 312 |
scheme.getName(), scheme.getType(), scheme.getAmount(), schemeInOut.getAmount(),
|
313 |
purchase.getId(), inRecord.getCreateTimestamp(), scheme.getStartDateTime(),
|
| 313 |
purchase.getId(), inRecord.getCreateTimestamp(), scheme.getStartDateTime(),
|
314 |
scheme.getEndDateTime(), scheme.getExpireTimestamp()));
|
| 314 |
scheme.getEndDateTime(), scheme.getExpireTimestamp()));
|
315 |
}
|
| 315 |
}
|
316 |
|
| 316 |
|
317 |
}
|
| 317 |
}
|
318 |
}
|
| 318 |
}
|
319 |
if (scheme.getType().equals(SchemeType.OUT)) {
|
| 319 |
if (scheme.getType().equals(SchemeType.OUT)) {
|
320 |
for (SchemeInOut schemeInOut : inOuts) {
|
| 320 |
for (SchemeInOut schemeInOut : inOuts) {
|
321 |
ScanRecord outRecord = scanRecordRepository
|
| 321 |
ScanRecord outRecord = scanRecordRepository
|
322 |
.selectByInventoryItemId(schemeInOut.getInventoryItemId()).stream()
|
| 322 |
.selectByInventoryItemId(schemeInOut.getInventoryItemId()).stream()
|
323 |
.filter(x -> x.getType().equals(ScanType.SALE))
|
| 323 |
.filter(x -> x.getType().equals(ScanType.SALE))
|
324 |
.sorted((x1, x2) -> x1.getId() - x2.getId()).collect(Collectors.toList()).get(0);
|
| 324 |
.sorted((x1, x2) -> x1.getId() - x2.getId()).collect(Collectors.toList()).get(0);
|
325 |
if (outRecord.getCreateTimestamp().isAfter(scheme.getExpireTimestamp())) {
|
| 325 |
if (outRecord.getCreateTimestamp().isAfter(scheme.getExpireTimestamp())) {
|
326 |
InventoryItem ii = inventoryItemRepository.selectById(outRecord.getInventoryItemId());
|
| 326 |
InventoryItem ii = inventoryItemRepository.selectById(outRecord.getInventoryItemId());
|
327 |
FofoOrder fofoOrder;
|
| 327 |
FofoOrder fofoOrder;
|
328 |
if (outRecord.getOrderId() == 0) {
|
| 328 |
if (outRecord.getOrderId() == 0) {
|
329 |
fofoOrder = orderService.getOrderByInventoryItemId(ii.getId());
|
| 329 |
fofoOrder = orderService.getOrderByInventoryItemId(ii.getId());
|
330 |
} else {
|
| 330 |
} else {
|
331 |
fofoOrder = fofoOrderRepository.selectByOrderId(outRecord.getOrderId());
|
| 331 |
fofoOrder = fofoOrderRepository.selectByOrderId(outRecord.getOrderId());
|
332 |
if (fofoOrder == null) {
|
| 332 |
if (fofoOrder == null) {
|
333 |
LOGGER.info("Order id does not exits - {}", outRecord.getOrderId());
|
| 333 |
LOGGER.info("Order id does not exits - {}", outRecord.getOrderId());
|
334 |
continue;
|
| 334 |
continue;
|
335 |
}
|
| 335 |
}
|
336 |
}
|
| 336 |
}
|
337 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%f\t%f\t%d\t%s\t%s\t%s\t%s",
|
| 337 |
System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%f\t%f\t%d\t%s\t%s\t%s\t%s",
|
338 |
ii.getId(), ii.getSerialNumber(), ii.getItemId(), schemeInOut.getSchemeId(),
|
| 338 |
ii.getId(), ii.getSerialNumber(), ii.getItemId(), schemeInOut.getSchemeId(),
|
339 |
scheme.getName(), scheme.getType(), scheme.getAmount(), schemeInOut.getAmount(),
|
| 339 |
scheme.getName(), scheme.getType(), scheme.getAmount(), schemeInOut.getAmount(),
|
340 |
fofoOrder.getId(), outRecord.getCreateTimestamp(), scheme.getStartDateTime(),
|
| 340 |
fofoOrder.getId(), outRecord.getCreateTimestamp(), scheme.getStartDateTime(),
|
341 |
scheme.getEndDateTime(), scheme.getExpireTimestamp()));
|
| 341 |
scheme.getEndDateTime(), scheme.getExpireTimestamp()));
|
342 |
|
| 342 |
|
343 |
}
|
| 343 |
}
|
344 |
|
| 344 |
|
345 |
}
|
| 345 |
}
|
346 |
}
|
| 346 |
}
|
347 |
}
|
| 347 |
}
|
348 |
}
|
| 348 |
}
|
349 |
}
|
| 349 |
}
|
350 |
|
| 350 |
|
351 |
}
|
| 351 |
}
|
352 |
|
| 352 |
|
353 |
private void checkForDblClickIssues() {
|
| 353 |
private void checkForDblClickIssues() {
|
354 |
// For all schmes from April onwards
|
| 354 |
// For all schmes from April onwards
|
355 |
}
|
| 355 |
}
|
356 |
|
| 356 |
|
357 |
private void dbClickPurchase() {
|
| 357 |
private void dbClickPurchase() {
|
358 |
List<Purchase> purchases = purchaseRepository
|
| 358 |
List<Purchase> purchases = purchaseRepository
|
359 |
.selectAllBetweenPurchaseDate(LocalDate.of(2019, 4, 1).atStartOfDay(), LocalDateTime.now());
|
| 359 |
.selectAllBetweenPurchaseDate(LocalDate.of(2019, 4, 1).atStartOfDay(), LocalDateTime.now());
|
360 |
}
|
| 360 |
}
|
361 |
|
| 361 |
|
362 |
private void dbClickSale() {
|
| 362 |
private void dbClickSale() {
|
363 |
List<FofoOrder> sales = fofoOrderRepository.selectBetweenSaleDate(LocalDate.of(2019, 4, 1).atStartOfDay(),
|
| 363 |
List<FofoOrder> sales = fofoOrderRepository.selectBetweenSaleDate(LocalDate.of(2019, 4, 1).atStartOfDay(),
|
364 |
LocalDateTime.now());
|
| 364 |
LocalDateTime.now());
|
- |
|
| 365 |
|
365 |
|
| 366 |
}
|
366 |
}
|
| 367 |
}
|
367 |
}
|