Subversion Repositories SmartDukaan

Rev

Rev 33885 | Rev 34085 | 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.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
33879 tejus.loha 112
    BrandCommitRepository brandCommitRepository;
33658 tejus.loha 113
    @Autowired
114
    BrandsRepository brandsRepository;
115
    @Autowired
116
    BrandCategoryRepository brandCategoryRepository;
117
    @Autowired
118
    BillingAddressRepository billingAddressRepository;
119
    @Autowired
120
    LoiBrandCommitmentRepository brandCommitmentRepository;
33747 tejus.loha 121
    @Autowired
122
    PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
123
 
33710 tejus.loha 124
    List<String> agreedBrandFeeChangerEmail = Arrays.asList("sm@smartdukaan.com");
33507 tejus.loha 125
 
33845 tejus.loha 126
    // Loi Form
127
    @RequestMapping(value = "/loiForm", method = RequestMethod.GET)
33507 tejus.loha 128
    public String loiForm(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
33579 tejus.loha 129
        List<AuthUser> authUsersList = loiFormService.getReferalAuthList();
33507 tejus.loha 130
        List<BrandFee> brandFee = brandFeeRepository.selectFeeOnDate(LocalDateTime.now());
131
        Set<Pair<StoreType, BrandFee>> storeTypeFeePairs = new HashSet<>();
132
        for (BrandFee fee : brandFee) {
133
            for (StoreType storeType : StoreType.values()) {
134
                storeTypeFeePairs.add(Pair.of(storeType, fee));
135
            }
136
        }
33885 tejus.loha 137
        List<BrandCommit> brandCommits = brandCommitRepository.selectAllActiveBrand();
33507 tejus.loha 138
        List<BusinessType> businessTypes = Arrays.asList(BusinessType.Proprietorship, BusinessType.Partnership, BusinessType.Company);
139
        model.addAttribute("businessTypes", businessTypes);
33658 tejus.loha 140
        model.addAttribute("brandCommits", brandCommits);
33507 tejus.loha 141
        model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);
142
        model.addAttribute("authUsersList", authUsersList);
33845 tejus.loha 143
        return "loi-form";
33507 tejus.loha 144
    }
145
 
146
    // use to validate GSTIN
147
    @RequestMapping(value = "/gstValidate", method = RequestMethod.GET)
148
    public ResponseEntity<?> gstValidate(HttpServletRequest request, @RequestParam String gstNo) throws Exception {
149
        LOGGER.info("gstNo -" + gstNo);
150
        GstDetails gstDetails = gstProService.getGstDetails(gstNo);
151
        LOGGER.info("gstDetails -" + gstDetails);
152
        List<GstDetails.Pradr> adadr = gstDetails.getAdadr();
153
        adadr.add(gstDetails.getPradr());
154
        Collections.reverse(adadr);
155
        LOGGER.info("list_of_adadr-" + adadr);
34026 tejus.loha 156
        return ResponseEntity.ok(gstDetails);
33507 tejus.loha 157
    }
158
 
34026 tejus.loha 159
    // Save Loi data in fofo.loi_form table
33577 tejus.loha 160
    @RequestMapping(value = "/submitLoiForm", method = RequestMethod.POST)
33568 tejus.loha 161
    public String submitLoiForm(HttpServletRequest request, @RequestBody LoiFormData loiFormData, Model model) throws Exception {
33507 tejus.loha 162
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
33577 tejus.loha 163
        LOGGER.info("filledBy - " + loginDetails.getEmailId());
164
        boolean isDataCreated = loiFormService.createLoiForm(loiFormData, loginDetails.getEmailId());
33507 tejus.loha 165
        if (isDataCreated) {
166
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
167
            return "response";
168
        } else {
169
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
170
            return "response";
171
        }
172
    }
173
 
34026 tejus.loha 174
    // show all pending loi form to specific auth user who have filled the form(type based)
33710 tejus.loha 175
    @RequestMapping(value = "/pendingLoiForm", method = RequestMethod.GET)
33507 tejus.loha 176
    public String pendingLoiForm(HttpServletRequest request, Model model) throws Exception {
177
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
178
        String email = loginDetails.getEmailId();
179
        boolean isDocApprover = false;
180
        String approverEmail = "gaurav.sharma@smartdukaan.com";
181
        if (email.equals(approverEmail)) {
182
            isDocApprover = true;
183
        }
33710 tejus.loha 184
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(email));
33711 tejus.loha 185
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
33710 tejus.loha 186
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
33507 tejus.loha 187
        List<LoiFormModel> pendingFormList = loiFormService.pendingFormList(email);
188
        model.addAttribute("isDocApprover", isDocApprover);
33617 tejus.loha 189
        model.addAttribute("isAuthUser", isAuthUser);
33710 tejus.loha 190
        model.addAttribute("isAgreedBrandFeeChanger", isAgreedBrandFeeChanger);
33507 tejus.loha 191
        model.addAttribute("pendingFormList", pendingFormList);
192
        return "pendingForm";
193
    }
194
 
195
    // generate LOI for specific Form data
196
    @RequestMapping(value = "/generateLoi", method = RequestMethod.GET)
33845 tejus.loha 197
    public String generateLoi(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
33507 tejus.loha 198
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33630 tejus.loha 199
        String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();
33507 tejus.loha 200
        loiForm.setLoiGeneratedOn(LocalDateTime.now());
201
        BrandFee brandFee = brandFeeRepository.selectById(1);
33658 tejus.loha 202
        BillingAddress address = billingAddressRepository.selectByLoiFormId(loiId);
203
        List<LoiBrandCommitment> loiBrandCommitments = brandCommitmentRepository.selectByLoiId(loiId).stream().filter(x -> x.getAmount() > 0).collect(Collectors.toList());
204
        double totalCommitment = loiBrandCommitments.stream().mapToDouble(x -> x.getAmount()).sum();
33845 tejus.loha 205
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId);
206
        double brandFeeAmount = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33658 tejus.loha 207
        BrandFeeCollection brandFeeCollection = brandFeeCollections.get(0);
208
        model.addAttribute("totalCommitment", totalCommitment);
209
        model.addAttribute("loiBrandCommitments", loiBrandCommitments);
33507 tejus.loha 210
        model.addAttribute("brandFeeAmount", brandFeeAmount);
211
        model.addAttribute("brandFee", brandFee);
212
        model.addAttribute("brandFeeCollection", brandFeeCollection);
213
        model.addAttribute("address", address);
33658 tejus.loha 214
        model.addAttribute("loiForm", loiForm);
33630 tejus.loha 215
        model.addAttribute("filledBy", filledBy);
33507 tejus.loha 216
        return "auto-Loi";
217
 
218
    }
219
 
220
 
221
    // send Filled form for update purpose
33658 tejus.loha 222
    @RequestMapping(value = "/updateLoiForm", method = RequestMethod.GET)
33507 tejus.loha 223
    public String UpdateLoiForm(HttpServletRequest request,
224
                                @RequestParam int loiId,
225
                                Model model) throws ProfitMandiBusinessException {
226
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33658 tejus.loha 227
        model.addAttribute("updateForm", true);
228
        model.addAttribute("loiForm", loiForm);
229
        return "loiForm/update-loiForm";
33507 tejus.loha 230
    }
231
 
33577 tejus.loha 232
    @RequestMapping(value = "/updateLoiFormData", method = RequestMethod.POST)
33507 tejus.loha 233
    public String UpdateLoiFormDetail(HttpServletRequest request,
234
                                      @RequestParam int loiId,
33658 tejus.loha 235
                                      @RequestBody LoiFormData loiFormData,
33507 tejus.loha 236
                                      Model model) throws Exception {
33658 tejus.loha 237
        loiFormService.updateLoiForm(loiFormData, loiId);
33507 tejus.loha 238
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
239
        return "response";
240
    }
241
 
242
    @RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)
243
    public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {
244
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33845 tejus.loha 245
        double totatcollectedfee = brandFeeCollectionRepository.selectAllByLoiFormId(loiId).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33507 tejus.loha 246
        double brandFee = loiForm.getAgreedBrandFees();
247
        double differanceAmount = brandFee - totatcollectedfee;
248
        if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {
249
            brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);
33658 tejus.loha 250
            brandFeeCollection.setLoiFormId(loiId);
251
            brandFeeCollectionRepository.persist(brandFeeCollection);
33885 tejus.loha 252
//            loiFormService.sentMailToPaymentApprover(brandFeeCollection, loiForm);
33507 tejus.loha 253
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
254
            return "response";
255
        } else {
256
            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");
257
        }
258
    }
259
 
260
 
33617 tejus.loha 261
    @RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)
33507 tejus.loha 262
    public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
263
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
264
        String email = loginDetails.getEmailId();
265
        boolean isNiraj = false;
266
        if (email.equals("neeraj.gupta@smartdukaan.com")) {
267
            isNiraj = true;
268
        }
33658 tejus.loha 269
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33507 tejus.loha 270
        model.addAttribute("brandFeeCollections", brandFeeCollections);
271
        model.addAttribute("confirm", FeePaymentStatus.CONFIRM);
272
        model.addAttribute("isNiraj", isNiraj);
273
        return "payment-collection-details";
274
    }
275
 
276
    // bfcId - Brand Fee Collection Id
33617 tejus.loha 277
    @RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)
33525 tejus.loha 278
    public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,
279
                                     Model model) throws Exception {
33507 tejus.loha 280
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
281
        String authEmail = loginDetails.getEmailId();
282
        BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);
283
        brandFeeCollection.setApproverEmail(authEmail);
284
        brandFeeCollection.setPaymentStatus(feePaymentStatus);
33525 tejus.loha 285
        brandFeeCollection.setDescription(description);
33848 tejus.loha 286
        if (feePaymentStatus.equals(FeePaymentStatus.CONFIRM)) {
287
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(brandFeeCollection.getLoiFormId()));
288
        }
33845 tejus.loha 289
        loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);
290
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33507 tejus.loha 291
        return "response";
292
 
293
    }
294
 
33658 tejus.loha 295
 
33617 tejus.loha 296
    @RequestMapping(value = "/uploadDocumentForm", method = RequestMethod.GET)
33507 tejus.loha 297
    public String uploadDocumentForm(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
298
        List<LoiDocMaster> activeDocMasterList = loiDocMasterList.getAllActiveDoc();
299
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
300
        String authEmail = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getEmailId();
301
        String approverEmail = "gaurav.sharma@smartdukaan.com";
302
        boolean isApprover = false;
303
        if (authEmail.equals(approverEmail)) {
304
            isApprover = true;
305
        }
33845 tejus.loha 306
        Map<Integer, LoiDoc> masterDocIdLoiDocMap = loiDocRepository.selectByLoiFormId(loiId).stream().collect(Collectors.toMap(x -> x.getMasterDocId(), x -> x));
33507 tejus.loha 307
        model.addAttribute("isApprover", isApprover);
308
        model.addAttribute("loiId", loiId);
309
        model.addAttribute("activeDocMasterList", activeDocMasterList);
310
        model.addAttribute("masterDocIdLoiDocMap", masterDocIdLoiDocMap);
311
        return "loiForm/document-upload";
312
    }
313
 
314
    @RequestMapping(value = "/uploadOnboardingDocument", method = RequestMethod.POST)
315
    public String uploadDocument(HttpServletRequest request, @RequestParam int loiId, @RequestBody List<LoiDocModel> loiDocModels, Model model) throws Exception {
316
        LoiForm loiForm = loiFormRepository.selectById(loiId);
317
        loiFormService.setDocs(loiForm, loiDocModels);
318
        model.addAttribute("response1", mvcResponseSender.createResponseString("Document uploaded successfully"));
319
        return "response";
320
 
321
    }
322
 
323
 
33617 tejus.loha 324
    @RequestMapping(value = "/documentVerify", method = RequestMethod.PUT)
33507 tejus.loha 325
    public String docApproval(HttpServletRequest request, @RequestParam int loiId, @RequestParam int docMasterId, @RequestParam boolean flag, Model model) throws Exception {
326
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
327
        AuthUser auth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
328
        LoiDoc loiDoc = loiDocRepository.selectByLoiIdAndMasterDocId(loiId, docMasterId);
329
        if (flag) {
330
            loiDoc.setOk(true);
331
            loiDoc.setVerifyBy(auth.getId());
332
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Accepted "));
333
            return "response";
334
        } else {
335
            loiDoc.setOk(false);
336
            loiDoc.setVerifyBy(auth.getId());
33658 tejus.loha 337
            loiFormService.sendDocRejectionMail(loiDoc);
33507 tejus.loha 338
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Rejected"));
339
            return "response";
340
        }
341
    }
342
 
343
    @RequestMapping(value = "/loiAcceptanceOtp", method = RequestMethod.POST)
344
    public String sentLoiAcceptanceOtp(@RequestParam int loiId, Model model) throws Exception {
33658 tejus.loha 345
        LoiForm loiForm = loiFormRepository.selectById(loiId);
346
        String mobile = String.valueOf(loiForm.getMobile());
33525 tejus.loha 347
        OTPResponse otpResponse = otpProcessor.generateOtp(mobile, OtpType.LOI_ACCEPTANCE);
33658 tejus.loha 348
        String message = "OTP send on your register mobile and EmailId - ";
33568 tejus.loha 349
        LOGGER.info("OTPResponse Message - " + otpResponse.getMessage());
350
        if (otpResponse.isResult()) {
33658 tejus.loha 351
            model.addAttribute("response1", mvcResponseSender.createResponseString(message + mobile + " and " + loiForm.getEmail()));
33568 tejus.loha 352
            return "response";
33525 tejus.loha 353
        } else {
354
            model.addAttribute("response1", mvcResponseSender.createResponseString("Something went wrong.."));
33568 tejus.loha 355
            return "response";
33525 tejus.loha 356
        }
33507 tejus.loha 357
    }
358
 
359
    @RequestMapping(value = "/validateLoiOtp", method = RequestMethod.PUT)
360
    public String validateLoiAcceptanceOtp(@RequestParam int loiId, @RequestParam String provideOtp, Model model) throws Exception {
33658 tejus.loha 361
        LoiForm loiForm = loiFormRepository.selectById(loiId);
362
        String mobile = String.valueOf(loiForm.getMobile());
33507 tejus.loha 363
        Otp otp = otpRepository.selectAllByMobileWithTime(mobile).get(0);
364
        OTPResponse otpResponse = otpProcessor.validateOtp(otp.getId(), mobile, provideOtp);
365
        if (otpResponse.isResult()) {
33658 tejus.loha 366
            loiForm.setLoiSignOtp(provideOtp);
367
            loiForm.setLoiSignedOn(LocalDateTime.now());
33507 tejus.loha 368
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33845 tejus.loha 369
        } else {
370
            throw new ProfitMandiBusinessException("Wrong OTP", "", "Your given Otp ->" + provideOtp + " is wrong , please check OTP and try again");
371
 
33507 tejus.loha 372
        }
373
        return "response";
374
    }
375
 
33845 tejus.loha 376
    @RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)
377
    public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {
378
        LOGGER.info("loi_docId-" + loiDocId);
379
        LoiForm loiForm = loiFormRepository.selectById(loiId);
380
        if (loiDocId > 0) {
381
            loiForm.setLoiDoc(loiDocId);
382
            loiFormService.sendSignedLoiPdfToPartner(loiForm);
33848 tejus.loha 383
            loiFormService.checkLoiDetailsCompletion(loiForm);
33845 tejus.loha 384
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
385
        } else {
386
            loiForm.setLoiSignOtp("");
387
            loiForm.setLoiGeneratedOn(null);
388
            loiForm.setLoiSignedOn(null);
389
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
390
        }
391
        return "response";
392
 
393
    }
394
 
33617 tejus.loha 395
    @RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)
396
    public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {
397
        ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);
398
        return responseEntity;
399
    }
400
 
33658 tejus.loha 401
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
402
    public String updateForm(@RequestBody BrandFeeCollection brandFeeCollection, Model model) throws Exception {
403
        BrandFeeCollection brandFeeCollection1 = brandFeeCollectionRepository.selectById(brandFeeCollection.getId());
33845 tejus.loha 404
        LoiForm loiForm = loiFormRepository.selectById(brandFeeCollection1.getLoiFormId());
33850 tejus.loha 405
        brandFeeCollection1.setFeeCollectingTimeStamp(brandFeeCollection.getFeeCollectingTimeStamp());
406
        brandFeeCollection1.setCollectedAmount(brandFeeCollection.getCollectedAmount());
33885 tejus.loha 407
        brandFeeCollection1.setPaymentAttachment(brandFeeCollection.getPaymentAttachment());
33850 tejus.loha 408
        brandFeeCollection1.setPaymentReferenceNo(brandFeeCollection.getPaymentReferenceNo());
409
        brandFeeCollection1.setPaymentMode(brandFeeCollection.getPaymentMode());
410
        double totalCollectedFee = brandFeeCollectionRepository.selectAllByLoiFormId(loiForm.getId()).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
411
        if (totalCollectedFee <= loiForm.getAgreedBrandFees()) {
33845 tejus.loha 412
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
413
        } else {
33850 tejus.loha 414
            double differanceAmount = totalCollectedFee - loiForm.getAgreedBrandFees();
33845 tejus.loha 415
            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");
416
        }
33658 tejus.loha 417
        return "response";
418
    }
33617 tejus.loha 419
 
33710 tejus.loha 420
    @RequestMapping(value = "/updateAgreedBrandFee", method = RequestMethod.PUT)
421
    public String updateAgreedBrandFee(HttpServletRequest request, @RequestParam int loiId, @RequestParam double brandFee, Model model) throws Exception {
422
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
423
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(loginDetails.getEmailId()));
424
        if (isAgreedBrandFeeChanger) {
425
            LoiForm loiForm = loiFormRepository.selectById(loiId);
426
            loiForm.setAgreedBrandFees(brandFee);
427
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
428
            return "response";
429
        } else {
430
            throw new ProfitMandiBusinessException("failed in Authority check ", null, "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to change agreed brand fee");
431
        }
432
    }
433
 
33845 tejus.loha 434
 
435
    @RequestMapping(value = "/approve-reject-Loi", method = RequestMethod.PUT)
436
    public String approveDetails(HttpServletRequest request, @RequestParam int loiId, @RequestParam boolean flag, Model model) throws Exception {
437
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
438
        String email = loginDetails.getEmailId();
33711 tejus.loha 439
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33845 tejus.loha 440
        AuthUser filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy());
441
        AuthUser manager = authRepository.selectById(filledBy.getManagerId());
442
        AuthUser upperManager = null;
443
        List<String> approverMail = new ArrayList<>();
444
        approverMail.add(manager.getEmailId());
445
        upperManager = authRepository.selectById(manager.getManagerId());
446
        approverMail.add(upperManager.getEmailId());
447
 
448
        LOGGER.info("approverMail-" + approverMail);
449
        // Approver - Manager or Upper manager of loi filledBy(BM)
450
        boolean isApprover = approverMail.contains(email);
451
        LOGGER.info("isApprover-" + isApprover);
452
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
453
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
454
        if (flag) {
455
            if (isApprover || isAuthUser) {
456
                loiForm.setLoiApprover(email);
457
                loiForm.setLoiApproved(flag);
458
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
459
            } else {
460
                throw new ProfitMandiBusinessException("Failed in Authority check ", "Read", "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to Approve this Loi Form ,Only reporting manager and upper can Approve");
461
            }
462
        } else {
463
            if (isApprover || isAuthUser) {
464
                loiForm.setLoiApprover(email);
465
                loiForm.setLoiApproved(flag);
466
                loiForm.setStatus(LoiStatus.LOI_REJECT);
467
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
468
            } else {
469
                throw new ProfitMandiBusinessException("Failed in Authority check ", "Read", "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to Approve this Loi Form ,Only reporting manager and upper can Approve");
470
            }
471
        }
33711 tejus.loha 472
        return "response";
473
    }
474
 
33845 tejus.loha 475
 
33507 tejus.loha 476
}