trabajando en modificar para obtener los servicios

This commit is contained in:
Jaime Jiménez
2025-09-23 15:09:08 +02:00
parent 85681b4d6e
commit c1df92b840
4 changed files with 39 additions and 24 deletions

View File

@ -21,9 +21,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.MediaType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.imprimelibros.erp.externalApi.skApiClient;
import com.imprimelibros.erp.presupuesto.classes.ImagenPresupuesto;
import com.imprimelibros.erp.presupuesto.classes.PresupuestoMaquetacion;
@ -116,21 +113,14 @@ public class PresupuestoController {
if (calcular) {
HashMap<String, Object> price = new HashMap<>();
String priceStr = apiClient.getPrice(presupuestoService.toSkApiRequest(presupuesto));
HashMap<String, Object> price = presupuestoService.calcularPresupuesto(presupuesto, locale);
try {
price = new ObjectMapper().readValue(priceStr, new TypeReference<>() {
});
} catch (JsonProcessingException e) {
price = new HashMap<>();
price.put("error", messageSource.getMessage("presupuesto.error-obtener-precio", null, locale));
}
if (!price.containsKey("data")) {
return ResponseEntity.badRequest()
.body(messageSource.getMessage("presupuesto.error-obtener-precio", null, locale));
}
return ResponseEntity.ok(price.get("data"));
}
return ResponseEntity.ok().build();
}
@ -154,7 +144,7 @@ public class PresupuestoController {
Map<String, Object> resultado = new HashMap<>();
// servicios extra
resultado.putAll(presupuestoService.obtenerServiciosExtras(presupuesto, locale, apiClient));
resultado.putAll(presupuestoService.obtenerServiciosExtras(presupuesto, locale));
Map<String, String> language = new HashMap<>();
language.put("calcular", messageSource.getMessage("presupuesto.calcular", null, locale));
resultado.put("language", language);

View File

@ -44,9 +44,6 @@ public class PresupuestoService {
@Autowired
protected MessageSource messageSource;
@Autowired
protected skApiClient skApiClient;
@Autowired
protected MaquetacionPreciosRepository maquetacionPreciosRepository;
@ -58,10 +55,12 @@ public class PresupuestoService {
private final PresupuestadorItems presupuestadorItems;
private final PresupuestoFormatter presupuestoFormatter;
private final skApiClient apiClient;
public PresupuestoService(PresupuestadorItems presupuestadorItems, PresupuestoFormatter presupuestoFormatter) {
public PresupuestoService(PresupuestadorItems presupuestadorItems, PresupuestoFormatter presupuestoFormatter, skApiClient apiClient) {
this.presupuestadorItems = presupuestadorItems;
this.presupuestoFormatter = presupuestoFormatter;
this.apiClient = apiClient;
}
public boolean validateDatosGenerales(int[] tiradas) {
@ -453,18 +452,18 @@ public class PresupuestoService {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("tirada",
presupuesto.getSelectedTirada() != null ? presupuesto.getSelectedTirada() : tirada_min);
Double precio_retractilado = skApiClient.getRetractilado(requestBody);
Double precio_retractilado = apiClient.getRetractilado(requestBody);
return precio_retractilado != null
? NumberFormat.getNumberInstance(locale)
.format(Math.round(precio_retractilado * 100.0) / 100.0)
: "0,00";
}
public Map<String, Object> obtenerServiciosExtras(Presupuesto presupuesto, Locale locale, skApiClient apiClient) {
public Map<String, Object> obtenerServiciosExtras(Presupuesto presupuesto, Locale locale) {
List<Object> opciones = new ArrayList<>();
Double price_prototipo = this.obtenerPrototipo(presupuesto, apiClient);
Double price_prototipo = this.obtenerPrototipo(presupuesto);
opciones.add(new HashMap<String, String>() {
{
@ -561,7 +560,7 @@ public class PresupuestoService {
return response;
}
private Double obtenerPrototipo(Presupuesto presupuesto, skApiClient apiClient) {
private Double obtenerPrototipo(Presupuesto presupuesto) {
// Obtenemos el precio de 1 unidad para el ejemplar de prueba
HashMap<String, Object> price = new HashMap<>();
@ -775,13 +774,30 @@ public class PresupuestoService {
return out;
}
public Map<String, Object> getResumen(Presupuesto presupuesto, String[] servicios, Locale locale) {
Map<String, Object> resumen = new HashMap<>();
resumen.put("titulo", presupuesto.getTitulo());
resumen.put("texto", presupuestoFormatter.resumen(presupuesto, servicios, locale));
return resumen;
}
public HashMap<String, Object> calcularPresupuesto(Presupuesto presupuesto, Locale locale) {
HashMap<String, Object> price = new HashMap<>();
String priceStr = apiClient.getPrice(this.toSkApiRequest(presupuesto));
try {
price = new ObjectMapper().readValue(priceStr, new TypeReference<>() {
});
} catch (JsonProcessingException e) {
price = new HashMap<>();
price.put("error", messageSource.getMessage("presupuesto.error-obtener-precio", null, locale));
}
return price;
}
}