| 22550 |
ashik.ali |
1 |
package com.spice.profitmandi.service.wallet;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34261 |
tejus.loha |
4 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 30740 |
amit.gupta |
5 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 34261 |
tejus.loha |
6 |
import com.spice.profitmandi.common.util.FileUtil;
|
| 23269 |
ashik.ali |
7 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 25542 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 34261 |
tejus.loha |
9 |
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
|
| 32494 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.entity.transaction.ManualPaymentType;
|
| 22550 |
ashik.ali |
11 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
|
|
12 |
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
|
| 34674 |
aman.kumar |
13 |
import com.spice.profitmandi.dao.entity.user.User;
|
| 22550 |
ashik.ali |
14 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| 34261 |
tejus.loha |
15 |
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
|
| 32494 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
|
| 25542 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 33845 |
tejus.loha |
19 |
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
|
| 33172 |
tejus.loha |
20 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 22550 |
ashik.ali |
21 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 33845 |
tejus.loha |
24 |
import com.spice.profitmandi.dao.repository.user.StoreTimelinetbRepository;
|
| 34674 |
aman.kumar |
25 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| 30025 |
amit.gupta |
26 |
import com.spice.profitmandi.service.NotificationService;
|
| 30740 |
amit.gupta |
27 |
import com.spice.profitmandi.service.PartnerInvestmentService;
|
| 34261 |
tejus.loha |
28 |
import com.spice.profitmandi.service.order.OrderService;
|
|
|
29 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 33845 |
tejus.loha |
30 |
import com.spice.profitmandi.service.user.StoreTimelineTatService;
|
| 22857 |
ashik.ali |
31 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 30025 |
amit.gupta |
32 |
import org.apache.logging.log4j.LogManager;
|
|
|
33 |
import org.apache.logging.log4j.Logger;
|
|
|
34 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 34261 |
tejus.loha |
35 |
import org.springframework.http.ResponseEntity;
|
| 30025 |
amit.gupta |
36 |
import org.springframework.stereotype.Component;
|
| 22857 |
ashik.ali |
37 |
|
| 30025 |
amit.gupta |
38 |
import java.time.LocalDateTime;
|
|
|
39 |
import java.util.*;
|
|
|
40 |
import java.util.stream.Collectors;
|
|
|
41 |
|
| 22550 |
ashik.ali |
42 |
@Component
|
|
|
43 |
public class WalletServiceImpl implements WalletService {
|
|
|
44 |
|
| 32494 |
amit.gupta |
45 |
private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
|
| 25395 |
tejbeer |
46 |
|
| 32494 |
amit.gupta |
47 |
@Autowired
|
|
|
48 |
ManualPaymentRequestRepository manualPaymentRequestRepository;
|
| 34261 |
tejus.loha |
49 |
@Autowired
|
|
|
50 |
AddWalletRequestRepository addWalletRequestRepository;
|
|
|
51 |
@Autowired
|
|
|
52 |
OrderService orderService;
|
|
|
53 |
@Autowired
|
|
|
54 |
RetailerService retailerService;
|
| 33194 |
amit.gupta |
55 |
ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet", "Wallet is under maintainance, please try after some time");
|
|
|
56 |
ProfitMandiBusinessException inactivepbse = new ProfitMandiBusinessException("Wallet", "Wallet", "Investment is incomplete, please add amount to wallet to complete the investment");
|
|
|
57 |
|
| 32494 |
amit.gupta |
58 |
@Autowired
|
|
|
59 |
PartnerInvestmentService partnerInvestmentService;
|
|
|
60 |
private boolean underMaintainance = false;
|
|
|
61 |
@Autowired
|
|
|
62 |
private UserAccountRepository userAccountRepository;
|
| 33172 |
tejus.loha |
63 |
|
| 32494 |
amit.gupta |
64 |
@Autowired
|
|
|
65 |
private UserWalletRepository userWalletRepository;
|
|
|
66 |
@Autowired
|
|
|
67 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
68 |
@Autowired
|
|
|
69 |
private UserWalletHistoryRepository userWalletHistoryRepository;
|
|
|
70 |
@Autowired
|
|
|
71 |
private NotificationService notificationService;
|
| 24739 |
tejbeer |
72 |
|
| 33172 |
tejus.loha |
73 |
@Autowired
|
|
|
74 |
RetailerRepository retailerRepository;
|
| 33845 |
tejus.loha |
75 |
@Autowired
|
|
|
76 |
PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
|
|
|
77 |
@Autowired
|
|
|
78 |
StoreTimelineTatService storeTimelineTatService;
|
|
|
79 |
@Autowired
|
|
|
80 |
StoreTimelinetbRepository storeTimelinetbRepository;
|
| 33172 |
tejus.loha |
81 |
|
| 34674 |
aman.kumar |
82 |
@Autowired
|
|
|
83 |
UserRepository userRepository;
|
|
|
84 |
|
| 32494 |
amit.gupta |
85 |
@Override
|
|
|
86 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
87 |
String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
|
|
88 |
if (Math.round(amount) == 0)
|
|
|
89 |
return;
|
| 36372 |
amit |
90 |
UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
|
| 35828 |
amit |
91 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
|
| 32494 |
amit.gupta |
92 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
|
| 36305 |
amit |
93 |
if (WalletReferenceType.AUTOMATED_ADVANCE.equals(referenceType)
|
|
|
94 |
|| WalletReferenceType.ADVANCE_AMOUNT.equals(referenceType)
|
|
|
95 |
|| WalletReferenceType.PAYMENT_GATEWAY.equals(referenceType)
|
|
|
96 |
|| WalletReferenceType.ONLINE_ORDER_PAYMENT.equals(referenceType)
|
|
|
97 |
|| WalletReferenceType.PURCHASE.equals(referenceType)) {
|
|
|
98 |
partnerInvestmentService.evictInvestmentCache(retailerId);
|
|
|
99 |
}
|
| 32494 |
amit.gupta |
100 |
}
|
| 25609 |
amit.gupta |
101 |
|
| 32494 |
amit.gupta |
102 |
@Override
|
|
|
103 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
104 |
String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
|
|
105 |
if (underMaintainance) {
|
|
|
106 |
throw pbse;
|
|
|
107 |
} else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
|
|
|
108 |
throw inactivepbse;
|
|
|
109 |
}
|
|
|
110 |
if (amount == 0)
|
|
|
111 |
return;
|
| 36372 |
amit |
112 |
UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
|
| 35828 |
amit |
113 |
int walletAmount = userWallet.getAmount();
|
| 33547 |
tejus.loha |
114 |
if (!WalletReferenceType.DAMAGE_PROTECTION.equals(referenceType) && amount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && Math.floor(amount) > walletAmount) {
|
| 34575 |
vikas.jang |
115 |
LOGGER.error("Wallet Balance is insufficient!, needed - {}, wallet has - {}, for retailer - {}", Math.floor(amount), walletAmount, retailerId);
|
| 34674 |
aman.kumar |
116 |
User user = userRepository.selectById(retailerId);
|
|
|
117 |
throw new ProfitMandiBusinessException(user.getName(), walletAmount, "WLT_1000");
|
| 32494 |
amit.gupta |
118 |
}
|
|
|
119 |
userWallet.setAmount(walletAmount - Math.round(amount));
|
|
|
120 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
|
|
|
121 |
}
|
| 24739 |
tejbeer |
122 |
|
| 32494 |
amit.gupta |
123 |
@Override
|
|
|
124 |
public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
|
|
125 |
String description, float amount, LocalDateTime businessTime, boolean forced) throws ProfitMandiBusinessException {
|
|
|
126 |
if (underMaintainance) {
|
|
|
127 |
throw pbse;
|
|
|
128 |
} else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
|
|
|
129 |
throw inactivepbse;
|
|
|
130 |
}
|
|
|
131 |
if (amount == 0)
|
|
|
132 |
return;
|
| 36372 |
amit |
133 |
UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
|
| 35828 |
amit |
134 |
int walletAmount = userWallet.getAmount();
|
| 32494 |
amit.gupta |
135 |
if (!forced && amount > 2 && amount > walletAmount) {
|
|
|
136 |
LOGGER.error("Wallet Balance is insufficient!");
|
|
|
137 |
throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
|
|
|
138 |
}
|
|
|
139 |
userWallet.setAmount(walletAmount - Math.round(amount));
|
|
|
140 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
|
|
|
141 |
}
|
| 24739 |
tejbeer |
142 |
|
| 32494 |
amit.gupta |
143 |
@Override
|
|
|
144 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
|
|
145 |
WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
|
| 25542 |
amit.gupta |
146 |
|
| 32494 |
amit.gupta |
147 |
if (amountToRollback == 0)
|
|
|
148 |
return;
|
| 36372 |
amit |
149 |
UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
|
| 35828 |
amit |
150 |
int walletAmount = userWallet.getAmount();
|
| 32494 |
amit.gupta |
151 |
List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
|
|
|
152 |
.stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
|
|
|
153 |
if (uwh.size() == 0) {
|
|
|
154 |
LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
|
|
|
155 |
retailerId, rollbackReference, walletReferenceType);
|
|
|
156 |
throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
|
|
|
157 |
}
|
|
|
158 |
userWallet.setAmount(walletAmount - Math.round(amountToRollback));
|
|
|
159 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
|
|
160 |
walletReferenceType, rollbackReason, businessTime);
|
|
|
161 |
userWalletRepository.persist(userWallet);
|
| 24739 |
tejbeer |
162 |
|
| 32494 |
amit.gupta |
163 |
}
|
| 30025 |
amit.gupta |
164 |
|
| 32494 |
amit.gupta |
165 |
private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
|
|
|
166 |
String description, LocalDateTime businessTimestamp) {
|
|
|
167 |
if (amount == 0)
|
|
|
168 |
return;
|
|
|
169 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
170 |
userWalletHistory.setWalletId(walletId);
|
|
|
171 |
userWalletHistory.setAmount(Math.round(amount));
|
|
|
172 |
userWalletHistory.setReference(referenceId);
|
|
|
173 |
userWalletHistory.setReferenceType(referenceType);
|
|
|
174 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
175 |
userWalletHistory.setDescription(description);
|
|
|
176 |
userWalletHistory.setBusinessTimestamp(businessTimestamp);
|
|
|
177 |
userWalletHistoryRepository.persist(userWalletHistory);
|
|
|
178 |
}
|
| 24739 |
tejbeer |
179 |
|
| 32494 |
amit.gupta |
180 |
@Override
|
|
|
181 |
public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
182 |
UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
|
| 23509 |
amit.gupta |
183 |
|
| 32494 |
amit.gupta |
184 |
if (underMaintainance) {
|
|
|
185 |
throw pbse;
|
|
|
186 |
}
|
|
|
187 |
return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
|
|
|
188 |
}
|
| 30889 |
amit.gupta |
189 |
|
| 32494 |
amit.gupta |
190 |
@Override
|
|
|
191 |
public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
|
|
|
192 |
if (underMaintainance || !isActive(userId)) {
|
|
|
193 |
throw pbse;
|
|
|
194 |
}
|
|
|
195 |
UserWallet userWallet = this.getUserWalletByUserId(userId);
|
|
|
196 |
List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
197 |
return userWalletHistories;
|
|
|
198 |
}
|
| 25395 |
tejbeer |
199 |
|
| 32494 |
amit.gupta |
200 |
//Definition is now changed, active also means valid investment should be ok
|
|
|
201 |
private boolean isActive(int userId) {
|
|
|
202 |
boolean active = true;
|
|
|
203 |
try {
|
|
|
204 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
|
|
|
205 |
active = fs.isActive() && partnerInvestmentService.isInvestmentOk(userId, ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE, ProfitMandiConstants.CUTOFF_INVESTMENT);
|
|
|
206 |
} catch (Exception e) {
|
| 24739 |
tejbeer |
207 |
|
| 32494 |
amit.gupta |
208 |
}
|
|
|
209 |
return active;
|
|
|
210 |
}
|
| 24739 |
tejbeer |
211 |
|
| 32494 |
amit.gupta |
212 |
@Override
|
|
|
213 |
public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
|
|
|
214 |
throws ProfitMandiBusinessException {
|
|
|
215 |
if (underMaintainance) {
|
|
|
216 |
throw pbse;
|
|
|
217 |
}
|
|
|
218 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
219 |
return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
|
|
|
220 |
}
|
| 24739 |
tejbeer |
221 |
|
| 32494 |
amit.gupta |
222 |
@Override
|
|
|
223 |
public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
|
|
|
224 |
throws ProfitMandiBusinessException {
|
|
|
225 |
if (underMaintainance) {
|
|
|
226 |
throw pbse;
|
|
|
227 |
}
|
|
|
228 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
229 |
return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
|
|
|
230 |
}
|
| 25542 |
amit.gupta |
231 |
|
| 32494 |
amit.gupta |
232 |
@Override
|
|
|
233 |
public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
|
|
|
234 |
LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
|
|
|
235 |
throws ProfitMandiBusinessException {
|
|
|
236 |
if (underMaintainance) {
|
|
|
237 |
throw pbse;
|
|
|
238 |
}
|
|
|
239 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
240 |
return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
|
|
|
241 |
offset, limit);
|
|
|
242 |
}
|
| 22550 |
ashik.ali |
243 |
|
| 32494 |
amit.gupta |
244 |
@Override
|
| 33247 |
ranu |
245 |
public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) throws ProfitMandiBusinessException {
|
| 32494 |
amit.gupta |
246 |
List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
|
|
|
247 |
Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
|
|
|
248 |
for (UserWallet userWallet : userWallets) {
|
|
|
249 |
retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
|
|
|
250 |
}
|
|
|
251 |
return retailerIdUserWalletMap;
|
|
|
252 |
}
|
| 24739 |
tejbeer |
253 |
|
| 32494 |
amit.gupta |
254 |
@Override
|
| 33247 |
ranu |
255 |
public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) throws ProfitMandiBusinessException {
|
| 32494 |
amit.gupta |
256 |
List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
|
|
|
257 |
Map<Integer, Integer> walletRetailerMap = new HashMap<>();
|
|
|
258 |
for (UserWallet userWallet : userWallets) {
|
|
|
259 |
walletRetailerMap.put(userWallet.getId(), userWallet.getUserId());
|
|
|
260 |
}
|
|
|
261 |
return walletRetailerMap;
|
|
|
262 |
}
|
| 30740 |
amit.gupta |
263 |
|
| 32494 |
amit.gupta |
264 |
@Override
|
|
|
265 |
public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
|
|
|
266 |
throws ProfitMandiBusinessException {
|
|
|
267 |
if (underMaintainance) {
|
|
|
268 |
throw pbse;
|
|
|
269 |
}
|
|
|
270 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
271 |
return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
|
|
|
272 |
}
|
| 25542 |
amit.gupta |
273 |
|
| 32494 |
amit.gupta |
274 |
@Override
|
|
|
275 |
public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
|
|
|
276 |
if (underMaintainance) {
|
|
|
277 |
throw pbse;
|
|
|
278 |
}
|
|
|
279 |
try {
|
|
|
280 |
return userWalletRepository.selectByRetailerId(retailerId);
|
|
|
281 |
} catch (Exception e) {
|
|
|
282 |
UserWallet uw = new UserWallet();
|
|
|
283 |
uw.setAmount(0);
|
|
|
284 |
uw.setRefundableAmount(0);
|
|
|
285 |
uw.setUserId(retailerId);
|
|
|
286 |
userWalletRepository.persist(uw);
|
|
|
287 |
return uw;
|
|
|
288 |
}
|
|
|
289 |
}
|
| 25542 |
amit.gupta |
290 |
|
| 32494 |
amit.gupta |
291 |
@Override
|
|
|
292 |
public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
293 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
294 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
|
|
|
295 |
}
|
| 24739 |
tejbeer |
296 |
|
| 32494 |
amit.gupta |
297 |
@Override
|
|
|
298 |
public float getOpeningTillExcludingPurchase(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
|
|
|
299 |
UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
300 |
return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date) - userWalletHistoryRepository.getSumTillDate(wallet.getId(), date, WalletReferenceType.PURCHASE);
|
|
|
301 |
}
|
| 24739 |
tejbeer |
302 |
|
| 32494 |
amit.gupta |
303 |
@Override
|
|
|
304 |
public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
|
|
|
305 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
| 24739 |
tejbeer |
306 |
|
| 32494 |
amit.gupta |
307 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
|
|
308 |
.selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
|
| 30449 |
amit.gupta |
309 |
|
| 32494 |
amit.gupta |
310 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 35828 |
amit |
311 |
int walletAmount = userWallet.getAmount();
|
| 32494 |
amit.gupta |
312 |
LOGGER.info("userWallet" + userWallet);
|
|
|
313 |
int max_eligible_credit_amount = 0;
|
| 24739 |
tejbeer |
314 |
|
| 32494 |
amit.gupta |
315 |
LOGGER.info("all_entries {}", all_entries);
|
|
|
316 |
for (UserWalletHistory history : all_entries) {
|
|
|
317 |
max_eligible_credit_amount -= history.getAmount();
|
|
|
318 |
}
|
|
|
319 |
if (max_eligible_credit_amount < amountToRefund) {
|
|
|
320 |
LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
|
|
|
321 |
return false;
|
|
|
322 |
}
|
| 22550 |
ashik.ali |
323 |
|
| 32494 |
amit.gupta |
324 |
userWallet.setAmount(walletAmount + Math.round(amountToRefund));
|
| 23509 |
amit.gupta |
325 |
|
| 32494 |
amit.gupta |
326 |
LOGGER.info("userWallet" + userWallet);
|
| 30449 |
amit.gupta |
327 |
|
| 32494 |
amit.gupta |
328 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
|
|
329 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
|
|
330 |
userWalletHistory.setReference(transactionId);
|
|
|
331 |
userWalletHistory.setReferenceType(walletReferenceType);
|
|
|
332 |
userWalletHistory.setWalletId(userWallet.getId());
|
|
|
333 |
userWalletHistory.setTimestamp(LocalDateTime.now());
|
|
|
334 |
userWalletHistory.setDescription(description);
|
|
|
335 |
userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
|
| 30449 |
amit.gupta |
336 |
|
| 32494 |
amit.gupta |
337 |
userWalletHistoryRepository.persist(userWalletHistory);
|
|
|
338 |
return true;
|
| 24739 |
tejbeer |
339 |
|
| 32494 |
amit.gupta |
340 |
}
|
| 26254 |
amit.gupta |
341 |
|
| 32494 |
amit.gupta |
342 |
@Override
|
|
|
343 |
public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
|
|
|
344 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
|
|
345 |
if (userWallet == null) {
|
| 33421 |
amit.gupta |
346 |
userWallet = new UserWallet();
|
|
|
347 |
userWallet.setUserId(retailerId);
|
|
|
348 |
userWalletRepository.persist(userWallet);
|
| 32494 |
amit.gupta |
349 |
}
|
| 35828 |
amit |
350 |
int sum = (int) userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
|
|
|
351 |
userWallet.setAmount(sum);
|
|
|
352 |
return sum;
|
| 32494 |
amit.gupta |
353 |
}
|
| 24739 |
tejbeer |
354 |
|
| 32494 |
amit.gupta |
355 |
@Override
|
|
|
356 |
public List<UserWalletHistory> getAllByReference(int fofoId, int reference, WalletReferenceType walletReferenceType)
|
|
|
357 |
throws ProfitMandiBusinessException {
|
|
|
358 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
|
|
|
359 |
if (userWallet == null) {
|
|
|
360 |
return new ArrayList<UserWalletHistory>();
|
|
|
361 |
}
|
|
|
362 |
return userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getId(), reference,
|
|
|
363 |
walletReferenceType);
|
|
|
364 |
}
|
| 30449 |
amit.gupta |
365 |
|
| 32494 |
amit.gupta |
366 |
@Override
|
|
|
367 |
public void resetWallet() throws ProfitMandiBusinessException {
|
|
|
368 |
List<UserWallet> userWallets = userWalletRepository.selectAll();
|
|
|
369 |
for (UserWallet userWallet : userWallets) {
|
|
|
370 |
userWallet.setAmount(this.getWalletAmount(userWallet.getUserId()));
|
| 24739 |
tejbeer |
371 |
|
| 32494 |
amit.gupta |
372 |
}
|
|
|
373 |
}
|
| 25395 |
tejbeer |
374 |
|
| 32494 |
amit.gupta |
375 |
@Override
|
|
|
376 |
public int getManualReference(WalletReferenceType referenceType) {
|
|
|
377 |
ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);
|
| 24739 |
tejbeer |
378 |
|
| 32494 |
amit.gupta |
379 |
if (paymentType == null) {
|
|
|
380 |
paymentType = new ManualPaymentType();
|
|
|
381 |
paymentType.setReferenceType(referenceType);
|
|
|
382 |
manualPaymentRequestRepository.persist(paymentType);
|
|
|
383 |
}
|
|
|
384 |
paymentType.setCounter(paymentType.getCounter() + 1);
|
|
|
385 |
return paymentType.getCounter();
|
| 24739 |
tejbeer |
386 |
|
| 32494 |
amit.gupta |
387 |
}
|
| 34261 |
tejus.loha |
388 |
|
|
|
389 |
@Override
|
|
|
390 |
public ResponseEntity<?> createAddWalletRequestReport(LocalDateTime startDate, LocalDateTime endDate, AddWalletRequestStatus status) throws Exception {
|
|
|
391 |
List<List<?>> rows = new ArrayList<>();
|
|
|
392 |
List<String> header = Arrays.asList(
|
|
|
393 |
"Retailer Id",
|
|
|
394 |
"Retailer Name",
|
|
|
395 |
"Email",
|
|
|
396 |
"City",
|
|
|
397 |
"State",
|
|
|
398 |
"Transaction Reference",
|
|
|
399 |
"Bank Name",
|
|
|
400 |
"Reference Date",
|
|
|
401 |
"Amount",
|
|
|
402 |
"status",
|
|
|
403 |
"Created On",
|
|
|
404 |
"Updated On"
|
|
|
405 |
);
|
|
|
406 |
|
|
|
407 |
List<AddWalletRequest> addWalletRequests = addWalletRequestRepository.selectAllRetailerIdByDateAndStatus(startDate, endDate, status);
|
|
|
408 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
|
|
409 |
for (AddWalletRequest addWalletRequest : addWalletRequests) {
|
|
|
410 |
CustomRetailer customRetailer = customRetailerMap.get(addWalletRequest.getRetailerId());
|
|
|
411 |
rows.add(Arrays
|
|
|
412 |
.asList(
|
|
|
413 |
addWalletRequest.getRetailerId(),
|
|
|
414 |
customRetailer.getBusinessName(),
|
|
|
415 |
customRetailer.getEmail(),
|
|
|
416 |
customRetailer.getAddress().getCity(),
|
|
|
417 |
customRetailer.getAddress().getState(),
|
|
|
418 |
addWalletRequest.getTransaction_reference(),
|
|
|
419 |
addWalletRequest.getBank_name(),
|
|
|
420 |
addWalletRequest.getReference_date(),
|
|
|
421 |
addWalletRequest.getAmount(),
|
|
|
422 |
addWalletRequest.getStatus(),
|
|
|
423 |
addWalletRequest.getCreateTimestamp(),
|
|
|
424 |
addWalletRequest.getUpdateTimestamp()
|
|
|
425 |
)
|
|
|
426 |
);
|
|
|
427 |
}
|
|
|
428 |
org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(header, rows);
|
|
|
429 |
ResponseEntity<?> responseEntity = orderService.downloadReportInCsv(baos, rows, "Add-wallet-request-"+status+"-Reort-" + startDate.toLocalDate() + "-To-" + endDate.toLocalDate());
|
|
|
430 |
return responseEntity;
|
|
|
431 |
}
|
| 32494 |
amit.gupta |
432 |
}
|
| 24739 |
tejbeer |
433 |
|