Subversion Repositories SmartDukaan

Rev

Rev 24509 | Rev 24768 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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