-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79d7a62
commit 38b6a63
Showing
17 changed files
with
541 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/com/springboot/app/controllers/FacturaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.springboot.app.controllers; | ||
|
||
import com.springboot.app.models.entity.Cliente; | ||
import com.springboot.app.models.entity.Factura; | ||
import com.springboot.app.models.entity.ItemFactura; | ||
import com.springboot.app.models.entity.Producto; | ||
import com.springboot.app.models.service.IClienteService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.bind.support.SessionStatus; | ||
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Controller | ||
@RequestMapping("/factura") | ||
@SessionAttributes("factura") | ||
public class FacturaController { | ||
|
||
@Autowired | ||
private IClienteService clienteService; | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
@GetMapping("/ver/{id}") | ||
public String ver(@PathVariable(value="id") Long id, | ||
Model model, | ||
RedirectAttributes flash) { | ||
Factura factura = clienteService.findFacturaById(id); | ||
|
||
if(factura == null) { | ||
flash.addFlashAttribute("error", "La factura no existe en la base de datos!"); | ||
return "redirect:/listar"; | ||
} | ||
|
||
model.addAttribute("factura", factura); | ||
model.addAttribute("titulo", "Factura: ".concat(factura.getDescripcion())); | ||
|
||
return "factura/ver"; | ||
} | ||
|
||
@GetMapping("/form/{clienteId}") | ||
public String crear(@PathVariable(value = "clienteId") Long clienteId, Map<String, Object> model, | ||
RedirectAttributes flash) { | ||
|
||
Cliente cliente = clienteService.findById(clienteId); | ||
|
||
if (cliente == null) { | ||
flash.addFlashAttribute("error", "El cliente no existe en la base de datos"); | ||
return "redirect:/listar"; | ||
} | ||
|
||
Factura factura = new Factura(); | ||
factura.setCliente(cliente); | ||
|
||
model.put("factura", factura); | ||
model.put("titulo", "Crear Factura"); | ||
|
||
return "factura/form"; | ||
} | ||
|
||
@GetMapping(value = "/cargar-productos/{term}", produces = { "application/json" }) | ||
public @ResponseBody | ||
List<Producto> cargarProductos(@PathVariable String term) { | ||
return clienteService.findByNombre(term); | ||
} | ||
|
||
@PostMapping("/form") | ||
public String guardar(@Valid Factura factura, | ||
BindingResult result, Model model, | ||
@RequestParam(name = "item_id[]", required = false) Long[] itemId, | ||
@RequestParam(name = "cantidad[]", required = false) Integer[] cantidad, | ||
RedirectAttributes flash, | ||
SessionStatus status) { | ||
|
||
if (result.hasErrors()) { | ||
model.addAttribute("titulo", "Crear Factura"); | ||
return "factura/form"; | ||
} | ||
|
||
if (itemId == null || itemId.length == 0) { | ||
model.addAttribute("titulo", "Crear Factura"); | ||
model.addAttribute("error", "Error: La factura NO puede no tener líneas!"); | ||
return "factura/form"; | ||
} | ||
|
||
for (int i = 0; i < itemId.length; i++) { | ||
Producto producto = clienteService.findProductoById(itemId[i]); | ||
|
||
ItemFactura linea = new ItemFactura(); | ||
linea.setCantidad(cantidad[i]); | ||
linea.setProducto(producto); | ||
factura.addItemFactura(linea); | ||
|
||
log.info("ID: " + itemId[i].toString() + ", cantidad: " + cantidad[i].toString()); | ||
} | ||
|
||
clienteService.saveFactura(factura); | ||
status.setComplete(); | ||
|
||
flash.addFlashAttribute("success", "Factura creada con éxito!"); | ||
|
||
return "redirect:/ver/" + factura.getCliente().getId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.springboot.app.models.dao; | ||
|
||
import com.springboot.app.models.entity.Factura; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface IFacturaDao extends CrudRepository<Factura, Long> { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/springboot/app/models/dao/IProductoDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.springboot.app.models.dao; | ||
|
||
import com.springboot.app.models.entity.Producto; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface IProductoDao extends CrudRepository<Producto,Long> { | ||
|
||
@Query("select p from Producto p where p.nombre like %?1%") | ||
public List<Producto> findByNombre(String term); | ||
|
||
public List<Producto> findByNombreLikeIgnoreCase(String term); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.