corregidos varios fallos de presupuesto

This commit is contained in:
2025-10-15 19:43:00 +02:00
parent 70856edc12
commit f20dd9068a
13 changed files with 130 additions and 76 deletions

View File

@ -5,10 +5,11 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.imprimelibros.erp.users.UserDetailsImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.imprimelibros.erp.users.User;
import org.springframework.security.core.Authentication;
import java.security.Principal;
import java.util.Locale;
@ -27,24 +28,23 @@ public class CartController {
* Adáptalo a tu UserDetails (e.g., SecurityContext con getId())
*/
private Long currentUserId(Principal principal) {
if (principal == null) {
throw new IllegalStateException("Usuario no autenticado");
}
if (principal instanceof Authentication auth) {
Object principalObj = auth.getPrincipal();
if (principalObj instanceof UserDetailsImpl udi) {
return udi.getId();
} else if (principalObj instanceof User u && u.getId() != null) {
return u.getId();
if (principal == null) {
throw new IllegalStateException("Usuario no autenticado");
}
if (principal instanceof Authentication auth) {
Object principalObj = auth.getPrincipal();
if (principalObj instanceof UserDetailsImpl udi) {
return udi.getId();
} else if (principalObj instanceof User u && u.getId() != null) {
return u.getId();
}
}
throw new IllegalStateException("No se pudo obtener el ID del usuario actual");
}
throw new IllegalStateException("No se pudo obtener el ID del usuario actual");
}
/** Vista del carrito */
@GetMapping
public String viewCart(Model model, Principal principal, Locale locale) {

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -65,8 +66,8 @@ public class CartService {
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);
elemento.put("cartItemId", item.getId());
resultados.add(elemento);
}
System.out.println("Cart items: " + resultados);
@ -157,12 +158,14 @@ public class CartService {
HashMap<String, Object> linea = new HashMap<>();
Double precio_unitario = 0.0;
Double precio_total = 0.0;
BigDecimal total = BigDecimal.ZERO;
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));
total = total.add(BigDecimal.valueOf(precio_total));
lineas.add(linea);
if (hayDepositoLegal) {
@ -171,6 +174,7 @@ public class CartService {
linea.put("cantidad", 4);
linea.put("precio_unitario", precio_unitario);
linea.put("precio_total", BigDecimal.valueOf(precio_unitario * 4).setScale(2, RoundingMode.HALF_UP));
total = total.add(BigDecimal.valueOf(precio_unitario * 4));
lineas.add(linea);
}
@ -184,10 +188,14 @@ public class CartService {
? Double.parseDouble(servicio.get("price").toString())
/ Double.parseDouble(servicio.get("units").toString())
: servicio.get("price"));
total = total.add(BigDecimal.valueOf(Double.parseDouble(servicioData.get("precio").toString())));
servicioData.put("unidades", servicio.get("units"));
serviciosExtras.add(servicioData);
}
}
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
String formattedString = currencyFormat.format(total.setScale(2, RoundingMode.HALF_UP).doubleValue());
resumen.put("total", formattedString);
resumen.put("lineas", lineas);
resumen.put("servicios", serviciosExtras);

View File

@ -1,6 +1,7 @@
package com.imprimelibros.erp.externalApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
@ -20,6 +21,7 @@ import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;
import java.util.Locale;
@Service
public class skApiClient {
@ -30,11 +32,13 @@ public class skApiClient {
private final AuthService authService;
private final RestTemplate restTemplate;
private final MargenPresupuestoDao margenPresupuestoDao;
private final MessageSource messageSource;
public skApiClient(AuthService authService, MargenPresupuestoDao margenPresupuestoDao) {
public skApiClient(AuthService authService, MargenPresupuestoDao margenPresupuestoDao, MessageSource messageSource) {
this.authService = authService;
this.restTemplate = new RestTemplate();
this.margenPresupuestoDao = margenPresupuestoDao;
this.messageSource = messageSource;
}
public String getPrice(Map<String, Object> requestBody, TipoEncuadernacion tipoEncuadernacion,
@ -113,7 +117,7 @@ public class skApiClient {
});
}
public Integer getMaxSolapas(Map<String, Object> requestBody) {
public Integer getMaxSolapas(Map<String, Object> requestBody, Locale locale) {
try {
String jsonResponse = performWithRetry(() -> {
String url = this.skApiUrl + "api/calcular-solapas";
@ -150,7 +154,7 @@ public class skApiClient {
JsonNode root = mapper.readTree(jsonResponse);
if (root.get("data") == null || !root.get("data").isInt()) {
throw new RuntimeException("Respuesta inesperada de calcular-solapas: " + jsonResponse);
throw new RuntimeException(messageSource.getMessage("presupuesto.errores.error-interior", new Object[]{1} , locale));
}
return root.get("data").asInt();

View File

@ -145,7 +145,7 @@ public class PresupuestoController {
return ResponseEntity.badRequest().body(errores);
}
Map<String, Object> resultado = new HashMap<>();
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto), locale));
resultado.putAll(presupuestoService.obtenerOpcionesAcabadosCubierta(presupuesto, locale));
return ResponseEntity.ok(resultado);
}
@ -265,7 +265,7 @@ public class PresupuestoController {
}
}
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto), locale));
return ResponseEntity.ok(resultado);
}
@ -298,7 +298,7 @@ public class PresupuestoController {
presupuesto.setGramajeInterior(Integer.parseInt(opciones.get(0))); // Asignar primera opción
}
}
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto), locale));
return ResponseEntity.ok(resultado);
}
@ -321,7 +321,7 @@ public class PresupuestoController {
}
Map<String, Object> resultado = new HashMap<>();
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto), locale));
return ResponseEntity.ok(resultado);
}

View File

@ -109,17 +109,23 @@ public class PresupuestoService {
// POD solo color foto
ImagenPresupuesto opcionColor = this.presupuestadorItems.getImpresionColor(locale);
opcionColor.setSelected(Presupuesto.TipoImpresion.color.equals(presupuesto.getTipoImpresion()));
if (Presupuesto.TipoImpresion.color.equals(presupuesto.getTipoImpresion()))
opcionColor.setSelected(true);
opciones.add(opcionColor);
}
ImagenPresupuesto opcionColorHq = this.presupuestadorItems.getImpresionColorPremium(locale);
if (Presupuesto.TipoImpresion.colorhq.equals(presupuesto.getTipoImpresion()))
opcionColorHq.setSelected(true);
opciones.add(opcionColorHq);
} else {
ImagenPresupuesto opcionNegro = this.presupuestadorItems.getImpresionNegro(locale);
if (Presupuesto.TipoImpresion.negro.equals(presupuesto.getTipoImpresion()))
opcionNegro.setSelected(true);
opciones.add(opcionNegro);
if (!this.isPOD(presupuesto)) {
// POD solo negro premium
ImagenPresupuesto opcionNegro = this.presupuestadorItems.getImpresionNegro(locale);
if (Presupuesto.TipoImpresion.negro.equals(presupuesto.getTipoImpresion()))
opcionNegro.setSelected(true);
opciones.add(opcionNegro);
}
ImagenPresupuesto opcionNegroHq = this.presupuestadorItems.getImpresionNegroPremium(locale);
if (Presupuesto.TipoImpresion.negrohq.equals(presupuesto.getTipoImpresion()))
opcionNegroHq.setSelected(true);
@ -1183,7 +1189,6 @@ public class PresupuestoService {
return true;
}
// =======================================================================
// Métodos privados
// =======================================================================