Rev 33624 | Rev 33658 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.fasterxml.jackson.databind.ObjectMapper;import com.spice.profitmandi.common.enumuration.BusinessType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.auth.AuthUser;import com.spice.profitmandi.dao.entity.brandFee.BrandFee;import com.spice.profitmandi.dao.entity.brandFee.BrandFeeCollection;import com.spice.profitmandi.dao.entity.dtr.Otp;import com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel;import com.spice.profitmandi.dao.entity.onBoarding.*;import com.spice.profitmandi.dao.enumuration.cs.EscalationType;import com.spice.profitmandi.dao.enumuration.dtr.OtpType;import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.FeePaymentStatus;import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.LoiStatus;import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.StoreType;import com.spice.profitmandi.dao.model.LoiFormModel;import com.spice.profitmandi.dao.repository.auth.AuthRepository;import com.spice.profitmandi.dao.repository.brandFee.BrandFeeCollectionRepository;import com.spice.profitmandi.dao.repository.brandFee.BrandFeeRepository;import com.spice.profitmandi.dao.repository.cs.CsService;import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.OtpRepository;import com.spice.profitmandi.dao.repository.inventory.PartnerOnboardingService;import com.spice.profitmandi.dao.repository.inventory.StateRepository;import com.spice.profitmandi.dao.repository.onboarding.LoiDocMasterRepository;import com.spice.profitmandi.dao.repository.onboarding.LoiDocRepository;import com.spice.profitmandi.dao.repository.transaction.OrderRepository;import com.spice.profitmandi.dao.repository.user.LoiFormRepository;import com.spice.profitmandi.dao.service.LoiDocModel;import com.spice.profitmandi.dao.service.OTPResponse;import com.spice.profitmandi.dao.service.OtpProcessor;import com.spice.profitmandi.dao.service.loiForm.LoiFormService;import com.spice.profitmandi.service.integrations.gstpro.GstProService;import com.spice.profitmandi.service.integrations.gstpro.entity.GstDetails;import com.spice.profitmandi.service.user.StoreTimelineTatService;import com.spice.profitmandi.web.model.LoginDetails;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.MVCResponseSender;import org.apache.commons.lang3.tuple.Pair;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;import java.time.LocalDate;import java.time.LocalDateTime;import java.util.*;import java.util.stream.Collectors;@Controller@Transactional(rollbackFor = Throwable.class)public class LoiFormController {private static final Logger LOGGER = LogManager.getLogger(PartnerOnBoardingPanelController.class);@AutowiredDocumentRepository documentRepository;@AutowiredAuthRepository authRepository;@AutowiredStateRepository stateRepository;@AutowiredDistrictMasterRepository districtMasterRepository;@AutowiredLoiFormRepository loiFormRepository;@AutowiredGstProService gstProService;@AutowiredObjectMapper objectMapper;@AutowiredOrderRepository orderRepository;@AutowiredLoiFormService loiFormService;@AutowiredBrandFeeCollectionRepository brandFeeCollectionRepository;@AutowiredBrandFeeRepository brandFeeRepository;@AutowiredOtpProcessor otpProcessor;@AutowiredOtpRepository otpRepository;@AutowiredJavaMailSender mailSender;@AutowiredLoiDocMasterRepository loiDocMasterList;@AutowiredCsService csService;@AutowiredPartnerOnboardingService partnerOnboardingService;@AutowiredStoreTimelineTatService storeTimelineTatService;@AutowiredLoiDocRepository loiDocRepository;@AutowiredMVCResponseSender mvcResponseSender;@Autowiredprivate CookiesProcessor cookiesProcessor;// OnBoarding Form@RequestMapping(value = "/onboardingForm", method = RequestMethod.GET)public String loiForm(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);AuthUser selfAuth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());List<AuthUser> authUsersList = loiFormService.getReferalAuthList();List<BrandFee> brandFee = brandFeeRepository.selectFeeOnDate(LocalDateTime.now());LOGGER.info("brandFee - " + brandFee);Set<Pair<StoreType, BrandFee>> storeTypeFeePairs = new HashSet<>();for (BrandFee fee : brandFee) {for (StoreType storeType : StoreType.values()) {storeTypeFeePairs.add(Pair.of(storeType, fee));}}List<BusinessType> businessTypes = Arrays.asList(BusinessType.Proprietorship, BusinessType.Partnership, BusinessType.Company);model.addAttribute("businessTypes", businessTypes);model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);model.addAttribute("authUsersList", authUsersList);model.addAttribute("selfAuth", selfAuth);return "partner-onboarding-form";}// use to validate GSTIN@RequestMapping(value = "/gstValidate", method = RequestMethod.GET)public ResponseEntity<?> gstValidate(HttpServletRequest request, @RequestParam String gstNo) throws Exception {LOGGER.info("gstNo -" + gstNo);GstDetails gstDetails = gstProService.getGstDetails(gstNo);LOGGER.info("gstDetails -" + gstDetails);List<GstDetails.Pradr> adadr = gstDetails.getAdadr();adadr.add(gstDetails.getPradr());Collections.reverse(adadr);LOGGER.info("list_of_adadr-" + adadr);String status = gstDetails.getSts();if (status.equals("Active")) {return ResponseEntity.ok(gstDetails);} else {String errorMsg = "GSTIN - " + gstNo + " Not Active";return new ResponseEntity(errorMsg, HttpStatus.INTERNAL_SERVER_ERROR);}}// bind the form data and save in fofo.Onboarding_form table@RequestMapping(value = "/submitLoiForm", method = RequestMethod.POST)public String submitLoiForm(HttpServletRequest request, @RequestBody LoiFormData loiFormData, Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);LOGGER.info("filledBy - " + loginDetails.getEmailId());boolean isDataCreated = loiFormService.createLoiForm(loiFormData, loginDetails.getEmailId());List<LoiFormModel> formList = loiFormService.pendingFormList(loginDetails.getEmailId());model.addAttribute("formList", formList);if (isDataCreated) {model.addAttribute("response1", mvcResponseSender.createResponseString(true));return "response";} else {model.addAttribute("response1", mvcResponseSender.createResponseString(false));return "response";}}// show all pending Form to specific auth user who have filled the form(type based)@RequestMapping(value = "/pendingOnboardingForm", method = RequestMethod.GET)public String pendingLoiForm(HttpServletRequest request, Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);String email = loginDetails.getEmailId();boolean isDocApprover = false;//todo:replaceString approverEmail = "gaurav.sharma@smartdukaan.com";if (email.equals(approverEmail)) {isDocApprover = true;}boolean isAuthUser = false;List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "gaurav.sharma@smartdukaan.com", "tejus.lohani@smartdukaan.com");isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));List<LoiFormModel> pendingFormList = loiFormService.pendingFormList(email);LOGGER.info("pendingFormList {}", pendingFormList);model.addAttribute("isDocApprover", isDocApprover);model.addAttribute("isAuthUser", isAuthUser);model.addAttribute("pendingFormList", pendingFormList);return "pendingForm";}// generate LOI for specific Form data@RequestMapping(value = "/generateLoi", method = RequestMethod.GET)public String generateLoi(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);int authId = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getId();LoiForm loiForm = loiFormRepository.selectById(loiId);String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();loiForm.setLoiGeneratedBy(authId);loiForm.setLoiGeneratedOn(LocalDateTime.now());loiForm.setStatus(LoiStatus.LOI_GENERATED);BrandFee brandFee = brandFeeRepository.selectById(1);BrandFee givenFee = brandFeeRepository.selectById(loiForm.getBrandFeeId());BillingAddress address = loiForm.getBillingAddress();double brandFeeAmount = loiForm.getBrandFeeCollection().stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM)).mapToDouble(x -> x.getCollectedAmount()).sum();BrandFeeCollection brandFeeCollection = loiForm.getBrandFeeCollection().get(0);model.addAttribute("brandFeeAmount", brandFeeAmount);model.addAttribute("brandFee", brandFee);model.addAttribute("givenFee", givenFee);model.addAttribute("brandFeeCollection", brandFeeCollection);model.addAttribute("address", address);model.addAttribute("pod", loiForm);model.addAttribute("filledBy", filledBy);return "auto-Loi";}@RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {LOGGER.info("loi_docId-" + loiDocId);LoiForm loiForm = loiFormRepository.selectById(loiId);LOGGER.info("loiForm - " + loiForm);int loiDoc = loiForm.getLoiDoc();if (loiDoc == 0) {loiForm.setLoiDoc(loiDocId);loiFormService.sendSignedLoiPdfToPartner(loiForm);model.addAttribute("response1", mvcResponseSender.createResponseString(true));return "response";} else {model.addAttribute("response1", mvcResponseSender.createResponseString(true));return "response";}}// send Filled form for update purpose@RequestMapping(value = "/updateOnboardingForm", method = RequestMethod.GET)public String UpdateLoiForm(HttpServletRequest request,@RequestParam int loiId,Model model) throws ProfitMandiBusinessException {LoiForm loiForm = loiFormRepository.selectById(loiId);List<AuthUser> salesAuthList = loiFormService.getReferalAuthList();List<BrandFee> brandFee = brandFeeRepository.selectFeeOnDate(LocalDateTime.now());Set<Pair<StoreType, BrandFee>> storeTypeFeePairs = new HashSet<>();for (BrandFee fee : brandFee) {for (StoreType storeType : StoreType.values()) {storeTypeFeePairs.add(Pair.of(storeType, fee));}}model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);model.addAttribute("authUsersList", salesAuthList);model.addAttribute("onBoardingData", loiForm);return "partner-onboarding-form";}@RequestMapping(value = "/updateLoiFormData", method = RequestMethod.POST)public String UpdateLoiFormDetail(HttpServletRequest request,@RequestParam int loiId,@RequestBody LoiFormData partnerOnBoardingFormData,Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);String email = loginDetails.getEmailId();loiFormService.updateLoiForm(partnerOnBoardingFormData, loiId);List<LoiFormModel> pendingFormList = loiFormService.pendingFormList(email);model.addAttribute("formList", pendingFormList);model.addAttribute("response1", mvcResponseSender.createResponseString(true));return "response";}// TODO , add paymnet if brand fee due@RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {LoiForm loiForm = loiFormRepository.selectById(loiId);List<BrandFeeCollection> brandFeeCollections = loiForm.getBrandFeeCollection();double totatcollectedfee = brandFeeCollections.stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM) || x.getPaymentStatus().equals(FeePaymentStatus.PENDING)).mapToDouble(x -> x.getCollectedAmount()).sum();double brandFee = loiForm.getAgreedBrandFees();double differanceAmount = brandFee - totatcollectedfee;if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {brandFeeCollection.setTimeStamp(LocalDateTime.now());brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);brandFeeCollection.setLoiData(loiForm);brandFeeCollections.add(brandFeeCollection);loiForm.setBrandFeeCollection(brandFeeCollections);loiFormService.sentMailToPaymentApprover(brandFeeCollection);model.addAttribute("response1", mvcResponseSender.createResponseString(true));return "response";} else {throw new ProfitMandiBusinessException("You need to add only due brand fee that is Only, " + differanceAmount + " INR", null, "Pay differance amount or less then differance amount");}}@RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);String email = loginDetails.getEmailId();boolean isNiraj = false;if (email.equals("neeraj.gupta@smartdukaan.com")) {isNiraj = true;}LoiForm loiForm = loiFormRepository.selectById(loiId);List<BrandFeeCollection> brandFeeCollections = loiForm.getBrandFeeCollection();model.addAttribute("brandFeeCollections", brandFeeCollections);model.addAttribute("confirm", FeePaymentStatus.CONFIRM);model.addAttribute("isNiraj", isNiraj);return "payment-collection-details";}// bfcId - Brand Fee Collection Id@RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);String authEmail = loginDetails.getEmailId();BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);LoiForm loiForm = brandFeeCollection.getLoiData();List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiForm(loiForm);brandFeeCollection.setApproverEmail(authEmail);brandFeeCollection.setPaymentStatus(feePaymentStatus);brandFeeCollection.setDescription(description);double agreedBrandFee = loiForm.getAgreedBrandFees();double totalBrandFeeCollection = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum() + brandFeeCollection.getCollectedAmount();LOGGER.info("agreedBrandeFee -" + agreedBrandFee);LOGGER.info("totalBrandFeeCollection -" + totalBrandFeeCollection);boolean b = agreedBrandFee == totalBrandFeeCollection && loiForm.getLoiSignOtp() != null;if (brandFeeCollection.getPaymentStatus().equals(FeePaymentStatus.REJECT)) {loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);model.addAttribute("response1", mvcResponseSender.createResponseString(false));} else {loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);model.addAttribute("response1", mvcResponseSender.createResponseString(true));}if (agreedBrandFee == totalBrandFeeCollection && loiForm.getLoiSignOtp() != null) {loiForm.setStatus(LoiStatus.PRE_ONBOARDING_DONE);}return "response";}@RequestMapping(value = "/uploadDocumentForm", method = RequestMethod.GET)public String uploadDocumentForm(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {List<LoiDocMaster> activeDocMasterList = loiDocMasterList.getAllActiveDoc();LoiForm loiForm = loiFormRepository.selectById(loiId);LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);String authEmail = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getEmailId();String approverEmail = "gaurav.sharma@smartdukaan.com";boolean isApprover = false;if (authEmail.equals(approverEmail)) {isApprover = true;}Map<Integer, LoiDoc> masterDocIdLoiDocMap = loiForm.getLoiDocs().stream().collect(Collectors.toMap(x -> x.getMasterDocId(), x -> x));model.addAttribute("isApprover", isApprover);model.addAttribute("loiId", loiId);model.addAttribute("activeDocMasterList", activeDocMasterList);model.addAttribute("masterDocIdLoiDocMap", masterDocIdLoiDocMap);return "loiForm/document-upload";}@RequestMapping(value = "/uploadOnboardingDocument", method = RequestMethod.POST)public String uploadDocument(HttpServletRequest request, @RequestParam int loiId, @RequestBody List<LoiDocModel> loiDocModels, Model model) throws Exception {LoiForm loiForm = loiFormRepository.selectById(loiId);LOGGER.info("loiForm - " + loiForm);LOGGER.info("loiDocModels - " + loiDocModels);loiFormService.setDocs(loiForm, loiDocModels);model.addAttribute("response1", mvcResponseSender.createResponseString("Document uploaded successfully"));return "response";}@RequestMapping(value = "/documentVerify", method = RequestMethod.PUT)public String docApproval(HttpServletRequest request, @RequestParam int loiId, @RequestParam int docMasterId, @RequestParam boolean flag, Model model) throws Exception {LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);AuthUser auth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());LoiDoc loiDoc = loiDocRepository.selectByLoiIdAndMasterDocId(loiId, docMasterId);if (flag) {loiDoc.setOk(true);loiDoc.setVerifyBy(auth.getId());model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Accepted "));return "response";} else {loiDoc.setOk(false);loiDoc.setVerifyBy(auth.getId());loiFormService.sendDocApprovalMail(loiDoc);model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Rejected"));return "response";}}@RequestMapping(value = "/loiAcceptanceOtp", method = RequestMethod.POST)public String sentLoiAcceptanceOtp(@RequestParam int loiId, Model model) throws Exception {LoiForm pod = loiFormRepository.selectById(loiId);String mobile = String.valueOf(pod.getMobile());OTPResponse otpResponse = otpProcessor.generateOtp(mobile, OtpType.LOI_ACCEPTANCE);String message = "OTP send to your register mobile number -";LOGGER.info("OTPResponse Message - " + otpResponse.getMessage());if (otpResponse.isResult()) {model.addAttribute("response1", mvcResponseSender.createResponseString(message + mobile));return "response";} else {model.addAttribute("response1", mvcResponseSender.createResponseString("Something went wrong.."));return "response";}}@RequestMapping(value = "/validateLoiOtp", method = RequestMethod.PUT)public String validateLoiAcceptanceOtp(@RequestParam int loiId, @RequestParam String provideOtp, Model model) throws Exception {LOGGER.info("loiId -" + loiId);LOGGER.info("provideOtp -" + provideOtp);LoiForm pod = loiFormRepository.selectById(loiId);double brandFee = pod.getAgreedBrandFees();double totalCollectfee = pod.getBrandFeeCollection().stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM)).mapToDouble(x -> x.getCollectedAmount()).sum();String mobile = String.valueOf(pod.getMobile());Otp otp = otpRepository.selectAllByMobileWithTime(mobile).get(0);LOGGER.info("otp-" + otp);OTPResponse otpResponse = otpProcessor.validateOtp(otp.getId(), mobile, provideOtp);if (!otpResponse.isResult()) {throw new ProfitMandiBusinessException("Wrong OTP", provideOtp, "Your given Otp ->" + provideOtp + " is wrong , please check OTP and try again");}if (otpResponse.isResult()) {pod.setLoiSignOtp(provideOtp);pod.setLoiSignedOn(LocalDateTime.now());if (brandFee == totalCollectfee && pod.getLoiSignOtp() != null) {pod.setStatus(LoiStatus.PRE_ONBOARDING_DONE);} else {pod.setStatus(LoiStatus.LOI_SIGNED);}model.addAttribute("response1", mvcResponseSender.createResponseString(true));}return "response";}@RequestMapping(value = "/completedLoiForms", method = RequestMethod.GET)public String completeLoiFormList(Model model) throws ProfitMandiBusinessException {List<LoiForm> loiForms = loiFormRepository.selectAllCompleteForm();List<LoiFormModel> loiFormModels = new ArrayList<>();for (LoiForm loiForm : loiForms) {LoiFormModel loiFormModel = new LoiFormModel();loiFormModel.setId(loiForm.getId());loiFormModel.setLoiFormFilledBy(authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName());loiFormModel.setCompanyName(loiForm.getCompanyName());loiFormModel.setOwnerName(loiForm.getFullName());loiFormModel.setMobile(loiForm.getMobile());loiFormModel.setCity(loiForm.getBillingAddress().getLoc());loiFormModels.add(loiFormModel);}List<EscalationType> EscalationTypeList = new ArrayList<>();EscalationTypeList.add(EscalationType.L2);EscalationTypeList.add(EscalationType.L1);EscalationTypeList.add(EscalationType.L4);List<AuthUser> authUsers = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_RBM,EscalationTypeList);model.addAttribute("loiFormModels", loiFormModels);model.addAttribute("authUsers", authUsers);return "loiForm/completed-loi-form";}@RequestMapping(value = "/createNewOnboardingPanel", method = RequestMethod.POST)public String CreateNewOnboardingPanel(@RequestParam int loiId, @RequestParam int authId, Model model) throws Exception {PartnerOnBoardingPanel partnerOnboardingPanel = loiFormService.createPartnerOnboardingPanel(loiId, authId);loiFormRepository.selectById(loiId).setStatus(LoiStatus.IN_ONBOARDING_PROCESS);if (partnerOnboardingPanel.getId() != 0)model.addAttribute("response1", mvcResponseSender.createResponseString(true));elsemodel.addAttribute("response1", mvcResponseSender.createResponseString(false));return "response";}@RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);return responseEntity;}}