Subversion Repositories SmartDukaan

Rev

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