Subversion Repositories SmartDukaan

Rev

Rev 31521 | Rev 32494 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22550 ashik.ali 1
package com.spice.profitmandi.service.wallet;
2
 
30066 amit.gupta 3
import com.spice.profitmandi.common.enumuration.MessageType;
22550 ashik.ali 4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30740 amit.gupta 5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
30025 amit.gupta 6
import com.spice.profitmandi.common.util.FormattingUtils;
23269 ashik.ali 7
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
25542 amit.gupta 8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
22550 ashik.ali 9
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
10
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
11
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
25542 amit.gupta 12
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
22550 ashik.ali 13
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
14
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
15
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
30025 amit.gupta 16
import com.spice.profitmandi.service.NotificationService;
30740 amit.gupta 17
import com.spice.profitmandi.service.PartnerInvestmentService;
22857 ashik.ali 18
import in.shop2020.model.v1.order.WalletReferenceType;
30025 amit.gupta 19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Component;
22857 ashik.ali 23
 
30025 amit.gupta 24
import java.time.LocalDateTime;
25
import java.util.*;
26
import java.util.stream.Collectors;
27
 
22550 ashik.ali 28
@Component
29
public class WalletServiceImpl implements WalletService {
30
 
23568 govind 31
	private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
25395 tejbeer 32
 
25557 amit.gupta 33
	private boolean underMaintainance = false;
25395 tejbeer 34
	ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet",
35
			"Wallet is under maintainance, please try after some time");
30741 amit.gupta 36
	ProfitMandiBusinessException inactivepbse = new ProfitMandiBusinessException("Wallet", "Wallet",
37
			"Investment is incomplete, please add amount to wallet to complete the investment");
24739 tejbeer 38
 
22550 ashik.ali 39
	@Autowired
22925 ashik.ali 40
	private UserAccountRepository userAccountRepository;
25609 amit.gupta 41
 
25547 amit.gupta 42
	@Autowired
43
	private WalletService walletService;
24739 tejbeer 44
 
22550 ashik.ali 45
	@Autowired
22925 ashik.ali 46
	private UserWalletRepository userWalletRepository;
24739 tejbeer 47
 
22550 ashik.ali 48
	@Autowired
25542 amit.gupta 49
	private FofoStoreRepository fofoStoreRepository;
50
 
51
	@Autowired
22925 ashik.ali 52
	private UserWalletHistoryRepository userWalletHistoryRepository;
24739 tejbeer 53
 
30025 amit.gupta 54
	@Autowired
55
	private NotificationService notificationService;
56
 
22550 ashik.ali 57
	@Override
24739 tejbeer 58
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
30025 amit.gupta 59
								  String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
31921 amit.gupta 60
		if (Math.round(amount) == 0)
24739 tejbeer 61
			return;
25265 amit.gupta 62
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25547 amit.gupta 63
		int walletAmount = walletService.getWalletAmount(retailerId);
25395 tejbeer 64
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
25547 amit.gupta 65
		userWallet.setAmount(walletAmount + Math.round(amount));
30449 amit.gupta 66
		if (amount > 0) {
67
			notificationService.sendNotification(retailerId, "walletcredit", MessageType.wallet, "Rs." + FormattingUtils.formatDecimal(amount) + " credited in SD Wallet", description);
68
		}
26498 amit.gupta 69
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
22857 ashik.ali 70
	}
24739 tejbeer 71
 
22857 ashik.ali 72
	@Override
73
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
30449 amit.gupta 74
										String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
30741 amit.gupta 75
		if (underMaintainance) {
25247 amit.gupta 76
			throw pbse;
30748 amit.gupta 77
		} else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
30741 amit.gupta 78
			throw inactivepbse;
25247 amit.gupta 79
		}
24739 tejbeer 80
		if (amount == 0)
81
			return;
22857 ashik.ali 82
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25547 amit.gupta 83
		int walletAmount = walletService.getWalletAmount(retailerId);
25395 tejbeer 84
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
31420 amit.gupta 85
		if (!WalletReferenceType.DAMAGE_PROTECTION.equals(referenceType) && amount > 2 && Math.floor(amount) > walletAmount) {
22857 ashik.ali 86
			LOGGER.error("Wallet Balance is insufficient!");
25547 amit.gupta 87
			throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
22857 ashik.ali 88
		}
25547 amit.gupta 89
		userWallet.setAmount(walletAmount - Math.round(amount));
22857 ashik.ali 90
		userWalletRepository.persist(userWallet);
26498 amit.gupta 91
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
22857 ashik.ali 92
	}
23509 amit.gupta 93
 
94
	@Override
30889 amit.gupta 95
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
96
										String description, float amount, LocalDateTime businessTime, boolean forced) throws ProfitMandiBusinessException {
97
		if (underMaintainance) {
98
			throw pbse;
99
		} else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
100
			throw inactivepbse;
101
		}
102
		if (amount == 0)
103
			return;
104
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
105
		int walletAmount = walletService.getWalletAmount(retailerId);
106
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
107
		if (!forced && amount > 2 && amount > walletAmount) {
108
			LOGGER.error("Wallet Balance is insufficient!");
109
			throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
110
		}
111
		userWallet.setAmount(walletAmount - Math.round(amount));
112
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
113
	}
114
 
115
	@Override
24739 tejbeer 116
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
30449 amit.gupta 117
										 WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
25395 tejbeer 118
 
24739 tejbeer 119
		if (amountToRollback == 0)
120
			return;
25547 amit.gupta 121
		int walletAmount = walletService.getWalletAmount(retailerId);
25265 amit.gupta 122
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
26498 amit.gupta 123
		List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
30449 amit.gupta 124
				.stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
125
		if (uwh.size() == 0) {
126
			LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
26498 amit.gupta 127
					retailerId, rollbackReference, walletReferenceType);
128
			throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
129
		}
25395 tejbeer 130
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
25547 amit.gupta 131
		userWallet.setAmount(walletAmount - Math.round(amountToRollback));
26498 amit.gupta 132
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
133
				walletReferenceType, rollbackReason, businessTime);
23509 amit.gupta 134
		userWalletRepository.persist(userWallet);
24739 tejbeer 135
 
23509 amit.gupta 136
	}
24739 tejbeer 137
 
138
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
30449 amit.gupta 139
										 String description, LocalDateTime businessTimestamp) {
24739 tejbeer 140
		if (amount == 0)
141
			return;
22857 ashik.ali 142
		UserWalletHistory userWalletHistory = new UserWalletHistory();
143
		userWalletHistory.setWalletId(walletId);
23444 amit.gupta 144
		userWalletHistory.setAmount(Math.round(amount));
22857 ashik.ali 145
		userWalletHistory.setReference(referenceId);
146
		userWalletHistory.setReferenceType(referenceType);
147
		userWalletHistory.setTimestamp(LocalDateTime.now());
148
		userWalletHistory.setDescription(description);
26498 amit.gupta 149
		userWalletHistory.setBusinessTimestamp(businessTimestamp);
24990 amit.gupta 150
		userWalletHistoryRepository.persist(userWalletHistory);
22857 ashik.ali 151
	}
24739 tejbeer 152
 
22857 ashik.ali 153
	@Override
22550 ashik.ali 154
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
25542 amit.gupta 155
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
156
 
30744 amit.gupta 157
		if (underMaintainance) {
25247 amit.gupta 158
			throw pbse;
159
		}
23269 ashik.ali 160
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
22550 ashik.ali 161
	}
162
 
163
	@Override
164
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
25542 amit.gupta 165
		if (underMaintainance || !isActive(userId)) {
25247 amit.gupta 166
			throw pbse;
167
		}
22550 ashik.ali 168
		UserWallet userWallet = this.getUserWalletByUserId(userId);
169
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
170
		return userWalletHistories;
171
	}
24739 tejbeer 172
 
30740 amit.gupta 173
	@Autowired
174
	PartnerInvestmentService partnerInvestmentService;
175
 
176
	//Definition is now changed, active also means valid investment should be ok
25542 amit.gupta 177
	private boolean isActive(int userId) {
178
		boolean active = true;
179
		try {
180
			FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
31521 amit.gupta 181
			active = fs.isActive() && partnerInvestmentService.isInvestmentOk(userId, ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE, ProfitMandiConstants.CUTOFF_INVESTMENT);
25542 amit.gupta 182
		} catch (Exception e) {
183
 
184
		}
185
		return active;
186
	}
187
 
22550 ashik.ali 188
	@Override
189
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
190
			throws ProfitMandiBusinessException {
30742 amit.gupta 191
		if (underMaintainance) {
25247 amit.gupta 192
			throw pbse;
193
		}
22550 ashik.ali 194
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
195
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
196
	}
24739 tejbeer 197
 
22550 ashik.ali 198
	@Override
24739 tejbeer 199
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
200
			throws ProfitMandiBusinessException {
25395 tejbeer 201
		if (underMaintainance) {
25247 amit.gupta 202
			throw pbse;
203
		}
22550 ashik.ali 204
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
205
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
206
	}
24739 tejbeer 207
 
22550 ashik.ali 208
	@Override
209
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
30449 amit.gupta 210
																			 LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
22550 ashik.ali 211
			throws ProfitMandiBusinessException {
25902 amit.gupta 212
		if (underMaintainance) {
25247 amit.gupta 213
			throw pbse;
214
		}
22550 ashik.ali 215
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
24739 tejbeer 216
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
217
				offset, limit);
22550 ashik.ali 218
	}
24739 tejbeer 219
 
23110 ashik.ali 220
	@Override
221
	public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
222
		List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
223
		Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
24739 tejbeer 224
		for (UserWallet userWallet : userWallets) {
23110 ashik.ali 225
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
226
		}
227
		return retailerIdUserWalletMap;
228
	}
30449 amit.gupta 229
 
28596 amit.gupta 230
	@Override
231
	public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) {
232
		List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
233
		Map<Integer, Integer> walletRetailerMap = new HashMap<>();
234
		for (UserWallet userWallet : userWallets) {
235
			walletRetailerMap.put(userWallet.getId(), userWallet.getUserId());
236
		}
237
		return walletRetailerMap;
238
	}
24739 tejbeer 239
 
23504 ashik.ali 240
	@Override
24739 tejbeer 241
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
242
			throws ProfitMandiBusinessException {
30742 amit.gupta 243
		if (underMaintainance) {
25247 amit.gupta 244
			throw pbse;
245
		}
23504 ashik.ali 246
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
247
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
248
	}
22550 ashik.ali 249
 
23509 amit.gupta 250
	@Override
25249 amit.gupta 251
	public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
25395 tejbeer 252
		if (underMaintainance) {
25249 amit.gupta 253
			throw pbse;
254
		}
23509 amit.gupta 255
		try {
256
			return userWalletRepository.selectByRetailerId(retailerId);
24739 tejbeer 257
		} catch (Exception e) {
23509 amit.gupta 258
			UserWallet uw = new UserWallet();
259
			uw.setAmount(0);
260
			uw.setRefundableAmount(0);
261
			uw.setUserId(retailerId);
262
			userWalletRepository.persist(uw);
263
			return uw;
264
		}
265
	}
266
 
24509 amit.gupta 267
	@Override
268
	public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
269
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
270
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
271
	}
30449 amit.gupta 272
 
28518 amit.gupta 273
	@Override
274
	public float getOpeningTillExcludingPurchase(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
275
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
30449 amit.gupta 276
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date) - userWalletHistoryRepository.getSumTillDate(wallet.getId(), date, WalletReferenceType.PURCHASE);
28518 amit.gupta 277
	}
30449 amit.gupta 278
 
24739 tejbeer 279
	@Override
28241 amit.gupta 280
	public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
30449 amit.gupta 281
								  WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
24739 tejbeer 282
 
283
		List<UserWalletHistory> all_entries = userWalletHistoryRepository
26037 amit.gupta 284
				.selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
26254 amit.gupta 285
 
24739 tejbeer 286
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25547 amit.gupta 287
		int walletAmount = walletService.getWalletAmount(retailerId);
24739 tejbeer 288
		LOGGER.info("userWallet" + userWallet);
26089 amit.gupta 289
		int max_eligible_credit_amount = 0;
24739 tejbeer 290
 
30449 amit.gupta 291
		LOGGER.info("all_entries {}", all_entries);
24739 tejbeer 292
		for (UserWalletHistory history : all_entries) {
26089 amit.gupta 293
			max_eligible_credit_amount -= history.getAmount();
25395 tejbeer 294
		}
26089 amit.gupta 295
		if (max_eligible_credit_amount < amountToRefund) {
26035 amit.gupta 296
			LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
26256 amit.gupta 297
			return false;
25395 tejbeer 298
		}
30449 amit.gupta 299
 
25547 amit.gupta 300
		userWallet.setAmount(walletAmount + Math.round(amountToRefund));
24739 tejbeer 301
 
25395 tejbeer 302
		LOGGER.info("userWallet" + userWallet);
303
 
24739 tejbeer 304
		UserWalletHistory userWalletHistory = new UserWalletHistory();
305
		userWalletHistory.setAmount(Math.round(amountToRefund));
306
		userWalletHistory.setReference(transactionId);
26037 amit.gupta 307
		userWalletHistory.setReferenceType(walletReferenceType);
24739 tejbeer 308
		userWalletHistory.setWalletId(userWallet.getId());
309
		userWalletHistory.setTimestamp(LocalDateTime.now());
310
		userWalletHistory.setDescription(description);
26498 amit.gupta 311
		userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
24739 tejbeer 312
 
24990 amit.gupta 313
		userWalletHistoryRepository.persist(userWalletHistory);
26256 amit.gupta 314
		return true;
24739 tejbeer 315
 
316
	}
317
 
25547 amit.gupta 318
	@Override
319
	public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
25559 amit.gupta 320
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25609 amit.gupta 321
		if (userWallet == null) {
25552 amit.gupta 322
			return 0;
323
		}
25609 amit.gupta 324
		return (int) userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
25547 amit.gupta 325
	}
326
 
25609 amit.gupta 327
	@Override
328
	public List<UserWalletHistory> getAllByReference(int fofoId, int reference, WalletReferenceType walletReferenceType)
329
			throws ProfitMandiBusinessException {
330
		UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
331
		if (userWallet == null) {
332
			return new ArrayList<UserWalletHistory>();
333
		}
334
		return userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getId(), reference,
335
				walletReferenceType);
336
	}
30677 amit.gupta 337
 
338
	@Override
339
	public void resetWallet() throws ProfitMandiBusinessException {
340
		List<UserWallet> userWallets = userWalletRepository.selectAll();
341
		for (UserWallet userWallet : userWallets) {
342
			userWallet.setAmount(this.getWalletAmount(userWallet.getUserId()));
343
 
344
		}
345
	}
22550 ashik.ali 346
}