Subversion Repositories SmartDukaan

Rev

Rev 35811 | Rev 35824 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
29577 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
30859 tejbeer 3
import java.math.BigDecimal;
30014 tejbeer 4
import java.time.LocalDateTime;
5
 
6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.MediaType;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.transaction.annotation.Transactional;
15
import org.springframework.web.bind.annotation.PathVariable;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
 
29577 amit.gupta 19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
29812 tejbeer 21
import com.spice.profitmandi.common.services.mandii.AccountStatusResponseOut;
22
import com.spice.profitmandi.common.services.mandii.EligibilityStatusEnum;
29577 amit.gupta 23
import com.spice.profitmandi.common.services.mandii.MandiiService;
24
import com.spice.profitmandi.common.web.util.ResponseSender;
29812 tejbeer 25
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
26
import com.spice.profitmandi.dao.entity.dtr.CreditStatus;
29577 amit.gupta 27
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
30859 tejbeer 28
import com.spice.profitmandi.dao.entity.transaction.Loan;
29
import com.spice.profitmandi.dao.entity.transaction.SDCreditRequirement;
29577 amit.gupta 30
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
30859 tejbeer 31
import com.spice.profitmandi.dao.model.SDCreditResponseOut;
29577 amit.gupta 32
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
29812 tejbeer 33
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
29577 amit.gupta 34
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
35
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
30859 tejbeer 36
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
29577 amit.gupta 37
import com.spice.profitmandi.dao.repository.transaction.PaymentRepository;
30859 tejbeer 38
import com.spice.profitmandi.dao.repository.transaction.SDCreditRequirementRepository;
29577 amit.gupta 39
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
30859 tejbeer 40
import com.spice.profitmandi.service.transaction.SDCreditService;
29577 amit.gupta 41
import com.spice.profitmandi.service.wallet.WalletService;
42
 
43
@Controller
44
@Transactional(rollbackFor = Throwable.class)
45
public class GatewayController {
46
 
47
	private static final Logger log = LogManager.getLogger(GatewayController.class);
48
 
49
	@Autowired
50
	UserWalletRepository userWalletRepository;
29812 tejbeer 51
 
29577 amit.gupta 52
	@Autowired
53
	PaymentRepository paymentRepository;
54
 
55
	@Autowired
56
	AddWalletRequestRepository addWalletRequestRepository;
57
 
58
	@Autowired
59
	WalletService walletService;
60
 
61
	@Autowired
62
	FofoStoreRepository fofoStoreRepository;
29812 tejbeer 63
 
29577 amit.gupta 64
	@Autowired
65
	private UserAccountRepository userAccountRepository;
66
 
67
	@Autowired
68
	private MandiiService mandiiService;
69
 
70
	@Autowired
71
	ResponseSender<?> responseSender;
72
 
29812 tejbeer 73
	@Autowired
74
	CreditAccountRepository creditAccountRepository;
29577 amit.gupta 75
 
30859 tejbeer 76
	@Autowired
77
	SDCreditService sdCreditService;
78
 
79
	@Autowired
80
	SDCreditRequirementRepository sdCreditRequirementRepository;
81
 
82
	@Autowired
83
	LoanRepository loanRepository;
84
 
29577 amit.gupta 85
	@RequestMapping(value = "payment/gateway/status/{gateway}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
29812 tejbeer 86
	public ResponseEntity<?> getLoanStatus(HttpServletRequest request, @PathVariable Gateway gateway)
87
			throws ProfitMandiBusinessException {
29577 amit.gupta 88
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
89
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
90
		FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
29812 tejbeer 91
		if (gateway != null && gateway.equals(Gateway.MANDII)) {
29577 amit.gupta 92
			try {
29812 tejbeer 93
 
94
				AccountStatusResponseOut accountStatusResponseOut = mandiiService.getStatus(fs.getPan());
29834 tejbeer 95
 
29813 tejbeer 96
				CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(retailerId,
97
						Gateway.MANDII);
29812 tejbeer 98
 
99
				if (creditAccount == null) {
100
 
101
					creditAccount = new CreditAccount();
102
 
103
					creditAccount.setFofoId(retailerId);
104
					creditAccount.setGateway(Gateway.MANDII);
105
 
106
				}
29834 tejbeer 107
 
108
				if (accountStatusResponseOut == null) {
109
					creditAccount.setCreditStatus(CreditStatus.UNKNOWN);
110
					creditAccount.setDescription("User company not found");
111
 
29812 tejbeer 112
				} else {
29834 tejbeer 113
					creditAccount.setSanctionedAmount(accountStatusResponseOut.getSanctionLimit().floatValue());
114
					creditAccount.setInterestRate(accountStatusResponseOut.getRateOfInterest());
115
					creditAccount.setAvailableAmount(accountStatusResponseOut.getBalanceAmount().floatValue());
116
					creditAccount.setDescription(accountStatusResponseOut.getCurrentStage().toString());
117
					if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.SANCTION_AVAILABLE)) {
118
						creditAccount.setCreditStatus(CreditStatus.SANCTIONED);
119
					} else if (accountStatusResponseOut.getStatus().equals(EligibilityStatusEnum.IN_ELIGIBLE)) {
120
						creditAccount.setCreditStatus(CreditStatus.INELIGIBLE);
121
					} else {
29812 tejbeer 122
 
29834 tejbeer 123
						creditAccount.setCreditStatus(CreditStatus.TO_BE_EVALUATED);
124
					}
29812 tejbeer 125
				}
29834 tejbeer 126
 
127
				creditAccount.setUpdatedOn(LocalDateTime.now());
128
 
29812 tejbeer 129
				creditAccountRepository.persist(creditAccount);
130
				return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 131
			} catch (Exception e) {
29928 amit.gupta 132
				log.info("Trace {}\n{}", e.getMessage(), e);
30014 tejbeer 133
				throw new ProfitMandiBusinessException("Loan Provider", gateway, "Can't fetch details for provider");
29577 amit.gupta 134
			}
30859 tejbeer 135
		} else if (gateway != null && gateway.equals(Gateway.SDDIRECT)) {
136
			CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(retailerId,
137
					Gateway.SDDIRECT);
138
 
35811 amit 139
			com.spice.profitmandi.dao.model.CreditSummary creditSummary = sdCreditService.getCreditSummary(retailerId);
30929 tejbeer 140
 
30859 tejbeer 141
			SDCreditRequirement sdCreditRequirement = sdCreditRequirementRepository.selectByFofoId(retailerId);
142
 
143
			AccountStatusResponseOut accountStatusResponseOut = new AccountStatusResponseOut();
30864 tejbeer 144
			BigDecimal availability = BigDecimal.ZERO;
145
			if (creditAccount != null) {
30859 tejbeer 146
 
30864 tejbeer 147
				availability = sdCreditService.getAvailableAmount(retailerId);
148
				creditAccount.setAvailableAmount(availability.floatValue());
30859 tejbeer 149
 
30864 tejbeer 150
				log.info("availability {}", availability);
151
				accountStatusResponseOut.setBalanceAmount(availability);
30859 tejbeer 152
 
30864 tejbeer 153
			}
154
 
35811 amit 155
			if (availability.signum() == 1 && creditAccount.isActive() && creditSummary.getOverdueCount() == 0) {
30859 tejbeer 156
				accountStatusResponseOut.setStatus(EligibilityStatusEnum.SANCTION_AVAILABLE);
157
				accountStatusResponseOut.setRateOfInterest(creditAccount.getInterestRate());
33798 ranu 158
				accountStatusResponseOut.setPenalRateOfInterest(ProfitMandiConstants.NEW_DELAYED_INTEREST_RATE);
30859 tejbeer 159
				accountStatusResponseOut.setCreditDays(sdCreditRequirement.getCreditDays());
35823 amit 160
			} else if (creditAccount != null && creditAccount.isActive() && creditSummary.getOverdueCount() > 0) {
161
				double freshMoney = sdCreditService.getAvailableFreshMoney(retailerId);
162
				if (freshMoney > 0 && availability.signum() == 1) {
163
					BigDecimal effectiveLimit = availability.min(BigDecimal.valueOf(freshMoney));
164
					accountStatusResponseOut.setStatus(EligibilityStatusEnum.SANCTION_AVAILABLE);
165
					accountStatusResponseOut.setBalanceAmount(effectiveLimit);
166
					accountStatusResponseOut.setRateOfInterest(creditAccount.getInterestRate());
167
					accountStatusResponseOut.setPenalRateOfInterest(ProfitMandiConstants.NEW_DELAYED_INTEREST_RATE);
168
					accountStatusResponseOut.setCreditDays(sdCreditRequirement.getCreditDays());
169
					accountStatusResponseOut.setStatusDescription(String.format(
170
							"%d loan(s) overdue. Purchase restricted up to Rs. %.0f",
171
							creditSummary.getOverdueCount(), effectiveLimit));
172
				} else {
173
					accountStatusResponseOut.setStatus(EligibilityStatusEnum.IN_ELIGIBLE);
174
					accountStatusResponseOut.setStatusDescription(String.format(
175
							"Due date have been exceeded for %d loans. Pls clear the overdues to utilize balance credit",
176
							creditSummary.getOverdueCount()));
177
				}
30859 tejbeer 178
			} else {
179
				accountStatusResponseOut.setStatus(EligibilityStatusEnum.IN_ELIGIBLE);
180
			}
181
			return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 182
		}
30014 tejbeer 183
 
184
		else {
185
			throw new ProfitMandiBusinessException("Provider", "Empty", "Provider can't be empty");
186
		}
29577 amit.gupta 187
	}
30014 tejbeer 188
 
30859 tejbeer 189
	@RequestMapping(value = "payment/gateway/sddirect/summary", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
190
	public ResponseEntity<?> getSdDirectLoan(HttpServletRequest request) throws ProfitMandiBusinessException {
191
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
192
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
193
		SDCreditResponseOut sdCreditResponseOut = sdCreditService.sdDirectService(retailerId);
194
		return responseSender.ok(sdCreditResponseOut);
195
	}
35059 amit 196
	@RequestMapping(value = "payment/gateway/sddirect/settle-loan/{loanId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
197
	public ResponseEntity<?> settleLoan(HttpServletRequest request, @PathVariable int loanId) throws ProfitMandiBusinessException {
198
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
199
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
200
		Loan loan = loanRepository.selectByLoanId(loanId);
201
		if(loan.getFofoId()!=retailerId) {
202
			throw new ProfitMandiBusinessException("Unauthorised", "Unauthorised", "Unauthorised");
203
		}
204
		sdCreditService.settleLoan(loan);
205
		return responseSender.ok(walletService.getWalletAmount(retailerId));
206
	}
30859 tejbeer 207
 
29577 amit.gupta 208
}