Subversion Repositories SmartDukaan

Rev

Rev 27344 | Rev 28538 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.swing.SortOrder;
import javax.transaction.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.UnsettledPaymentModel;
import com.spice.profitmandi.common.util.ExcelUtils;
import com.spice.profitmandi.common.util.StringUtils;
import com.spice.profitmandi.common.util.WalletHistoryModel;
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
import com.spice.profitmandi.dao.entity.transaction.ManualPaymentType;
import com.spice.profitmandi.dao.entity.transaction.UnsettledPayment;
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
import com.spice.profitmandi.dao.enumuration.transaction.TransactionType;
import com.spice.profitmandi.dao.model.ContentPojo;
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
import com.spice.profitmandi.dao.repository.catalog.UnsettledPaymentsRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
import com.spice.profitmandi.service.authentication.RoleManager;
import com.spice.profitmandi.service.user.RetailerService;
import com.spice.profitmandi.service.wallet.WalletService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import com.spice.profitmandi.web.util.MVCResponseSender;

import in.shop2020.model.v1.order.WalletReferenceType;

@Controller
@Transactional(rollbackOn = Throwable.class)
public class WalletController {

        @Autowired
        private CookiesProcessor cookiesProcessor;

        @Autowired
        private WalletService walletService;

        @Autowired
        private UserWalletRepository userWalletRepository;

        @Autowired
        private UserWalletHistoryRepository userWalletHistoryRepository;

        @Autowired
        private MVCResponseSender mvcResponseSender;

        @Autowired
        private UserAccountRepository userAccountRepository;

        @Autowired
        JavaMailSender mailSender;

        @Autowired
        private UnsettledPaymentsRepository unsettledPaymentsRepository;

        @Autowired
        private RetailerService retailerService;

        @Autowired
        private RoleManager roleManager;

        @Autowired
        private FofoStoreRepository fofoStoreRepository;

        @Autowired
        private ManualPaymentRequestRepository manualPaymentRequestRepository;

        @Autowired
        AddWalletRequestRepository addWalletRequestRepository;

        @Autowired
        private RetailerRepository retailerRepository;

        @Autowired
        private UserRepository userRepository;
        private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
        
        
        @PostMapping(value = "/wallet/upload")
        public String uploadWalletBulk(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
                        throws Exception {
                List<WalletHistoryModel> walletHistoryModelList = ExcelUtils.parseWalletBulkCredit(file.getInputStream());
                for(WalletHistoryModel walletHistoryModel : walletHistoryModelList) {
                        if(walletHistoryModel.getReference()==0) {
                                ManualPaymentType paymentType =
                                                manualPaymentRequestRepository.selectByReferenceType(walletHistoryModel.getWalletReferenceType());
                                if (paymentType == null) {
                                        paymentType = new ManualPaymentType();
                                        paymentType.setReferenceType(walletHistoryModel.getWalletReferenceType());
                                }
                                paymentType.setCounter(paymentType.getCounter() + 1);
                                manualPaymentRequestRepository.persist(paymentType);
                                int reference = paymentType.getCounter();
                                walletService.addAmountToWallet(walletHistoryModel.getFofoId(), reference, walletHistoryModel.getWalletReferenceType(), 
                                                walletHistoryModel.getDescription(), (float)walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
                        }  else {
                                walletService.addAmountToWallet(walletHistoryModel.getFofoId(), walletHistoryModel.getReference(), walletHistoryModel.getWalletReferenceType(), 
                                                walletHistoryModel.getDescription(), (float)walletHistoryModel.getAmount(), walletHistoryModel.getBusinessDate());
                        }
                }

                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }

        @RequestMapping(value = "/walletDetails", method = RequestMethod.GET)
        public String dashboard(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString,
                        @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                /*
                 * boolean underMaintainance = true; if(underMaintainance) { throw new
                 * ProfitMandiBusinessException("Wallet", "Wallet",
                 * "Wallet is under Maintenance"); }
                 */
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                boolean isAdmin = roleManager.isAdmin(fofoDetails.getRoleIds());
                UserWallet userWallet = walletService.getUserWallet(fofoDetails.getFofoId());

                LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
                LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);

                List<UserWalletHistory> userWalletHistories = new ArrayList<>();
                try {
                        userWalletHistories = walletService.getPaginatedUserWalletHistoryByRetailerId(fofoDetails.getFofoId(),
                                        startDateTime, endDateTime, offset, limit);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("UserWallet History not found : ", profitMandiBusinessException);
                }

                long countItems = 0;
                try {
                        countItems = walletService.getSizeByRetailerId(fofoDetails.getFofoId(), startDateTime, endDateTime);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("UserWallet not found : ", profitMandiBusinessException);
                }
                model.addAttribute("userWallet", userWallet);
                model.addAttribute("walletHistories", userWalletHistories);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", countItems);
                model.addAttribute("isAdmin", isAdmin);
                model.addAttribute(ProfitMandiConstants.START_TIME, startTimeString);
                model.addAttribute(ProfitMandiConstants.END_TIME, endTimeString);
                if (userWalletHistories.size() < limit) {
                        model.addAttribute("end", offset + userWalletHistories.size());
                } else {
                        model.addAttribute("end", offset + limit);
                }
                return "wallet-details";
        }

        @RequestMapping(value = "/getPaginatedWalletHistory")
        public String getSaleHistoryPaginated(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString,
                        @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

                LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
                LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);

                List<UserWalletHistory> userWalletHistories = new ArrayList<>();
                try {
                        userWalletHistories = walletService.getPaginatedUserWalletHistoryByRetailerId(loginDetails.getFofoId(),
                                        startDateTime, endDateTime, offset, limit);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("UserWallet History not found : ", profitMandiBusinessException);
                }

                model.addAttribute("walletHistories", userWalletHistories);
                return "wallet-history-paginated";
        }

        @RequestMapping(value = "/getAddWalletRequest", method = RequestMethod.GET)
        public String getAddwalletRequest(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
                List<AddWalletRequest> walletRequest = null;
                long size = 0;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.pending);
                LOGGER.info("walletRequest" + walletRequest);
                size = addWalletRequestRepository.selectCountByStatus(AddWalletRequestStatus.pending);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("rStatus", "pending");
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedWalletRequest");

                        if (walletRequest.size() < limit) {
                                model.addAttribute("end", offset + walletRequest.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }

                } else {

                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("size", size);

                }
                return "add-wallet-request";
        }

        @RequestMapping(value = "/getPaginatedWalletRequest", method = RequestMethod.GET)
        public String getPaginatedWalletRequest(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
                LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
                List<AddWalletRequest> walletRequest = null;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.pending);
                LOGGER.info("walletRequest" + walletRequest);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("rStatus", "pending");
                        model.addAttribute("url", "/getPaginatedWalletRequest");

                } else {
                        model.addAttribute("walletRequest", walletRequest);

                }

                return "add-wallet-request-paginated";
        }

        @RequestMapping(value = "/getAddWalletApproved", method = RequestMethod.GET)
        public String getAddwalletRequestApproved(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                List<AddWalletRequest> walletRequest = null;

                long size = 0;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.approved);
                size = addWalletRequestRepository.selectCountByStatus(AddWalletRequestStatus.approved);
                LOGGER.info("walletRequest" + walletRequest);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedWalletApproved");

                        if (walletRequest.size() < limit) {
                                model.addAttribute("end", offset + walletRequest.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }
                } else {

                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("size", size);

                }
                return "add-wallet-request";
        }

        @RequestMapping(value = "/getPaginatedWalletApproved", method = RequestMethod.GET)
        public String getPaginatedWalletApproved(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
                List<AddWalletRequest> walletRequest = null;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.approved);
                LOGGER.info("walletRequest" + walletRequest);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("url", "/getPaginatedWalletApproved");
                } else {
                        model.addAttribute("walletRequest", walletRequest);

                }
                return "add-wallet-request-paginated";
        }

        @RequestMapping(value = "/getAddWalletRequestRejected", method = RequestMethod.GET)
        public String getAddwalletRequestRejected(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                List<AddWalletRequest> walletRequest = null;

                long size = 0;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.rejected);
                size = addWalletRequestRepository.selectCountByStatus(AddWalletRequestStatus.rejected);
                LOGGER.info("walletRequest" + walletRequest);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedWalletRequestRejected");

                        if (walletRequest.size() < limit) {
                                model.addAttribute("end", offset + walletRequest.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }
                } else {
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("size", size);
                }

                return "add-wallet-request";
        }

        @RequestMapping(value = "/getPaginatedWalletRequestRejected", method = RequestMethod.GET)
        public String getPaginatedWalletRequestRejected(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
                List<AddWalletRequest> walletRequest = null;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.rejected);
                LOGGER.info("walletRequest" + walletRequest);
                if (!walletRequest.isEmpty()) {
                        List<Integer> fofoIds = this.getFofoIdsFromWalletRequest(walletRequest);
                        Map<Integer, CustomRetailer> fofoIdsAndRetailerName = retailerService.getFofoRetailers(fofoIds);

                        model.addAttribute("fofoIdsAndRetailerName", fofoIdsAndRetailerName);
                        model.addAttribute("walletRequest", walletRequest);
                        model.addAttribute("url", "/getPaginatedWalletRequestRejected");
                } else {
                        model.addAttribute("walletRequest", walletRequest);

                }

                return "add-wallet-request-paginated";
        }

        @RequestMapping(value = "/addAmountToWallet", method = RequestMethod.PUT)
        public String addAmountToWallet(HttpServletRequest request, @RequestParam(name = "id", defaultValue = "0") int id,
                        @RequestParam(name = "walletRequestid", defaultValue = "0") int walletRequestid, Model model)
                        throws Exception {

                AddWalletRequest addWalletRequest = addWalletRequestRepository.selectById(walletRequestid);
                if (addWalletRequest.getStatus().equals(AddWalletRequestStatus.pending)) {
                        walletService.addAmountToWallet(addWalletRequest.getRetailerId(), walletRequestid,
                                        WalletReferenceType.ADVANCE_AMOUNT, "ntfs/rgfs", addWalletRequest.getAmount(), addWalletRequest.getCreateTimestamp());
                        addWalletRequest.setStatus(AddWalletRequestStatus.approved);
                        addWalletRequest.setUpdateTimestamp(LocalDateTime.now());
                        addWalletRequestRepository.persist(addWalletRequest);
                        unsettledPaymentsRepository.deleteById(id);
                        model.addAttribute("response", mvcResponseSender.createResponseString(true));
                        CustomRetailer customRetailer = retailerService.getFofoRetailer(addWalletRequest.getRetailerId());
                        String subject = "Request Approved for " + customRetailer.getBusinessName() + " of Rs."
                                        + addWalletRequest.getAmount();
                        String messageText = MessageFormat.format(
                                        "User Id - {0}\n Name -{1}\n Email -{2}\n mobile -{3}\n Reference - {4}\n Amount - Rs.{5}",
                                        new Integer(addWalletRequest.getRetailerId()), customRetailer.getBusinessName(),
                                        customRetailer.getEmail(), customRetailer.getMobileNumber(),
                                        addWalletRequest.getTransaction_reference(), new Float(addWalletRequest.getAmount()));

                        this.sendMailWithAttachments(subject, messageText);
                        return "response";
                } else {
                        model.addAttribute("response", mvcResponseSender.createResponseString(false));
                        return "response";
                }
        }

        @RequestMapping(value = "/addAmountToWalletRequestRejected", method = RequestMethod.PUT)
        public String addAmountToWalletRequestRejected(HttpServletRequest request,
                        @RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {

                AddWalletRequest addWalletRequest = addWalletRequestRepository.selectById(id);
                if (addWalletRequest.getStatus().equals(AddWalletRequestStatus.pending)) {
                        addWalletRequest.setStatus(AddWalletRequestStatus.rejected);
                        addWalletRequest.setUpdateTimestamp(LocalDateTime.now());
                        addWalletRequestRepository.persist(addWalletRequest);
                        model.addAttribute("response", mvcResponseSender.createResponseString(true));
                        CustomRetailer customRetailer = retailerService.getFofoRetailer(addWalletRequest.getRetailerId());
                        String subject = "Request Rejected for " + customRetailer.getBusinessName() + " of Rs."
                                        + addWalletRequest.getAmount();
                        String messageText = MessageFormat.format(
                                        "User Id - {0}\n Name -{1}\n Email -{2}\n mobile -{3}\n Reference - {4}\n Amount - Rs.{5}",
                                        new Integer(addWalletRequest.getRetailerId()), customRetailer.getBusinessName(),
                                        customRetailer.getEmail(), customRetailer.getMobileNumber(),
                                        addWalletRequest.getTransaction_reference(), new Float(addWalletRequest.getAmount()));

                        this.sendMailWithAttachments(subject, messageText);
                        return "response";
                } else {
                        model.addAttribute("response", mvcResponseSender.createResponseString(false));
                        return "response";
                }
        }

        private void sendMailWithAttachments(String subject, String messageText) throws Exception {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                String[] email = { "neerajgupta2021@gmail.com", "adeel.yazdani@smartdukaan.com",
                                "kamini.sharma@smartdukaan.com", "care@smartdukaan.com", "mohinder.mutreja@smartdukaan.com" };
                helper.setSubject(subject);
                helper.setText(messageText);
                helper.setTo(email);
                InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "Smartdukaan Alerts");
                helper.setFrom(senderAddress);
                mailSender.send(message);

        }

        private List<Integer> getFofoIdsFromWalletRequest(List<AddWalletRequest> walletRequest) {
                List<Integer> fofoIds = new ArrayList<>();
                for (AddWalletRequest walletdetail : walletRequest) {
                        fofoIds.add(walletdetail.getRetailerId());
                }
                return fofoIds;
        }

        @RequestMapping(value = "/getcreateUnsettledPayments", method = RequestMethod.GET)
        public String getcreateUnsettledPayment(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "15") int limit, Model model) throws Exception {
                List<UnsettledPayment> up = null;

                long size = 0;
                up = unsettledPaymentsRepository.selectAllById(offset, limit);

                size = unsettledPaymentsRepository.selectAllCount();
                if (!up.isEmpty()) {
                        model.addAttribute("unsettledPayment", up);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedUnsettledPayments");

                        if (up.size() < limit) {
                                model.addAttribute("end", offset + up.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }
                } else {
                        model.addAttribute("unsettledPayment", up);
                        model.addAttribute("size", size);
                }

                LOGGER.info("unsettledPaymentList" + up);
                return "unsettled-payments";
        }

        @RequestMapping(value = "/getUnsettledPaymentsByAmount", method = RequestMethod.GET)
        public String getUnsettledPaymentByAmount(HttpServletRequest request,
                        @RequestParam(name = "amount", defaultValue = "0") float amount,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
                List<UnsettledPayment> up = null;

                long size = 0;
                up = unsettledPaymentsRepository.selectAllByAmount(amount, offset, limit);

                size = unsettledPaymentsRepository.selectAllCount();
                if (!up.isEmpty()) {
                        model.addAttribute("unsettledPayment", up);
                        model.addAttribute("start", offset + 1);
                        model.addAttribute("size", size);
                        model.addAttribute("url", "/getPaginatedUnsettledPayments");

                        if (up.size() < limit) {
                                model.addAttribute("end", offset + up.size());
                        } else {
                                model.addAttribute("end", offset + limit);
                        }
                        return "unsettle-payment-modal";
                } else {
                        model.addAttribute("unsettledPayment", up);
                        model.addAttribute("size", size);
                }

                return "unsettle-payment-modal";
        }

        @RequestMapping(value = "/createUnsettledPaymentsEntries", method = RequestMethod.POST)
        public String createUnsettledPaymentsEntries(HttpServletRequest request,
                        @RequestBody UnsettledPaymentModel unsettledPaymentModel, Model model) throws Exception {

                UnsettledPayment up = new UnsettledPayment();
                up.setTransaction_reference(unsettledPaymentModel.getTransactionReference());
                up.setAmount(unsettledPaymentModel.getAmount());
                up.setDescription(unsettledPaymentModel.getDescription());
                up.setReference_date(unsettledPaymentModel.getReferenceDate());
                LOGGER.info("uppaynments" + up);
                unsettledPaymentsRepository.persist(up);
                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }

        @RequestMapping(value = "/removeUnsettledPaymentsEntries", method = RequestMethod.DELETE)
        public String removeUnsettledPaymentsEntries(HttpServletRequest request,
                        @RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {

                unsettledPaymentsRepository.deleteById(id);

                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }

        @RequestMapping(value = "/wallet/statement", method = RequestMethod.GET)
        public String getWalletStatement(HttpServletRequest request, @RequestParam LocalDateTime startDate,
                        @RequestParam LocalDateTime endDate, Model model, @RequestParam(defaultValue = "0") int fofoId)
                        throws Exception {
                boolean isAdmin = roleManager.isAdmin(cookiesProcessor.getCookiesObject(request).getRoleIds());
                if (fofoId > 0) {
                        if (!isAdmin) {
                                throw new ProfitMandiBusinessException("Unauthorised access", "PartnerId", "Permission Denied");
                        }
                } else {
                        fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
                }
                float openingBalance = walletService.getOpeningTill(fofoId, startDate);
                float closingBalance = walletService.getOpeningTill(fofoId, endDate);
                List<UserWalletHistory> uwhList = walletService.getPaginatedUserWalletHistoryByRetailerId(fofoId, startDate,
                                endDate, 0, 0);
                Collections.reverse(uwhList);
                model.addAttribute("opening", openingBalance);
                model.addAttribute("closing", closingBalance);
                model.addAttribute("history", uwhList);
                model.addAttribute("startDate", startDate);
                model.addAttribute("endDate", endDate);
                return "walletStatement";
        }

        @RequestMapping(value = "/manualPayment", method = RequestMethod.GET)
        public String ManualPayment(HttpServletRequest request, Model model) throws Exception {

                List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
                                .collect(Collectors.toList());
                Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
                String customRetailers = JSONObject.valueToString(customRetailersMap.values());
                model.addAttribute("customRetailers", customRetailers);

                model.addAttribute("referenceTypes", WalletReferenceType.referenceType);
                model.addAttribute("transactionTypes", TransactionType.values());
                return "wallet-edit";

        }

        @RequestMapping(value = "/getWalletHistory", method = RequestMethod.GET)
        public String getWalletHistory(HttpServletRequest request,
                        @RequestParam(name = "reference", defaultValue = "0") int reference,
                        @RequestParam WalletReferenceType referenceType, Model model) throws Exception {

                LOGGER.info("type" + referenceType);
                List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository
                                .selectAllByreferenceIdandreferenceType(reference, referenceType);

                if (userWalletHistory.isEmpty()) {
                        throw new ProfitMandiBusinessException("RefrenceId", reference, "Reference Id not found");
                }

                UserWallet userWallet = userWalletRepository.selectById(userWalletHistory.get(0).getWalletId());

                LOGGER.info("userWallet" + userWallet);
                CustomRetailer customretailer = retailerService.getFofoRetailer(userWallet.getUserId());

                model.addAttribute("userWallet", userWallet);
                model.addAttribute("customretailer", customretailer);

                model.addAttribute("wallethistory", userWalletHistory);

                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "wallet-history";

        }

        @RequestMapping(value = "/getWalletHistoryByPartner", method = RequestMethod.GET)
        public String getWalletHistoryByPartner(HttpServletRequest request, int fofoId,
                        @RequestParam(name = "referenceType", required = false) WalletReferenceType referenceType,
                        @RequestParam(name = "offset", required = false, defaultValue = "0") int offset,
                        @RequestParam(name = "limit", required = false, defaultValue = "30") int limit, Model model)
                        throws Exception {

                UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
                List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository.selectPaginatedByWalletIdReferenceType(
                                userWallet.getId(), referenceType, SortOrder.DESCENDING, offset, limit);

                CustomRetailer customretailer = retailerService.getFofoRetailer(fofoId);

                model.addAttribute("userWallet", userWallet);
                model.addAttribute("customretailer", customretailer);
                model.addAttribute("wallethistory", userWalletHistory);

                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "wallet-history";

        }

        @RequestMapping(value = "/getPartnerName", method = RequestMethod.GET)
        public String getPartnerName(HttpServletRequest request,
                        @RequestParam(name = "reference", defaultValue = "0") int reference,
                        @RequestParam WalletReferenceType referenceType, Model model) throws Exception {

                List<UserWalletHistory> userWalletHistory = userWalletHistoryRepository
                                .selectAllByreferenceIdandreferenceType(reference, referenceType);
                if (userWalletHistory.isEmpty()) {
                        throw new ProfitMandiBusinessException("RefrenceId", reference, "Reference Id not found");
                }
                UserWallet userWallet = userWalletRepository.selectById(userWalletHistory.get(0).getWalletId());

                CustomRetailer retailer = retailerService.getFofoRetailer(userWallet.getUserId());

                model.addAttribute("response", mvcResponseSender.createResponseString(retailer));

                return "response";

        }

        @RequestMapping(value = "/walletUpdate", method = RequestMethod.POST)
        public String walletUpdate(HttpServletRequest request,
                        @RequestParam(name = "reference", defaultValue = "0") int reference,
                        @RequestParam int referenceTypeValue, @RequestParam TransactionType transactiontype,
                        @RequestParam int amount, @RequestParam String description, @RequestParam int retailerId, Model model,
                        @RequestParam LocalDateTime businessTimestamp)
                        throws Exception {
                WalletReferenceType referenceType = WalletReferenceType.findByValue(referenceTypeValue);
                if (reference==0 && referenceType.getValue()>= WalletReferenceType.INCENTIVES.getValue()) {
                        LOGGER.error("referenceType: " + referenceType);
                        ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);

                        if (paymentType == null) {
                                paymentType = new ManualPaymentType();
                                paymentType.setReferenceType(referenceType);
                        }
                        paymentType.setCounter(paymentType.getCounter() + 1);
                        manualPaymentRequestRepository.persist(paymentType);
                        reference = paymentType.getCounter();
                        
                } else if(reference==0){
                        throw new ProfitMandiBusinessException("RefrenceId", reference,
                                        "System specific reference type cant be used manually");
                }
                        
                List<UserWalletHistory> userWalletHistoryList = userWalletHistoryRepository
                                .selectAllByreferenceIdandreferenceType(reference, referenceType);
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                int walletId = userWallet.getId();
                int walletAmount = walletService.getWalletAmount(retailerId);

                int returnReference = 0;
                UserWalletHistory newUserWalletHistory = new UserWalletHistory();

                if (!userWalletHistoryList.isEmpty()) {
                        long validRetailerEntries = userWalletHistoryList.stream()
                                        .filter(x-> x.getWalletId() == walletId)
                                        .count();
                        if(validRetailerEntries == 0) {
                                throw new ProfitMandiBusinessException("RefrenceId", reference,
                                                "Reference Id assign to Other  partner");
                        }
                }
                
                if (TransactionType.DEBIT.equals(transactiontype)) {
                        amount = -amount;
                }
                
                userWallet.setAmount(walletAmount + amount);
                newUserWalletHistory.setAmount(amount);
                newUserWalletHistory.setBusinessTimestamp(businessTimestamp);
                newUserWalletHistory.setDescription(description);
                newUserWalletHistory.setReference(reference);
                newUserWalletHistory.setWalletId(userWallet.getId());
                newUserWalletHistory.setReferenceType(referenceType);
                newUserWalletHistory.setTimestamp(LocalDateTime.now());
                userWalletHistoryRepository.persist(newUserWalletHistory);

                model.addAttribute("response", mvcResponseSender.createResponseString(reference));
                return "response";

        }

        @RequestMapping(value = "/addMoney", method = RequestMethod.POST)
        public String addMoney(HttpServletRequest request, @RequestParam float amount,
                        @RequestParam String transactionReference, @RequestParam LocalDateTime referenceTime,
                        @RequestParam String bankName, @RequestParam int fofoId, Model model) throws Exception {

                AddWalletRequest addWalletRequest = new AddWalletRequest();
                addWalletRequest.setRetailerId(fofoId);
                addWalletRequest.setAmount(amount);
                addWalletRequest.setTransaction_reference(transactionReference);
                addWalletRequest.setCreateTimestamp(LocalDateTime.now());
                addWalletRequest.setBank_name(bankName);
                addWalletRequest.setReference_date(referenceTime.toLocalDate());
                addWalletRequest.setStatus(AddWalletRequestStatus.pending);

                LOGGER.info("info" + addWalletRequest);

                addWalletRequestRepository.persist(addWalletRequest);
                model.addAttribute("response", mvcResponseSender.createResponseString(true));
                return "response";
        }
}