Subversion Repositories SmartDukaan

Rev

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