Subversion Repositories SmartDukaan

Rev

Rev 34310 | Rev 34947 | 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);
34310 tejus.loha 157
        if(gstDetails !=null){
158
            List<GstDetails.Pradr> adadr = gstDetails.getAdadr();
159
            adadr.add(gstDetails.getPradr());
160
            Collections.reverse(adadr);
161
            LOGGER.info("list_of_adadr-" + adadr);
162
        }
34026 tejus.loha 163
        return ResponseEntity.ok(gstDetails);
33507 tejus.loha 164
    }
165
 
34026 tejus.loha 166
    // Save Loi data in fofo.loi_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
 
34026 tejus.loha 181
    // show all pending loi 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);
34085 tejus.loha 199
        model.addAttribute("brandType", StoreType.valueList);
33507 tejus.loha 200
        return "pendingForm";
201
    }
202
 
34085 tejus.loha 203
//    @RequestMapping(value = "/pendingLoiFormList", method = RequestMethod.GET)
204
//    public String pendingLoiForm1(HttpServletRequest request, @RequestParam int pageSize, @RequestParam int pageNumber, Model model) throws Exception {
205
//        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
206
//        String email = loginDetails.getEmailId();
207
//        Map<String, Object> pendingFormMap = loiFormService.pendingFormList(email);
208
//        model.addAttribute("pendingFormList", pendingFormMap.get("pendingFormModelList"));
209
//        model.addAttribute("paginationInfo", this.getPaginationInfo(pageNumber,pageSize,(long)pendingFormMap.get("totalCount")));
210
//        return "loiData";
211
//    }
212
//    public String getPaginationInfo(int currentPage, int pageSize, long totalEntries) {
213
//        // Calculate the starting index
214
//        int start = (currentPage - 1) * pageSize + 1; // Start from 1
215
//        // Calculate the ending index
216
//        int end = Math.min(start + pageSize - 1, (int) totalEntries); // Ensure it doesn't exceed totalEntries
217
//
218
//        return "Showing " + start + " to " + end + " of " + totalEntries + " entries";
219
//    }
220
 
33507 tejus.loha 221
    // generate LOI for specific Form data
222
    @RequestMapping(value = "/generateLoi", method = RequestMethod.GET)
33845 tejus.loha 223
    public String generateLoi(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
33507 tejus.loha 224
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33630 tejus.loha 225
        String filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy()).getFullName();
33507 tejus.loha 226
        loiForm.setLoiGeneratedOn(LocalDateTime.now());
227
        BrandFee brandFee = brandFeeRepository.selectById(1);
33658 tejus.loha 228
        BillingAddress address = billingAddressRepository.selectByLoiFormId(loiId);
229
        List<LoiBrandCommitment> loiBrandCommitments = brandCommitmentRepository.selectByLoiId(loiId).stream().filter(x -> x.getAmount() > 0).collect(Collectors.toList());
230
        double totalCommitment = loiBrandCommitments.stream().mapToDouble(x -> x.getAmount()).sum();
33845 tejus.loha 231
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId);
232
        double brandFeeAmount = brandFeeCollections.stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33658 tejus.loha 233
        BrandFeeCollection brandFeeCollection = brandFeeCollections.get(0);
234
        model.addAttribute("totalCommitment", totalCommitment);
235
        model.addAttribute("loiBrandCommitments", loiBrandCommitments);
33507 tejus.loha 236
        model.addAttribute("brandFeeAmount", brandFeeAmount);
237
        model.addAttribute("brandFee", brandFee);
238
        model.addAttribute("brandFeeCollection", brandFeeCollection);
239
        model.addAttribute("address", address);
33658 tejus.loha 240
        model.addAttribute("loiForm", loiForm);
33630 tejus.loha 241
        model.addAttribute("filledBy", filledBy);
33507 tejus.loha 242
        return "auto-Loi";
243
 
244
    }
245
 
246
 
247
    // send Filled form for update purpose
33658 tejus.loha 248
    @RequestMapping(value = "/updateLoiForm", method = RequestMethod.GET)
33507 tejus.loha 249
    public String UpdateLoiForm(HttpServletRequest request,
250
                                @RequestParam int loiId,
251
                                Model model) throws ProfitMandiBusinessException {
252
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33658 tejus.loha 253
        model.addAttribute("updateForm", true);
254
        model.addAttribute("loiForm", loiForm);
255
        return "loiForm/update-loiForm";
33507 tejus.loha 256
    }
257
 
33577 tejus.loha 258
    @RequestMapping(value = "/updateLoiFormData", method = RequestMethod.POST)
33507 tejus.loha 259
    public String UpdateLoiFormDetail(HttpServletRequest request,
260
                                      @RequestParam int loiId,
33658 tejus.loha 261
                                      @RequestBody LoiFormData loiFormData,
33507 tejus.loha 262
                                      Model model) throws Exception {
33658 tejus.loha 263
        loiFormService.updateLoiForm(loiFormData, loiId);
33507 tejus.loha 264
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
265
        return "response";
266
    }
267
 
268
    @RequestMapping(value = "/brandfeeCollection", method = RequestMethod.POST)
269
    public String addBrandFee(HttpServletRequest request, @RequestBody BrandFeeCollection brandFeeCollection, @RequestParam int loiId, Model model) throws Exception {
270
        LoiForm loiForm = loiFormRepository.selectById(loiId);
34086 tejus.loha 271
        double totatcollectedfee = brandFeeCollectionRepository.selectAllConfirmPaymetByLoiId(loiId).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
33507 tejus.loha 272
        double brandFee = loiForm.getAgreedBrandFees();
273
        double differanceAmount = brandFee - totatcollectedfee;
274
        if (totatcollectedfee + brandFeeCollection.getCollectedAmount() <= brandFee) {
275
            brandFeeCollection.setPaymentStatus(FeePaymentStatus.PENDING);
33658 tejus.loha 276
            brandFeeCollection.setLoiFormId(loiId);
277
            brandFeeCollectionRepository.persist(brandFeeCollection);
33885 tejus.loha 278
//            loiFormService.sentMailToPaymentApprover(brandFeeCollection, loiForm);
33507 tejus.loha 279
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
280
            return "response";
281
        } else {
282
            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");
283
        }
284
    }
285
 
286
 
33617 tejus.loha 287
    @RequestMapping(value = "/paymentsDetail", method = RequestMethod.GET)
33507 tejus.loha 288
    public String paymentsDetails(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
289
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
290
        String email = loginDetails.getEmailId();
291
        boolean isNiraj = false;
292
        if (email.equals("neeraj.gupta@smartdukaan.com")) {
293
            isNiraj = true;
294
        }
33658 tejus.loha 295
        List<BrandFeeCollection> brandFeeCollections = brandFeeCollectionRepository.selectAllByLoiFormId(loiId);
33507 tejus.loha 296
        model.addAttribute("brandFeeCollections", brandFeeCollections);
297
        model.addAttribute("confirm", FeePaymentStatus.CONFIRM);
298
        model.addAttribute("isNiraj", isNiraj);
299
        return "payment-collection-details";
300
    }
301
 
302
    // bfcId - Brand Fee Collection Id
33617 tejus.loha 303
    @RequestMapping(value = "/feePaymentApproval", method = RequestMethod.PUT)
33525 tejus.loha 304
    public String feePaymentApproval(HttpServletRequest request, @RequestParam int bfcId, @RequestParam FeePaymentStatus feePaymentStatus, @RequestParam String description,
305
                                     Model model) throws Exception {
33507 tejus.loha 306
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
307
        String authEmail = loginDetails.getEmailId();
308
        BrandFeeCollection brandFeeCollection = brandFeeCollectionRepository.selectById(bfcId);
309
        brandFeeCollection.setApproverEmail(authEmail);
310
        brandFeeCollection.setPaymentStatus(feePaymentStatus);
33525 tejus.loha 311
        brandFeeCollection.setDescription(description);
34195 tejus.loha 312
        brandFeeCollection.setApprovalTimeStamp(LocalDateTime.now());
33848 tejus.loha 313
        if (feePaymentStatus.equals(FeePaymentStatus.CONFIRM)) {
314
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(brandFeeCollection.getLoiFormId()));
315
        }
33845 tejus.loha 316
        loiFormService.sendPaymentStatusMailToLoiFormFilledBy(brandFeeCollection);
317
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33507 tejus.loha 318
        return "response";
319
 
320
    }
321
 
34739 aman.kumar 322
    @RequestMapping(value = "/feePaymentDeletion", method = RequestMethod.PUT)
323
    public String feePaymentDeletion(HttpServletRequest request, @RequestParam int bfcId,
324
                                     Model model) throws Exception {
325
        brandFeeCollectionRepository.deleteById(bfcId);
326
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
327
        return "response";
33658 tejus.loha 328
 
34739 aman.kumar 329
    }
330
 
331
 
33617 tejus.loha 332
    @RequestMapping(value = "/uploadDocumentForm", method = RequestMethod.GET)
33507 tejus.loha 333
    public String uploadDocumentForm(HttpServletRequest request, @RequestParam int loiId, Model model) throws ProfitMandiBusinessException {
334
        List<LoiDocMaster> activeDocMasterList = loiDocMasterList.getAllActiveDoc();
335
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
336
        String authEmail = authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getEmailId();
337
        String approverEmail = "gaurav.sharma@smartdukaan.com";
338
        boolean isApprover = false;
339
        if (authEmail.equals(approverEmail)) {
340
            isApprover = true;
341
        }
33845 tejus.loha 342
        Map<Integer, LoiDoc> masterDocIdLoiDocMap = loiDocRepository.selectByLoiFormId(loiId).stream().collect(Collectors.toMap(x -> x.getMasterDocId(), x -> x));
33507 tejus.loha 343
        model.addAttribute("isApprover", isApprover);
344
        model.addAttribute("loiId", loiId);
345
        model.addAttribute("activeDocMasterList", activeDocMasterList);
346
        model.addAttribute("masterDocIdLoiDocMap", masterDocIdLoiDocMap);
347
        return "loiForm/document-upload";
348
    }
349
 
350
    @RequestMapping(value = "/uploadOnboardingDocument", method = RequestMethod.POST)
351
    public String uploadDocument(HttpServletRequest request, @RequestParam int loiId, @RequestBody List<LoiDocModel> loiDocModels, Model model) throws Exception {
352
        LoiForm loiForm = loiFormRepository.selectById(loiId);
353
        loiFormService.setDocs(loiForm, loiDocModels);
354
        model.addAttribute("response1", mvcResponseSender.createResponseString("Document uploaded successfully"));
355
        return "response";
356
 
357
    }
358
 
359
 
33617 tejus.loha 360
    @RequestMapping(value = "/documentVerify", method = RequestMethod.PUT)
33507 tejus.loha 361
    public String docApproval(HttpServletRequest request, @RequestParam int loiId, @RequestParam int docMasterId, @RequestParam boolean flag, Model model) throws Exception {
362
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
363
        AuthUser auth = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
364
        LoiDoc loiDoc = loiDocRepository.selectByLoiIdAndMasterDocId(loiId, docMasterId);
365
        if (flag) {
366
            loiDoc.setOk(true);
367
            loiDoc.setVerifyBy(auth.getId());
368
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Accepted "));
369
            return "response";
370
        } else {
371
            loiDoc.setOk(false);
372
            loiDoc.setVerifyBy(auth.getId());
33658 tejus.loha 373
            loiFormService.sendDocRejectionMail(loiDoc);
33507 tejus.loha 374
            model.addAttribute("response1", mvcResponseSender.createResponseString(loiDoc.getDocType() + " is Rejected"));
375
            return "response";
376
        }
377
    }
378
 
379
    @RequestMapping(value = "/loiAcceptanceOtp", method = RequestMethod.POST)
380
    public String sentLoiAcceptanceOtp(@RequestParam int loiId, Model model) throws Exception {
33658 tejus.loha 381
        LoiForm loiForm = loiFormRepository.selectById(loiId);
382
        String mobile = String.valueOf(loiForm.getMobile());
33525 tejus.loha 383
        OTPResponse otpResponse = otpProcessor.generateOtp(mobile, OtpType.LOI_ACCEPTANCE);
33658 tejus.loha 384
        String message = "OTP send on your register mobile and EmailId - ";
33568 tejus.loha 385
        LOGGER.info("OTPResponse Message - " + otpResponse.getMessage());
386
        if (otpResponse.isResult()) {
33658 tejus.loha 387
            model.addAttribute("response1", mvcResponseSender.createResponseString(message + mobile + " and " + loiForm.getEmail()));
33568 tejus.loha 388
            return "response";
33525 tejus.loha 389
        } else {
390
            model.addAttribute("response1", mvcResponseSender.createResponseString("Something went wrong.."));
33568 tejus.loha 391
            return "response";
33525 tejus.loha 392
        }
33507 tejus.loha 393
    }
394
 
395
    @RequestMapping(value = "/validateLoiOtp", method = RequestMethod.PUT)
396
    public String validateLoiAcceptanceOtp(@RequestParam int loiId, @RequestParam String provideOtp, Model model) throws Exception {
33658 tejus.loha 397
        LoiForm loiForm = loiFormRepository.selectById(loiId);
398
        String mobile = String.valueOf(loiForm.getMobile());
33507 tejus.loha 399
        Otp otp = otpRepository.selectAllByMobileWithTime(mobile).get(0);
400
        OTPResponse otpResponse = otpProcessor.validateOtp(otp.getId(), mobile, provideOtp);
401
        if (otpResponse.isResult()) {
33658 tejus.loha 402
            loiForm.setLoiSignOtp(provideOtp);
403
            loiForm.setLoiSignedOn(LocalDateTime.now());
33507 tejus.loha 404
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
33845 tejus.loha 405
        } else {
406
            throw new ProfitMandiBusinessException("Wrong OTP", "", "Your given Otp ->" + provideOtp + " is wrong , please check OTP and try again");
33507 tejus.loha 407
        }
408
        return "response";
409
    }
410
 
33845 tejus.loha 411
    @RequestMapping(value = "/saveLoiDoc", method = RequestMethod.POST)
412
    public String saveLoiDoc(@RequestParam int loiId, @RequestParam int loiDocId, Model model) throws Exception {
413
        LOGGER.info("loi_docId-" + loiDocId);
414
        LoiForm loiForm = loiFormRepository.selectById(loiId);
415
        if (loiDocId > 0) {
416
            loiForm.setLoiDoc(loiDocId);
417
            loiFormService.sendSignedLoiPdfToPartner(loiForm);
33848 tejus.loha 418
            loiFormService.checkLoiDetailsCompletion(loiForm);
33845 tejus.loha 419
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
420
        } else {
421
            loiForm.setLoiSignOtp("");
422
            loiForm.setLoiGeneratedOn(null);
423
            loiForm.setLoiSignedOn(null);
424
            model.addAttribute("response1", mvcResponseSender.createResponseString(false));
425
        }
426
        return "response";
427
 
428
    }
429
 
33617 tejus.loha 430
    @RequestMapping(value = "/downloadLoiFromReport", method = RequestMethod.GET)
431
    public ResponseEntity<?> downloadAllLoiForm(@RequestParam LocalDate from, @RequestParam LocalDate to) throws Exception {
432
        ResponseEntity<?> responseEntity = loiFormService.createLoiFormReport(from, to);
433
        return responseEntity;
434
    }
435
 
33658 tejus.loha 436
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
437
    public String updateForm(@RequestBody BrandFeeCollection brandFeeCollection, Model model) throws Exception {
438
        BrandFeeCollection brandFeeCollection1 = brandFeeCollectionRepository.selectById(brandFeeCollection.getId());
33845 tejus.loha 439
        LoiForm loiForm = loiFormRepository.selectById(brandFeeCollection1.getLoiFormId());
33850 tejus.loha 440
        brandFeeCollection1.setFeeCollectingTimeStamp(brandFeeCollection.getFeeCollectingTimeStamp());
441
        brandFeeCollection1.setCollectedAmount(brandFeeCollection.getCollectedAmount());
33885 tejus.loha 442
        brandFeeCollection1.setPaymentAttachment(brandFeeCollection.getPaymentAttachment());
33850 tejus.loha 443
        brandFeeCollection1.setPaymentReferenceNo(brandFeeCollection.getPaymentReferenceNo());
444
        brandFeeCollection1.setPaymentMode(brandFeeCollection.getPaymentMode());
445
        double totalCollectedFee = brandFeeCollectionRepository.selectAllByLoiFormId(loiForm.getId()).stream().mapToDouble(x -> x.getCollectedAmount()).sum();
446
        if (totalCollectedFee <= loiForm.getAgreedBrandFees()) {
33845 tejus.loha 447
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
448
        } else {
33850 tejus.loha 449
            double differanceAmount = totalCollectedFee - loiForm.getAgreedBrandFees();
33845 tejus.loha 450
            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");
451
        }
33658 tejus.loha 452
        return "response";
453
    }
33617 tejus.loha 454
 
33710 tejus.loha 455
    @RequestMapping(value = "/updateAgreedBrandFee", method = RequestMethod.PUT)
34085 tejus.loha 456
    public String updateAgreedBrandFee(HttpServletRequest request, @RequestParam int loiId, @RequestParam double brandFee, @RequestParam StoreType storeType, Model model) throws Exception {
33710 tejus.loha 457
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
458
        boolean isAgreedBrandFeeChanger = agreedBrandFeeChangerEmail.stream().anyMatch(x -> x.equals(loginDetails.getEmailId()));
459
        if (isAgreedBrandFeeChanger) {
460
            LoiForm loiForm = loiFormRepository.selectById(loiId);
461
            loiForm.setAgreedBrandFees(brandFee);
34085 tejus.loha 462
            loiForm.setBrandType(storeType);
34086 tejus.loha 463
            loiFormService.checkLoiDetailsCompletion(loiFormRepository.selectById(loiId));
33710 tejus.loha 464
            model.addAttribute("response1", mvcResponseSender.createResponseString(true));
465
            return "response";
466
        } else {
467
            throw new ProfitMandiBusinessException("failed in Authority check ", null, "Sorry " + authRepository.selectByEmailOrMobile(loginDetails.getEmailId()).getName() + " you have no authority to change agreed brand fee");
468
        }
469
    }
470
 
33845 tejus.loha 471
 
472
    @RequestMapping(value = "/approve-reject-Loi", method = RequestMethod.PUT)
473
    public String approveDetails(HttpServletRequest request, @RequestParam int loiId, @RequestParam boolean flag, Model model) throws Exception {
474
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
475
        String email = loginDetails.getEmailId();
33711 tejus.loha 476
        LoiForm loiForm = loiFormRepository.selectById(loiId);
33845 tejus.loha 477
        AuthUser filledBy = authRepository.selectByEmailOrMobile(loiForm.getFilledBy());
478
        AuthUser manager = authRepository.selectById(filledBy.getManagerId());
479
        AuthUser upperManager = null;
480
        List<String> approverMail = new ArrayList<>();
481
        approverMail.add(manager.getEmailId());
482
        upperManager = authRepository.selectById(manager.getManagerId());
483
        approverMail.add(upperManager.getEmailId());
484
 
485
        LOGGER.info("approverMail-" + approverMail);
486
        // Approver - Manager or Upper manager of loi filledBy(BM)
487
        boolean isApprover = approverMail.contains(email);
488
        LOGGER.info("isApprover-" + isApprover);
489
        List<String> authEmail = Arrays.asList("sm@smartdukaan.com", "tarun.verma@smartdukaan.com", "ashutosh.verma@smartdukaan.com");
490
        boolean isAuthUser = authEmail.stream().anyMatch(x -> x.equals(email));
491
        if (flag) {
492
            if (isApprover || isAuthUser) {
493
                loiForm.setLoiApprover(email);
494
                loiForm.setLoiApproved(flag);
495
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
496
            } else {
497
                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");
498
            }
499
        } else {
500
            if (isApprover || isAuthUser) {
501
                loiForm.setLoiApprover(email);
502
                loiForm.setLoiApproved(flag);
503
                loiForm.setStatus(LoiStatus.LOI_REJECT);
504
                model.addAttribute("response1", mvcResponseSender.createResponseString(true));
505
            } else {
506
                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");
507
            }
508
        }
33711 tejus.loha 509
        return "response";
510
    }
511
 
33845 tejus.loha 512
 
33507 tejus.loha 513
}