Subversion Repositories SmartDukaan

Rev

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