Subversion Repositories SmartDukaan

Rev

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