terminado carrito

This commit is contained in:
2025-10-31 11:36:03 +01:00
parent 90c191d8f8
commit 40dc719e89
13 changed files with 234 additions and 59 deletions

View File

@ -16,10 +16,10 @@ $(() => {
$(this).find('.direccion-id').attr('name', 'direcciones[' + i + '].id');
$(this).find('.direccion-cp').attr('name', 'direcciones[' + i + '].cp');
$(this).find('.direccion-pais-code3').attr('name', 'direcciones[' + i + '].paisCode3');
if($(this).find('.presupuesto-id').length > 0 && $(this).find('.presupuesto-id').val() !== null
&& $(this).find('.presupuesto-id').val() !== "")
if ($(this).find('.presupuesto-id').length > 0 && $(this).find('.presupuesto-id').val() !== null
&& $(this).find('.presupuesto-id').val() !== "")
$(this).find('.presupuesto-id').attr('name', 'direcciones[' + i + '].presupuestoId');
if($(this).find('.item-tirada').length > 0 && $(this).find('.item-tirada').val() !== null
if ($(this).find('.item-tirada').length > 0 && $(this).find('.item-tirada').val() !== null
&& $(this).find('.item-tirada').val() !== "")
$(this).find('.item-tirada').attr('name', 'direcciones[' + i + '].unidades');
});
@ -31,15 +31,28 @@ $(() => {
}).always(() => {
hideLoader();
});
checkAddressesForItems();
});
checkAddressesForItems();
function checkAddressesForItems(){
if($('#onlyOneShipment').is(':checked')){
if($("#shippingAddressesContainer .direccion-card").length === 0){
function checkAddressesForItems() {
if ($('.product').length === 0) {
$("#alert-empty").removeClass("d-none");
$('.cart-content').addClass('d-none');
return;
}
else {
$('.cart-content').removeClass('d-none');
$("#alert-empty").addClass("d-none");
// check if select2 is initialized
if ($('#select-customer').length && !$('#select-customer').hasClass('select2-hidden-accessible')) {
initMoveCartToCustomer();
}
}
if ($('#onlyOneShipment').is(':checked')) {
if ($("#shippingAddressesContainer .direccion-card").length === 0) {
$(".alert-shipment").removeClass("d-none");
$('#btn-checkout').prop('disabled', true);
return;
@ -47,42 +60,42 @@ $(() => {
$(".alert-shipment").addClass("d-none");
$('#btn-checkout').prop('disabled', false);
}
else{
else {
const items = $(".product");
let errorFound = false;
for(let i=0; i<items.length; i++){
for (let i = 0; i < items.length; i++) {
let errorFoundItem = false;
const item = $(items[i]);
const tirada = parseInt(item.find(".item-tirada").val()) || 0;
const direcciones = item.find(".direccion-card");
let totalUnidades = 0;
direcciones.each(function(){
direcciones.each(function () {
const unidades = parseInt($(this).find(".item-tirada").val()) || 0;
totalUnidades += unidades;
});
if(totalUnidades < tirada){
if (totalUnidades < tirada) {
errorFoundItem = true;
}
if(item.find(".shipping-addresses-sample")){
if (item.find(".shipping-addresses-sample")) {
const container = item.find(".shipping-addresses-sample");
if(container.find('.direccion-card').toArray().length === 0){
if (container.find('.direccion-card').toArray().length === 0) {
errorFoundItem = true;
}
}
if(errorFoundItem){
if (errorFoundItem) {
errorFound = true;
item.find(".alert-icon-shipment").removeClass("d-none");
}
else{
else {
item.find(".alert-icon-shipment").addClass("d-none");
}
}
if(errorFound){
if (errorFound) {
$(".alert-shipment").removeClass("d-none");
$('#btn-checkout').prop('disabled', true);
}
else{
else {
$(".alert-shipment").addClass("d-none");
$('#btn-checkout').prop('disabled', false);
}
@ -92,7 +105,7 @@ $(() => {
$(document).on("click", ".delete-item", async function (event) {
event.preventDefault();
const cartItemId = $(this).data("cart-item-id");
const presupuestoId = $(this).data("cart-item-id");
const card = $(this).closest('.card.product');
// CSRF (Spring Security)
@ -100,7 +113,7 @@ $(() => {
const csrfHeader = document.querySelector('meta[name="_csrf_header"]')?.content || 'X-CSRF-TOKEN';
try {
const res = await fetch(`/cart/delete/item/${cartItemId}`, {
const res = await fetch(`/cart/delete/item/${presupuestoId}`, {
method: 'DELETE',
headers: { [csrfHeader]: csrfToken }
});
@ -111,11 +124,79 @@ $(() => {
}
else {
card?.remove();
updateTotal();
$(document).trigger('updateCart');
}
} catch (err) {
console.error('Error en la solicitud:', err);
}
});
function initMoveCartToCustomer() {
if ($('#select-customer').length) {
$('#moveCart').on('click', async function (e) {
e.preventDefault();
const customerId = $('#select-customer').val();
if (!customerId) {
// set text and show alert
$('#alert-select-customer').text(window.languageBundle['cart.pass-to.customer.error'] || 'Debe seleccionar un cliente para mover la cesta.');
$('#alert-select-customer').removeClass('d-none').hide().fadeIn();
setTimeout(() => {
$('#alert-select-customer').fadeOut(function () {
$(this).addClass('d-none');
});
}, 5000);
return;
}
// CSRF (Spring Security)
const csrfToken = document.querySelector('meta[name="_csrf"]')?.content || '';
const csrfHeader = document.querySelector('meta[name="_csrf_header"]')?.content || 'X-CSRF-TOKEN';
try {
const res = await fetch(`/cart/pass-to-customer/${customerId}`, {
method: 'POST',
headers: { [csrfHeader]: csrfToken }
});
if (!res.ok) {
$('#alert-select-customer').text(window.languageBundle['cart.pass-to.customer.move.error'] || 'Error al mover la cesta de la compra');
$('#alert-select-customer').removeClass('d-none').hide().fadeIn();
setTimeout(() => {
$('#alert-select-customer').fadeOut(function () {
$(this).addClass('d-none');
});
}, 5000);
return;
}
else {
window.location.href = '/cart';
}
} catch (err) {
console.error('Error en la solicitud:', err);
$('#alert-select-customer').text(window.languageBundle['cart.errors.move-cart'] || 'Error al mover la cesta de la compra');
$('#alert-select-customer').removeClass('d-none').hide().fadeIn();
setTimeout(() => {
$('#alert-select-customer').fadeOut(function () {
$(this).addClass('d-none');
});
}, 5000);
}
});
$('#select-customer').select2({
width: '100%',
ajax: {
url: 'users/api/get-users',
dataType: 'json',
delay: 250,
},
allowClear: true
});
}
}
initMoveCartToCustomer();
});