Subversion Repositories SmartDukaan

Rev

Rev 30265 | Rev 30276 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30219 tejbeer 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.sql.Timestamp;
4
import java.time.DayOfWeek;
5
import java.time.LocalDate;
6
import java.time.LocalDateTime;
7
import java.time.Month;
8
import java.time.YearMonth;
30265 tejbeer 9
import java.time.format.DateTimeFormatter;
30219 tejbeer 10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.Map.Entry;
14
import java.util.stream.Collectors;
15
 
16
import javax.servlet.http.HttpServletRequest;
17
 
18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Controller;
22
import org.springframework.transaction.annotation.Transactional;
23
import org.springframework.ui.Model;
24
import org.springframework.web.bind.annotation.PathVariable;
25
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestMethod;
28
 
29
import com.spice.profitmandi.common.enumuration.FofoType;
30
import com.spice.profitmandi.common.model.CustomRetailer;
31
import com.spice.profitmandi.common.services.fundfina.PartnerUserData;
32
import com.spice.profitmandi.common.services.fundfina.PreApprovalRequest;
33
import com.spice.profitmandi.common.services.fundfina.PreApprovalResponse;
34
import com.spice.profitmandi.common.services.fundfina.PreApprovals;
35
import com.spice.profitmandi.common.services.fundfina.PreApprovalsResponse;
36
import com.spice.profitmandi.common.services.fundfina.PushNotificationRequest;
37
import com.spice.profitmandi.common.services.fundfina.PushNotificationResponse;
38
import com.spice.profitmandi.common.services.fundfina.TransactionRequest;
39
import com.spice.profitmandi.common.services.fundfina.TransactionResponse;
40
import com.spice.profitmandi.common.services.fundfina.TransactionSummary;
41
import com.spice.profitmandi.common.services.fundfina.TransactionalData;
42
import com.spice.profitmandi.dao.entity.dtr.CreditAccount;
30265 tejbeer 43
import com.spice.profitmandi.dao.entity.dtr.CreditStatus;
30219 tejbeer 44
import com.spice.profitmandi.dao.entity.dtr.Document;
45
import com.spice.profitmandi.dao.entity.dtr.FundFinaPreApproval;
46
import com.spice.profitmandi.dao.entity.fofo.FofoKyc;
47
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
48
import com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel;
49
import com.spice.profitmandi.dao.entity.user.User;
50
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
51
import com.spice.profitmandi.dao.model.UserWalletCountVolumeModel;
52
import com.spice.profitmandi.dao.model.WalletMinMaxAmountModel;
53
import com.spice.profitmandi.dao.repository.dtr.CreditAccountRepository;
54
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
55
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
56
import com.spice.profitmandi.dao.repository.dtr.FundFinaPreApprovalRepository;
57
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
58
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
59
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
60
import com.spice.profitmandi.dao.repository.user.AddressRepository;
61
import com.spice.profitmandi.dao.repository.user.FofoKycRepository;
62
import com.spice.profitmandi.dao.repository.user.UserRepository;
63
import com.spice.profitmandi.service.user.RetailerService;
64
import com.spice.profitmandi.web.util.MVCResponseSender;
65
 
66
@Controller
67
@Transactional(rollbackFor = Throwable.class)
68
public class FundFinaController {
69
 
70
	@Autowired
71
	private RetailerService retailerService;
72
 
73
	@Autowired
74
	private UserWalletHistoryRepository userWalletHistoryRepository;
75
 
76
	@Autowired
77
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
78
 
79
	@Autowired
80
	private FofoStoreRepository fofoStoreRepository;
81
 
82
	@Autowired
83
	private UserRepository userRepository;
84
 
85
	@Autowired
86
	private AddressRepository addressRepository;
87
 
88
	@Autowired
89
	private MVCResponseSender mvcResponseSender;
90
 
91
	@Autowired
92
	private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
93
 
94
	@Autowired
95
	private FofoKycRepository fofoKycRepository;
96
 
97
	@Autowired
98
	private DocumentRepository documentRepository;
99
 
100
	@Autowired
101
	private FundFinaPreApprovalRepository fundFinaPreApprovalRepository;
102
 
103
	@Autowired
104
	private CreditAccountRepository creditAccountRepository;
105
	private static final Logger logger = LogManager.getLogger(FundFinaController.class);
106
 
30229 tejbeer 107
	private String authKey = "$mart@123";
30219 tejbeer 108
 
109
	@RequestMapping(value = "/fundfina/getTransactions", method = RequestMethod.POST)
110
	public String getTransactions(HttpServletRequest request, @RequestBody TransactionRequest transactionRequest,
111
			Model model) throws Exception {
112
 
113
		TransactionResponse tr = new TransactionResponse();
114
 
115
		String key = request.getHeader("auth-key");
116
		logger.info("authKey {}", authKey);
117
 
118
		if (key.equals(authKey)) {
119
			int offset = 0;
120
			offset = ((0 + transactionRequest.getPageNumber() - 1) * 100) + 1;
121
 
122
			List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores().stream()
123
					.filter(x -> x.getFofoType().equals(FofoType.FRANCHISE)).collect(Collectors.toList());
124
			int remainder = fofoStores.size() / 100;
125
			int modulas = fofoStores.size() % 100;
126
			if (modulas != 0) {
127
				tr.setTotalPages(remainder + 1);
128
 
129
			} else {
130
				tr.setTotalPages(remainder);
131
			}
132
 
133
			logger.info("offset {}", offset);
134
 
135
			Map<Integer, CustomRetailer> customRetailerMap = retailerService.getFofoRetailersPaginated(true, offset,
136
					100, FofoType.FRANCHISE);
137
 
138
			tr.setStatus("success");
139
			tr.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
140
			tr.setTotalRecords(fofoStores.size());
141
			tr.setCurrentPage(transactionRequest.getPageNumber());
142
			tr.setCurrentPageRecords(customRetailerMap.size());
143
 
144
			List<TransactionalData> transactionData = new ArrayList<>();
145
 
146
			for (Entry<Integer, CustomRetailer> customRetailerEntry : customRetailerMap.entrySet()) {
147
 
148
				int fofoId = customRetailerEntry.getKey();
149
 
150
				User user = userRepository.selectById(fofoId);
151
				TransactionalData td = new TransactionalData();
152
				td.setJoinedOn(user.getCreateTimestamp().toLocalDate().toString());
30265 tejbeer 153
				td.setRegistrationDate(user.getCreateTimestamp().toLocalDate().toString());
30219 tejbeer 154
				td.setUserId(Integer.toString(user.getId()));
155
				td.setMerchantType("Retailer");
156
				td.setEmailId(user.getEmailId());
157
				td.setMobileNo(user.getMobileNumber());
158
				td.setShopType("Mobile Shop");
159
				td.setIpAddress("50.116.3.101");
160
				td.setPincode(Integer.parseInt(addressRepository.selectById(user.getAddressId()).getPinCode()));
161
				List<UserWalletCountVolumeModel> countVolumeModels = userWalletHistoryRepository
162
						.selectCountVolumeTransactionByYearMonth(fofoId);
163
 
164
				Map<YearMonth, WalletMinMaxAmountModel> walletMinMaxModel = partnerDailyInvestmentRepository
165
						.selectMinMaxWalletBalanceGroupByYearMonth(fofoId).stream()
166
						.collect(Collectors.toMap(x -> YearMonth.of(Integer.parseInt(x.getYear()),
167
								Month.of(Integer.parseInt(x.getMonth()))), x -> x));
168
 
169
				List<TransactionSummary> transactionSummmary = new ArrayList<>();
170
 
171
				for (UserWalletCountVolumeModel uwcm : countVolumeModels) {
172
					TransactionSummary ts = new TransactionSummary();
173
					ts.setCount(String.valueOf(uwcm.getCount()));
174
					ts.setVolume(uwcm.getAmount());
175
					ts.setYear(Integer.parseInt(uwcm.getYear()));
176
 
177
					YearMonth yearMonthObject = YearMonth.of(Integer.parseInt(uwcm.getYear()),
178
							Month.of(Integer.parseInt(uwcm.getMonth())));
179
					int daysInMonth = yearMonthObject.lengthOfMonth();
180
					ts.setTotalDays(daysInMonth);
181
 
182
					LocalDate result = yearMonthObject.atDay(1);
183
					int activeDays = 0;
184
					while (activeDays < daysInMonth) {
185
						result = result.plusDays(1);
186
 
187
						logger.info("result {}", result);
188
 
189
						if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY
190
								|| result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
191
							++activeDays;
192
						}
193
					}
194
 
195
					ts.setActiveDays(activeDays);
196
 
197
					ts.setMonth(Integer.parseInt(uwcm.getMonth()));
198
					if (walletMinMaxModel.get(yearMonthObject) != null) {
199
						ts.setMinWalletBalance(walletMinMaxModel.get(yearMonthObject).getMin());
200
						ts.setMaxWalletBalance(walletMinMaxModel.get(yearMonthObject).getMax());
201
					}
202
					transactionSummmary.add(ts);
203
 
204
				}
205
 
206
				td.setTransactionSummary(transactionSummmary);
207
 
208
				transactionData.add(td);
209
 
210
			}
211
			tr.setTransactionalData(transactionData);
212
		} else {
213
			tr.setErrCode("401");
214
			tr.setDesc("auth key is not valid");
215
 
216
		}
217
 
218
		model.addAttribute("response", mvcResponseSender.createResponseString(tr));
219
		return "response";
220
	}
221
 
222
	@RequestMapping(value = "/fundfina/getUserData/{userId}", method = RequestMethod.GET)
223
	public String getUserData(HttpServletRequest request, @PathVariable int userId, Model model) throws Exception {
224
 
225
		FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
226
 
227
		User user = userRepository.selectById(fs.getId());
228
		String key = request.getHeader("auth-key");
229
		logger.info("authKey {}", authKey);
230
		PartnerUserData pud = new PartnerUserData();
231
 
232
		if (key.equals(authKey)) {
233
			PartnerOnBoardingPanel pobp = partnerOnBoardingPanelRepository.selectByCode(fs.getCode());
234
			CustomRetailer customRetailer = retailerService.getFofoRetailer(fs.getId());
235
			List<FofoKyc> fofoKycs = fofoKycRepository.selectByPartnerOnBoardingId(pobp.getId());
236
			FofoKyc fk = fofoKycs.get(0);
237
			pud.setName(fk.getFirstName() + " " + fk.getLastName());
238
			pud.setShopName(customRetailer.getBusinessName());
239
			pud.setShopType("Mobile Store");
240
			pud.setDob(fk.getDob().toString());
241
			pud.setShopOwnership("rented");
242
			pud.setShopPincode(customRetailer.getAddress().getPinCode());
243
			pud.setShopAddress(customRetailer.getAddress().getLine1() + " " + customRetailer.getAddress().getLine2());
244
			pud.setShopCity(customRetailer.getAddress().getCity());
245
			pud.setShopState(customRetailer.getAddress().getState());
246
			pud.setMobile(fk.getMobile());
247
			pud.setCity(fk.getCity());
248
			pud.setAddress(fk.getAddress1() + " " + fk.getAddress2() + " " + fk.getAddress3());
249
			pud.setEmailId(fk.getEmail());
250
			pud.setPan(fk.getPan());
251
			pud.setPincode(fk.getPincode());
252
			pud.setBusinessActivationDate(user.getCreateTimestamp().toString());
253
			pud.setState(fk.getState());
254
 
255
			if (fk.getPanDoc() != 0) {
256
				Document document = documentRepository.selectById(fk.getPanDoc());
257
				pud.setPanFront(
258
						"http://shop2020.in:8080/profitmandi-web/open-attachment?documentId=" + document.getId());
259
			}
260
			pud.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
261
			pud.setStatus("success");
262
		} else {
263
			pud.setErrCode("401");
264
			pud.setDesc("auth key is not valid");
265
 
266
		}
267
		model.addAttribute("response", mvcResponseSender.createResponseString(pud));
268
 
269
		return "response";
270
 
271
	}
272
 
273
	@RequestMapping(value = "/fundfina/pushPreApproval", method = RequestMethod.POST)
274
	public String pushPreApproval(HttpServletRequest request, @RequestBody PreApprovalRequest preApprovalRequest,
275
			Model model) throws Exception {
276
 
277
		List<PreApprovals> preApprovals = preApprovalRequest.getPreApprovals();
278
		String key = request.getHeader("auth-key");
279
		logger.info("authKey {}", authKey);
280
		PreApprovalResponse par = new PreApprovalResponse();
281
 
282
		if (key.equals(authKey)) {
283
 
284
			par.setStatus("success");
285
			par.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
286
 
287
			List<PreApprovalsResponse> pars = new ArrayList<>();
288
 
289
			for (PreApprovals pa : preApprovals) {
290
 
291
				if (pa.getProductId() != null) {
292
					FundFinaPreApproval fpa = fundFinaPreApprovalRepository
293
							.selectByUserIdAndProductId(Integer.parseInt(pa.getUserId()), pa.getProductId());
30265 tejbeer 294
 
295
					CreditAccount creditAccount = creditAccountRepository
296
							.selectByFofoIdAndGateway(Integer.parseInt(pa.getUserId()), Gateway.FUNDFINA);
297
 
298
					DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
299
 
30266 tejbeer 300
					if (creditAccount == null) {
301
						creditAccount = new CreditAccount();
302
						creditAccount.setLoanReferenceId(pa.getProductId());
30265 tejbeer 303
 
30266 tejbeer 304
					} else {
30265 tejbeer 305
 
30266 tejbeer 306
						if (!pa.getProductId().equals(creditAccount.getLoanReferenceId())) {
307
							creditAccount.setLoanReferenceId(pa.getProductId());
30265 tejbeer 308
 
309
						}
30266 tejbeer 310
					}
30265 tejbeer 311
 
30266 tejbeer 312
					creditAccount.setExpiredOn(LocalDateTime.parse(pa.getExpiryDate(), formatter));
313
					creditAccount.setProcessingFee(pa.getProcessingFee());
314
					creditAccount.setSanctionedAmount(Float.parseFloat(pa.getApprovedAmount()));
315
					creditAccount.setGateway(Gateway.FUNDFINA);
30265 tejbeer 316
 
30266 tejbeer 317
					String in[] = pa.getAnnualInterestRate().split("%");
318
					creditAccount.setInterestRate(Float.parseFloat(in[0]));
319
					creditAccount.setFofoId(Integer.parseInt(pa.getUserId()));
320
					creditAccount.setUpdatedOn(LocalDateTime.now());
30265 tejbeer 321
 
30266 tejbeer 322
					creditAccount.setCreditStatus(CreditStatus.SANCTIONED);
30265 tejbeer 323
 
30266 tejbeer 324
					creditAccountRepository.persist(creditAccount);
30265 tejbeer 325
 
30219 tejbeer 326
					if (fpa == null) {
327
						fpa = new FundFinaPreApproval();
328
						fpa.setFofoId(Integer.parseInt(pa.getUserId()));
30265 tejbeer 329
						fpa.setProductId(pa.getProductId());
330
						fpa.setTenureInDays(pa.getTenureInDays());
331
						fpa.setProcessingFee(pa.getProcessingFee());
332
						fpa.setAnnualInterestRate(pa.getAnnualInterestRate());
333
						fpa.setApprovedAmount(pa.getApprovedAmount());
334
						fpa.setEmiAmount(pa.getEmiAmount());
335
						fpa.setExpiryDate(LocalDateTime.parse(pa.getExpiryDate(), formatter));
30219 tejbeer 336
						fpa.setCreateTimestamp(LocalDateTime.now());
337
						fpa.setUpdateTimestamp(LocalDateTime.now());
338
						fundFinaPreApprovalRepository.persist(fpa);
339
 
340
					}
341
 
342
					PreApprovalsResponse paar = new PreApprovalsResponse();
343
					paar.setUserId(pa.getUserId());
344
					paar.setProductId(pa.getProductId());
345
					pars.add(paar);
346
 
347
				}
348
			}
349
 
350
			par.setPreApprvalsRes(pars);
351
 
352
			model.addAttribute("response", mvcResponseSender.createResponseString(par));
353
		} else {
354
			par.setErrCode("401");
355
			par.setDesc("auth key is not valid");
356
		}
357
		return "response";
358
 
359
	}
360
 
361
	@RequestMapping(value = "/fundfina/pushNotification", method = RequestMethod.POST)
362
	public String pushNotification(HttpServletRequest request,
363
			@RequestBody PushNotificationRequest pushNotificationRequest, Model model) throws Exception {
364
 
365
		PushNotificationResponse pnr = new PushNotificationResponse();
366
		pnr.setRequestRef(String.valueOf(Timestamp.valueOf(LocalDateTime.now()).getTime()));
367
		pnr.setStatus("success");
368
 
369
		model.addAttribute("response", mvcResponseSender.createResponseString(pnr));
370
 
371
		return "response";
372
	}
373
 
374
}