com.nkapps.billing.controllers.PaymentController.java Source code

Java tutorial

Introduction

Here is the source code for com.nkapps.billing.controllers.PaymentController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nkapps.billing.controllers;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nkapps.billing.models.Subject;
import com.nkapps.billing.services.AuthService;
import com.nkapps.billing.services.PaymentService;
import com.nkapps.billing.services.SearchService;

/**
 *
 * @author nuraddin
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private AuthService authService;

    @Autowired
    private SearchService searchService;

    @Autowired
    private Environment environment;

    @Autowired
    private PaymentService paymentService;

    Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Subject subject = authService.getSubject();
        map.put("userName", subject.getName());
        map.put("userRole", subject.getRole());
        map.put("ns10Code", subject.getNs10Code());
        map.put("ns11Code", subject.getNs11Code());
        map.put("searchBy", searchService.execSearchBy(request, response));
        map.put("searchWithinDate", searchService.execSearchWithinDate(request, response));
        map.put("searchByDate", searchService.execSearchByDate(request, response));

        return "payment/index";
    }

    @RequestMapping(value = "/payment-list", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> paymentList(HttpServletRequest request,
            HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {
            // for saving search parameters
            searchService.execSearchBy(request, response);
            searchService.execSearchWithinDate(request, response);
            searchService.execSearchByDate(request, response);

            Map<String, String[]> map = request.getParameterMap();
            HashMap<String, String> parameters = new HashMap<>();

            for (String key : map.keySet()) {
                String[] mapValue = map.get(key);
                parameters.put(key, mapValue[0]);
            }

            resMap.put("success", true);
            resMap.put("draw", request.getParameter("draw"));

            resMap.put("data", paymentService.getPaymentList(parameters));

        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }

    @RequestMapping(value = "/key-payment-list", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> keyPaymentList(HttpServletRequest request,
            HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {

            Map<String, String[]> map = request.getParameterMap();
            HashMap<String, String> parameters = new HashMap<>();

            for (String key : map.keySet()) {
                String[] mapValue = map.get(key);
                parameters.put(key, mapValue[0]);
            }

            resMap.put("success", true);
            resMap.put("draw", request.getParameter("draw"));

            if (parameters.get("paymentId") != null) {
                resMap.put("data", paymentService.getKeyPaymentList(parameters));
            } else {
                resMap.put("data", new HashMap<>());
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }

    @RequestMapping(value = "/source", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> source(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {
            resMap.put("success", true);
            resMap.put("data", paymentService.getSources());
        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }

    @RequestMapping(value = "/payment-save", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> paymentSave(HttpServletRequest request,
            HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {

            String ip = authService.getClientIp(request);
            if (!authService.isAllowedByPropertyIp("bank_statement_payment_manual_allowed_ips", ip)) {
                throw new Exception(messageSource.getMessage("upload.you_have_not_privelege_to_upload_excel", null,
                        LocaleContextHolder.getLocale()));
            }

            String tin = request.getParameter("tin");
            String paymentNum = request.getParameter("paymentNum");
            Date paymentDate = new SimpleDateFormat("dd.MM.yyyy").parse(request.getParameter("paymentDate"));
            BigDecimal paymentSum = new BigDecimal(request.getParameter("paymentSum"));
            Short sourceCode = Short.parseShort(request.getParameter("sourceCode")); // no where used
            String tinDebtor = request.getParameter("tinDebtor");
            Long issuerSerialNumber = authService.getCertificateInfo().getSerialNumber().longValue();
            String issuerIp = authService.getClientIp(request);

            if ("true".equals(request.getParameter("edit"))) {
                BigDecimal paymentId = new BigDecimal(request.getParameter("paymentId"));
                paymentService.updatePaymentManual(paymentId, tin, paymentNum, paymentDate, paymentSum, tinDebtor,
                        issuerSerialNumber, issuerIp);

            } else {
                paymentService.insertPaymentManual(tin, paymentNum, paymentDate, paymentSum, tinDebtor,
                        issuerSerialNumber, issuerIp);
            }

            resMap.put("success", true);

        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }

    @RequestMapping(value = "/payment-remove", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> paymentRemove(HttpServletRequest request,
            HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {
            String ip = authService.getClientIp(request);
            if (!authService.isAllowedByPropertyIp("bank_statement_payment_manual_allowed_ips", ip)) {
                throw new Exception(messageSource.getMessage("upload.you_have_not_privelege_to_upload_excel", null,
                        LocaleContextHolder.getLocale()));
            }

            BigDecimal paymentId = new BigDecimal(request.getParameter("paymentId"));

            paymentService.removePaymentManual(paymentId);

            resMap.put("success", true);

        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }

    @RequestMapping(value = "/claim", method = RequestMethod.GET)
    public String claim(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Subject subject = authService.getSubject();
        map.put("userName", subject.getName());
        map.put("userRole", subject.getRole());
        map.put("ns10Code", subject.getNs10Code());
        map.put("ns11Code", subject.getNs11Code());
        map.put("searchBy", searchService.execSearchBy(request, response));
        map.put("searchWithinDate", searchService.execSearchWithinDate(request, response));
        map.put("searchByDate", searchService.execSearchByDate(request, response));

        return "payment/claim";
    }

    @RequestMapping(value = "/claim-list", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> claimList(HttpServletRequest request,
            HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<String, Object>();

        try {
            // for saving search parameters
            searchService.execSearchBy(request, response);
            searchService.execSearchWithinDate(request, response);
            searchService.execSearchByDate(request, response);

            Map<String, String[]> map = request.getParameterMap();
            HashMap<String, String> parameters = new HashMap<>();

            for (String key : map.keySet()) {
                String[] mapValue = map.get(key);
                parameters.put(key, mapValue[0]);
            }

            resMap.put("success", true);
            resMap.put("draw", request.getParameter("draw"));
            if ("".equals(request.getParameter("searchBy"))) {
                resMap.put("data", new HashMap());
            } else {
                resMap.put("data", paymentService.getClaimList(parameters));
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            resMap.put("success", false);
            resMap.put("reason", e.getMessage());
        }

        return resMap;
    }
}