Subversion Repositories SmartDukaan

Rev

Rev 34107 | Rev 34195 | 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();
33507 tejus.loha 142
        List<BusinessType> businessTypes = Arrays.asList(BusinessType.Proprietorship, BusinessType.Partnership, BusinessType.Company);
143
        model.addAttribute("businessTypes", businessTypes);
33658 tejus.loha 144
        model.addAttribute("brandCommits", brandCommits);
33507 tejus.loha 145
        model.addAttribute("storeTypeFeePairs", storeTypeFeePairs);
146
        model.addAttribute("authUsersList", authUsersList);
34107 tejus.loha 147
        model.addAttribute("stateHeadList", stateHeadList);
148
        model.addAttribute("bdmList", bdmList);
33845 tejus.loha 149
        return "loi-form";
33507 tejus.loha 150
    }
151
 
152
    // use to validate GSTIN
153
    @RequestMapping(value = "/gstValidate", method = RequestMethod.GET)
154
    public ResponseEntity<?> gstValidate(HttpServletRequest request, @RequestParam String gstNo) throws Exception {
155
        LOGGER.info("gstNo -" + gstNo);
156
        GstDetails gstDetails = gstProService.getGstDetails(gstNo);
157
        LOGGER.info("gstDetails -" + gstDetails);
158
        List<GstDetails.Pradr> adadr = gstDetails.getAdadr();
159
        adadr.add(gstDetails.getPradr());
160
        Collections.reverse(adadr);
161
        LOGGER.info("list_of_adadr-" + adadr);
34026 tejus.loha 162
        return ResponseEntity.ok(gstDetails);
33507 tejus.loha 163
    }
164
 
34026 tejus.loha 165
    // Save Loi data in fofo.loi_form table
33577 tejus.loha 166
    @RequestMapping(value = "/submitLoiForm", method = RequestMethod.POST)
33568 tejus.loha 167
    public String submitLoiForm(HttpServletRequest request, @RequestBody LoiFormData loiFormData, Model model) throws Exception {
33507 tejus.loha 168
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
33577 tejus.loha 169
        LOGGER.info("filledBy - " + loginDetails.getEmailId());
170
        boolean isDataCreated = loiFormService.createLoiForm(loiFormData, loginDetails.getEmailId());
33507 tejus.loha 171
        if (isDataCreated) {
172
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
173
            return "response";
174
        } else {
175
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
176
            return "response";
177
        }
178
    }
179
 
34026 tejus.loha 180
    // show all pending loi form to specific auth user who have filled the form(type based)
33710 tejus.loha 181
    @RequestMapping(value = "/pendingLoiForm", method = RequestMethod.GET)
33507 tejus.loha 182
    public String pendingLoiForm(HttpServletRequest request, Model model) throws Exception {
183
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
184
        String email = loginDetails.getEmailId();
185
        boolean isDocApprover = false;
186
        String approverEmail = "gaurav.sharma@smartdukaan.com";
187
        if (email.equals(approverEmail)) {
188
            isDocApprover = true;
189
        }
33710 tejus.loha 190
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(email));
33711 tejus.loha 191
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
33710 tejus.loha 192
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
33507 tejus.loha 193
        List<LoiFormModel> pendingFormList = loiFormService.pendingFormList(email);
194
        model.addAttribute("isDocApprover", isDocApprover);
33617 tejus.loha 195
        model.addAttribute("isAuthUser", isAuthUser);
33710 tejus.loha 196
        model.addAttribute("isAgreedBrandFeeChanger", isAgreedBrandFeeChanger);
33507 tejus.loha 197
        model.addAttribute("pendingFormList", pendingFormList);
34085 tejus.loha 198
        model.addAttribute("brandType", StoreType.valueList);
33507 tejus.loha 199
        return "pendingForm";
200
    }
201
 
34085 tejus.loha 202
//    @RequestMapping(value = "/pendingLoiFormList", method = RequestMethod.GET)
203
//    public String pendingLoiForm1(HttpServletRequest request, @RequestParam int pageSize, @RequestParam int pageNumber, Model model) throws Exception {
204
//        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
205
//        String email = loginDetails.getEmailId();
206
//        Map<String, Object> pendingFormMap = loiFormService.pendingFormList(email);
207
//        model.addAttribute("pendingFormList", pendingFormMap.get("pendingFormModelList"));
208
//        model.addAttribute("paginationInfo", this.getPaginationInfo(pageNumber,pageSize,(long)pendingFormMap.get("totalCount")));
209
//        return "loiData";
210
//    }
211
//    public String getPaginationInfo(int currentPage, int pageSize, long totalEntries) {
212
//        // Calculate the starting index
213
//        int start = (currentPage - 1) * pageSize + 1; // Start from 1
214
//        // Calculate the ending index
215
//        int end = Math.min(start + pageSize - 1, (int) totalEntries); // Ensure it doesn't exceed totalEntries
216
//
217
//        return "Showing " + start + " to " + end + " of " + totalEntries + " entries";
218
//    }
219
 
33507 tejus.loha 220
    // generate LOI for specific Form data
221
    @RequestMapping(value = "/generateLoi", method = RequestMethod.GET)
33845 tejus.loha 222
    public String generateLoi(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
33507 tejus.loha 223
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33630 tejus.loha 224
        String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();
33507 tejus.loha 225
        loiForm.setLoiGeneratedOn(LocalDateTime.now());
226
        BrandFee brandFee = brandFeeRepository.selectById(1);
33658 tejus.loha 227
        BillingAddress address = billingAddressRepository.selectByLoiFormId(loiId);
228
        List<LoiBrandCommitment> loiBrandCommitments = brandCommitmentRepository.selectByLoiId(loiId).stream().filter(x -> x.getAmount() > 0).collect(Collectors.toList());
229
        double totalCommitment = loiBrandCommitments.stream().mapToDouble(x -> x.getAmount()).sum();
33845 tejus.loha 230
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId);
231
        double brandFeeAmount = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33658 tejus.loha 232
        BrandFeeCollection brandFeeCollection = brandFeeCollections.get(0);
233
        model.addAttribute("totalCommitment", totalCommitment);
234
        model.addAttribute("loiBrandCommitments", loiBrandCommitments);
33507 tejus.loha 235
        model.addAttribute("brandFeeAmount", brandFeeAmount);
236
        model.addAttribute("brandFee", brandFee);
237
        model.addAttribute("brandFeeCollection", brandFeeCollection);
238
        model.addAttribute("address", address);
33658 tejus.loha 239
        model.addAttribute("loiForm", loiForm);
33630 tejus.loha 240
        model.addAttribute("filledBy", filledBy);
33507 tejus.loha 241
        return "auto-Loi";
242
 
243
    }
244
 
245
 
246
    // send Filled form for update purpose
33658 tejus.loha 247
    @RequestMapping(value = "/updateLoiForm", method = RequestMethod.GET)
33507 tejus.loha 248
    public String UpdateLoiForm(HttpServletRequest request,
249
                                @RequestParam int loiId,
250
                                Model model) throws ProfitMandiBusinessException {
251
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33658 tejus.loha 252
        model.addAttribute("updateForm", true);
253
        model.addAttribute("loiForm", loiForm);
254
        return "loiForm/update-loiForm";
33507 tejus.loha 255
    }
256
 
33577 tejus.loha 257
    @RequestMapping(value = "/updateLoiFormData", method = RequestMethod.POST)
33507 tejus.loha 258
    public String UpdateLoiFormDetail(HttpServletRequest request,
259
                                      @RequestParam int loiId,
33658 tejus.loha 260
                                      @RequestBody LoiFormData loiFormData,
33507 tejus.loha 261
                                      Model model) throws Exception {
33658 tejus.loha 262
        loiFormService.updateLoiForm(loiFormData, loiId);
33507 tejus.loha 263
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
264
        return "response";
265
    }
266
 
267
    @RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)
268
    public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {
269
        LoiForm loiForm = loiFormRepository.selectById(loiId);
34086 tejus.loha 270
        double totatcollectedfee = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33507 tejus.loha 271
        double brandFee = loiForm.getAgreedBrandFees();
272
        double differanceAmount = brandFee - totatcollectedfee;
273
        if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {
274
            brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);
33658 tejus.loha 275
            brandFeeCollection.setLoiFormId(loiId);
276
            brandFeeCollectionRepository.persist(brandFeeCollection);
33885 tejus.loha 277
//            loiFormService.sentMailToPaymentApprover(brandFeeCollection, loiForm);
33507 tejus.loha 278
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
279
            return "response";
280
        } else {
281
            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");
282
        }
283
    }
284
 
285
 
33617 tejus.loha 286
    @RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)
33507 tejus.loha 287
    public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
288
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
289
        String email = loginDetails.getEmailId();
290
        boolean isNiraj = false;
291
        if (email.equals("neeraj.gupta@smartdukaan.com")) {
292
            isNiraj = true;
293
        }
33658 tejus.loha 294
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33507 tejus.loha 295
        model.addAttribute("brandFeeCollections", brandFeeCollections);
296
        model.addAttribute("confirm", FeePaymentStatus.CONFIRM);
297
        model.addAttribute("isNiraj", isNiraj);
298
        return "payment-collection-details";
299
    }
300
 
301
    // bfcId - Brand Fee Collection Id
33617 tejus.loha 302
    @RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)
33525 tejus.loha 303
    public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,
304
                                     Model model) throws Exception {
33507 tejus.loha 305
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
306
        String authEmail = loginDetails.getEmailId();
307
        BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);
308
        brandFeeCollection.setApproverEmail(authEmail);
309
        brandFeeCollection.setPaymentStatus(feePaymentStatus);
33525 tejus.loha 310
        brandFeeCollection.setDescription(description);
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");
396
 
33507 tejus.loha 397
        }
398
        return "response";
399
    }
400
 
33845 tejus.loha 401
    @RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)
402
    public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {
403
        LOGGER.info("loi_docId-" + loiDocId);
404
        LoiForm loiForm = loiFormRepository.selectById(loiId);
405
        if (loiDocId > 0) {
406
            loiForm.setLoiDoc(loiDocId);
407
            loiFormService.sendSignedLoiPdfToPartner(loiForm);
33848 tejus.loha 408
            loiFormService.checkLoiDetailsCompletion(loiForm);
33845 tejus.loha 409
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
410
        } else {
411
            loiForm.setLoiSignOtp("");
412
            loiForm.setLoiGeneratedOn(null);
413
            loiForm.setLoiSignedOn(null);
414
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
415
        }
416
        return "response";
417
 
418
    }
419
 
33617 tejus.loha 420
    @RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)
421
    public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {
422
        ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);
423
        return responseEntity;
424
    }
425
 
33658 tejus.loha 426
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
427
    public String updateForm(@RequestBody BrandFeeCollection brandFeeCollection, Model model) throws Exception {
428
        BrandFeeCollection brandFeeCollection1 = brandFeeCollectionRepository.selectById(brandFeeCollection.getId());
33845 tejus.loha 429
        LoiForm loiForm = loiFormRepository.selectById(brandFeeCollection1.getLoiFormId());
33850 tejus.loha 430
        brandFeeCollection1.setFeeCollectingTimeStamp(brandFeeCollection.getFeeCollectingTimeStamp());
431
        brandFeeCollection1.setCollectedAmount(brandFeeCollection.getCollectedAmount());
33885 tejus.loha 432
        brandFeeCollection1.setPaymentAttachment(brandFeeCollection.getPaymentAttachment());
33850 tejus.loha 433
        brandFeeCollection1.setPaymentReferenceNo(brandFeeCollection.getPaymentReferenceNo());
434
        brandFeeCollection1.setPaymentMode(brandFeeCollection.getPaymentMode());
435
        double totalCollectedFee = brandFeeCollectionRepository.selectAllByLoiFormId(loiForm.getId()).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
436
        if (totalCollectedFee <= loiForm.getAgreedBrandFees()) {
33845 tejus.loha 437
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
438
        } else {
33850 tejus.loha 439
            double differanceAmount = totalCollectedFee - loiForm.getAgreedBrandFees();
33845 tejus.loha 440
            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");
441
        }
33658 tejus.loha 442
        return "response";
443
    }
33617 tejus.loha 444
 
33710 tejus.loha 445
    @RequestMapping(value = "/updateAgreedBrandFee", method = RequestMethod.PUT)
34085 tejus.loha 446
    public String updateAgreedBrandFee(HttpServletRequest request, @RequestParam int loiId, @RequestParam double brandFee, @RequestParam StoreType storeType, Model model) throws Exception {
33710 tejus.loha 447
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
448
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(loginDetails.getEmailId()));
449
        if (isAgreedBrandFeeChanger) {
450
            LoiForm loiForm = loiFormRepository.selectById(loiId);
451
            loiForm.setAgreedBrandFees(brandFee);
34085 tejus.loha 452
            loiForm.setBrandType(storeType);
34086 tejus.loha 453
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(loiId));
33710 tejus.loha 454
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
455
            return "response";
456
        } else {
457
            throw new ProfitMandiBusinessException("failed in Authority check ", null, "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to change agreed brand fee");
458
        }
459
    }
460
 
33845 tejus.loha 461
 
462
    @RequestMapping(value = "/approve-reject-Loi", method = RequestMethod.PUT)
463
    public String approveDetails(HttpServletRequest request, @RequestParam int loiId, @RequestParam boolean flag, Model model) throws Exception {
464
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
465
        String email = loginDetails.getEmailId();
33711 tejus.loha 466
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33845 tejus.loha 467
        AuthUser filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy());
468
        AuthUser manager = authRepository.selectById(filledBy.getManagerId());
469
        AuthUser upperManager = null;
470
        List<String> approverMail = new ArrayList<>();
471
        approverMail.add(manager.getEmailId());
472
        upperManager = authRepository.selectById(manager.getManagerId());
473
        approverMail.add(upperManager.getEmailId());
474
 
475
        LOGGER.info("approverMail-" + approverMail);
476
        // Approver - Manager or Upper manager of loi filledBy(BM)
477
        boolean isApprover = approverMail.contains(email);
478
        LOGGER.info("isApprover-" + isApprover);
479
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
480
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
481
        if (flag) {
482
            if (isApprover || isAuthUser) {
483
                loiForm.setLoiApprover(email);
484
                loiForm.setLoiApproved(flag);
485
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
486
            } else {
487
                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");
488
            }
489
        } else {
490
            if (isApprover || isAuthUser) {
491
                loiForm.setLoiApprover(email);
492
                loiForm.setLoiApproved(flag);
493
                loiForm.setStatus(LoiStatus.LOI_REJECT);
494
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
495
            } else {
496
                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");
497
            }
498
        }
33711 tejus.loha 499
        return "response";
500
    }
501
 
33845 tejus.loha 502
 
33507 tejus.loha 503
}