Subversion Repositories SmartDukaan

Rev

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