Subversion Repositories SmartDukaan

Rev

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