Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
33507 tejus.loha 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import com.spice.profitmandi.common.enumuration.BusinessType;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.dao.entity.auth.AuthUser;
7
import com.spice.profitmandi.dao.entity.brandFee.BrandFee;
8
import com.spice.profitmandi.dao.entity.brandFee.BrandFeeCollection;
9
import com.spice.profitmandi.dao.entity.dtr.Otp;
10
import com.spice.profitmandi.dao.entity.onBoarding.*;
11
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
12
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.FeePaymentStatus;
13
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.LoiStatus;
14
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.StoreType;
15
import com.spice.profitmandi.dao.model.LoiFormModel;
16
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
17
import com.spice.profitmandi.dao.repository.brandFee.BrandFeeCollectionRepository;
18
import com.spice.profitmandi.dao.repository.brandFee.BrandFeeRepository;
33658 tejus.loha 19
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;
20
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
33507 tejus.loha 21
import com.spice.profitmandi.dao.repository.cs.CsService;
22
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
23
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
25
import com.spice.profitmandi.dao.repository.inventory.PartnerOnboardingService;
26
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
33658 tejus.loha 27
import com.spice.profitmandi.dao.repository.onboarding.BrandCommitRepository;
28
import com.spice.profitmandi.dao.repository.onboarding.LoiBrandCommitmentRepository;
33507 tejus.loha 29
import com.spice.profitmandi.dao.repository.onboarding.LoiDocMasterRepository;
30
import com.spice.profitmandi.dao.repository.onboarding.LoiDocRepository;
31
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
33658 tejus.loha 32
import com.spice.profitmandi.dao.repository.user.BillingAddressRepository;
33507 tejus.loha 33
import com.spice.profitmandi.dao.repository.user.LoiFormRepository;
34
import com.spice.profitmandi.dao.service.LoiDocModel;
35
import com.spice.profitmandi.dao.service.OTPResponse;
36
import com.spice.profitmandi.dao.service.OtpProcessor;
37
import com.spice.profitmandi.dao.service.loiForm.LoiFormService;
38
import com.spice.profitmandi.service.integrations.gstpro.GstProService;
39
import com.spice.profitmandi.service.integrations.gstpro.entity.GstDetails;
40
import com.spice.profitmandi.service.user.StoreTimelineTatService;
41
import com.spice.profitmandi.web.model.LoginDetails;
42
import com.spice.profitmandi.web.util.CookiesProcessor;
43
import com.spice.profitmandi.web.util.MVCResponseSender;
44
import org.apache.commons.lang3.tuple.Pair;
45
import org.apache.logging.log4j.LogManager;
46
import org.apache.logging.log4j.Logger;
47
import org.springframework.beans.factory.annotation.Autowired;
48
import org.springframework.http.HttpStatus;
49
import org.springframework.http.ResponseEntity;
50
import org.springframework.mail.javamail.JavaMailSender;
51
import org.springframework.stereotype.Controller;
52
import org.springframework.transaction.annotation.Transactional;
53
import org.springframework.ui.Model;
54
import org.springframework.web.bind.annotation.RequestBody;
55
import org.springframework.web.bind.annotation.RequestMapping;
56
import org.springframework.web.bind.annotation.RequestMethod;
57
import org.springframework.web.bind.annotation.RequestParam;
58
 
59
import javax.servlet.http.HttpServletRequest;
33617 tejus.loha 60
import java.time.LocalDate;
33507 tejus.loha 61
import java.time.LocalDateTime;
62
import java.util.*;
63
import java.util.stream.Collectors;
64
 
65
@Controller
66
@Transactional(rollbackFor = Throwable.class)
67
public class LoiFormController {
68
    private static final Logger LOGGER = LogManager.getLogger(PartnerOnBoardingPanelController.class);
69
    @Autowired
70
    DocumentRepository documentRepository;
71
    @Autowired
72
    AuthRepository authRepository;
73
    @Autowired
74
    StateRepository stateRepository;
75
    @Autowired
76
    DistrictMasterRepository districtMasterRepository;
77
    @Autowired
78
    LoiFormRepository loiFormRepository;
79
    @Autowired
80
    GstProService gstProService;
81
    @Autowired
82
    ObjectMapper objectMapper;
83
    @Autowired
84
    OrderRepository orderRepository;
85
    @Autowired
86
    LoiFormService loiFormService;
87
    @Autowired
88
    BrandFeeCollectionRepository brandFeeCollectionRepository;
89
    @Autowired
90
    BrandFeeRepository brandFeeRepository;
91
    @Autowired
92
    OtpProcessor otpProcessor;
93
    @Autowired
94
    OtpRepository otpRepository;
95
    @Autowired
96
    JavaMailSender mailSender;
97
    @Autowired
98
    LoiDocMasterRepository loiDocMasterList;
99
    @Autowired
100
    CsService csService;
101
    @Autowired
102
    PartnerOnboardingService partnerOnboardingService;
103
    @Autowired
104
    StoreTimelineTatService storeTimelineTatService;
105
    @Autowired
106
    LoiDocRepository loiDocRepository;
107
    @Autowired
108
    MVCResponseSender mvcResponseSender;
109
    @Autowired
110
    private CookiesProcessor cookiesProcessor;
33658 tejus.loha 111
    @Autowired
112
    BrandCommitRepository brandMasterRepository;
113
    @Autowired
114
    BrandsRepository brandsRepository;
115
    @Autowired
116
    BrandCategoryRepository brandCategoryRepository;
117
    @Autowired
118
    BillingAddressRepository billingAddressRepository;
119
    @Autowired
120
    LoiBrandCommitmentRepository brandCommitmentRepository;
33710 tejus.loha 121
    List<String> agreedBrandFeeChangerEmail = Arrays.asList("sm@smartdukaan.com");
33507 tejus.loha 122
 
123
    // OnBoarding Form
124
    @RequestMapping(value = "/onboardingForm", method = RequestMethod.GET)
125
    public String loiForm(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
126
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
33579 tejus.loha 127
        List<AuthUser> authUsersList = loiFormService.getReferalAuthList();
33507 tejus.loha 128
        List<BrandFee> brandFee = brandFeeRepository.selectFeeOnDate(LocalDateTime.now());
129
        Set<Pair<StoreType, BrandFee>> storeTypeFeePairs = new HashSet<>();
130
        for (BrandFee fee : brandFee) {
131
            for (StoreType storeType : StoreType.values()) {
132
                storeTypeFeePairs.add(Pair.of(storeType, fee));
133
            }
134
        }
33658 tejus.loha 135
        List<BrandCommit> brandCommits = brandMasterRepository.selectAllBrand();
33507 tejus.loha 136
        List<BusinessType> businessTypes = Arrays.asList(BusinessType.Proprietorship, BusinessType.Partnership, BusinessType.Company);
137
        model.addAttribute("businessTypes", businessTypes);
33658 tejus.loha 138
        model.addAttribute("brandCommits", brandCommits);
33507 tejus.loha 139
        model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);
140
        model.addAttribute("authUsersList", authUsersList);
141
        return "partner-onboarding-form";
142
    }
143
 
144
    // use to validate GSTIN
145
    @RequestMapping(value = "/gstValidate", method = RequestMethod.GET)
146
    public ResponseEntity<?> gstValidate(HttpServletRequest request, @RequestParam String gstNo) throws Exception {
147
        LOGGER.info("gstNo -" + gstNo);
148
        GstDetails gstDetails = gstProService.getGstDetails(gstNo);
149
        LOGGER.info("gstDetails -" + gstDetails);
150
        List<GstDetails.Pradr> adadr = gstDetails.getAdadr();
151
        adadr.add(gstDetails.getPradr());
152
        Collections.reverse(adadr);
153
        LOGGER.info("list_of_adadr-" + adadr);
154
        String status = gstDetails.getSts();
155
        if (status.equals("Active")) {
156
            return ResponseEntity.ok(gstDetails);
157
        } else {
158
            String errorMsg = "GSTIN - " + gstNo + " Not Active";
159
            return new ResponseEntity(errorMsg, HttpStatus.INTERNAL_SERVER_ERROR);
160
        }
161
    }
162
 
163
    // bind the form data and save in fofo.Onboarding_form table
33577 tejus.loha 164
    @RequestMapping(value = "/submitLoiForm", method = RequestMethod.POST)
33568 tejus.loha 165
    public String submitLoiForm(HttpServletRequest request, @RequestBody LoiFormData loiFormData, Model model) throws Exception {
33507 tejus.loha 166
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
33577 tejus.loha 167
        LOGGER.info("filledBy - " + loginDetails.getEmailId());
168
        boolean isDataCreated = loiFormService.createLoiForm(loiFormData, loginDetails.getEmailId());
33507 tejus.loha 169
        if (isDataCreated) {
170
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
171
            return "response";
172
        } else {
173
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
174
            return "response";
175
        }
176
    }
177
 
178
    // show all pending Form to specific auth user who have filled the form(type based)
33710 tejus.loha 179
    @RequestMapping(value = "/pendingLoiForm", method = RequestMethod.GET)
33507 tejus.loha 180
    public String pendingLoiForm(HttpServletRequest request, Model model) throws Exception {
181
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
182
        String email = loginDetails.getEmailId();
183
        boolean isDocApprover = false;
184
        //todo:replace
185
        String approverEmail = "gaurav.sharma@smartdukaan.com";
186
        if (email.equals(approverEmail)) {
187
            isDocApprover = true;
188
        }
33710 tejus.loha 189
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(email));
33617 tejus.loha 190
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "gaurav.sharma@smartdukaan.com", "tejus.lohani@smartdukaan.com");
33710 tejus.loha 191
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
33507 tejus.loha 192
        List<LoiFormModel> pendingFormList = loiFormService.pendingFormList(email);
193
        model.addAttribute("isDocApprover", isDocApprover);
33617 tejus.loha 194
        model.addAttribute("isAuthUser", isAuthUser);
33710 tejus.loha 195
        model.addAttribute("isAgreedBrandFeeChanger", isAgreedBrandFeeChanger);
33507 tejus.loha 196
        model.addAttribute("pendingFormList", pendingFormList);
197
        return "pendingForm";
198
    }
199
 
200
    // generate LOI for specific Form data
201
    @RequestMapping(value = "/generateLoi", method = RequestMethod.GET)
33658 tejus.loha 202
    public String generateLoi(@RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
33507 tejus.loha 203
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33630 tejus.loha 204
        String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();
33507 tejus.loha 205
        loiForm.setLoiGeneratedOn(LocalDateTime.now());
206
        loiForm.setStatus(LoiStatus.LOI_GENERATED);
207
        BrandFee brandFee = brandFeeRepository.selectById(1);
208
        BrandFee givenFee = brandFeeRepository.selectById(loiForm.getBrandFeeId());
33658 tejus.loha 209
        BillingAddress address = billingAddressRepository.selectByLoiFormId(loiId);
210
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
211
        List<LoiBrandCommitment> loiBrandCommitments = brandCommitmentRepository.selectByLoiId(loiId).stream().filter(x -> x.getAmount() > 0).collect(Collectors.toList());
212
        double totalCommitment = loiBrandCommitments.stream().mapToDouble(x -> x.getAmount()).sum();
213
        double brandFeeAmount = brandFeeCollections.stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM)).mapToDouble(x -> x.getCollectedAmount()).sum();
214
        BrandFeeCollection brandFeeCollection = brandFeeCollections.get(0);
215
        model.addAttribute("totalCommitment", totalCommitment);
216
        model.addAttribute("loiBrandCommitments", loiBrandCommitments);
33507 tejus.loha 217
        model.addAttribute("brandFeeAmount", brandFeeAmount);
218
        model.addAttribute("brandFee", brandFee);
219
        model.addAttribute("givenFee", givenFee);
220
        model.addAttribute("brandFeeCollection", brandFeeCollection);
221
        model.addAttribute("address", address);
33658 tejus.loha 222
        model.addAttribute("loiForm", loiForm);
33630 tejus.loha 223
        model.addAttribute("filledBy", filledBy);
33507 tejus.loha 224
        return "auto-Loi";
225
 
226
    }
227
 
33617 tejus.loha 228
    @RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)
33507 tejus.loha 229
    public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {
230
        LOGGER.info("loi_docId-" + loiDocId);
231
        LoiForm loiForm = loiFormRepository.selectById(loiId);
232
        int loiDoc = loiForm.getLoiDoc();
233
        if (loiDoc == 0) {
234
            loiForm.setLoiDoc(loiDocId);
235
            loiFormService.sendSignedLoiPdfToPartner(loiForm);
236
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
237
            return "response";
238
        } else {
239
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
240
            return "response";
241
        }
242
 
243
    }
244
 
245
    // send Filled form for update purpose
33658 tejus.loha 246
    @RequestMapping(value = "/updateLoiForm", method = RequestMethod.GET)
33507 tejus.loha 247
    public String UpdateLoiForm(HttpServletRequest request,
248
                                @RequestParam int loiId,
249
                                Model model) throws ProfitMandiBusinessException {
250
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33658 tejus.loha 251
        model.addAttribute("updateForm", true);
252
        model.addAttribute("loiForm", loiForm);
253
        return "loiForm/update-loiForm";
33507 tejus.loha 254
    }
255
 
33577 tejus.loha 256
    @RequestMapping(value = "/updateLoiFormData", method = RequestMethod.POST)
33507 tejus.loha 257
    public String UpdateLoiFormDetail(HttpServletRequest request,
258
                                      @RequestParam int loiId,
33658 tejus.loha 259
                                      @RequestBody LoiFormData loiFormData,
33507 tejus.loha 260
                                      Model model) throws Exception {
33658 tejus.loha 261
        loiFormService.updateLoiForm(loiFormData, loiId);
33507 tejus.loha 262
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
263
        return "response";
264
    }
265
 
266
    // TODO , add paymnet if brand fee due
267
    @RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)
268
    public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {
269
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33658 tejus.loha 270
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33525 tejus.loha 271
        double totatcollectedfee = brandFeeCollections.stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM) || x.getPaymentStatus().equals(FeePaymentStatus.PENDING)).mapToDouble(x -> x.getCollectedAmount()).sum();
33507 tejus.loha 272
        double brandFee = loiForm.getAgreedBrandFees();
273
        double differanceAmount = brandFee - totatcollectedfee;
274
        if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {
275
            brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);
33658 tejus.loha 276
            brandFeeCollection.setLoiFormId(loiId);
277
            brandFeeCollectionRepository.persist(brandFeeCollection);
33507 tejus.loha 278
            loiFormService.sentMailToPaymentApprover(brandFeeCollection);
279
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
280
            return "response";
281
        } else {
282
            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");
283
        }
284
    }
285
 
286
 
33617 tejus.loha 287
    @RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)
33507 tejus.loha 288
    public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
289
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
290
        String email = loginDetails.getEmailId();
291
        boolean isNiraj = false;
292
        if (email.equals("neeraj.gupta@smartdukaan.com")) {
293
            isNiraj = true;
294
        }
33658 tejus.loha 295
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33507 tejus.loha 296
        model.addAttribute("brandFeeCollections", brandFeeCollections);
297
        model.addAttribute("confirm", FeePaymentStatus.CONFIRM);
298
        model.addAttribute("isNiraj", isNiraj);
299
        return "payment-collection-details";
300
    }
301
 
302
    // bfcId - Brand Fee Collection Id
33617 tejus.loha 303
    @RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)
33525 tejus.loha 304
    public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,
305
                                     Model model) throws Exception {
33507 tejus.loha 306
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
307
        String authEmail = loginDetails.getEmailId();
308
        BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);
33658 tejus.loha 309
        LoiForm loiForm = loiFormRepository.selectById(brandFeeCollection.getLoiFormId());
33710 tejus.loha 310
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(brandFeeCollection.getLoiFormId());
33507 tejus.loha 311
        brandFeeCollection.setApproverEmail(authEmail);
312
        brandFeeCollection.setPaymentStatus(feePaymentStatus);
33525 tejus.loha 313
        brandFeeCollection.setDescription(description);
33507 tejus.loha 314
        double agreedBrandFee = loiForm.getAgreedBrandFees();
315
        double totalBrandFeeCollection = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum() + brandFeeCollection.getCollectedAmount();
33525 tejus.loha 316
        if (brandFeeCollection.getPaymentStatus().equals(FeePaymentStatus.REJECT)) {
33617 tejus.loha 317
            loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);
33525 tejus.loha 318
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
319
        } else {
33617 tejus.loha 320
            loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);
33525 tejus.loha 321
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
322
        }
33507 tejus.loha 323
        if (agreedBrandFee == totalBrandFeeCollection && loiForm.getLoiSignOtp() != null) {
324
            loiForm.setStatus(LoiStatus.PRE_ONBOARDING_DONE);
33710 tejus.loha 325
            loiFormService.createPartnerOnboardingPanel(loiForm.getId());
33507 tejus.loha 326
        }
327
        return "response";
328
 
329
    }
330
 
33658 tejus.loha 331
 
33617 tejus.loha 332
    @RequestMapping(value = "/uploadDocumentForm", method = RequestMethod.GET)
33507 tejus.loha 333
    public String uploadDocumentForm(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
334
        List<LoiDocMaster> activeDocMasterList = loiDocMasterList.getAllActiveDoc();
335
        LoiForm loiForm = loiFormRepository.selectById(loiId);
336
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
337
        String authEmail = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getEmailId();
338
        String approverEmail = "gaurav.sharma@smartdukaan.com";
339
        boolean isApprover = false;
340
        if (authEmail.equals(approverEmail)) {
341
            isApprover = true;
342
        }
33658 tejus.loha 343
        Map<Integer, LoiDoc> masterDocIdLoiDocMap = loiDocRepository.selectLoiFormId(loiId).stream().collect(Collectors.toMap(x -> x.getMasterDocId(), x -> x));
33507 tejus.loha 344
        model.addAttribute("isApprover", isApprover);
345
        model.addAttribute("loiId", loiId);
346
        model.addAttribute("activeDocMasterList", activeDocMasterList);
347
        model.addAttribute("masterDocIdLoiDocMap", masterDocIdLoiDocMap);
348
        return "loiForm/document-upload";
349
    }
350
 
351
    @RequestMapping(value = "/uploadOnboardingDocument", method = RequestMethod.POST)
352
    public String uploadDocument(HttpServletRequest request, @RequestParam int loiId, @RequestBody List<LoiDocModel> loiDocModels, Model model) throws Exception {
353
        LoiForm loiForm = loiFormRepository.selectById(loiId);
354
        loiFormService.setDocs(loiForm, loiDocModels);
355
        model.addAttribute("response1", mvcResponseSender.createResponseString("Document uploaded successfully"));
356
        return "response";
357
 
358
    }
359
 
360
 
33617 tejus.loha 361
    @RequestMapping(value = "/documentVerify", method = RequestMethod.PUT)
33507 tejus.loha 362
    public String docApproval(HttpServletRequest request, @RequestParam int loiId, @RequestParam int docMasterId, @RequestParam boolean flag, Model model) throws Exception {
363
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
364
        AuthUser auth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
365
        LoiDoc loiDoc = loiDocRepository.selectByLoiIdAndMasterDocId(loiId, docMasterId);
366
        if (flag) {
367
            loiDoc.setOk(true);
368
            loiDoc.setVerifyBy(auth.getId());
369
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Accepted "));
370
            return "response";
371
        } else {
372
            loiDoc.setOk(false);
373
            loiDoc.setVerifyBy(auth.getId());
33658 tejus.loha 374
            loiFormService.sendDocRejectionMail(loiDoc);
33507 tejus.loha 375
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Rejected"));
376
            return "response";
377
        }
378
    }
379
 
380
    @RequestMapping(value = "/loiAcceptanceOtp", method = RequestMethod.POST)
381
    public String sentLoiAcceptanceOtp(@RequestParam int loiId, Model model) throws Exception {
33658 tejus.loha 382
        LoiForm loiForm = loiFormRepository.selectById(loiId);
383
        String mobile = String.valueOf(loiForm.getMobile());
33525 tejus.loha 384
        OTPResponse otpResponse = otpProcessor.generateOtp(mobile, OtpType.LOI_ACCEPTANCE);
33658 tejus.loha 385
        String message = "OTP send on your register mobile and EmailId - ";
33568 tejus.loha 386
        LOGGER.info("OTPResponse Message - " + otpResponse.getMessage());
387
        if (otpResponse.isResult()) {
33658 tejus.loha 388
            model.addAttribute("response1", mvcResponseSender.createResponseString(message + mobile + " and " + loiForm.getEmail()));
33568 tejus.loha 389
            return "response";
33525 tejus.loha 390
        } else {
391
            model.addAttribute("response1", mvcResponseSender.createResponseString("Something went wrong.."));
33568 tejus.loha 392
            return "response";
33525 tejus.loha 393
        }
33507 tejus.loha 394
    }
395
 
396
    @RequestMapping(value = "/validateLoiOtp", method = RequestMethod.PUT)
397
    public String validateLoiAcceptanceOtp(@RequestParam int loiId, @RequestParam String provideOtp, Model model) throws Exception {
33658 tejus.loha 398
        LoiForm loiForm = loiFormRepository.selectById(loiId);
399
        double agreedBrandFees = loiForm.getAgreedBrandFees();
400
        double totalCollectfee = brandFeeCollectionRepository.selectAllByLoiFormId(loiId).stream().filter(x -> x.getPaymentStatus().equals(FeePaymentStatus.CONFIRM)).mapToDouble(x -> x.getCollectedAmount()).sum();
401
        String mobile = String.valueOf(loiForm.getMobile());
33507 tejus.loha 402
        Otp otp = otpRepository.selectAllByMobileWithTime(mobile).get(0);
403
        OTPResponse otpResponse = otpProcessor.validateOtp(otp.getId(), mobile, provideOtp);
404
        if (!otpResponse.isResult()) {
33577 tejus.loha 405
            throw new ProfitMandiBusinessException("Wrong OTP", provideOtp, "Your given Otp ->" + provideOtp + " is wrong , please check OTP and try again");
33507 tejus.loha 406
        }
407
        if (otpResponse.isResult()) {
33658 tejus.loha 408
            loiForm.setLoiSignOtp(provideOtp);
409
            loiForm.setLoiSignedOn(LocalDateTime.now());
410
            if (agreedBrandFees == totalCollectfee && loiForm.getLoiSignOtp() != null) {
411
                loiForm.setStatus(LoiStatus.PRE_ONBOARDING_DONE);
33710 tejus.loha 412
                loiFormService.createPartnerOnboardingPanel(loiId);
33507 tejus.loha 413
            } else {
33658 tejus.loha 414
                loiForm.setStatus(LoiStatus.LOI_SIGNED);
33507 tejus.loha 415
            }
416
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
417
        }
418
        return "response";
419
    }
420
 
33617 tejus.loha 421
    @RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)
422
    public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {
423
        ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);
424
        return responseEntity;
425
    }
426
 
33658 tejus.loha 427
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
428
    public String updateForm(@RequestBody BrandFeeCollection brandFeeCollection, Model model) throws Exception {
429
        BrandFeeCollection brandFeeCollection1 = brandFeeCollectionRepository.selectById(brandFeeCollection.getId());
430
        brandFeeCollection1.setFeeCollectingTimeStamp(brandFeeCollection.getFeeCollectingTimeStamp());
431
        brandFeeCollection1.setCollectedAmount(brandFeeCollection.getCollectedAmount());
432
        brandFeeCollection1.setPaymentReferenceNo(brandFeeCollection.getPaymentReferenceNo());
433
        brandFeeCollection1.setPaymentMode(brandFeeCollection.getPaymentMode());
434
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
435
        return "response";
436
    }
33617 tejus.loha 437
 
33710 tejus.loha 438
    @RequestMapping(value = "/updateAgreedBrandFee", method = RequestMethod.PUT)
439
    public String updateAgreedBrandFee(HttpServletRequest request, @RequestParam int loiId, @RequestParam double brandFee, Model model) throws Exception {
440
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
441
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(loginDetails.getEmailId()));
442
        if (isAgreedBrandFeeChanger) {
443
            LoiForm loiForm = loiFormRepository.selectById(loiId);
444
            loiForm.setAgreedBrandFees(brandFee);
445
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
446
            return "response";
447
        } else {
448
            throw new ProfitMandiBusinessException("failed in Authority check ", null, "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to change agreed brand fee");
449
        }
450
 
451
    }
452
 
33507 tejus.loha 453
}