Subversion Repositories SmartDukaan

Rev

Rev 24990 | Rev 25248 | 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
 
31
	private boolean underMaintainance = true;
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 {
25247 amit.gupta 46
		if(underMaintainance) {
47
			throw pbse;
48
		}
24739 tejbeer 49
		if (amount == 0)
50
			return;
22857 ashik.ali 51
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
23456 amit.gupta 52
		userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
22857 ashik.ali 53
		userWalletRepository.persist(userWallet);
23456 amit.gupta 54
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
22857 ashik.ali 55
	}
24739 tejbeer 56
 
22857 ashik.ali 57
	@Override
58
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
59
			String description, float amount) throws ProfitMandiBusinessException {
25247 amit.gupta 60
		if(underMaintainance) {
61
			throw pbse;
62
		}
24739 tejbeer 63
		if (amount == 0)
64
			return;
22857 ashik.ali 65
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
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,
77
			WalletReferenceType walletReferenceType, String rollbackReason) {
25247 amit.gupta 78
 
24739 tejbeer 79
		if (amountToRollback == 0)
80
			return;
23509 amit.gupta 81
		UserWallet userWallet = this.getUserWallet(retailerId);
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
175
	public UserWallet getUserWallet(int retailerId) {
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
}