Subversion Repositories SmartDukaan

Rev

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

Rev 30066 Rev 30449
Line 57... Line 57...
57
			return;
57
			return;
58
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
58
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
59
		int walletAmount = walletService.getWalletAmount(retailerId);
59
		int walletAmount = walletService.getWalletAmount(retailerId);
60
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
60
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
61
		userWallet.setAmount(walletAmount + Math.round(amount));
61
		userWallet.setAmount(walletAmount + Math.round(amount));
-
 
62
		if (amount > 0) {
62
		notificationService.sendNotification(retailerId, "walletcredit", MessageType.wallet, "Rs." + FormattingUtils.formatDecimal(amount) + " credited in SD Wallet", description);
63
			notificationService.sendNotification(retailerId, "walletcredit", MessageType.wallet, "Rs." + FormattingUtils.formatDecimal(amount) + " credited in SD Wallet", description);
-
 
64
		}
63
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
65
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
64
	}
66
	}
65
 
67
 
66
	@Override
68
	@Override
67
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
69
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
68
			String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
70
										String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
69
		if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
71
		if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
70
			throw pbse;
72
			throw pbse;
71
		}
73
		}
72
		if (amount == 0)
74
		if (amount == 0)
73
			return;
75
			return;
Line 83... Line 85...
83
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
85
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
84
	}
86
	}
85
 
87
 
86
	@Override
88
	@Override
87
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
89
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
88
			WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
90
										 WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
89
 
91
 
90
		if (amountToRollback == 0)
92
		if (amountToRollback == 0)
91
			return;
93
			return;
92
		int walletAmount = walletService.getWalletAmount(retailerId);
94
		int walletAmount = walletService.getWalletAmount(retailerId);
93
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
95
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
94
		List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
96
		List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
95
				.stream().filter(x->x.getWalletId()==userWallet.getId()).collect(Collectors.toList());
97
				.stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
96
		if(uwh.size() == 0) {
98
		if (uwh.size() == 0) {
97
			LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}", 
99
			LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
98
					retailerId, rollbackReference, walletReferenceType);
100
					retailerId, rollbackReference, walletReferenceType);
99
			throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
101
			throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
100
		}
102
		}
101
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
103
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
102
		userWallet.setAmount(walletAmount - Math.round(amountToRollback));
104
		userWallet.setAmount(walletAmount - Math.round(amountToRollback));
Line 105... Line 107...
105
		userWalletRepository.persist(userWallet);
107
		userWalletRepository.persist(userWallet);
106
 
108
 
107
	}
109
	}
108
 
110
 
109
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
111
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
110
			String description, LocalDateTime businessTimestamp) {
112
										 String description, LocalDateTime businessTimestamp) {
111
		if (amount == 0)
113
		if (amount == 0)
112
			return;
114
			return;
113
		UserWalletHistory userWalletHistory = new UserWalletHistory();
115
		UserWalletHistory userWalletHistory = new UserWalletHistory();
114
		userWalletHistory.setWalletId(walletId);
116
		userWalletHistory.setWalletId(walletId);
115
		userWalletHistory.setAmount(Math.round(amount));
117
		userWalletHistory.setAmount(Math.round(amount));
Line 172... Line 174...
172
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
174
		return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
173
	}
175
	}
174
 
176
 
175
	@Override
177
	@Override
176
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
178
	public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
177
			LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
179
																			 LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
178
			throws ProfitMandiBusinessException {
180
			throws ProfitMandiBusinessException {
179
		if (underMaintainance) {
181
		if (underMaintainance) {
180
			throw pbse;
182
			throw pbse;
181
		}
183
		}
182
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
184
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
Line 191... Line 193...
191
		for (UserWallet userWallet : userWallets) {
193
		for (UserWallet userWallet : userWallets) {
192
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
194
			retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
193
		}
195
		}
194
		return retailerIdUserWalletMap;
196
		return retailerIdUserWalletMap;
195
	}
197
	}
196
	
198
 
197
	@Override
199
	@Override
198
	public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) {
200
	public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) {
199
		List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
201
		List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
200
		Map<Integer, Integer> walletRetailerMap = new HashMap<>();
202
		Map<Integer, Integer> walletRetailerMap = new HashMap<>();
201
		for (UserWallet userWallet : userWallets) {
203
		for (UserWallet userWallet : userWallets) {
Line 234... Line 236...
234
	@Override
236
	@Override
235
	public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
237
	public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
236
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
238
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
237
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
239
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
238
	}
240
	}
239
	
241
 
240
	@Override
242
	@Override
241
	public float getOpeningTillExcludingPurchase(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
243
	public float getOpeningTillExcludingPurchase(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
242
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
244
		UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
243
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date) -userWalletHistoryRepository.getSumTillDate(wallet.getId(), date, WalletReferenceType.PURCHASE);
245
		return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date) - userWalletHistoryRepository.getSumTillDate(wallet.getId(), date, WalletReferenceType.PURCHASE);
244
	}
246
	}
245
	
247
 
246
	@Override
248
	@Override
247
	public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
249
	public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
248
			WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
250
								  WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
249
 
251
 
250
		List<UserWalletHistory> all_entries = userWalletHistoryRepository
252
		List<UserWalletHistory> all_entries = userWalletHistoryRepository
251
				.selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
253
				.selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
252
 
254
 
253
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
255
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
254
		int walletAmount = walletService.getWalletAmount(retailerId);
256
		int walletAmount = walletService.getWalletAmount(retailerId);
255
		LOGGER.info("userWallet" + userWallet);
257
		LOGGER.info("userWallet" + userWallet);
256
		int max_eligible_credit_amount = 0;
258
		int max_eligible_credit_amount = 0;
257
 
259
 
258
		LOGGER.info("all_entries {}",all_entries);
260
		LOGGER.info("all_entries {}", all_entries);
259
		for (UserWalletHistory history : all_entries) {
261
		for (UserWalletHistory history : all_entries) {
260
			max_eligible_credit_amount -= history.getAmount();
262
			max_eligible_credit_amount -= history.getAmount();
261
		}
263
		}
262
		if (max_eligible_credit_amount < amountToRefund) {
264
		if (max_eligible_credit_amount < amountToRefund) {
263
			LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
265
			LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
264
			return false;
266
			return false;
265
		}
267
		}
266
		
268
 
267
		userWallet.setAmount(walletAmount + Math.round(amountToRefund));
269
		userWallet.setAmount(walletAmount + Math.round(amountToRefund));
268
 
270
 
269
		LOGGER.info("userWallet" + userWallet);
271
		LOGGER.info("userWallet" + userWallet);
270
 
272
 
271
		UserWalletHistory userWalletHistory = new UserWalletHistory();
273
		UserWalletHistory userWalletHistory = new UserWalletHistory();