| 33354 |
amit.gupta |
1 |
package com.spice.profitmandi.dao.service;
|
|
|
2 |
|
| 33359 |
amit.gupta |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 33354 |
amit.gupta |
4 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.transaction.FofoSidbiSanction;
|
|
|
6 |
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
|
|
|
7 |
import com.spice.profitmandi.dao.repository.transaction.FofoSidbiSanctionRepository;
|
| 33364 |
amit.gupta |
8 |
import com.spice.profitmandi.service.transaction.SDCreditService;
|
| 33359 |
amit.gupta |
9 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
10 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 33354 |
amit.gupta |
11 |
import org.apache.logging.log4j.LogManager;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import java.math.BigDecimal;
|
|
|
16 |
import java.time.LocalDate;
|
|
|
17 |
import java.time.LocalDateTime;
|
|
|
18 |
import java.time.temporal.ChronoUnit;
|
|
|
19 |
import java.util.*;
|
|
|
20 |
import java.util.stream.Collectors;
|
|
|
21 |
|
|
|
22 |
@Component
|
|
|
23 |
public class SidbiService {
|
|
|
24 |
|
|
|
25 |
private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger(SidbiService.class);
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
FofoSidbiSanctionRepository fofoSidbiSanctionRepository;
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
CreditAccountRepository creditAccountRepository;
|
|
|
32 |
|
|
|
33 |
private static final double ONE_LAC = 100000d;
|
|
|
34 |
|
|
|
35 |
private static final double PROCESSING_FEE = 1.18;
|
|
|
36 |
|
|
|
37 |
private static final double STAMP_FEE = 200;
|
|
|
38 |
|
|
|
39 |
private static final String WALLET_DEDUCTION_NARRATION = "SIDBI Processing Fee @1% + GST@18% + STAMP DUTY(Rs.200)";
|
|
|
40 |
|
|
|
41 |
//Logic specific to Sidbi Related Sanctions
|
|
|
42 |
//Assumptions that 1Lac upto 12 months
|
|
|
43 |
private static final List<Integer> PRINCIPAL_DEDUCTIONS_PER_LAC = Arrays.asList(2115, 2147, 2179, 2212, 2245, 2279, 2313, 2348, 2383, 2419, 2455, 2492);
|
|
|
44 |
private static final int CUTOFF_DATE = 10;
|
|
|
45 |
|
|
|
46 |
public Map<Integer, BigDecimal> getSuggestedLimitMap() {
|
|
|
47 |
Map<Integer, BigDecimal> fofoSuggetedLimitMap = new HashMap<>();
|
|
|
48 |
List<FofoSidbiSanction> fofoSidbiSanctions = fofoSidbiSanctionRepository.selectAllUnsettled();
|
|
|
49 |
Map<Integer, List<FofoSidbiSanction>> fofoSidbiSanctionMap = fofoSidbiSanctions.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
|
|
|
50 |
for (Map.Entry<Integer, List<FofoSidbiSanction>> fofoSactionEntry : fofoSidbiSanctionMap.entrySet()) {
|
|
|
51 |
List<FofoSidbiSanction> fofoSidbiSanctionList = fofoSactionEntry.getValue();
|
|
|
52 |
int fofoId = fofoSactionEntry.getKey();
|
|
|
53 |
BigDecimal suggestedLimit = BigDecimal.valueOf(fofoSidbiSanctionList.stream().mapToDouble(x -> x.getUnsettledAmount()).sum());
|
|
|
54 |
fofoSuggetedLimitMap.put(fofoId, suggestedLimit);
|
|
|
55 |
}
|
|
|
56 |
return fofoSuggetedLimitMap;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
private double getActualLoan(double receivedAmount) {
|
|
|
60 |
double number = Math.ceil(receivedAmount / ONE_LAC);
|
|
|
61 |
return number * ONE_LAC;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
private double getProcessingDeductions(FofoSidbiSanction fofoSidbiSanction) {
|
|
|
65 |
double processingDeductions = (fofoSidbiSanction.getLoanAmount() * PROCESSING_FEE / 100) + STAMP_FEE;
|
|
|
66 |
System.out.println("Processing deductions " + processingDeductions);
|
|
|
67 |
return processingDeductions;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
//When advance is deducted from Original disbursements then deduction happens from next month
|
|
|
71 |
private double getAdvancePaid(FofoSidbiSanction fofoSidbiSanction) {
|
|
|
72 |
return fofoSidbiSanction.getLoanAmount() - fofoSidbiSanction.getSanctionAmount() - this.getProcessingDeductions(fofoSidbiSanction);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private double getDeduction(FofoSidbiSanction fofoSidbiSanction) {
|
|
|
76 |
double deduction = 0d;
|
|
|
77 |
LocalDate loanStartDate = fofoSidbiSanction.getCreatedOn().withDayOfMonth(5).toLocalDate();
|
|
|
78 |
if (fofoSidbiSanction.getAdvanceInterestAmount() > 0d) {
|
|
|
79 |
loanStartDate = loanStartDate.plusMonths(1);
|
|
|
80 |
}
|
|
|
81 |
int monthsBetween = (int) ChronoUnit.MONTHS.between(loanStartDate, LocalDate.now());
|
|
|
82 |
LOGGER.info("loanStartDate - {}, now - {}, monthsBetween - {} ", loanStartDate, LocalDateTime.now(), monthsBetween);
|
|
|
83 |
if (LocalDate.now().getDayOfMonth() < 5) {
|
|
|
84 |
monthsBetween -= 1;
|
|
|
85 |
}
|
|
|
86 |
if (monthsBetween > 0) {
|
|
|
87 |
for (int month = 1; month <= monthsBetween; month++) {
|
|
|
88 |
deduction += PRINCIPAL_DEDUCTIONS_PER_LAC.get(monthsBetween - 1) * (fofoSidbiSanction.getLoanAmount() / ONE_LAC);
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
LOGGER.info("loanStartDate- {}, Loan Amount - {}, Deduction - {}", loanStartDate, fofoSidbiSanction.getLoanAmount(), deduction);
|
|
|
92 |
return deduction;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public void processDeductions() {
|
|
|
96 |
List<FofoSidbiSanction> fofoSidbiSanctions = fofoSidbiSanctionRepository.selectAllUnsettled();
|
|
|
97 |
LOGGER.info("Sanctions Count - {}", fofoSidbiSanctions);
|
|
|
98 |
Map<Integer, List<FofoSidbiSanction>> fofoSidbiSanctionMap = fofoSidbiSanctions.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
|
|
|
99 |
for (Map.Entry<Integer, List<FofoSidbiSanction>> fofoSactionEntry : fofoSidbiSanctionMap.entrySet()) {
|
|
|
100 |
for (FofoSidbiSanction fofoSidbiSanction : fofoSactionEntry.getValue()) {
|
| 33357 |
amit.gupta |
101 |
//fofoSidbiSanction.setSettledAmount(0);
|
| 33354 |
amit.gupta |
102 |
fofoSidbiSanction.setSettledAmount(this.getDeduction(fofoSidbiSanction));
|
|
|
103 |
if (Utils.compareDouble(fofoSidbiSanction.getSettledAmount(), fofoSidbiSanction.getLoanAmount()) == 0) {
|
|
|
104 |
fofoSidbiSanction.setSettledOn(LocalDateTime.now());
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public void createSanction(int fofoId, LocalDateTime createTimestamp, double receivedAmount) {
|
|
|
111 |
//Add table here
|
|
|
112 |
FofoSidbiSanction fofoSidbiSanction = new FofoSidbiSanction();
|
|
|
113 |
fofoSidbiSanction.setCreatedOn(createTimestamp);
|
|
|
114 |
fofoSidbiSanction.setFofoId(fofoId);
|
|
|
115 |
fofoSidbiSanction.setSanctionAmount(receivedAmount);
|
|
|
116 |
fofoSidbiSanction.setLoanAmount(this.getActualLoan(receivedAmount));
|
|
|
117 |
fofoSidbiSanction.setAdvanceInterestAmount(this.getAdvancePaid(fofoSidbiSanction));
|
|
|
118 |
fofoSidbiSanctionRepository.persist(fofoSidbiSanction);
|
|
|
119 |
}
|
|
|
120 |
|
| 33359 |
amit.gupta |
121 |
public void migrateSanctions() throws ProfitMandiBusinessException {
|
| 33354 |
amit.gupta |
122 |
List<FofoSidbiSanction> fofoSidbiSanctions = fofoSidbiSanctionRepository.selectAll(Optional.empty());
|
|
|
123 |
for (FofoSidbiSanction fofoSidbiSanction : fofoSidbiSanctions) {
|
|
|
124 |
fofoSidbiSanction.setLoanAmount(this.getActualLoan(fofoSidbiSanction.getSanctionAmount()));
|
|
|
125 |
fofoSidbiSanction.setAdvanceInterestAmount(this.getAdvancePaid(fofoSidbiSanction));
|
| 33359 |
amit.gupta |
126 |
if (!Arrays.asList(175139259, 175139638).contains(fofoSidbiSanction.getFofoId())) {
|
|
|
127 |
issueLimit(fofoSidbiSanction);
|
|
|
128 |
}
|
| 33354 |
amit.gupta |
129 |
}
|
|
|
130 |
}
|
| 33359 |
amit.gupta |
131 |
|
|
|
132 |
@Autowired
|
|
|
133 |
WalletService walletService;
|
|
|
134 |
|
| 33364 |
amit.gupta |
135 |
@Autowired
|
|
|
136 |
SDCreditService sdCreditService;
|
|
|
137 |
|
| 33359 |
amit.gupta |
138 |
public void issueLimit(FofoSidbiSanction fofoSidbiSanction) throws ProfitMandiBusinessException {
|
|
|
139 |
if (fofoSidbiSanction.getIssuedOn() != null) {
|
|
|
140 |
throw new ProfitMandiBusinessException("Sidbi Sanction Issue",
|
|
|
141 |
"Party - " + fofoSidbiSanction.getFofoId() + " - " + fofoSidbiSanction.getLoanAmount(), "");
|
|
|
142 |
}
|
|
|
143 |
fofoSidbiSanction.setIssuedOn(fofoSidbiSanction.getCreatedOn().plusDays(2));
|
|
|
144 |
double deductions = this.getProcessingDeductions(fofoSidbiSanction);
|
| 33364 |
amit.gupta |
145 |
walletService.consumeAmountFromWallet(fofoSidbiSanction.getFofoId(), fofoSidbiSanction.getId(), WalletReferenceType.ADVANCE_SECURITY, "Deducted to fund advance", (float) deductions, fofoSidbiSanction.getCreatedOn(), true);
|
|
|
146 |
try {
|
|
|
147 |
sdCreditService.fundWallet(fofoSidbiSanction.getFofoId());
|
|
|
148 |
} catch(Exception e) {
|
|
|
149 |
LOGGER.info("Could not fund wallet for - ", fofoSidbiSanction.getFofoId());
|
|
|
150 |
}
|
| 33359 |
amit.gupta |
151 |
}
|
| 33354 |
amit.gupta |
152 |
}
|