Subversion Repositories SmartDukaan

Rev

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