Subversion Repositories SmartDukaan

Rev

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