mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
trabajando en la vista del carro de la compra
This commit is contained in:
@ -10,6 +10,7 @@ import org.springframework.security.core.Authentication;
|
||||
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Locale;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/cart")
|
||||
@ -46,15 +47,15 @@ public class CartController {
|
||||
|
||||
/** Vista del carrito */
|
||||
@GetMapping
|
||||
public String viewCart(Model model, Principal principal) {
|
||||
var items = service.listItems(currentUserId(principal));
|
||||
public String viewCart(Model model, Principal principal, Locale locale) {
|
||||
var items = service.listItems(currentUserId(principal), locale);
|
||||
model.addAttribute("items", items);
|
||||
return "imprimelibros/cart/cart"; // crea esta vista si quieres (tabla simple)
|
||||
}
|
||||
|
||||
/** Añadir presupuesto via POST form */
|
||||
@PostMapping("/add")
|
||||
public String add(@RequestParam("presupuestoId") Long presupuestoId, Principal principal) {
|
||||
public String add(@PathVariable(name = "presupuestoId", required = true) Long presupuestoId, Principal principal) {
|
||||
service.addPresupuesto(currentUserId(principal), presupuestoId);
|
||||
return "redirect:/cart";
|
||||
}
|
||||
|
||||
@ -1,19 +1,45 @@
|
||||
package com.imprimelibros.erp.cart;
|
||||
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imprimelibros.erp.presupuesto.classes.PresupuestoFormatter;
|
||||
import com.imprimelibros.erp.presupuesto.dto.Presupuesto;
|
||||
import com.imprimelibros.erp.presupuesto.PresupuestoRepository;
|
||||
import com.imprimelibros.erp.presupuesto.service.PresupuestoService;
|
||||
|
||||
|
||||
@Service
|
||||
public class CartService {
|
||||
|
||||
private final CartRepository cartRepo;
|
||||
private final CartItemRepository itemRepo;
|
||||
private final MessageSource messageSource;
|
||||
private final PresupuestoFormatter presupuestoFormatter;
|
||||
private final PresupuestoRepository presupuestoRepo;
|
||||
|
||||
public CartService(CartRepository cartRepo, CartItemRepository itemRepo) {
|
||||
public CartService(CartRepository cartRepo, CartItemRepository itemRepo,
|
||||
MessageSource messageSource, PresupuestoFormatter presupuestoFormatter,
|
||||
PresupuestoRepository presupuestoRepo) {
|
||||
this.cartRepo = cartRepo;
|
||||
this.itemRepo = itemRepo;
|
||||
this.messageSource = messageSource;
|
||||
this.presupuestoFormatter = presupuestoFormatter;
|
||||
this.presupuestoRepo = presupuestoRepo;
|
||||
}
|
||||
|
||||
/** Devuelve el carrito activo o lo crea si no existe. */
|
||||
@ -30,9 +56,21 @@ public class CartService {
|
||||
|
||||
/** Lista items (presupuestos) del carrito activo del usuario. */
|
||||
@Transactional
|
||||
public List<CartItem> listItems(Long userId) {
|
||||
public List<Map<String, Object>> listItems(Long userId, Locale locale) {
|
||||
Cart cart = getOrCreateActiveCart(userId);
|
||||
return itemRepo.findByCartId(cart.getId());
|
||||
List<Map<String, Object>> resultados = new ArrayList<>();
|
||||
List<CartItem> items = itemRepo.findByCartId(cart.getId());
|
||||
for (CartItem item : items) {
|
||||
|
||||
Presupuesto p = presupuestoRepo.findById(item.getPresupuestoId())
|
||||
.orElseThrow(() -> new IllegalStateException("Presupuesto no encontrado: " + item.getPresupuestoId()));
|
||||
|
||||
this.getElementoCart(p, locale);
|
||||
Map<String, Object> elemento = getElementoCart(p, locale);
|
||||
resultados.add(elemento);
|
||||
}
|
||||
System.out.println("Cart items: " + resultados);
|
||||
return resultados;
|
||||
}
|
||||
|
||||
/** Añade un presupuesto al carrito. Si ya está, no hace nada (idempotente). */
|
||||
@ -86,4 +124,73 @@ public class CartService {
|
||||
Cart cart = getOrCreateActiveCart(userId);
|
||||
return itemRepo.findByCartId(cart.getId()).size();
|
||||
}
|
||||
|
||||
private Map<String, Object> getElementoCart(Presupuesto presupuesto, Locale locale) {
|
||||
|
||||
Map<String, Object> resumen = new HashMap<>();
|
||||
|
||||
resumen.put("titulo", presupuesto.getTitulo());
|
||||
|
||||
resumen.put("imagen",
|
||||
"/assets/images/imprimelibros/presupuestador/" + presupuesto.getTipoEncuadernacion() + ".png");
|
||||
resumen.put("imagen_alt",
|
||||
messageSource.getMessage("presupuesto." + presupuesto.getTipoEncuadernacion(), null, locale));
|
||||
|
||||
resumen.put("presupuestoId", presupuesto.getId());
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
List<Map<String, Object>> servicios = new ArrayList<>();
|
||||
if (presupuesto.getServiciosJson() != null && !presupuesto.getServiciosJson().isBlank())
|
||||
try{
|
||||
servicios = mapper.readValue(presupuesto.getServiciosJson(), new TypeReference<>() {
|
||||
});
|
||||
} catch (JsonProcessingException e) {
|
||||
// Manejar la excepción
|
||||
}
|
||||
|
||||
boolean hayDepositoLegal = servicios != null && servicios.stream()
|
||||
.map(m -> java.util.Objects.toString(m.get("id"), ""))
|
||||
.map(String::trim)
|
||||
.anyMatch("deposito-legal"::equals);
|
||||
|
||||
List<HashMap<String, Object>> lineas = new ArrayList<>();
|
||||
HashMap<String, Object> linea = new HashMap<>();
|
||||
Double precio_unitario = 0.0;
|
||||
Double precio_total = 0.0;
|
||||
linea.put("descripcion", presupuestoFormatter.resumen(presupuesto, servicios, locale));
|
||||
linea.put("cantidad", presupuesto.getSelectedTirada() != null ? presupuesto.getSelectedTirada() : 0);
|
||||
precio_unitario = (presupuesto.getPrecioUnitario() != null ? presupuesto.getPrecioUnitario().doubleValue() : 0.0);
|
||||
precio_total = (presupuesto.getPrecioTotalTirada() != null ? presupuesto.getPrecioTotalTirada().doubleValue() : 0.0);
|
||||
linea.put("precio_unitario", precio_unitario);
|
||||
linea.put("precio_total", BigDecimal.valueOf(precio_total).setScale(2, RoundingMode.HALF_UP));
|
||||
lineas.add(linea);
|
||||
|
||||
if (hayDepositoLegal) {
|
||||
linea = new HashMap<>();
|
||||
linea.put("descripcion", messageSource.getMessage("presupuesto.resumen-deposito-legal", null, locale));
|
||||
linea.put("cantidad", 4);
|
||||
linea.put("precio_unitario", precio_unitario);
|
||||
linea.put("precio_total", BigDecimal.valueOf(precio_unitario * 4).setScale(2, RoundingMode.HALF_UP));
|
||||
lineas.add(linea);
|
||||
}
|
||||
|
||||
List<Map<String, Object>> serviciosExtras = new ArrayList<>();
|
||||
if (servicios != null) {
|
||||
for (Map<String, Object> servicio : servicios) {
|
||||
HashMap<String, Object> servicioData = new HashMap<>();
|
||||
servicioData.put("id", servicio.get("id"));
|
||||
servicioData.put("descripcion", servicio.get("label"));
|
||||
servicioData.put("precio", servicio.get("id").equals("marcapaginas")
|
||||
? Double.parseDouble(servicio.get("price").toString())
|
||||
/ Double.parseDouble(servicio.get("units").toString())
|
||||
: servicio.get("price"));
|
||||
servicioData.put("unidades", servicio.get("units"));
|
||||
serviciosExtras.add(servicioData);
|
||||
}
|
||||
}
|
||||
resumen.put("lineas", lineas);
|
||||
resumen.put("servicios", serviciosExtras);
|
||||
|
||||
return resumen;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user