Subversion Repositories SmartDukaan

Rev

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