Subversion Repositories SmartDukaan

Rev

Rev 34149 | Rev 34310 | 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;
34107 tejus.loha 6
import com.spice.profitmandi.common.model.ProfitMandiConstants;
33507 tejus.loha 7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
8
import com.spice.profitmandi.dao.entity.brandFee.BrandFee;
9
import com.spice.profitmandi.dao.entity.brandFee.BrandFeeCollection;
10
import com.spice.profitmandi.dao.entity.dtr.Otp;
11
import com.spice.profitmandi.dao.entity.onBoarding.*;
34107 tejus.loha 12
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
33507 tejus.loha 13
import com.spice.profitmandi.dao.enumuration.dtr.OtpType;
14
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.FeePaymentStatus;
15
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.LoiStatus;
16
import com.spice.profitmandi.dao.enumuration.onBorading.onBoardingFormEnums.StoreType;
17
import com.spice.profitmandi.dao.model.LoiFormModel;
18
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
19
import com.spice.profitmandi.dao.repository.brandFee.BrandFeeCollectionRepository;
20
import com.spice.profitmandi.dao.repository.brandFee.BrandFeeRepository;
33658 tejus.loha 21
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;
22
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
33507 tejus.loha 23
import com.spice.profitmandi.dao.repository.cs.CsService;
24
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
25
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
26
import com.spice.profitmandi.dao.repository.dtr.OtpRepository;
33747 tejus.loha 27
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
33507 tejus.loha 28
import com.spice.profitmandi.dao.repository.inventory.PartnerOnboardingService;
29
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
33658 tejus.loha 30
import com.spice.profitmandi.dao.repository.onboarding.BrandCommitRepository;
31
import com.spice.profitmandi.dao.repository.onboarding.LoiBrandCommitmentRepository;
33507 tejus.loha 32
import com.spice.profitmandi.dao.repository.onboarding.LoiDocMasterRepository;
33
import com.spice.profitmandi.dao.repository.onboarding.LoiDocRepository;
34
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
33658 tejus.loha 35
import com.spice.profitmandi.dao.repository.user.BillingAddressRepository;
33507 tejus.loha 36
import com.spice.profitmandi.dao.repository.user.LoiFormRepository;
37
import com.spice.profitmandi.dao.service.LoiDocModel;
38
import com.spice.profitmandi.dao.service.OTPResponse;
39
import com.spice.profitmandi.dao.service.OtpProcessor;
40
import com.spice.profitmandi.dao.service.loiForm.LoiFormService;
41
import com.spice.profitmandi.service.integrations.gstpro.GstProService;
42
import com.spice.profitmandi.service.integrations.gstpro.entity.GstDetails;
43
import com.spice.profitmandi.service.user.StoreTimelineTatService;
44
import com.spice.profitmandi.web.model.LoginDetails;
45
import com.spice.profitmandi.web.util.CookiesProcessor;
46
import com.spice.profitmandi.web.util.MVCResponseSender;
47
import org.apache.commons.lang3.tuple.Pair;
48
import org.apache.logging.log4j.LogManager;
49
import org.apache.logging.log4j.Logger;
50
import org.springframework.beans.factory.annotation.Autowired;
51
import org.springframework.http.ResponseEntity;
52
import org.springframework.mail.javamail.JavaMailSender;
53
import org.springframework.stereotype.Controller;
54
import org.springframework.transaction.annotation.Transactional;
55
import org.springframework.ui.Model;
56
import org.springframework.web.bind.annotation.RequestBody;
57
import org.springframework.web.bind.annotation.RequestMapping;
58
import org.springframework.web.bind.annotation.RequestMethod;
59
import org.springframework.web.bind.annotation.RequestParam;
60
 
61
import javax.servlet.http.HttpServletRequest;
33617 tejus.loha 62
import java.time.LocalDate;
33507 tejus.loha 63
import java.time.LocalDateTime;
64
import java.util.*;
65
import java.util.stream.Collectors;
66
 
67
@Controller
68
@Transactional(rollbackFor = Throwable.class)
69
public class LoiFormController {
34149 tejus.loha 70
    private static final Logger LOGGER = LogManager.getLogger(LoiFormController.class);
33507 tejus.loha 71
    @Autowired
72
    DocumentRepository documentRepository;
73
    @Autowired
74
    AuthRepository authRepository;
75
    @Autowired
76
    StateRepository stateRepository;
77
    @Autowired
78
    DistrictMasterRepository districtMasterRepository;
79
    @Autowired
80
    LoiFormRepository loiFormRepository;
81
    @Autowired
82
    GstProService gstProService;
83
    @Autowired
84
    ObjectMapper objectMapper;
85
    @Autowired
86
    OrderRepository orderRepository;
87
    @Autowired
88
    LoiFormService loiFormService;
89
    @Autowired
90
    BrandFeeCollectionRepository brandFeeCollectionRepository;
91
    @Autowired
92
    BrandFeeRepository brandFeeRepository;
93
    @Autowired
94
    OtpProcessor otpProcessor;
95
    @Autowired
96
    OtpRepository otpRepository;
97
    @Autowired
98
    JavaMailSender mailSender;
99
    @Autowired
100
    LoiDocMasterRepository loiDocMasterList;
101
    @Autowired
102
    CsService csService;
103
    @Autowired
104
    PartnerOnboardingService partnerOnboardingService;
105
    @Autowired
106
    StoreTimelineTatService storeTimelineTatService;
107
    @Autowired
108
    LoiDocRepository loiDocRepository;
109
    @Autowired
110
    MVCResponseSender mvcResponseSender;
111
    @Autowired
112
    private CookiesProcessor cookiesProcessor;
33658 tejus.loha 113
    @Autowired
33879 tejus.loha 114
    BrandCommitRepository brandCommitRepository;
33658 tejus.loha 115
    @Autowired
116
    BrandsRepository brandsRepository;
117
    @Autowired
118
    BrandCategoryRepository brandCategoryRepository;
119
    @Autowired
120
    BillingAddressRepository billingAddressRepository;
121
    @Autowired
122
    LoiBrandCommitmentRepository brandCommitmentRepository;
33747 tejus.loha 123
    @Autowired
124
    PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
125
 
33710 tejus.loha 126
    List<String> agreedBrandFeeChangerEmail = Arrays.asList("sm@smartdukaan.com");
33507 tejus.loha 127
 
33845 tejus.loha 128
    // Loi Form
129
    @RequestMapping(value = "/loiForm", method = RequestMethod.GET)
33507 tejus.loha 130
    public String loiForm(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
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
        }
34107 tejus.loha 139
        List<AuthUser> stateHeadList = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Arrays.asList(EscalationType.L2, EscalationType.L3));
140
        List<AuthUser> bdmList = csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
33885 tejus.loha 141
        List<BrandCommit> brandCommits = brandCommitRepository.selectAllActiveBrand();
34195 tejus.loha 142
        model.addAttribute("businessTypes", BusinessType.values());
33658 tejus.loha 143
        model.addAttribute("brandCommits", brandCommits);
33507 tejus.loha 144
        model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);
145
        model.addAttribute("authUsersList", authUsersList);
34107 tejus.loha 146
        model.addAttribute("stateHeadList", stateHeadList);
147
        model.addAttribute("bdmList", bdmList);
33845 tejus.loha 148
        return "loi-form";
33507 tejus.loha 149
    }
150
 
151
    // use to validate GSTIN
152
    @RequestMapping(value = "/gstValidate", method = RequestMethod.GET)
153
    public ResponseEntity<?> gstValidate(HttpServletRequest request, @RequestParam String gstNo) throws Exception {
154
        LOGGER.info("gstNo -" + gstNo);
155
        GstDetails gstDetails = gstProService.getGstDetails(gstNo);
156
        LOGGER.info("gstDetails -" + gstDetails);
157
        List<GstDetails.Pradr> adadr = gstDetails.getAdadr();
158
        adadr.add(gstDetails.getPradr());
159
        Collections.reverse(adadr);
160
        LOGGER.info("list_of_adadr-" + adadr);
34026 tejus.loha 161
        return ResponseEntity.ok(gstDetails);
33507 tejus.loha 162
    }
163
 
34026 tejus.loha 164
    // Save Loi data in fofo.loi_form table
33577 tejus.loha 165
    @RequestMapping(value = "/submitLoiForm", method = RequestMethod.POST)
33568 tejus.loha 166
    public String submitLoiForm(HttpServletRequest request, @RequestBody LoiFormData loiFormData, Model model) throws Exception {
33507 tejus.loha 167
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
33577 tejus.loha 168
        LOGGER.info("filledBy - " + loginDetails.getEmailId());
169
        boolean isDataCreated = loiFormService.createLoiForm(loiFormData, loginDetails.getEmailId());
33507 tejus.loha 170
        if (isDataCreated) {
171
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
172
            return "response";
173
        } else {
174
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
175
            return "response";
176
        }
177
    }
178
 
34026 tejus.loha 179
    // show all pending loi form to specific auth user who have filled the form(type based)
33710 tejus.loha 180
    @RequestMapping(value = "/pendingLoiForm", method = RequestMethod.GET)
33507 tejus.loha 181
    public String pendingLoiForm(HttpServletRequest request, Model model) throws Exception {
182
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
183
        String email = loginDetails.getEmailId();
184
        boolean isDocApprover = false;
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));
33711 tejus.loha 190
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@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);
34085 tejus.loha 197
        model.addAttribute("brandType", StoreType.valueList);
33507 tejus.loha 198
        return "pendingForm";
199
    }
200
 
34085 tejus.loha 201
//    @RequestMapping(value = "/pendingLoiFormList", method = RequestMethod.GET)
202
//    public String pendingLoiForm1(HttpServletRequest request, @RequestParam int pageSize, @RequestParam int pageNumber, Model model) throws Exception {
203
//        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
204
//        String email = loginDetails.getEmailId();
205
//        Map<String, Object> pendingFormMap = loiFormService.pendingFormList(email);
206
//        model.addAttribute("pendingFormList", pendingFormMap.get("pendingFormModelList"));
207
//        model.addAttribute("paginationInfo", this.getPaginationInfo(pageNumber,pageSize,(long)pendingFormMap.get("totalCount")));
208
//        return "loiData";
209
//    }
210
//    public String getPaginationInfo(int currentPage, int pageSize, long totalEntries) {
211
//        // Calculate the starting index
212
//        int start = (currentPage - 1) * pageSize + 1; // Start from 1
213
//        // Calculate the ending index
214
//        int end = Math.min(start + pageSize - 1, (int) totalEntries); // Ensure it doesn't exceed totalEntries
215
//
216
//        return "Showing " + start + " to " + end + " of " + totalEntries + " entries";
217
//    }
218
 
33507 tejus.loha 219
    // generate LOI for specific Form data
220
    @RequestMapping(value = "/generateLoi", method = RequestMethod.GET)
33845 tejus.loha 221
    public String generateLoi(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
33507 tejus.loha 222
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33630 tejus.loha 223
        String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();
33507 tejus.loha 224
        loiForm.setLoiGeneratedOn(LocalDateTime.now());
225
        BrandFee brandFee = brandFeeRepository.selectById(1);
33658 tejus.loha 226
        BillingAddress address = billingAddressRepository.selectByLoiFormId(loiId);
227
        List<LoiBrandCommitment> loiBrandCommitments = brandCommitmentRepository.selectByLoiId(loiId).stream().filter(x -> x.getAmount() > 0).collect(Collectors.toList());
228
        double totalCommitment = loiBrandCommitments.stream().mapToDouble(x -> x.getAmount()).sum();
33845 tejus.loha 229
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId);
230
        double brandFeeAmount = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33658 tejus.loha 231
        BrandFeeCollection brandFeeCollection = brandFeeCollections.get(0);
232
        model.addAttribute("totalCommitment", totalCommitment);
233
        model.addAttribute("loiBrandCommitments", loiBrandCommitments);
33507 tejus.loha 234
        model.addAttribute("brandFeeAmount", brandFeeAmount);
235
        model.addAttribute("brandFee", brandFee);
236
        model.addAttribute("brandFeeCollection", brandFeeCollection);
237
        model.addAttribute("address", address);
33658 tejus.loha 238
        model.addAttribute("loiForm", loiForm);
33630 tejus.loha 239
        model.addAttribute("filledBy", filledBy);
33507 tejus.loha 240
        return "auto-Loi";
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
    @RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)
267
    public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {
268
        LoiForm loiForm = loiFormRepository.selectById(loiId);
34086 tejus.loha 269
        double totatcollectedfee = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33507 tejus.loha 270
        double brandFee = loiForm.getAgreedBrandFees();
271
        double differanceAmount = brandFee - totatcollectedfee;
272
        if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {
273
            brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);
33658 tejus.loha 274
            brandFeeCollection.setLoiFormId(loiId);
275
            brandFeeCollectionRepository.persist(brandFeeCollection);
33885 tejus.loha 276
//            loiFormService.sentMailToPaymentApprover(brandFeeCollection, loiForm);
33507 tejus.loha 277
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
278
            return "response";
279
        } else {
280
            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");
281
        }
282
    }
283
 
284
 
33617 tejus.loha 285
    @RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)
33507 tejus.loha 286
    public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
287
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
288
        String email = loginDetails.getEmailId();
289
        boolean isNiraj = false;
290
        if (email.equals("neeraj.gupta@smartdukaan.com")) {
291
            isNiraj = true;
292
        }
33658 tejus.loha 293
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33507 tejus.loha 294
        model.addAttribute("brandFeeCollections", brandFeeCollections);
295
        model.addAttribute("confirm", FeePaymentStatus.CONFIRM);
296
        model.addAttribute("isNiraj", isNiraj);
297
        return "payment-collection-details";
298
    }
299
 
300
    // bfcId - Brand Fee Collection Id
33617 tejus.loha 301
    @RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)
33525 tejus.loha 302
    public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,
303
                                     Model model) throws Exception {
33507 tejus.loha 304
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
305
        String authEmail = loginDetails.getEmailId();
306
        BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);
307
        brandFeeCollection.setApproverEmail(authEmail);
308
        brandFeeCollection.setPaymentStatus(feePaymentStatus);
33525 tejus.loha 309
        brandFeeCollection.setDescription(description);
34195 tejus.loha 310
        brandFeeCollection.setApprovalTimeStamp(LocalDateTime.now());
33848 tejus.loha 311
        if (feePaymentStatus.equals(FeePaymentStatus.CONFIRM)) {
312
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(brandFeeCollection.getLoiFormId()));
313
        }
33845 tejus.loha 314
        loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);
315
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33507 tejus.loha 316
        return "response";
317
 
318
    }
319
 
33658 tejus.loha 320
 
33617 tejus.loha 321
    @RequestMapping(value = "/uploadDocumentForm", method = RequestMethod.GET)
33507 tejus.loha 322
    public String uploadDocumentForm(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
323
        List<LoiDocMaster> activeDocMasterList = loiDocMasterList.getAllActiveDoc();
324
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
325
        String authEmail = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getEmailId();
326
        String approverEmail = "gaurav.sharma@smartdukaan.com";
327
        boolean isApprover = false;
328
        if (authEmail.equals(approverEmail)) {
329
            isApprover = true;
330
        }
33845 tejus.loha 331
        Map<Integer, LoiDoc> masterDocIdLoiDocMap = loiDocRepository.selectByLoiFormId(loiId).stream().collect(Collectors.toMap(x -> x.getMasterDocId(), x -> x));
33507 tejus.loha 332
        model.addAttribute("isApprover", isApprover);
333
        model.addAttribute("loiId", loiId);
334
        model.addAttribute("activeDocMasterList", activeDocMasterList);
335
        model.addAttribute("masterDocIdLoiDocMap", masterDocIdLoiDocMap);
336
        return "loiForm/document-upload";
337
    }
338
 
339
    @RequestMapping(value = "/uploadOnboardingDocument", method = RequestMethod.POST)
340
    public String uploadDocument(HttpServletRequest request, @RequestParam int loiId, @RequestBody List<LoiDocModel> loiDocModels, Model model) throws Exception {
341
        LoiForm loiForm = loiFormRepository.selectById(loiId);
342
        loiFormService.setDocs(loiForm, loiDocModels);
343
        model.addAttribute("response1", mvcResponseSender.createResponseString("Document uploaded successfully"));
344
        return "response";
345
 
346
    }
347
 
348
 
33617 tejus.loha 349
    @RequestMapping(value = "/documentVerify", method = RequestMethod.PUT)
33507 tejus.loha 350
    public String docApproval(HttpServletRequest request, @RequestParam int loiId, @RequestParam int docMasterId, @RequestParam boolean flag, Model model) throws Exception {
351
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
352
        AuthUser auth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
353
        LoiDoc loiDoc = loiDocRepository.selectByLoiIdAndMasterDocId(loiId, docMasterId);
354
        if (flag) {
355
            loiDoc.setOk(true);
356
            loiDoc.setVerifyBy(auth.getId());
357
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Accepted "));
358
            return "response";
359
        } else {
360
            loiDoc.setOk(false);
361
            loiDoc.setVerifyBy(auth.getId());
33658 tejus.loha 362
            loiFormService.sendDocRejectionMail(loiDoc);
33507 tejus.loha 363
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Rejected"));
364
            return "response";
365
        }
366
    }
367
 
368
    @RequestMapping(value = "/loiAcceptanceOtp", method = RequestMethod.POST)
369
    public String sentLoiAcceptanceOtp(@RequestParam int loiId, Model model) throws Exception {
33658 tejus.loha 370
        LoiForm loiForm = loiFormRepository.selectById(loiId);
371
        String mobile = String.valueOf(loiForm.getMobile());
33525 tejus.loha 372
        OTPResponse otpResponse = otpProcessor.generateOtp(mobile, OtpType.LOI_ACCEPTANCE);
33658 tejus.loha 373
        String message = "OTP send on your register mobile and EmailId - ";
33568 tejus.loha 374
        LOGGER.info("OTPResponse Message - " + otpResponse.getMessage());
375
        if (otpResponse.isResult()) {
33658 tejus.loha 376
            model.addAttribute("response1", mvcResponseSender.createResponseString(message + mobile + " and " + loiForm.getEmail()));
33568 tejus.loha 377
            return "response";
33525 tejus.loha 378
        } else {
379
            model.addAttribute("response1", mvcResponseSender.createResponseString("Something went wrong.."));
33568 tejus.loha 380
            return "response";
33525 tejus.loha 381
        }
33507 tejus.loha 382
    }
383
 
384
    @RequestMapping(value = "/validateLoiOtp", method = RequestMethod.PUT)
385
    public String validateLoiAcceptanceOtp(@RequestParam int loiId, @RequestParam String provideOtp, Model model) throws Exception {
33658 tejus.loha 386
        LoiForm loiForm = loiFormRepository.selectById(loiId);
387
        String mobile = String.valueOf(loiForm.getMobile());
33507 tejus.loha 388
        Otp otp = otpRepository.selectAllByMobileWithTime(mobile).get(0);
389
        OTPResponse otpResponse = otpProcessor.validateOtp(otp.getId(), mobile, provideOtp);
390
        if (otpResponse.isResult()) {
33658 tejus.loha 391
            loiForm.setLoiSignOtp(provideOtp);
392
            loiForm.setLoiSignedOn(LocalDateTime.now());
33507 tejus.loha 393
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33845 tejus.loha 394
        } else {
395
            throw new ProfitMandiBusinessException("Wrong OTP", "", "Your given Otp ->" + provideOtp + " is wrong , please check OTP and try again");
33507 tejus.loha 396
        }
397
        return "response";
398
    }
399
 
33845 tejus.loha 400
    @RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)
401
    public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {
402
        LOGGER.info("loi_docId-" + loiDocId);
403
        LoiForm loiForm = loiFormRepository.selectById(loiId);
404
        if (loiDocId > 0) {
405
            loiForm.setLoiDoc(loiDocId);
406
            loiFormService.sendSignedLoiPdfToPartner(loiForm);
33848 tejus.loha 407
            loiFormService.checkLoiDetailsCompletion(loiForm);
33845 tejus.loha 408
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
409
        } else {
410
            loiForm.setLoiSignOtp("");
411
            loiForm.setLoiGeneratedOn(null);
412
            loiForm.setLoiSignedOn(null);
413
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
414
        }
415
        return "response";
416
 
417
    }
418
 
33617 tejus.loha 419
    @RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)
420
    public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {
421
        ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);
422
        return responseEntity;
423
    }
424
 
33658 tejus.loha 425
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
426
    public String updateForm(@RequestBody BrandFeeCollection brandFeeCollection, Model model) throws Exception {
427
        BrandFeeCollection brandFeeCollection1 = brandFeeCollectionRepository.selectById(brandFeeCollection.getId());
33845 tejus.loha 428
        LoiForm loiForm = loiFormRepository.selectById(brandFeeCollection1.getLoiFormId());
33850 tejus.loha 429
        brandFeeCollection1.setFeeCollectingTimeStamp(brandFeeCollection.getFeeCollectingTimeStamp());
430
        brandFeeCollection1.setCollectedAmount(brandFeeCollection.getCollectedAmount());
33885 tejus.loha 431
        brandFeeCollection1.setPaymentAttachment(brandFeeCollection.getPaymentAttachment());
33850 tejus.loha 432
        brandFeeCollection1.setPaymentReferenceNo(brandFeeCollection.getPaymentReferenceNo());
433
        brandFeeCollection1.setPaymentMode(brandFeeCollection.getPaymentMode());
434
        double totalCollectedFee = brandFeeCollectionRepository.selectAllByLoiFormId(loiForm.getId()).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
435
        if (totalCollectedFee <= loiForm.getAgreedBrandFees()) {
33845 tejus.loha 436
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
437
        } else {
33850 tejus.loha 438
            double differanceAmount = totalCollectedFee - loiForm.getAgreedBrandFees();
33845 tejus.loha 439
            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");
440
        }
33658 tejus.loha 441
        return "response";
442
    }
33617 tejus.loha 443
 
33710 tejus.loha 444
    @RequestMapping(value = "/updateAgreedBrandFee", method = RequestMethod.PUT)
34085 tejus.loha 445
    public String updateAgreedBrandFee(HttpServletRequest request, @RequestParam int loiId, @RequestParam double brandFee, @RequestParam StoreType storeType, Model model) throws Exception {
33710 tejus.loha 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);
34085 tejus.loha 451
            loiForm.setBrandType(storeType);
34086 tejus.loha 452
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(loiId));
33710 tejus.loha 453
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
454
            return "response";
455
        } else {
456
            throw new ProfitMandiBusinessException("failed in Authority check ", null, "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to change agreed brand fee");
457
        }
458
    }
459
 
33845 tejus.loha 460
 
461
    @RequestMapping(value = "/approve-reject-Loi", method = RequestMethod.PUT)
462
    public String approveDetails(HttpServletRequest request, @RequestParam int loiId, @RequestParam boolean flag, Model model) throws Exception {
463
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
464
        String email = loginDetails.getEmailId();
33711 tejus.loha 465
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33845 tejus.loha 466
        AuthUser filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy());
467
        AuthUser manager = authRepository.selectById(filledBy.getManagerId());
468
        AuthUser upperManager = null;
469
        List<String> approverMail = new ArrayList<>();
470
        approverMail.add(manager.getEmailId());
471
        upperManager = authRepository.selectById(manager.getManagerId());
472
        approverMail.add(upperManager.getEmailId());
473
 
474
        LOGGER.info("approverMail-" + approverMail);
475
        // Approver - Manager or Upper manager of loi filledBy(BM)
476
        boolean isApprover = approverMail.contains(email);
477
        LOGGER.info("isApprover-" + isApprover);
478
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
479
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
480
        if (flag) {
481
            if (isApprover || isAuthUser) {
482
                loiForm.setLoiApprover(email);
483
                loiForm.setLoiApproved(flag);
484
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
485
            } else {
486
                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");
487
            }
488
        } else {
489
            if (isApprover || isAuthUser) {
490
                loiForm.setLoiApprover(email);
491
                loiForm.setLoiApproved(flag);
492
                loiForm.setStatus(LoiStatus.LOI_REJECT);
493
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
494
            } else {
495
                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");
496
            }
497
        }
33711 tejus.loha 498
        return "response";
499
    }
500
 
33845 tejus.loha 501
 
33507 tejus.loha 502
}