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;
}
}

View File

@ -170,7 +170,7 @@ presupuesto.resumen-texto-guardas-cabezada= <li>Guardas {0} en {1} {2}. Color de
presupuesto.resumen-texto-acabado-cubierta= <li>Acabado {0}. </li>
presupuesto.resumen-texto-end=</ul>
presupuesto.resumen-texto-sobrecubierta=<li>Sobrecubierta impresa en {0} {1} gr. <ul><li>Acabado {2}</li><li>Solapas: {3} mm.</li></ul></li>
presupuesto.resumen-texto-faja=<li>Faja impresa en {0} {1} gr. con un alto de {2} mm. <ul><li>Acabado {3}</li><li>Solapas:{4} mm.</li></ul></li>
presupuesto.resumen-texto-faja=<li>Faja impresa en {0} {1} gr. con un alto de {2} mm. <ul><li>Acabado {3}</li><li>Solapas: {4} mm.</li></ul></li>
presupuesto.volver-extras=Volver a extras
# Resumen del presupuesto

View File

@ -1390,9 +1390,18 @@ class PresupuestoCliente {
this.summaryTableExtras.addClass('d-none');
} else {
const servicios = [];
$('.service-checkbox:checked').each(function () {
const $servicio = $(this);
servicios.push({
id: $(`label[for="${$servicio.attr('id')}"] .service-title`).text().trim() || $servicio.attr('id'),
price: $(`label[for="${$servicio.attr('id')}"] .service-price`).text().trim() || $servicio.attr('price')
});
});
const body = {
presupuesto: this.#getPresupuestoData(), // objeto JS con campos que coincidan con la entidad
servicios: this.formData.servicios.servicios // array de strings
servicios: servicios
};
$.ajax({