Subversion Repositories SmartDukaan

Rev

Rev 35458 | Rev 35823 | 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());
160
			} else {
161
				accountStatusResponseOut.setStatus(EligibilityStatusEnum.IN_ELIGIBLE);
35811 amit 162
				String statusDescription = null;
163
				if (creditSummary.getOverdueCount() > 0) {
164
					statusDescription = String.format("Due date have been exceeded for %d loans. Pls clear the overdues to utilize balance credit", creditSummary.getOverdueCount());
31773 amit.gupta 165
				}
166
				accountStatusResponseOut.setStatusDescription(statusDescription);
30859 tejbeer 167
			}
168
			return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 169
		}
30014 tejbeer 170
 
171
		else {
172
			throw new ProfitMandiBusinessException("Provider", "Empty", "Provider can't be empty");
173
		}
29577 amit.gupta 174
	}
30014 tejbeer 175
 
30859 tejbeer 176
	@RequestMapping(value = "payment/gateway/sddirect/summary", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
177
	public ResponseEntity<?> getSdDirectLoan(HttpServletRequest request) throws ProfitMandiBusinessException {
178
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
179
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
180
		SDCreditResponseOut sdCreditResponseOut = sdCreditService.sdDirectService(retailerId);
181
		return responseSender.ok(sdCreditResponseOut);
182
	}
35059 amit 183
	@RequestMapping(value = "payment/gateway/sddirect/settle-loan/{loanId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
184
	public ResponseEntity<?> settleLoan(HttpServletRequest request, @PathVariable int loanId) throws ProfitMandiBusinessException {
185
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
186
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
187
		Loan loan = loanRepository.selectByLoanId(loanId);
188
		if(loan.getFofoId()!=retailerId) {
189
			throw new ProfitMandiBusinessException("Unauthorised", "Unauthorised", "Unauthorised");
190
		}
191
		sdCreditService.settleLoan(loan);
192
		return responseSender.ok(walletService.getWalletAmount(retailerId));
193
	}
30859 tejbeer 194
 
29577 amit.gupta 195
}