HTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Cobrança :: Débitos</title>
<script type="text/javascript" src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript"
src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/debito.js"></script>
<link rel="stylesheet"
href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/debito.css" />
</head>
<body>
<div class="container">
<div class="card">
<div class="card-block">
<div class="div-filtro">
<h2>Lista de Débitos</h2>
<h5 id="titulo-filtro">Filtro:</h5>
<input type="text" id="nome_search"
placeholder="Por nome do cliente"
oninput="javascript:filtrarDebitos(1);" />
<select onchange="javascript:filtrarDebitos(1);" id="status_search">
<option value="">Por status</option>
<option th:each="st : ${listaStatus}"
th:text="${st}" th:value="${st}">
</select>
</div>
<table class="table table-hover" id="resultList"
th:fragment="frag-debitos">
<thead>
<tr>
<th>Nome cliente</th>
<th>Vencimento</th>
<th>Valor</th>
<th>Status</th>
</tr>
</thead>
<thead>
</thead>
<tbody>
<tr th:each="deb : ${listaDebitos}"
th:onclick="|javascript:editDebito(${deb.codigo});|">
<td th:text="${deb.cliente.nome}">
<td th:text="${#dates.format(deb.vencimento, 'dd/MM/yyyy')}">
<td
th:text="|R$ ${#numbers.formatDecimal(deb.valor, 1, 'DEFAULT', 2, 'DEFAULT')}|">
<td th:text="${deb.status}">
</tr>
</tbody>
<tr>
<td colspan="4">
<ul class="nav nav-pills">
<li class="nav-item"
th:each="i : ${#numbers.sequence(1,listaDebitos.totalPages)}">
<a th:text="${i}"
th:href="|javascript:filtrarDebitos(${i});|" class="nav-link"></a>
</li>
</ul>
</td>
</tr>
</table>
<hr />
</div>
</div>
<div class="card">
<h4>Cartas de Cobrança</h4>
<form action="/gerarCartas" method="post">
<select name="mes">
<option th:each="i : ${#numbers.sequence(1,12)}" th:text="${i}"
th:value="${i-1}"></option>
</select> <select name="ano">
<option th:each="i : ${#numbers.sequence(2019,2002)}"
th:text="${i}" th:value="${i}"></option>
</select>
<button>Gerar Cartas</button>
<div class="alert alert-sucess" th:if="${numCartas}"
th:text="${numCartas}"></div>
</form>
</div>
</div>
<div id="debitoModalHolder"></div>
<div id="debitoModal" class="modal fade" role="dialog"
th:fragment="modalContents" th:if="${debito}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" th:text="${debito.cliente.nome}">Nome
do Cliente</h4>
</div>
<div class="modal-body">
<form th:object="${debito}" th:action="salvarDebito" method="post">
<input type="hidden" name="codigo" th:value="${debito.codigo}">
<input type="hidden" th:field="*{cliente}"
th:value="${debito.cliente}">
<div class="form-group row">
<label class="col-sm-2 col-form-label">Data: </label> <input
class="col-sm-5" type="date" name="vencimento"
th:value="${debito.vencimento}"> <span
class="col-sm-1 input-group-text">R$</span> <input type="text"
th:field="*{valor}" class="form-control col-sm-3" size="10"
th:value="${#numbers.formatDecimal(debito.valor, 1, 'DEFAULT', 2, 'DEFAULT')}">
</div>
<div class="form-group form-check-inline">
<label class=" form-check-label">Status: </label> <input
type="radio" th:field="*{status}" th:each="st : ${listaStatus}"
th:text="|${st} |" th:value="${st}"
class="form-check-input">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Salvar</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
</body>
</html>
CONTROLLER:
package br.ifpe.web2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import br.ifpe.web2.domain.Debito;
import br.ifpe.web2.service.CartaCobrancaService;
import br.ifpe.web2.service.DebitoService;
@Controller
public class CobrancaController {
@Autowired
private DebitoService debitoService;
@Autowired
private CartaCobrancaService cartaCobrancaService;
// @GetMapping("/")
// public ModelAndView debitos(@RequestParam(defaultValue="1") int page) {
// ModelAndView mv = new ModelAndView("debito-list");
// Page<Debito> pagina = this.debitoService.findAll(PageRequest.of(page - 1, 6, Sort.by("cliente.nome")));
// mv.addObject("listaDebitos", pagina) ;
// return mv;
// }
@PostMapping("/gerarCartas")
public ModelAndView gerarCartas(@RequestParam int mes, @RequestParam int ano) {
Integer numCartas = cartaCobrancaService.gerarCartas(mes, ano);
ModelAndView mv = new ModelAndView("debito-list");
Page<Debito> pagina = this.debitoService.listarTodos(PageRequest.of(0, 6, Sort.by("vencimento")));
mv.addObject("listaDebitos", pagina) ;
mv.addObject("numCartas", numCartas);
return mv;
}
// @GetMapping("/filtrar")
// @ResponseBody
// public Page<Debito> filtrarPorNomeCliente(String nome, @RequestParam int page){
// return this.debitoService.findByNomeCliente(nome, PageRequest.of(page - 1, 2, Sort.by("vencimento")));
// }
}
CONTROLLER
package br.ifpe.web2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import br.ifpe.web2.domain.Debito;
import br.ifpe.web2.domain.FiltroDebito;
import br.ifpe.web2.domain.Status;
import br.ifpe.web2.service.DebitoService;
@Controller
public class DebitoController {
@Autowired
private DebitoService debitoService;
@GetMapping("/debito/{codigo}")
public ModelAndView editDebito(@PathVariable("codigo") Integer codigo) {
ModelAndView mv = new ModelAndView("debito-list :: modalContents");
mv.addObject("debito", this.debitoService.findDebito(codigo));
mv.addObject("listaStatus", Status.values());
return mv;
}
@PostMapping("salvarDebito")
public String salvarDebito(Debito debito) {
this.debitoService.salvarDebito(debito);
return "redirect:/";
}
@GetMapping("/")
public ModelAndView listarTodos(@RequestParam(defaultValue="1") int page) {
ModelAndView mv = new ModelAndView("debito-list");
Pageable paginaReq = PageRequest.of(page - 1, 6, Sort.by("cliente.nome", "vencimento"));
Page<Debito> paginaResult = this.debitoService.listarTodos(paginaReq);
System.out.println("Resultados: " + paginaResult.getTotalElements());
mv.addObject("listaDebitos", paginaResult);
mv.addObject("listaStatus", Status.values());
return mv;
}
@PostMapping(value= {"pesquisarDebitos"}, consumes = MediaType.APPLICATION_JSON_VALUE)
public ModelAndView pesquisarDebito(@RequestBody FiltroDebito filtro) {
System.out.println("Nome: " + filtro.getNomeCliente());
System.out.println("Status: " + filtro.getStatus());
int page = filtro.getPage();
ModelAndView mv = new ModelAndView("debito-list :: frag-debitos");
Page<Debito> paginaResult;
Pageable paginaReq = PageRequest.of(page - 1, 6, Sort.by("cliente.nome", "vencimento"));
paginaResult = this.debitoService.pesquisarDebitosPorFiltro(filtro, paginaReq);
System.out.println("Resultados: " + paginaResult.getTotalElements());
mv.addObject("listaDebitos", paginaResult);
mv.addObject("listaStatus", Status.values());
return mv;
}
}