Subversion Repositories SmartDukaan

Rev

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

Rev 26256 Rev 26498
Line 4... Line 4...
4
import java.util.ArrayList;
4
import java.util.ArrayList;
5
import java.util.HashMap;
5
import java.util.HashMap;
6
import java.util.List;
6
import java.util.List;
7
import java.util.Map;
7
import java.util.Map;
8
import java.util.Set;
8
import java.util.Set;
-
 
9
import java.util.stream.Collectors;
9
 
10
 
10
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.LogManager;
11
import org.apache.logging.log4j.Logger;
12
import org.apache.logging.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Component;
14
import org.springframework.stereotype.Component;
14
 
15
 
15
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
16
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
17
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
17
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
18
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
18
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
19
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
19
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
20
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
20
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
21
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
21
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
Line 50... Line 50...
50
	@Autowired
50
	@Autowired
51
	private UserWalletHistoryRepository userWalletHistoryRepository;
51
	private UserWalletHistoryRepository userWalletHistoryRepository;
52
 
52
 
53
	@Override
53
	@Override
54
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
54
	public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
55
			String description, float amount) throws ProfitMandiBusinessException {
55
			String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
56
		if (amount == 0)
56
		if (amount == 0)
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
		userWalletRepository.persist(userWallet);
62
		userWalletRepository.persist(userWallet);
63
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
63
		this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
64
	}
64
	}
65
 
65
 
66
	@Override
66
	@Override
67
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
67
	public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
68
			String description, float amount) throws ProfitMandiBusinessException {
68
			String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
69
		if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
69
		if ((underMaintainance && referenceType.equals(WalletReferenceType.PURCHASE) || !isActive(retailerId))) {
70
			throw pbse;
70
			throw pbse;
71
		}
71
		}
72
		if (amount == 0)
72
		if (amount == 0)
73
			return;
73
			return;
Line 78... Line 78...
78
			LOGGER.error("Wallet Balance is insufficient!");
78
			LOGGER.error("Wallet Balance is insufficient!");
79
			throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
79
			throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
80
		}
80
		}
81
		userWallet.setAmount(walletAmount - Math.round(amount));
81
		userWallet.setAmount(walletAmount - Math.round(amount));
82
		userWalletRepository.persist(userWallet);
82
		userWalletRepository.persist(userWallet);
83
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
83
		this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
84
	}
84
	}
85
 
85
 
86
	@Override
86
	@Override
87
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
87
	public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
88
			WalletReferenceType walletReferenceType, String rollbackReason) throws ProfitMandiBusinessException {
88
			WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
89
 
89
 
90
		if (amountToRollback == 0)
90
		if (amountToRollback == 0)
91
			return;
91
			return;
92
		int walletAmount = walletService.getWalletAmount(retailerId);
92
		int walletAmount = walletService.getWalletAmount(retailerId);
93
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
93
		UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
-
 
94
		List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
-
 
95
				.stream().filter(x->x.getWalletId()==userWallet.getId()).collect(Collectors.toList());
-
 
96
		if(uwh.size() == 0) {
-
 
97
			LOGGER.info("Retailer with id {} dont have valid reference and reference type {}", 
-
 
98
					retailerId, rollbackReference, walletReferenceType);
-
 
99
			throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
-
 
100
		}
94
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
101
		// userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
95
		userWallet.setAmount(walletAmount - Math.round(amountToRollback));
102
		userWallet.setAmount(walletAmount - Math.round(amountToRollback));
96
		userWalletRepository.persist(userWallet);
-
 
97
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
103
		this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
98
				walletReferenceType, rollbackReason);
104
				walletReferenceType, rollbackReason, businessTime);
-
 
105
		userWalletRepository.persist(userWallet);
99
 
106
 
100
	}
107
	}
101
 
108
 
102
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
109
	private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
103
			String description) {
110
			String description, LocalDateTime businessTimestamp) {
104
		if (amount == 0)
111
		if (amount == 0)
105
			return;
112
			return;
106
		UserWalletHistory userWalletHistory = new UserWalletHistory();
113
		UserWalletHistory userWalletHistory = new UserWalletHistory();
107
		userWalletHistory.setWalletId(walletId);
114
		userWalletHistory.setWalletId(walletId);
108
		userWalletHistory.setAmount(Math.round(amount));
115
		userWalletHistory.setAmount(Math.round(amount));
109
		userWalletHistory.setReference(referenceId);
116
		userWalletHistory.setReference(referenceId);
110
		userWalletHistory.setReferenceType(referenceType);
117
		userWalletHistory.setReferenceType(referenceType);
111
		userWalletHistory.setTimestamp(LocalDateTime.now());
118
		userWalletHistory.setTimestamp(LocalDateTime.now());
112
		userWalletHistory.setDescription(description);
119
		userWalletHistory.setDescription(description);
-
 
120
		userWalletHistory.setBusinessTimestamp(businessTimestamp);
113
		userWalletHistoryRepository.persist(userWalletHistory);
121
		userWalletHistoryRepository.persist(userWalletHistory);
114
	}
122
	}
115
 
123
 
116
	@Override
124
	@Override
117
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
125
	public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
Line 249... Line 257...
249
		userWalletHistory.setReference(transactionId);
257
		userWalletHistory.setReference(transactionId);
250
		userWalletHistory.setReferenceType(walletReferenceType);
258
		userWalletHistory.setReferenceType(walletReferenceType);
251
		userWalletHistory.setWalletId(userWallet.getId());
259
		userWalletHistory.setWalletId(userWallet.getId());
252
		userWalletHistory.setTimestamp(LocalDateTime.now());
260
		userWalletHistory.setTimestamp(LocalDateTime.now());
253
		userWalletHistory.setDescription(description);
261
		userWalletHistory.setDescription(description);
-
 
262
		userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
254
 
263
 
255
		userWalletHistoryRepository.persist(userWalletHistory);
264
		userWalletHistoryRepository.persist(userWalletHistory);
256
		return true;
265
		return true;
257
 
266
 
258
	}
267
	}