Subversion Repositories SmartDukaan

Rev

Rev 36557 | 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;
37657 amit 21
import com.spice.profitmandi.common.services.sdcredit.AccountStatusResponseOut;
22
import com.spice.profitmandi.common.services.sdcredit.EligibilityStatusEnum;
29577 amit.gupta 23
import com.spice.profitmandi.common.web.util.ResponseSender;
29812 tejbeer 24
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
25
import com.spice.profitmandi.dao.entity.dtr.CreditStatus;
29577 amit.gupta 26
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
30859 tejbeer 27
import com.spice.profitmandi.dao.entity.transaction.Loan;
28
import com.spice.profitmandi.dao.entity.transaction.SDCreditRequirement;
29577 amit.gupta 29
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
35824 amit 30
import com.spice.profitmandi.dao.model.CreditSummary;
30859 tejbeer 31
import com.spice.profitmandi.dao.model.SDCreditResponseOut;
29577 amit.gupta 32
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
29812 tejbeer 33
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
29577 amit.gupta 34
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
35
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
30859 tejbeer 36
import com.spice.profitmandi.dao.repository.transaction.LoanRepository;
29577 amit.gupta 37
import com.spice.profitmandi.dao.repository.transaction.PaymentRepository;
30859 tejbeer 38
import com.spice.profitmandi.dao.repository.transaction.SDCreditRequirementRepository;
29577 amit.gupta 39
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
30859 tejbeer 40
import com.spice.profitmandi.service.transaction.SDCreditService;
35914 amit 41
import com.spice.profitmandi.service.transaction.SDCreditServiceImpl;
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
 
69
	@Autowired
70
	ResponseSender<?> responseSender;
71
 
29812 tejbeer 72
	@Autowired
73
	CreditAccountRepository creditAccountRepository;
29577 amit.gupta 74
 
30859 tejbeer 75
	@Autowired
76
	SDCreditService sdCreditService;
77
 
78
	@Autowired
79
	SDCreditRequirementRepository sdCreditRequirementRepository;
80
 
81
	@Autowired
35914 amit 82
	com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService partnerTypeChangeService;
83
 
84
	@Autowired
30859 tejbeer 85
	LoanRepository loanRepository;
86
 
29577 amit.gupta 87
	@RequestMapping(value = "payment/gateway/status/{gateway}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
29812 tejbeer 88
	public ResponseEntity<?> getLoanStatus(HttpServletRequest request, @PathVariable Gateway gateway)
89
			throws ProfitMandiBusinessException {
29577 amit.gupta 90
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
91
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
37657 amit 92
		if (gateway != null && gateway.equals(Gateway.SDDIRECT)) {
30859 tejbeer 93
			CreditAccount creditAccount = creditAccountRepository.selectByFofoIdAndGateway(retailerId,
94
					Gateway.SDDIRECT);
95
 
35811 amit 96
			com.spice.profitmandi.dao.model.CreditSummary creditSummary = sdCreditService.getCreditSummary(retailerId);
30929 tejbeer 97
 
30859 tejbeer 98
			SDCreditRequirement sdCreditRequirement = sdCreditRequirementRepository.selectByFofoId(retailerId);
99
 
35914 amit 100
			// Resolve effective credit days based on partner type (Diamond/Platinum=20, others=15)
101
			com.spice.profitmandi.dao.entity.fofo.PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(retailerId, java.time.LocalDate.now());
36077 amit 102
			int effectiveCreditDays = SDCreditServiceImpl.getTier1Days(partnerType);
35914 amit 103
 
30859 tejbeer 104
			AccountStatusResponseOut accountStatusResponseOut = new AccountStatusResponseOut();
30864 tejbeer 105
			BigDecimal availability = BigDecimal.ZERO;
106
			if (creditAccount != null) {
30859 tejbeer 107
 
30864 tejbeer 108
				availability = sdCreditService.getAvailableAmount(retailerId);
109
				creditAccount.setAvailableAmount(availability.floatValue());
30859 tejbeer 110
 
30864 tejbeer 111
				log.info("availability {}", availability);
112
				accountStatusResponseOut.setBalanceAmount(availability);
30859 tejbeer 113
 
30864 tejbeer 114
			}
115
 
35811 amit 116
			if (availability.signum() == 1 && creditAccount.isActive() && creditSummary.getOverdueCount() == 0) {
30859 tejbeer 117
				accountStatusResponseOut.setStatus(EligibilityStatusEnum.SANCTION_AVAILABLE);
118
				accountStatusResponseOut.setRateOfInterest(creditAccount.getInterestRate());
33798 ranu 119
				accountStatusResponseOut.setPenalRateOfInterest(ProfitMandiConstants.NEW_DELAYED_INTEREST_RATE);
35914 amit 120
				accountStatusResponseOut.setCreditDays(effectiveCreditDays);
35823 amit 121
			} else if (creditAccount != null && creditAccount.isActive() && creditSummary.getOverdueCount() > 0) {
35824 amit 122
				CreditSummary orderSummary = sdCreditService.getCreditSummaryForOrder(retailerId, 0);
123
				if (orderSummary.isCanPlaceOrder() && availability.signum() == 1) {
124
					// Exception partner or has fresh money — allow ordering
35823 amit 125
					accountStatusResponseOut.setStatus(EligibilityStatusEnum.SANCTION_AVAILABLE);
126
					accountStatusResponseOut.setRateOfInterest(creditAccount.getInterestRate());
127
					accountStatusResponseOut.setPenalRateOfInterest(ProfitMandiConstants.NEW_DELAYED_INTEREST_RATE);
35914 amit 128
					accountStatusResponseOut.setCreditDays(effectiveCreditDays);
35824 amit 129
					if (orderSummary.getFreshMoneyAvailable() > 0) {
130
						BigDecimal effectiveLimit = availability.min(BigDecimal.valueOf(orderSummary.getFreshMoneyAvailable()));
131
						accountStatusResponseOut.setBalanceAmount(effectiveLimit);
132
						accountStatusResponseOut.setStatusDescription(String.format(
133
								"%d loan(s) overdue. Purchase restricted up to Rs. %.0f",
134
								creditSummary.getOverdueCount(), effectiveLimit));
135
					}
35823 amit 136
				} else {
137
					accountStatusResponseOut.setStatus(EligibilityStatusEnum.IN_ELIGIBLE);
138
					accountStatusResponseOut.setStatusDescription(String.format(
139
							"Due date have been exceeded for %d loans. Pls clear the overdues to utilize balance credit",
140
							creditSummary.getOverdueCount()));
141
				}
30859 tejbeer 142
			} else {
143
				accountStatusResponseOut.setStatus(EligibilityStatusEnum.IN_ELIGIBLE);
144
			}
35914 amit 145
			if (sdCreditRequirement != null) {
146
				accountStatusResponseOut.setOverdueRateOfInterest(ProfitMandiConstants.TIER2_INTEREST_RATE);
147
				accountStatusResponseOut.setFreeDays(sdCreditRequirement.getFreeDays());
148
				accountStatusResponseOut.setCreditTerms(SDCreditServiceImpl.buildCreditTerms(
149
						sdCreditRequirement.getInterestRate(),
150
						ProfitMandiConstants.TIER2_INTEREST_RATE,
151
						ProfitMandiConstants.NEW_DELAYED_INTEREST_RATE,
36071 amit 152
						effectiveCreditDays,
153
						SDCreditServiceImpl.getPartialCreditDays(partnerType),
154
						SDCreditServiceImpl.getFullCreditDays(partnerType)));
36557 amit 155
				accountStatusResponseOut.setFlagshipCreditTerms(SDCreditServiceImpl.buildFlagshipCreditTerms());
35919 amit 156
				accountStatusResponseOut.setPartnerType(partnerType.getValue());
36071 amit 157
				accountStatusResponseOut.setPartialCdDays(SDCreditServiceImpl.getPartialCreditDays(partnerType));
158
				accountStatusResponseOut.setFullCdDays(SDCreditServiceImpl.getFullCreditDays(partnerType));
35914 amit 159
			}
30859 tejbeer 160
			return responseSender.ok(accountStatusResponseOut);
29577 amit.gupta 161
		}
30014 tejbeer 162
 
163
		else {
164
			throw new ProfitMandiBusinessException("Provider", "Empty", "Provider can't be empty");
165
		}
29577 amit.gupta 166
	}
30014 tejbeer 167
 
30859 tejbeer 168
	@RequestMapping(value = "payment/gateway/sddirect/summary", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
169
	public ResponseEntity<?> getSdDirectLoan(HttpServletRequest request) throws ProfitMandiBusinessException {
170
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
171
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
172
		SDCreditResponseOut sdCreditResponseOut = sdCreditService.sdDirectService(retailerId);
173
		return responseSender.ok(sdCreditResponseOut);
174
	}
35059 amit 175
	@RequestMapping(value = "payment/gateway/sddirect/settle-loan/{loanId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
176
	public ResponseEntity<?> settleLoan(HttpServletRequest request, @PathVariable int loanId) throws ProfitMandiBusinessException {
177
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
178
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
179
		Loan loan = loanRepository.selectByLoanId(loanId);
180
		if(loan.getFofoId()!=retailerId) {
181
			throw new ProfitMandiBusinessException("Unauthorised", "Unauthorised", "Unauthorised");
182
		}
183
		sdCreditService.settleLoan(loan);
184
		return responseSender.ok(walletService.getWalletAmount(retailerId));
185
	}
30859 tejbeer 186
 
29577 amit.gupta 187
}