Subversion Repositories SmartDukaan

Rev

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