Subversion Repositories SmartDukaan

Rev

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