modificando carrito

This commit is contained in:
2025-10-27 20:30:11 +01:00
parent de7a392e07
commit f6a683de81
22 changed files with 1050 additions and 42 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -20,6 +21,7 @@ import com.imprimelibros.erp.presupuesto.dto.Presupuesto.TipoEncuadernacion;
import java.util.Map;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;
@ -219,6 +221,49 @@ public class skApiClient {
}
}
public Map<String, Object> getCosteEnvio(Map<String, Object> data, Locale locale) {
return performWithRetryMap(() -> {
String url = this.skApiUrl + "api/calcular-envio";
URI uri = UriComponentsBuilder.fromUriString(url)
.queryParam("pais_code3", data.get("pais_code3"))
.queryParam("cp", data.get("cp"))
.queryParam("peso", data.get("peso"))
.queryParam("unidades", data.get("unidades"))
.queryParam("palets", data.get("palets"))
.build(true) // no re-encode []
.toUri();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(authService.getToken());
ResponseEntity<String> response = restTemplate.exchange(
uri,
HttpMethod.GET,
new HttpEntity<>(headers),
String.class);
try {
Map<String, Object> responseBody = new ObjectMapper().readValue(
response.getBody(),
new TypeReference<Map<String, Object>>() {
});
Boolean error = (Boolean) responseBody.get("error");
if (error != null && error) {
return Map.of("error", messageSource.getMessage("direcciones.error.noShippingCost", null, locale));
} else {
Double total = (Double) responseBody.get("data");
return Map.of("data", total);
}
} catch (JsonProcessingException e) {
e.printStackTrace();
return Map.of("error", "Internal Server Error: 1"); // Fallback en caso de error
}
});
}
/******************
* PRIVATE METHODS
******************/
@ -236,6 +281,20 @@ public class skApiClient {
}
}
private Map<String, Object> performWithRetryMap(Supplier<Map<String, Object>> request) {
try {
return request.get();
} catch (HttpClientErrorException.Unauthorized e) {
// Token expirado, renovar y reintentar
authService.invalidateToken();
try {
return request.get(); // segundo intento
} catch (HttpClientErrorException ex) {
throw new RuntimeException("La autenticación ha fallado tras renovar el token.", ex);
}
}
}
private static BigDecimal calcularMargen(
BigDecimal importe, BigDecimal importeMin, BigDecimal importeMax,
BigDecimal margenMax, BigDecimal margenMin) {