Subversion Repositories SmartDukaan

Rev

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