Subversion Repositories SmartDukaan

Rev

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

Rev 25266 Rev 25395
Line 25... Line 25...
25
 
25
 
26
@Component
26
@Component
27
public class WalletServiceImpl implements WalletService {
27
public class WalletServiceImpl implements WalletService {
28
 
28
 
29
	private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
29
	private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
30
	
30
 
31
	private boolean underMaintainance = false;
31
	private boolean underMaintainance = false;
32
	ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet", "Wallet is under maintainance, please try after some time");
32
	ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet",
-
 
33
			"Wallet is under maintainance, please try after some time");
33
 
34
 
34
	@Autowired
35
	@Autowired
35
	private UserAccountRepository userAccountRepository;
36
	private UserAccountRepository userAccountRepository;
36
 
37
 
37
	@Autowired
38
	@Autowired
Line 44... Line 45...
44
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
45
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
45
			String description, float amount) throws ProfitMandiBusinessException {
46
			String description, float amount) throws ProfitMandiBusinessException {
46
		if (amount == 0)
47
		if (amount == 0)
47
			return;
48
			return;
48
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
49
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
49
		//userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
50
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
50
		userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
51
		userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
51
		userWalletRepository.persist(userWallet);
52
		userWalletRepository.persist(userWallet);
52
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
53
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
53
	}
54
	}
54
 
55
 
55
	@Override
56
	@Override
56
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
57
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
57
			String description, float amount) throws ProfitMandiBusinessException {
58
			String description, float amount) throws ProfitMandiBusinessException {
58
		if(underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE)) {
59
		if (underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE)) {
59
			throw pbse;
60
			throw pbse;
60
		}
61
		}
61
		if (amount == 0)
62
		if (amount == 0)
62
			return;
63
			return;
63
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
64
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
64
		//userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
65
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
65
		if (amount > userWallet.getAmount()) {
66
		if (amount > userWallet.getAmount()) {
66
			LOGGER.error("Wallet Balance is insufficient!");
67
			LOGGER.error("Wallet Balance is insufficient!");
67
			throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
68
			throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
68
		}
69
		}
69
		userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
70
		userWallet.setAmount(userWallet.getAmount() - Math.round(amount));
Line 71... Line 72...
71
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
72
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
72
	}
73
	}
73
 
74
 
74
	@Override
75
	@Override
75
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
76
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
76
			WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException{
77
			WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
77
		
78
 
78
		if (amountToRollback == 0)
79
		if (amountToRollback == 0)
79
			return;
80
			return;
80
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
81
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
81
		//userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
82
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
82
		userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
83
		userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
83
		userWalletRepository.persist(userWallet);
84
		userWalletRepository.persist(userWallet);
84
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
85
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
85
				walletReferenceType, rollbackReason);
86
				walletReferenceType, rollbackReason);
86
 
87
 
Line 100... Line 101...
100
		userWalletHistoryRepository.persist(userWalletHistory);
101
		userWalletHistoryRepository.persist(userWalletHistory);
101
	}
102
	}
102
 
103
 
103
	@Override
104
	@Override
104
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
105
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
105
		if(underMaintainance) {
106
		if (underMaintainance) {
106
			throw pbse;
107
			throw pbse;
107
		}
108
		}
108
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
109
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
109
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
110
		return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
110
	}
111
	}
111
 
112
 
112
	@Override
113
	@Override
113
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
114
	public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
114
		if(underMaintainance) {
115
		if (underMaintainance) {
115
			throw pbse;
116
			throw pbse;
116
		}
117
		}
117
		UserWallet userWallet = this.getUserWalletByUserId(userId);
118
		UserWallet userWallet = this.getUserWalletByUserId(userId);
118
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
119
		List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
119
		return userWalletHistories;
120
		return userWalletHistories;
120
	}
121
	}
121
 
122
 
122
	@Override
123
	@Override
123
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
124
	public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
124
			throws ProfitMandiBusinessException {
125
			throws ProfitMandiBusinessException {
125
		if(underMaintainance) {
126
		if (underMaintainance) {
126
			throw pbse;
127
			throw pbse;
127
		}
128
		}
128
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
129
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
129
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
130
		return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
130
	}
131
	}
131
 
132
 
132
	@Override
133
	@Override
133
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
134
	public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
134
			throws ProfitMandiBusinessException {
135
			throws ProfitMandiBusinessException {
135
		if(underMaintainance) {
136
		if (underMaintainance) {
136
			throw pbse;
137
			throw pbse;
137
		}
138
		}
138
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
139
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
139
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
140
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
140
	}
141
	}
141
 
142
 
142
	@Override
143
	@Override
143
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
144
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
144
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
145
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
145
			throws ProfitMandiBusinessException {
146
			throws ProfitMandiBusinessException {
146
		if(underMaintainance) {
147
		if (underMaintainance) {
147
			throw pbse;
148
			throw pbse;
148
		}
149
		}
149
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
150
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
150
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
151
		return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
151
				offset, limit);
152
				offset, limit);
Line 162... Line 163...
162
	}
163
	}
163
 
164
 
164
	@Override
165
	@Override
165
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
166
	public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
166
			throws ProfitMandiBusinessException {
167
			throws ProfitMandiBusinessException {
167
		if(underMaintainance) {
168
		if (underMaintainance) {
168
			throw pbse;
169
			throw pbse;
169
		}
170
		}
170
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
171
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
171
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
172
		return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
172
	}
173
	}
173
 
174
 
174
	@Override
175
	@Override
175
	public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
176
	public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
176
		if(underMaintainance) {
177
		if (underMaintainance) {
177
			throw pbse;
178
			throw pbse;
178
		}
179
		}
179
		try {
180
		try {
180
			return userWalletRepository.selectByRetailerId(retailerId);
181
			return userWalletRepository.selectByRetailerId(retailerId);
181
		} catch (Exception e) {
182
		} catch (Exception e) {
Line 209... Line 210...
209
			creditable_amount_for_reference -= history.getAmount();
210
			creditable_amount_for_reference -= history.getAmount();
210
 
211
 
211
			LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);
212
			LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);
212
 
213
 
213
			LOGGER.info("walletAmount" + amountToRefund);
214
			LOGGER.info("walletAmount" + amountToRefund);
214
			if (creditable_amount_for_reference < amountToRefund) {
-
 
215
 
215
 
216
				LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
-
 
217
				throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID,userWallet.getUserId(), "Cant be credited back to wallet as most of it has been already credited");
-
 
218
			}
216
		}
-
 
217
		if (creditable_amount_for_reference < amountToRefund) {
219
 
218
 
-
 
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(),
220
			userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));
221
					"Cant be credited back to wallet as most of it has been already credited");
-
 
222
		}
221
 
223
 
-
 
224
		userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));
-
 
225
 
222
			LOGGER.info("userWallet" + userWallet);
226
		LOGGER.info("userWallet" + userWallet);
223
 
227
 
224
		}
-
 
225
		UserWalletHistory userWalletHistory = new UserWalletHistory();
228
		UserWalletHistory userWalletHistory = new UserWalletHistory();
226
		userWalletHistory.setAmount(Math.round(amountToRefund));
229
		userWalletHistory.setAmount(Math.round(amountToRefund));
227
		userWalletHistory.setReference(transactionId);
230
		userWalletHistory.setReference(transactionId);
228
		userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
231
		userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
229
		userWalletHistory.setWalletId(userWallet.getId());
232
		userWalletHistory.setWalletId(userWallet.getId());
Line 231... Line 234...
231
		userWalletHistory.setDescription(description);
234
		userWalletHistory.setDescription(description);
232
 
235
 
233
		LOGGER.info("all_entries" + userWalletHistory);
236
		LOGGER.info("all_entries" + userWalletHistory);
234
		userWalletHistoryRepository.persist(userWalletHistory);
237
		userWalletHistoryRepository.persist(userWalletHistory);
235
 
238
 
236
		
-
 
237
	}
239
	}
238
 
240
 
239
}
241
}