| Line 18... |
Line 18... |
| 18 |
|
18 |
|
| 19 |
import com.spice.profitmandi.common.util.FileUtil;
|
19 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 20 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
20 |
import com.spice.profitmandi.common.util.FormattingUtils;
|
| 21 |
import com.spice.profitmandi.common.util.Utils;
|
21 |
import com.spice.profitmandi.common.util.Utils;
|
| 22 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
22 |
import com.spice.profitmandi.dao.entity.catalog.Scheme;
|
| - |
|
23 |
import com.spice.profitmandi.dao.entity.fofo.FofoLineItem;
|
| 23 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
24 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 24 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
25 |
import com.spice.profitmandi.dao.entity.fofo.SchemeInOut;
|
| 25 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
26 |
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.ReturnOrder;
|
| 27 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
28 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
| 28 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
29 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 29 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
30 |
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
|
| 30 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
31 |
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
|
| 31 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
32 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| - |
|
33 |
import com.spice.profitmandi.dao.repository.fofo.FofoLineItemRepository;
|
| - |
|
34 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
| 32 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
35 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 33 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
36 |
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
|
| 34 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
37 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 35 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
|
38 |
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
|
| 36 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
39 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
| Line 43... |
Line 46... |
| 43 |
@Component
|
46 |
@Component
|
| 44 |
public class Reconciliation {
|
47 |
public class Reconciliation {
|
| 45 |
@Autowired
|
48 |
@Autowired
|
| 46 |
private FofoStoreRepository fofoStoreRepository;
|
49 |
private FofoStoreRepository fofoStoreRepository;
|
| 47 |
@Autowired
|
50 |
@Autowired
|
| - |
|
51 |
private FofoOrderItemRepository fofoOrderItemRepository;
|
| - |
|
52 |
@Autowired
|
| - |
|
53 |
private FofoLineItemRepository fofoLineItemRepository;
|
| - |
|
54 |
@Autowired
|
| 48 |
private UserWalletRepository userWalletRepository;
|
55 |
private UserWalletRepository userWalletRepository;
|
| 49 |
@Autowired
|
56 |
@Autowired
|
| 50 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
57 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
| 51 |
@Autowired
|
58 |
@Autowired
|
| 52 |
private SchemeInOutRepository schemeInOutRepository;
|
59 |
private SchemeInOutRepository schemeInOutRepository;
|
| Line 81... |
Line 88... |
| 81 |
|
88 |
|
| 82 |
for (Map.Entry<WalletReferenceType, List<UserWalletHistory>> entry : referenceWiseWalletHistory
|
89 |
for (Map.Entry<WalletReferenceType, List<UserWalletHistory>> entry : referenceWiseWalletHistory
|
| 83 |
.entrySet()) {
|
90 |
.entrySet()) {
|
| 84 |
LocalDate dateToReconcile = yesterday;
|
91 |
LocalDate dateToReconcile = yesterday;
|
| 85 |
List<UserWalletHistory> history = entry.getValue();
|
92 |
List<UserWalletHistory> history = entry.getValue();
|
| 86 |
List<Serializable> reconciliation = Arrays.asList(retailerNameMap.get(partnerId));
|
93 |
List<Serializable> reconciliation = Arrays.asList(partnerId, retailerNameMap.get(partnerId),
|
| - |
|
94 |
dateToReconcile);
|
| 87 |
Map<Integer, Integer> referenceWalletMap = entry.getValue().stream().collect(
|
95 |
Map<Integer, Integer> referenceWalletMap = entry.getValue().stream().collect(
|
| 88 |
Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
96 |
Collectors.groupingBy(x -> x.getReference(), Collectors.summingInt(x -> x.getAmount())));
|
| 89 |
switch (entry.getKey()) {
|
97 |
switch (entry.getKey()) {
|
| 90 |
case PURCHASE:
|
98 |
case PURCHASE:
|
| 91 |
reconciliation.addAll(
|
99 |
reconciliation.addAll(
|
| Line 94... |
Line 102... |
| 94 |
case SCHEME_IN:
|
102 |
case SCHEME_IN:
|
| 95 |
reconciliation.addAll(reconcileSchemeInAndWallet(uw.getUserId(), dateToReconcile,
|
103 |
reconciliation.addAll(reconcileSchemeInAndWallet(uw.getUserId(), dateToReconcile,
|
| 96 |
referenceWalletMap, history, schemeTypeMap.get(SchemeType.IN)));
|
104 |
referenceWalletMap, history, schemeTypeMap.get(SchemeType.IN)));
|
| 97 |
break;
|
105 |
break;
|
| 98 |
case SCHEME_OUT:
|
106 |
case SCHEME_OUT:
|
| - |
|
107 |
reconciliation.addAll(reconcileSchemeOutAndWallet(uw.getUserId(), dateToReconcile,
|
| - |
|
108 |
referenceWalletMap, history, schemeTypeMap.get(SchemeType.OUT)));
|
| 99 |
break;
|
109 |
break;
|
| 100 |
default:
|
110 |
default:
|
| 101 |
break;
|
111 |
break;
|
| 102 |
|
112 |
|
| 103 |
}
|
113 |
}
|
| 104 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.get(0));
|
114 |
reconciled = reconciled || Boolean.TRUE.equals(reconciliation.get(0));
|
| 105 |
rows.add(reconciliation);
|
115 |
rows.add(reconciliation);
|
| 106 |
}
|
116 |
}
|
| 107 |
}
|
117 |
}
|
| 108 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Name", "Wallet amount consumed",
|
118 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("PartnerId", "Store Name",
|
| - |
|
119 |
"Reconciliation Date", "Purchase Reconciled", "Wallet amount consumed", "Ordered Total",
|
| 109 |
"Ordered Total", "Cancelled Total", "Refunded Total", "Scheme In to Wallet", "Scheme In disbursed", "Scheme In rolledback",
|
120 |
"Cancelled Total", "Refunded Total", "", "Scheme In to Wallet", "Scheme In disbursed",
|
| 110 |
"Scheme Out to Wallet", "Scheme Out Disbursed", "Scheme Out Rolledback"), rows);
|
121 |
"Scheme In rolledback", "Scheme Out to Wallet", "Scheme Out Disbursed", "Scheme Out Rolledback"), rows);
|
| 111 |
|
122 |
|
| 112 |
Utils.sendMailWithAttachment(mailSender, new String[] { "amit.gupta@shop2020.in" }, new String[] {},
|
123 |
Utils.sendMailWithAttachment(mailSender, new String[] { "amit.gupta@shop2020.in" }, new String[] {},
|
| 113 |
reconciled ? "Reconciled Successfully" : "Reconciliation failed", "Report attached",
|
124 |
reconciled ? "Reconciled Successfully" : "Reconciliation failed", "Report attached",
|
| 114 |
String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())),
|
125 |
String.format("reconciliation-%s.csv", FormattingUtils.formatDate(yesterday.atStartOfDay())),
|
| 115 |
new ByteArrayResource(baos.toByteArray()));
|
126 |
new ByteArrayResource(baos.toByteArray()));
|
| Line 153... |
Line 164... |
| 153 |
|
164 |
|
| 154 |
}
|
165 |
}
|
| 155 |
boolean reconciled = Math
|
166 |
boolean reconciled = Math
|
| 156 |
.abs(totalWalletConsumed - (totalDeductedAmount - cancelledAmount - returnedAmount)) < 2;
|
167 |
.abs(totalWalletConsumed - (totalDeductedAmount - cancelledAmount - returnedAmount)) < 2;
|
| 157 |
|
168 |
|
| 158 |
return Arrays.asList(reconciled, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount);
|
169 |
return Arrays.asList(reconciled, totalWalletConsumed, totalDeductedAmount, cancelledAmount, returnedAmount, "");
|
| 159 |
|
170 |
|
| 160 |
}
|
171 |
}
|
| 161 |
|
172 |
|
| 162 |
private List<? extends Serializable> reconcileSchemeInAndWallet(int fofoId, LocalDate localDate,
|
173 |
private List<? extends Serializable> reconcileSchemeInAndWallet(int fofoId, LocalDate localDate,
|
| 163 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
174 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
| Line 175... |
Line 186... |
| 175 |
for (SchemeInOut sio : sios) {
|
186 |
for (SchemeInOut sio : sios) {
|
| 176 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
187 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
| 177 |
schemeInAmountAdded += sio.getAmount();
|
188 |
schemeInAmountAdded += sio.getAmount();
|
| 178 |
}
|
189 |
}
|
| 179 |
if (sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
190 |
if (sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
| 180 |
schemeInAmountAdded += sio.getAmount();
|
191 |
schemeInAmountRolledBack += sio.getAmount();
|
| 181 |
}
|
192 |
}
|
| 182 |
}
|
193 |
}
|
| 183 |
|
194 |
|
| 184 |
}
|
195 |
}
|
| - |
|
196 |
boolean reconciled = Math
|
| 185 |
boolean reconciled = Math.abs(totalSchemeInWalletCredited - (schemeInAmountAdded - schemeInAmountRolledBack)) < 5;
|
197 |
.abs(totalSchemeInWalletCredited - (schemeInAmountAdded - schemeInAmountRolledBack)) < 5;
|
| 186 |
|
198 |
|
| 187 |
return Arrays.asList(reconciled, totalSchemeInWalletCredited, schemeInAmountAdded, schemeInAmountRolledBack);
|
199 |
return Arrays.asList(reconciled, totalSchemeInWalletCredited, schemeInAmountAdded, schemeInAmountRolledBack,
|
| - |
|
200 |
"");
|
| 188 |
|
201 |
|
| 189 |
}
|
202 |
}
|
| 190 |
|
203 |
|
| - |
|
204 |
private List<? extends Serializable> reconcileSchemeOutAndWallet(int fofoId, LocalDate localDate,
|
| - |
|
205 |
Map<Integer, Integer> transactionsOnThatDate, List<UserWalletHistory> history, Set<Integer> schemeIds) {
|
| 191 |
private void reconcileSchemeOutAndWallet() {
|
206 |
int totalSchemeOutWalletCredited = 0;
|
| - |
|
207 |
float schemeOutAmountAdded = 0;
|
| - |
|
208 |
float schemeOutAmountRolledBack = 0;
|
| - |
|
209 |
for (int fofoOrderId : transactionsOnThatDate.keySet()) {
|
| - |
|
210 |
Set<Integer> fofoOrderItemIds = fofoOrderItemRepository.selectByOrderId(fofoOrderId).stream()
|
| - |
|
211 |
.map(x -> x.getId()).collect(Collectors.toSet());
|
| - |
|
212 |
List<FofoLineItem> fofoLineItems = fofoLineItemRepository.selectByFofoOrderItemIds(fofoOrderItemIds);
|
| - |
|
213 |
Set<Integer> inventoryItemIds = fofoLineItems.stream().map(x -> x.getInventoryItemId())
|
| - |
|
214 |
.collect(Collectors.toSet());
|
| - |
|
215 |
List<SchemeInOut> sios = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds))
|
| - |
|
216 |
.stream().filter(x -> schemeIds.contains(x.getSchemeId())).collect(Collectors.toList());
|
| - |
|
217 |
totalSchemeOutWalletCredited += transactionsOnThatDate.get(fofoOrderId);
|
| - |
|
218 |
for (SchemeInOut sio : sios) {
|
| - |
|
219 |
if (sio.getCreateTimestamp().toLocalDate().equals(localDate)) {
|
| - |
|
220 |
schemeOutAmountAdded += sio.getAmount();
|
| - |
|
221 |
}
|
| - |
|
222 |
if (sio.getRolledBackTimestamp().toLocalDate().equals(localDate)) {
|
| - |
|
223 |
schemeOutAmountRolledBack += sio.getAmount();
|
| - |
|
224 |
}
|
| - |
|
225 |
}
|
| 192 |
|
226 |
}
|
| - |
|
227 |
boolean reconciled = Math
|
| - |
|
228 |
.abs(totalSchemeOutWalletCredited - (schemeOutAmountAdded - schemeOutAmountRolledBack)) < 5;
|
| - |
|
229 |
return Arrays.asList(reconciled, totalSchemeOutWalletCredited, schemeOutAmountAdded, schemeOutAmountRolledBack,
|
| - |
|
230 |
"");
|
| 193 |
}
|
231 |
}
|
| 194 |
}
|
232 |
}
|