| Line 34... |
Line 34... |
| 34 |
ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet",
|
34 |
ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet",
|
| 35 |
"Wallet is under maintainance, please try after some time");
|
35 |
"Wallet is under maintainance, please try after some time");
|
| 36 |
|
36 |
|
| 37 |
@Autowired
|
37 |
@Autowired
|
| 38 |
private UserAccountRepository userAccountRepository;
|
38 |
private UserAccountRepository userAccountRepository;
|
| - |
|
39 |
|
| - |
|
40 |
@Autowired
|
| - |
|
41 |
private WalletService walletService;
|
| 39 |
|
42 |
|
| 40 |
@Autowired
|
43 |
@Autowired
|
| 41 |
private UserWalletRepository userWalletRepository;
|
44 |
private UserWalletRepository userWalletRepository;
|
| 42 |
|
45 |
|
| 43 |
@Autowired
|
46 |
@Autowired
|
| Line 50... |
Line 53... |
| 50 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
53 |
public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
|
| 51 |
String description, float amount) throws ProfitMandiBusinessException {
|
54 |
String description, float amount) throws ProfitMandiBusinessException {
|
| 52 |
if (amount == 0)
|
55 |
if (amount == 0)
|
| 53 |
return;
|
56 |
return;
|
| 54 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
57 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| - |
|
58 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 55 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
59 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 56 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
|
60 |
userWallet.setAmount(walletAmount + Math.round(amount));
|
| 57 |
userWalletRepository.persist(userWallet);
|
61 |
userWalletRepository.persist(userWallet);
|
| 58 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
62 |
this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 59 |
}
|
63 |
}
|
| 60 |
|
64 |
|
| 61 |
@Override
|
65 |
@Override
|
| Line 65... |
Line 69... |
| 65 |
throw pbse;
|
69 |
throw pbse;
|
| 66 |
}
|
70 |
}
|
| 67 |
if (amount == 0)
|
71 |
if (amount == 0)
|
| 68 |
return;
|
72 |
return;
|
| 69 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
73 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| - |
|
74 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 70 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
75 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 71 |
if (amount > userWallet.getAmount()) {
|
76 |
if (amount > walletAmount) {
|
| 72 |
LOGGER.error("Wallet Balance is insufficient!");
|
77 |
LOGGER.error("Wallet Balance is insufficient!");
|
| 73 |
throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
|
78 |
throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
|
| 74 |
}
|
79 |
}
|
| 75 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
|
80 |
userWallet.setAmount(walletAmount - Math.round(amount));
|
| 76 |
userWalletRepository.persist(userWallet);
|
81 |
userWalletRepository.persist(userWallet);
|
| 77 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
82 |
this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
|
| 78 |
}
|
83 |
}
|
| 79 |
|
84 |
|
| 80 |
@Override
|
85 |
@Override
|
| 81 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
86 |
public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
|
| 82 |
WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
|
87 |
WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
|
| 83 |
|
88 |
|
| 84 |
if (amountToRollback == 0)
|
89 |
if (amountToRollback == 0)
|
| 85 |
return;
|
90 |
return;
|
| - |
|
91 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 86 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
92 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 87 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
93 |
// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
|
| 88 |
userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
|
94 |
userWallet.setAmount(walletAmount - Math.round(amountToRollback));
|
| 89 |
userWalletRepository.persist(userWallet);
|
95 |
userWalletRepository.persist(userWallet);
|
| 90 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
96 |
this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
|
| 91 |
walletReferenceType, rollbackReason);
|
97 |
walletReferenceType, rollbackReason);
|
| 92 |
|
98 |
|
| 93 |
}
|
99 |
}
|
| Line 217... |
Line 223... |
| 217 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
223 |
WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
|
| 218 |
|
224 |
|
| 219 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
225 |
List<UserWalletHistory> all_entries = userWalletHistoryRepository
|
| 220 |
.selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
|
226 |
.selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
|
| 221 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
227 |
UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
|
| 222 |
|
- |
|
| - |
|
228 |
int walletAmount = walletService.getWalletAmount(retailerId);
|
| 223 |
LOGGER.info("userWallet" + userWallet);
|
229 |
LOGGER.info("userWallet" + userWallet);
|
| 224 |
int creditable_amount_for_reference = 0;
|
230 |
int creditable_amount_for_reference = 0;
|
| 225 |
|
231 |
|
| 226 |
for (UserWalletHistory history : all_entries) {
|
232 |
for (UserWalletHistory history : all_entries) {
|
| 227 |
creditable_amount_for_reference -= history.getAmount();
|
233 |
creditable_amount_for_reference -= history.getAmount();
|
| Line 236... |
Line 242... |
| 236 |
LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
|
242 |
LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
|
| 237 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, userWallet.getUserId(),
|
243 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, userWallet.getUserId(),
|
| 238 |
"Cant be credited back to wallet as most of it has been already credited");
|
244 |
"Cant be credited back to wallet as most of it has been already credited");
|
| 239 |
}
|
245 |
}
|
| 240 |
|
246 |
|
| 241 |
userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));
|
247 |
userWallet.setAmount(walletAmount + Math.round(amountToRefund));
|
| 242 |
|
248 |
|
| 243 |
LOGGER.info("userWallet" + userWallet);
|
249 |
LOGGER.info("userWallet" + userWallet);
|
| 244 |
|
250 |
|
| 245 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
251 |
UserWalletHistory userWalletHistory = new UserWalletHistory();
|
| 246 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
252 |
userWalletHistory.setAmount(Math.round(amountToRefund));
|
| Line 253... |
Line 259... |
| 253 |
LOGGER.info("all_entries" + userWalletHistory);
|
259 |
LOGGER.info("all_entries" + userWalletHistory);
|
| 254 |
userWalletHistoryRepository.persist(userWalletHistory);
|
260 |
userWalletHistoryRepository.persist(userWalletHistory);
|
| 255 |
|
261 |
|
| 256 |
}
|
262 |
}
|
| 257 |
|
263 |
|
| - |
|
264 |
@Override
|
| - |
|
265 |
public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
|
| - |
|
266 |
UserWallet userWallet = userWalletRepository.selectById(retailerId);
|
| - |
|
267 |
return (int)userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
|
| - |
|
268 |
}
|
| - |
|
269 |
|
| 258 |
}
|
270 |
}
|