trabajando en la vista del carro de la compra

This commit is contained in:
2025-10-14 21:52:25 +02:00
parent a33ba3256b
commit 9f33db4055
12 changed files with 528 additions and 76 deletions

View File

@ -33,7 +33,7 @@ export default class PresupuestoWizard {
paginasColor: 0,
posicionPaginasColor: '',
tipoEncuadernacion: 'fresado',
entregaTipo: 'peninsula',
entregaTipo: 'peninsula',
ivaReducido: true,
},
interior: {
@ -254,67 +254,32 @@ export default class PresupuestoWizard {
if (this.opts.canSave) {
$('.guardar-presupuesto').on('click', async () => {
const alert = $('#form-errors');
const servicios = [];
const payload = {
id: this.opts.presupuestoId,
mode: this.opts.mode,
presupuesto: this.#getPresupuestoData(),
servicios: this.formData.servicios.servicios,
datosMaquetacion: this.formData.servicios.datosMaquetacion,
datosMarcapaginas: this.formData.servicios.datosMarcapaginas,
cliente_id: $('#cliente_id').val() || null,
};
try {
alert.addClass('d-none').find('ul').empty();
$.ajax({
url: '/presupuesto/api/save',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(payload)
}).then((data) => {
Swal.fire({
icon: 'success',
title: window.languageBundle?.get('presupuesto.exito.guardado') || 'Guardado',
timer: 1800,
buttonsStyling: false,
customClass: {
confirmButton: 'btn btn-secondary me-2', // clases para el botón confirmar
cancelButton: 'btn btn-light' // clases para cancelar
},
showConfirmButton: false
});
// opcional: actualizar window.PRESUPUESTO_ID/resumen con el id devuelto
if (data.id) this.opts.presupuestoId = data.id;
}).catch((xhr, status, error) => {
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 => {
alert.find('ul').append(`<li>${errorMsg}</li>`);
});
alert.removeClass('d-none');
} else {
alert.find('ul').append('<li>Error desconocido. Por favor, inténtelo de nuevo más tarde.</li>');
$(window).scrollTop(0);
alert.removeClass('d-none');
}
window.scrollTo({ top: 0, behavior: 'smooth' });
});
} catch (e) {
const success = await this.#guardarPresupuesto();
if (success) {
Swal.fire({
icon: 'error',
title: window.languageBundle?.get('presupuesto.add.error.save.title') || 'Error',
text: e?.message || '',
icon: 'success',
title: window.languageBundle?.get('presupuesto.exito.guardado') || 'Guardado',
timer: 1800,
buttonsStyling: false,
customClass: {
confirmButton: 'btn btn-secondary me-2', // clases para el botón confirmar
cancelButton: 'btn btn-light' // clases para cancelar
},
showConfirmButton: false
});
}
});
$('.add-cart-btn').on('click', async () => {
const success = await this.#guardarPresupuesto();
if (success) {
await $.ajax({
url: `/cart/add/${this.opts.presupuestoId}`,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
});
}
});
@ -322,6 +287,68 @@ export default class PresupuestoWizard {
}
async #guardarPresupuesto() {
const alert = $('#form-errors');
const servicios = [];
const payload = {
id: this.opts.presupuestoId,
mode: this.opts.mode,
presupuesto: this.#getPresupuestoData(),
servicios: this.formData.servicios.servicios,
datosMaquetacion: this.formData.servicios.datosMaquetacion,
datosMarcapaginas: this.formData.servicios.datosMarcapaginas,
cliente_id: $('#cliente_id').val() || null,
};
try {
alert.addClass('d-none').find('ul').empty();
return await $.ajax({
url: '/presupuesto/api/save',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(payload)
}).then((data) => {
if (data.id) this.opts.presupuestoId = data.id;
return true;
}).catch((xhr, status, error) => {
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 => {
alert.find('ul').append(`<li>${errorMsg}</li>`);
});
alert.removeClass('d-none');
} else {
alert.find('ul').append('<li>Error desconocido. Por favor, inténtelo de nuevo más tarde.</li>');
$(window).scrollTop(0);
alert.removeClass('d-none');
}
window.scrollTo({ top: 0, behavior: 'smooth' });
return false;
});
} catch (e) {
Swal.fire({
icon: 'error',
title: window.languageBundle?.get('presupuesto.add.error.save.title') || 'Error',
text: e?.message || '',
buttonsStyling: false,
customClass: {
confirmButton: 'btn btn-secondary me-2', // clases para el botón confirmar
cancelButton: 'btn btn-light' // clases para cancelar
},
});
return false;
}
}
#cacheFormData() {
if (!this.opts.useSessionCache) return;
sessionStorage.setItem('formData', JSON.stringify(this.formData));
@ -1633,7 +1660,6 @@ export default class PresupuestoWizard {
if ($target.prop('checked')) {
this.formData.servicios.servicios.push(
$target.val(),
{
id: $target.attr('id') ?? $(`label[for="${$target.attr('id')}"] .service-title`).text().trim(),
label: $(`label[for="${$target.attr('id')}"] .service-title`).text().trim(),
@ -1675,6 +1701,14 @@ export default class PresupuestoWizard {
this.divExtras.append(item.render());
}
if(!this.formData.servicios.servicios.includes(s => s.id === "ferro-digital")) {
this.formData.servicios.servicios.push({
id: "ferro-digital",
label: "Ferro Digital",
units: 1,
price: 0
});
}
this.#cacheFormData();
Summary.updateExtras();
}
@ -1813,15 +1847,15 @@ export default class PresupuestoWizard {
}
$('#resumen-base').text(formateaMoneda(data.base_imponible, 2, locale));
if(data.iva_importe_4 > 0) {
if (data.iva_importe_4 > 0) {
$('#tr-resumen-iva4').removeClass('d-none');
$('#resumen-iva4').text(formateaMoneda(data.iva_importe_4, 2, locale));
}
else{
else {
$('#tr-resumen-iva4').addClass('d-none');
$('#resumen-iva4').text(formateaMoneda(0, 2, locale));
}
if(data.iva_importe_21 > 0) {
if (data.iva_importe_21 > 0) {
$('#tr-resumen-iva21').removeClass('d-none');
$('#resumen-iva21').text(formateaMoneda(data.iva_importe_21, 2, locale));
} else {