Subversion Repositories SmartDukaan

Rev

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