Subversion Repositories SmartDukaan

Rev

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