Subversion Repositories SmartDukaan

Rev

Rev 25515 | Rev 25547 | 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
 
3
import java.time.LocalDateTime;
23110 ashik.ali 4
import java.util.HashMap;
22550 ashik.ali 5
import java.util.List;
23110 ashik.ali 6
import java.util.Map;
7
import java.util.Set;
22550 ashik.ali 8
 
24509 amit.gupta 9
import org.apache.logging.log4j.LogManager;
23568 govind 10
import org.apache.logging.log4j.Logger;
22550 ashik.ali 11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Component;
13
 
14
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24739 tejbeer 15
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23269 ashik.ali 16
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
25542 amit.gupta 17
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
22550 ashik.ali 18
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
19
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
20
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
25542 amit.gupta 21
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
22550 ashik.ali 22
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
23
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
24
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
25
 
22857 ashik.ali 26
import in.shop2020.model.v1.order.WalletReferenceType;
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
 
25515 amit.gupta 33
	private boolean underMaintainance = true;
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;
24739 tejbeer 39
 
22550 ashik.ali 40
	@Autowired
22925 ashik.ali 41
	private UserWalletRepository userWalletRepository;
24739 tejbeer 42
 
22550 ashik.ali 43
	@Autowired
25542 amit.gupta 44
	private FofoStoreRepository fofoStoreRepository;
45
 
46
	@Autowired
22925 ashik.ali 47
	private UserWalletHistoryRepository userWalletHistoryRepository;
24739 tejbeer 48
 
22550 ashik.ali 49
	@Override
24739 tejbeer 50
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
51
			String description, float amount) throws ProfitMandiBusinessException {
52
		if (amount == 0)
53
			return;
25265 amit.gupta 54
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25395 tejbeer 55
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
23456 amit.gupta 56
		userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
22857 ashik.ali 57
		userWalletRepository.persist(userWallet);
23456 amit.gupta 58
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 59
	}
24739 tejbeer 60
 
22857 ashik.ali 61
	@Override
62
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
63
			String description, float amount) throws ProfitMandiBusinessException {
25542 amit.gupta 64
		if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
25247 amit.gupta 65
			throw pbse;
66
		}
24739 tejbeer 67
		if (amount == 0)
68
			return;
22857 ashik.ali 69
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25395 tejbeer 70
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
24739 tejbeer 71
		if (amount > userWallet.getAmount()) {
22857 ashik.ali 72
			LOGGER.error("Wallet Balance is insufficient!");
73
			throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
74
		}
23509 amit.gupta 75
		userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
22857 ashik.ali 76
		userWalletRepository.persist(userWallet);
23509 amit.gupta 77
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 78
	}
23509 amit.gupta 79
 
80
	@Override
24739 tejbeer 81
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
25395 tejbeer 82
			WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
83
 
24739 tejbeer 84
		if (amountToRollback == 0)
85
			return;
25265 amit.gupta 86
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
25395 tejbeer 87
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
23509 amit.gupta 88
		userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
89
		userWalletRepository.persist(userWallet);
24739 tejbeer 90
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
91
				walletReferenceType, rollbackReason);
92
 
23509 amit.gupta 93
	}
24739 tejbeer 94
 
95
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
96
			String description) {
97
		if (amount == 0)
98
			return;
22857 ashik.ali 99
		UserWalletHistory userWalletHistory = new UserWalletHistory();
100
		userWalletHistory.setWalletId(walletId);
23444 amit.gupta 101
		userWalletHistory.setAmount(Math.round(amount));
22857 ashik.ali 102
		userWalletHistory.setReference(referenceId);
103
		userWalletHistory.setReferenceType(referenceType);
104
		userWalletHistory.setTimestamp(LocalDateTime.now());
105
		userWalletHistory.setDescription(description);
24990 amit.gupta 106
		userWalletHistoryRepository.persist(userWalletHistory);
22857 ashik.ali 107
	}
24739 tejbeer 108
 
22857 ashik.ali 109
	@Override
22550 ashik.ali 110
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
25542 amit.gupta 111
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
112
 
113
		if (underMaintainance || !isActive(userAccount.getAccountKey())) {
25247 amit.gupta 114
			throw pbse;
115
		}
23269 ashik.ali 116
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
22550 ashik.ali 117
	}
118
 
119
	@Override
120
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
25542 amit.gupta 121
		if (underMaintainance || !isActive(userId)) {
25247 amit.gupta 122
			throw pbse;
123
		}
22550 ashik.ali 124
		UserWallet userWallet = this.getUserWalletByUserId(userId);
125
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
126
		return userWalletHistories;
127
	}
24739 tejbeer 128
 
25542 amit.gupta 129
	private boolean isActive(int userId) {
130
		boolean active = true;
131
		try {
132
			FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
133
			active = fs.isActive();
134
		} catch (Exception e) {
135
 
136
		}
137
		return active;
138
	}
139
 
22550 ashik.ali 140
	@Override
141
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
142
			throws ProfitMandiBusinessException {
25542 amit.gupta 143
		if (underMaintainance || !isActive(retailerId)) {
25247 amit.gupta 144
			throw pbse;
145
		}
22550 ashik.ali 146
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
147
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
148
	}
24739 tejbeer 149
 
22550 ashik.ali 150
	@Override
24739 tejbeer 151
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
152
			throws ProfitMandiBusinessException {
25395 tejbeer 153
		if (underMaintainance) {
25247 amit.gupta 154
			throw pbse;
155
		}
22550 ashik.ali 156
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
157
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
158
	}
24739 tejbeer 159
 
22550 ashik.ali 160
	@Override
161
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
162
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
163
			throws ProfitMandiBusinessException {
25542 amit.gupta 164
		if (underMaintainance || !isActive(retailerId)) {
25247 amit.gupta 165
			throw pbse;
166
		}
22550 ashik.ali 167
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
24739 tejbeer 168
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
169
				offset, limit);
22550 ashik.ali 170
	}
24739 tejbeer 171
 
23110 ashik.ali 172
	@Override
173
	public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
174
		List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
175
		Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
24739 tejbeer 176
		for (UserWallet userWallet : userWallets) {
23110 ashik.ali 177
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
178
		}
179
		return retailerIdUserWalletMap;
180
	}
24739 tejbeer 181
 
23504 ashik.ali 182
	@Override
24739 tejbeer 183
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
184
			throws ProfitMandiBusinessException {
25542 amit.gupta 185
		if (underMaintainance || !isActive(retailerId)) {
25247 amit.gupta 186
			throw pbse;
187
		}
23504 ashik.ali 188
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
189
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
190
	}
22550 ashik.ali 191
 
23509 amit.gupta 192
	@Override
25249 amit.gupta 193
	public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
25395 tejbeer 194
		if (underMaintainance) {
25249 amit.gupta 195
			throw pbse;
196
		}
23509 amit.gupta 197
		try {
198
			return userWalletRepository.selectByRetailerId(retailerId);
24739 tejbeer 199
		} catch (Exception e) {
23509 amit.gupta 200
			UserWallet uw = new UserWallet();
201
			uw.setAmount(0);
202
			uw.setRefundableAmount(0);
203
			uw.setUserId(retailerId);
204
			userWalletRepository.persist(uw);
205
			return uw;
206
		}
207
	}
208
 
24509 amit.gupta 209
	@Override
210
	public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
211
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
212
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
213
	}
24739 tejbeer 214
 
215
	@Override
216
	public void refundToWallet(Integer retailerId, Float amountToRefund, Integer transactionId,
217
			WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
218
 
219
		List<UserWalletHistory> all_entries = userWalletHistoryRepository
220
				.selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
221
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
222
 
223
		LOGGER.info("userWallet" + userWallet);
224
		int creditable_amount_for_reference = 0;
225
 
226
		for (UserWalletHistory history : all_entries) {
227
			creditable_amount_for_reference -= history.getAmount();
228
 
229
			LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);
230
 
231
			LOGGER.info("walletAmount" + amountToRefund);
232
 
25395 tejbeer 233
		}
234
		if (creditable_amount_for_reference < amountToRefund) {
24739 tejbeer 235
 
25395 tejbeer 236
			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(),
238
					"Cant be credited back to wallet as most of it has been already credited");
239
		}
24739 tejbeer 240
 
25395 tejbeer 241
		userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));
24739 tejbeer 242
 
25395 tejbeer 243
		LOGGER.info("userWallet" + userWallet);
244
 
24739 tejbeer 245
		UserWalletHistory userWalletHistory = new UserWalletHistory();
246
		userWalletHistory.setAmount(Math.round(amountToRefund));
247
		userWalletHistory.setReference(transactionId);
248
		userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
249
		userWalletHistory.setWalletId(userWallet.getId());
250
		userWalletHistory.setTimestamp(LocalDateTime.now());
251
		userWalletHistory.setDescription(description);
252
 
253
		LOGGER.info("all_entries" + userWalletHistory);
24990 amit.gupta 254
		userWalletHistoryRepository.persist(userWalletHistory);
24739 tejbeer 255
 
256
	}
257
 
22550 ashik.ali 258
}