diff --git a/src/main/java/com/springboot/app/controllers/FacturaController.java b/src/main/java/com/springboot/app/controllers/FacturaController.java index 9eba145..69d85b6 100644 --- a/src/main/java/com/springboot/app/controllers/FacturaController.java +++ b/src/main/java/com/springboot/app/controllers/FacturaController.java @@ -109,4 +109,18 @@ public String guardar(@Valid Factura factura, return "redirect:/ver/" + factura.getCliente().getId(); } + + @GetMapping("/eliminar/{id}") + public String eliminar(@PathVariable(value = "id") Long id, RedirectAttributes flash){ + + Factura factura = clienteService.findFacturaById(id); + if (factura != null){ + clienteService.deleteFactura(id); + flash.addFlashAttribute("success", "Factura eliminada con éxito!"); + return "redirect:/ver/" + factura.getCliente().getId(); + } + flash.addFlashAttribute("error", "No existe la factura en la base de datos, no se puede eliminar!"); + return "redirect:/listar"; + } + } diff --git a/src/main/java/com/springboot/app/models/entity/Cliente.java b/src/main/java/com/springboot/app/models/entity/Cliente.java index 0dbc730..c8bdb65 100644 --- a/src/main/java/com/springboot/app/models/entity/Cliente.java +++ b/src/main/java/com/springboot/app/models/entity/Cliente.java @@ -110,4 +110,9 @@ public void setFacturas(List facturas) { public void addFactura(Factura factura){ facturas.add(factura); } + + @Override + public String toString() { + return nombre + ' ' + apellido ; + } } diff --git a/src/main/java/com/springboot/app/models/entity/Factura.java b/src/main/java/com/springboot/app/models/entity/Factura.java index ea4d822..ba15c07 100644 --- a/src/main/java/com/springboot/app/models/entity/Factura.java +++ b/src/main/java/com/springboot/app/models/entity/Factura.java @@ -1,6 +1,7 @@ package com.springboot.app.models.entity; import javax.persistence.*; +import javax.validation.constraints.NotEmpty; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; @@ -16,7 +17,9 @@ public class Factura implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @NotEmpty private String descripcion; + private String observacion; @Temporal(TemporalType.DATE) diff --git a/src/main/java/com/springboot/app/models/service/IClienteService.java b/src/main/java/com/springboot/app/models/service/IClienteService.java index f80fc93..34f81b3 100644 --- a/src/main/java/com/springboot/app/models/service/IClienteService.java +++ b/src/main/java/com/springboot/app/models/service/IClienteService.java @@ -12,21 +12,23 @@ import com.springboot.app.models.entity.Cliente; public interface IClienteService { - public List findAll(); + List findAll(); - public Page findAll(Pageable pageable); + Page findAll(Pageable pageable); - public void save(Cliente cliente); + void save(Cliente cliente); - public Cliente findById(Long id); + Cliente findById(Long id); - public void delete(Long id); + void delete(Long id); - public List findByNombre(String term); + List findByNombre(String term); - public void saveFactura(Factura factura); + void saveFactura(Factura factura); - public Producto findProductoById(Long id); + Producto findProductoById(Long id); - public Factura findFacturaById(Long id); + Factura findFacturaById(Long id); + + void deleteFactura(Long id); } diff --git a/src/main/java/com/springboot/app/models/service/impl/ClienteServiceImpl.java b/src/main/java/com/springboot/app/models/service/impl/ClienteServiceImpl.java index 5340132..96094a3 100644 --- a/src/main/java/com/springboot/app/models/service/impl/ClienteServiceImpl.java +++ b/src/main/java/com/springboot/app/models/service/impl/ClienteServiceImpl.java @@ -1,10 +1,9 @@ package com.springboot.app.models.service.impl; -import java.util.List; -import java.util.Optional; - +import com.springboot.app.models.dao.IClienteDao; import com.springboot.app.models.dao.IFacturaDao; import com.springboot.app.models.dao.IProductoDao; +import com.springboot.app.models.entity.Cliente; import com.springboot.app.models.entity.Factura; import com.springboot.app.models.entity.Producto; import com.springboot.app.models.service.IClienteService; @@ -14,73 +13,75 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import com.springboot.app.models.dao.IClienteDao; -import com.springboot.app.models.entity.Cliente; +import java.util.List; @Service public class ClienteServiceImpl implements IClienteService { - @Autowired - private IClienteDao clienteDao; - - @Autowired - private IProductoDao productoDao; - - @Autowired - private IFacturaDao facturaDao; - - @Override - @Transactional(readOnly = true) - public List findAll() { - return (List) clienteDao.findAll(); - } - - @Override - @Transactional - public void save(Cliente cliente) { - clienteDao.save(cliente); - - } - - @Override - @Transactional(readOnly = true) - public Cliente findById(Long id) { - return clienteDao.findById(id).orElse(null); - } - - @Override - @Transactional - public void delete(Long id) { - clienteDao.deleteById(id); - - } - - @Override - @Transactional(readOnly = true) - public Page findAll(Pageable pageable) { - return clienteDao.findAll(pageable); - } - - @Override - @Transactional(readOnly = true) - public List findByNombre(String term) { - return productoDao.findByNombreLikeIgnoreCase("%" + term + "%"); - } - - @Override - @Transactional - public void saveFactura(Factura factura) { - facturaDao.save(factura); - } - - @Override - @Transactional(readOnly=true) - public Producto findProductoById(Long id) { - return productoDao.findById(id).orElse(null); - } - - @Override - @Transactional(readOnly=true) - public Factura findFacturaById(Long id) { - return facturaDao.findById(id).orElse(null); - } + @Autowired + private IClienteDao clienteDao; + + @Autowired + private IProductoDao productoDao; + + @Autowired + private IFacturaDao facturaDao; + + @Override + @Transactional(readOnly = true) + public List findAll() { + return (List) clienteDao.findAll(); + } + + @Override + @Transactional + public void save(Cliente cliente) { + clienteDao.save(cliente); + } + + @Override + @Transactional(readOnly = true) + public Cliente findById(Long id) { + return clienteDao.findById(id).orElse(null); + } + + @Override + @Transactional + public void delete(Long id) { + clienteDao.deleteById(id); + } + + @Override + @Transactional(readOnly = true) + public Page findAll(Pageable pageable) { + return clienteDao.findAll(pageable); + } + + @Override + @Transactional(readOnly = true) + public List findByNombre(String term) { + return productoDao.findByNombreLikeIgnoreCase("%" + term + "%"); + } + + @Override + @Transactional + public void saveFactura(Factura factura) { + facturaDao.save(factura); + } + + @Override + @Transactional(readOnly = true) + public Producto findProductoById(Long id) { + return productoDao.findById(id).orElse(null); + } + + @Override + @Transactional(readOnly = true) + public Factura findFacturaById(Long id) { + return facturaDao.findById(id).orElse(null); + } + + @Override + public void deleteFactura(Long id) { + facturaDao.deleteById(id); + } } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 30c25d7..6336ec9 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -1,6 +1,7 @@ -NotEmpty.cliente.nombre= El nombre del clientes es requerido -NotEmpty.cliente.apellido= El apellido del cliente es requerido -NotEmpty.cliente.email= El email es requerido -Email.cliente.email= La direccion de correo no es valida -NotNull.cliente.createdAt= La fecha no puede ser nula -type.Mismatch.cliente.createdAt= Formato de la fecha invalido, debe ser: yyy-MM-dd \ No newline at end of file +NotEmpty.cliente.nombre=El nombre del clientes es requerido +NotEmpty.cliente.apellido=El apellido del cliente es requerido +NotEmpty.cliente.email=El email es requerido +Email.cliente.email=La direccion de correo no es valida +NotNull.cliente.createdAt=La fecha no puede ser nula +type.Mismatch.cliente.createdAt=Formato de la fecha invalido, debe ser: yyy-MM-dd +NotEmpty.factura.descripcion=La descripción es requerida! \ No newline at end of file diff --git a/src/main/resources/templates/factura/form.html b/src/main/resources/templates/factura/form.html index 2449444..e625351 100644 --- a/src/main/resources/templates/factura/form.html +++ b/src/main/resources/templates/factura/form.html @@ -70,6 +70,7 @@

+

Total 0
@@ -78,8 +79,8 @@
Total 0
class="btn btn-secondary" />
- + diff --git a/src/main/resources/templates/factura/ver.html b/src/main/resources/templates/factura/ver.html index 9a60846..a3da31b 100644 --- a/src/main/resources/templates/factura/ver.html +++ b/src/main/resources/templates/factura/ver.html @@ -36,7 +36,7 @@

+ th:text="'No hay lineas para la factura' + ${factura.descripcion}"> diff --git a/src/main/resources/templates/layout/layout.html b/src/main/resources/templates/layout/layout.html index 430166e..af8ccce 100644 --- a/src/main/resources/templates/layout/layout.html +++ b/src/main/resources/templates/layout/layout.html @@ -6,6 +6,7 @@ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" /> <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" /> + <link rel="stylesheet" th:href="@{/css/jquery-ui.min.css}" /> <link rel="stylesheet" th:href="@{/css/styles.css}" /> </head> diff --git a/src/main/resources/templates/ver.html b/src/main/resources/templates/ver.html index 630f806..0286829 100644 --- a/src/main/resources/templates/ver.html +++ b/src/main/resources/templates/ver.html @@ -50,6 +50,8 @@ <h4 class="card-title"> <th>Descripción</th> <th>Fecha</th> <th>Total</th> + <th>Ver</th> + <th>Eliminar</th> </tr> </thead> @@ -59,6 +61,9 @@ <h4 class="card-title"> <td th:text="${factura.descripcion}"></td> <td th:text="${factura.createAt}"></td> <td th:text="${factura.total}"></td> + <td><a class="btn btn-primary btn-xs" th:href="@{'/factura/ver/' + ${factura.id}}" th:text="'detalle'"></a></td> + <td><a class="btn btn-danger btn-xs" th:href="@{'/factura/eliminar/' + ${factura.id}}" th:text="'eliminar'" + onclick="return confirm('Esta seguro que desea eliminar la factura?');"></a></td> </tr> </tbody> </table>