Subversion Repositories SmartDukaan

Rev

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