Subversion Repositories SmartDukaan

Rev

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