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
// =======================================================================

View File

@ -2,3 +2,4 @@ cart.title=Cesta de la compra
cart.empty=Tu cesta de la compra está vacía.
cart.item.presupuesto-numero=Presupuesto #
cart.precio=Precio

View File

@ -328,6 +328,7 @@ presupuesto.errores.solapas-cubierta=Seleccione si desea o no solapas en la cubi
presupuesto.errores.papel-cubierta=Seleccione el tipo de papel para la cubierta
presupuesto.errores.gramaje-cubierta=Seleccione el gramaje del papel para la cubierta
presupuesto.errores.acabado-cubierta=Seleccione el acabado de la cubierta
presupuesto.errores.error-interior=Se ha producido un error al procesar el interior. Error {0}. Por favor, contacte con soporte.
presupuesto.errores.presupuesto-no-existe=El presupuesto con ID {0} no existe.

View File

@ -160,6 +160,29 @@
}
}
function elementInViewport(el) {
if (el) {
var top = el.offsetTop;
var left = el.offsetLeft;
var width = el.offsetWidth;
var height = el.offsetHeight;
if (el.offsetParent) {
while (el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
left += el.offsetLeft;
}
}
return (
top >= window.pageYOffset &&
left >= window.pageXOffset &&
top + height <= window.pageYOffset + window.innerHeight &&
left + width <= window.pageXOffset + window.innerWidth
);
}
}
function initsAlert() {
var alerts = document.querySelectorAll('.alert.alert-dismissible');

View File

@ -7,9 +7,9 @@ export function updateEncuadernacion() {
}
}
export function updateFormato(){
export function updateFormato() {
if($('#formato-personalizado').is(':checked')) {
if ($('#formato-personalizado').is(':checked')) {
$('#summary-formato').text($('#ancho').val() + 'x' + $('#alto').val() + ' mm');
} else {
const $selected = $('#formato option:selected');
@ -21,13 +21,13 @@ export function updateFormato(){
export function updatePaginas() {
const paginas = $('#paginas').val();
$('#summary-paginas').text(paginas );
$('#summary-paginas').text(paginas);
const paginasColor = $('#paginas-color').val();
$('#summary-paginas-color').text(paginasColor );
$('#summary-paginas-color').text(paginasColor);
const paginasNegro = $('#paginas-negro').val();
$('#summary-paginas-negro').text(paginasNegro );
$('#summary-paginas-negro').text(paginasNegro);
}
export function updateTipoImpresion() {
@ -51,34 +51,36 @@ export function updatePapelInterior() {
export function updateGramajeInterior() {
const gramaje = $('input[name="gramaje-interior"]:checked');
if(gramaje.length > 0) {
if (gramaje.length > 0) {
$('#summary-gramaje-interior').text(gramaje.data('gramaje'));
}
}
export function updateTapaCubierta(){
export function updateTapaCubierta() {
const $selected = $('.tapa-cubierta input:checked');
if ($selected.length > 0) {
const resumen = $selected.closest('.tapa-cubierta').data('summary-text') || $selected.closest('.tapa-cubierta').attr('id');
$('#summary-tapa-cubierta').text(resumen);
}
if($selected.closest('.tapa-cubierta').attr('id') === 'tapaBlanda') {
if ($selected.closest('.tapa-cubierta').attr('id') === 'tapaBlanda') {
$('.tapa-blanda-row').removeClass('d-none');
$('.tapa-dura-row').addClass('d-none');
$('#summary-cubierta-solapas').text($('#sin-solapas').hasClass('selected') ? $('#sin-solapas').data('summary-text') : $('#con-solapas').data('summary-text'));
if($('#con-solapas').hasClass('selected')) {
$('#summary-tamanio-solapa-row').removeClass('d-none');
const $solapasSelected = $('.solapas-cubierta input:checked');
if ($solapasSelected.val() === 'conSolapas') {
$('#summary-cubierta-solapas').text($('#con-solapas').data('summary-text'));
$('.summary-tamanio-solapa-row').removeClass('d-none');
$('#summary-tamanio-solapa').text($('#tamanio-solapas-cubierta').val() + ' mm');
} else {
$('#summary-tamanio-solapa-row').addClass('d-none');
$('#summary-cubierta-solapas').text($('#sin-solapas').data('summary-text'));
$('.summary-tamanio-solapa-row').addClass('d-none');
$('#summary-tamanio-solapa').text('');
}
$('#summary-impresion-cubierta-row').removeClass('d-none');
$('#summary-impresion-cubierta').text($('#impresion-cubierta option:selected').text());
}
else{
else {
$('.tapa-blanda-row').addClass('d-none');
$('.tapa-dura-row').removeClass('d-none');
$('#summary-papel-guardas').text($('#papel-guardas option:selected').text());
@ -95,19 +97,24 @@ export function updatePapelCubierta() {
}
}
export function updateGramajeCubierta() {
const gramaje = $('input[name="gramaje-cubierta"]:checked');
if(gramaje.length > 0) {
$('#summary-gramaje-cubierta').text(gramaje.data('gramaje'));
export function updateGramajeCubierta(gramaje) {
if (!gramaje) {
const gramaje = $('input[name="gramaje-cubierta"]:checked');
if (gramaje.length > 0) {
$('#summary-gramaje-cubierta').text(gramaje.data('gramaje'));
}
} else {
$('#summary-gramaje-cubierta').text(gramaje);
}
}
export function updateAcabadoCubierta() {
const acabado = $('input[name="acabado-cubierta"]:checked');
if(acabado.length > 0) {
if (acabado.length > 0) {
let labelText = '';
const id = acabado.attr('id');
if (id) {
labelText = $(`label[for="${id}"]`).text().trim();
}
@ -123,20 +130,20 @@ export function updateAcabadoCubierta() {
export function updateSobreCubierta() {
if($('#sobrecubierta').hasClass('active')) {
if ($('#sobrecubierta').hasClass('active')) {
$('#summary-sobrecubierta-papel-gramaje').text($('#papel-sobrecubierta option:selected').text());
$('#summary-sobrecubierta-tamanio-solapa').text($('#tamanio-solapas-sobrecubierta').val() + ' mm');
$('#summary-sobrecubierta-acabado').text($('#sobrecubierta-acabado option:selected').text());
$('#summary-sobrecubierta-acabado').text($('#sobrecubierta-acabado option:selected').text());
}
}
export function updateFaja() {
if($('#faja').hasClass('active')) {
if ($('#faja').hasClass('active')) {
$('#summary-faja-papel-gramaje').text($('#papel-faja option:selected').text());
$('#summary-faja-alto-faja').text($('#alto-faja').val() + ' mm');
$('#summary-faja-tamanio-solapa').text($('#tamanio-solapas-faja').val() + ' mm');
$('#summary-faja-acabado').text($('#faja-acabado option:selected').text());
$('#summary-faja-acabado').text($('#faja-acabado option:selected').text());
}
}
@ -146,7 +153,7 @@ export function updateExtras() {
$tbody.empty();
// Agregar las filas de servicios extras
$('.service-checkbox:checked').each(function() {
$('.service-checkbox:checked').each(function () {
const $servicio = $(this);
const resumen = $(`label[for="${$servicio.attr('id')}"] .service-title`).text().trim() || $servicio.attr('id');
const price = $(`label[for="${$servicio.attr('id')}"] .service-price`).text().trim() || $servicio.attr('price');

View File

@ -37,7 +37,7 @@ export default class PresupuestoWizard {
ivaReducido: true,
},
interior: {
tipoImpresion: 'negro',
tipoImpresion: 'negrohq',
papelInteriorId: 3,
gramajeInterior: 80,
},
@ -135,7 +135,7 @@ export default class PresupuestoWizard {
this.divOpcionesColor = $('#div-opciones-color');
this.divPapelInterior = $('#div-papel-interior');
this.divGramajeInterior = $("#div-gramaje-interior");
this.interior_alert = $('#interior-alert');
this.interior_alert = $('#form-errors');
// pestaña cubierta
this.divSolapasCubierta = $('#div-solapas-cubierta');
@ -777,6 +777,7 @@ export default class PresupuestoWizard {
if (opcion.id === this.formData.interior.papelInteriorId) {
item.setSelected(true);
}
item.group = 'papel-interior';
this.divPapelInterior.append(item.render());
}
@ -808,6 +809,8 @@ export default class PresupuestoWizard {
const data = this.#getPresupuestoData();
this.interior_alert.addClass('d-none').find('#form-errors-alert-list').empty();
Summary.updatePapelInterior();
this.divGramajeInterior.removeClass('animate-fadeInUpBounce');
@ -832,9 +835,16 @@ export default class PresupuestoWizard {
const dataInterior = this.#getInteriorData();
this.#updateInteriorData(dataInterior);
this.#cacheFormData();
Summary.updatePapelInterior();
Summary.updateGramajeInterior();
}).fail((xhr, status, error) => {
this.interior_alert.removeClass('d-none');
const errors = xhr.responseJSON;
if (errors && typeof errors === 'object') {
this.interior_alert.find('#form-errors-alert-list').append(`<li>${errors.message}</li>`);
}
console.error("Error al obtener los gramajes de interior: ", xhr.responseText);
});
});
@ -871,6 +881,7 @@ export default class PresupuestoWizard {
let data = this.#getPresupuestoData();
const id = e.currentTarget.id;
this.interior_alert.addClass('d-none').find('#form-errors-alert-list').empty();
$.ajax({
url: '/presupuesto/public/validar/interior',
@ -928,18 +939,16 @@ export default class PresupuestoWizard {
error: (xhr, status, error) => {
this.interior_alert.removeClass('d-none');
this.interior_alert.find('#inside-alert-list').empty();
this.interior_alert.find('#form-errors-alert-list').empty();
const errors = xhr.responseJSON;
if (errors && typeof errors === 'object') {
if (!this.DEBUG && xhr.responseJSON.error && xhr.responseJSON.error == 'Internal Server Error') {
console.error("Error al validar los datos generales. Internal Server Error");
return;
}
Object.values(errors).forEach(errorMsg => {
this.interior_alert.find('#inside-alert-list').append(`<li>${errorMsg}</li>`);
});
this.interior_alert.find('#form-errors-alert-list').append(`<li>${errors.message}</li>`);
} else {
this.interior_alert.find('#inside-alert-list').append('<li>Error desconocido. Por favor, inténtelo de nuevo más tarde.</li>');
this.interior_alert.find('#form-errors-alert-list').append('<li>Error desconocido. Por favor, inténtelo de nuevo más tarde.</li>');
}
$(window).scrollTop(0);
}
@ -1144,6 +1153,7 @@ export default class PresupuestoWizard {
}).done((data) => {
const gramajes = data.opciones_gramaje_cubierta;
this.divGramajeCubierta.empty();
this.#addGramajesCubierta(gramajes);
this.divGramajeCubierta.addClass('animate-fadeInUpBounce');
@ -1152,6 +1162,7 @@ export default class PresupuestoWizard {
this.#cacheFormData();
Summary.updatePapelCubierta();
Summary.updateGramajeCubierta();
}).fail((xhr, status, error) => {
@ -1165,7 +1176,7 @@ export default class PresupuestoWizard {
const gramaje = parseInt($('#' + inputId).data('gramaje'));
this.formData.cubierta.gramajeCubierta = gramaje;
this.#cacheFormData();
Summary.updateGramajeCubierta();
Summary.updateGramajeCubierta(gramaje);
});
$(document).on('change', '.datos-cubierta', (e) => {
@ -1701,7 +1712,7 @@ export default class PresupuestoWizard {
this.divExtras.append(item.render());
}
if(!this.formData.servicios.servicios.includes(s => s.id === "ferro-digital")) {
if (!this.formData.servicios.servicios.includes(s => s.id === "ferro-digital")) {
this.formData.servicios.servicios.push({
id: "ferro-digital",
label: "Ferro Digital",

View File

@ -36,9 +36,9 @@
<!-- Precio o totales (si los tienes) -->
<div class="col-sm-auto text-end">
<p class="text-muted mb-1">Precio estimado</p>
<p class="text-muted mb-1" th:text="#{cart.precio}">Precio</p>
<h5 class="fs-14 mb-0">
<span th:text="${item.price != null ? #numbers.formatDecimal(item.price, 1, 2) : '-'}">0,00</span>
<span th:text="${item.total != null ? item.total : '-'}">0,00</span>
</h5>
</div>
</div>
@ -50,7 +50,8 @@
<div class="d-flex flex-wrap my-n1">
<!-- Botón eliminar -->
<div>
<form th:action="@{|/cart/${item.id}/remove|}" method="post" class="d-inline">
<form th:action="@{|/cart/${item.cartItemId}/remove|}" method="post" class="d-inline">
<input type="hidden" name="_method" value="delete" />
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
<button type="submit" class="btn btn-sm btn-link text-body p-1 px-2">
<i class="ri-delete-bin-fill text-muted align-bottom me-1"></i> Eliminar
@ -59,16 +60,7 @@
</div>
</div>
</div>
<div class="col-sm-auto text-end">
<div class="d-flex align-items-center gap-2 text-muted">
<div>Total:</div>
<h5 class="fs-14 mb-0">
<span
th:text="${item.price != null ? #numbers.formatDecimal(item.price, 1, 2) : '-'}">0,00</span>
</h5>
</div>
</div>
</div>
</div>
</div>

View File

@ -32,6 +32,8 @@
</nav>
</div>
<div class="container-fluid">
<div th:if="${items.isEmpty()}">
<div class="alert alert-info" role="alert" th:text="#{cart.empty}"></div>

View File

@ -95,7 +95,7 @@
</td>
<td id="summary-cubierta-solapas" class="text-end data-summary" data-id-summary="cubierta-solapas"></td>
</tr>
<tr class="tapa-blanda-row d-none">
<tr class="tapa-blanda-row summary-tamanio-solapa-row d-none">
<td class="ps-3">
<span class="ps-3" th:text="#{presupuesto.tamanio-solapa}"></span>
</td>