trabajando en envios dentro del presupuesto

This commit is contained in:
2025-10-28 22:28:27 +01:00
parent f770bd07d6
commit c272fd7b9b
8 changed files with 277 additions and 104 deletions

View File

@ -1,7 +1,31 @@
import { formateaMoneda } from '../../imprimelibros/utils.js';
import { showLoader, hideLoader } from '../loader.js';
$(() => {
$(document).ajaxStart(showLoader).ajaxStop(hideLoader);
$(document).on('updateCart', () => {
// get form and submit
const form = $('#cartForm');
const container = $("#onlyOneShipping").is(':checked') ? $('#shippingAddressesContainer') : $('.product');
// remove name from container . direccion-card
container.find('.direccion-card input[type="hidden"]').removeAttr('name');
container.find('.direccion-card').each(function (i) {
$(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 + '].pais.code3');
// añade aquí más campos si quieres que viajen
});
$.post(form.attr('action'), form.serialize(), (response) => {
// handle response
}).always(() => {
hideLoader();
});
updateTotal();
});
updateTotal();
function updateTotal() {
@ -55,7 +79,7 @@ $(() => {
console.error('Error al eliminar. Status:', res.status);
return;
}
else{
else {
card?.remove();
updateTotal();
}

View File

@ -1,5 +1,10 @@
import { showLoader, hideLoader } from '../loader.js';
$(() => {
// Si usas jQuery AJAX:
$(document).ajaxStart(showLoader).ajaxStop(hideLoader);
$("#onlyOneShipping").on('change', function () {
if ($(this).is(':checked')) {
$('.nav-product').hide();
@ -8,21 +13,29 @@ $(() => {
if (detailsBtn) new bootstrap.Tab(detailsBtn).show();
});
$('#shippingAddressesContainer').empty().show();
$('.div-shipping-product').show();
$('.div-shipping-product').toArray().forEach(element => {
$(element).empty().hide();
});
$('#addOrderAddress').show();
} else {
$('.nav-product').show();
$('#shippingAddressesContainer').hide();
$('.div-shipping-product').empty().hide();
$('#shippingAddressesContainer').empty().hide();
$('.div-shipping-product').toArray().forEach(element => {
$(element).empty().show();
});
$('.div-shipping-product').empty().show();
$('#addOrderAddress').hide();
}
});
$('#shippingAddressesContainer').on('click', '.btn-delete-direccion', function (e) {
$(document).on('click', '.btn-delete-direccion', function (e) {
e.preventDefault();
const $card = $(this).closest('.direccion-card');
const $div = $card.parent();
$card.remove();
$('#addOrderAddress').show();
if($div.hasClass('shipping-order-address')){
$('#addOrderAddress').show();
}
});
const language = document.documentElement.lang || 'es-ES';
@ -41,17 +54,24 @@ $(() => {
}
});
$('#addOrderAddress').on('click', () => {
$('#addOrderAddress').on('click', async () => {
if ($('#onlyOneShipping').is(':checked')) {
if(seleccionarDireccionEnvio()){
if(await seleccionarDireccionEnvio()){
$('#addOrderAddress').hide();
}
} else {
// Add address to multiple shipping addresses container
}
});
async function seleccionarDireccionEnvio() {
$(document).on('click', '.btn-add-shipping', function () {
const presupuestoId = $(this).closest('.product').find('.item-presupuesto-id').val();
const container = $(this).closest('.product').find('.shipping-addresses-item');
const tirada = $(this).closest('.product').find('.item-tirada').val();
const totalTirada = container.find('.item-tirada').toArray().reduce((acc, el) => acc + parseInt($(el).val() || 0), 0);
const remainingTirada = parseInt(tirada) - parseInt(totalTirada);
seleccionarDireccionEnvio(presupuestoId, remainingTirada, container);
});
async function seleccionarDireccionEnvio(presupuestoId = null, tirada = null, container = null) {
const { value: direccionId, isDenied } = await Swal.fire({
title: window.languageBundle['cart.shipping.add.title'] || 'Seleccione una dirección',
@ -161,16 +181,60 @@ $(() => {
});
}
let unidades = null;
if(tirada !== null && tirada >= 1 && direccionId){
// Swal preguntando numero de unidades a asignar con máximo de tirada, necesito guardar el valor
const { value: unidadesValue } = await Swal.fire({
title: window.languageBundle['cart.shipping.enter-units'] || 'Introduzca el número de unidades para esta dirección',
input: 'number',
inputLabel: window.languageBundle['cart.shipping.units-label']?.replace('{max}', tirada) || `Número de unidades (máximo ${tirada})`,
inputAttributes: {
min: 1,
max: tirada,
step: 1,
value: tirada
},
inputValue: 1,
showCancelButton: true,
confirmButtonText: window.languageBundle['app.ok'] || 'Aceptar',
cancelButtonText: window.languageBundle['app.cancel'] || 'Cancelar',
inputValidator: (value) => {
if (!value || isNaN(value) || value < 1 || value > tirada) {
return window.languageBundle['cart.shipping.units-error']?.replace('{max}', tirada) || `Por favor, introduzca un número válido entre 1 y ${tirada}.`;
}
return null;
}
});
if (unidadesValue) {
unidades = parseInt(unidadesValue);
} else {
// Si el usuario cancela, salir de la función
return false;
}
}
if (direccionId) {
// Obtén el objeto completo seleccionado
const response = await fetch(`/cart/get-address/${direccionId}`);
showLoader();
let uri = `/cart/get-address/${direccionId}`;
if (presupuestoId !== null && unidades !== null) {
uri += `?presupuestoId=${presupuestoId}&unidades=${unidades}`;
}
const response = await fetch(uri);
if (response.ok) {
const html = await response.text();
$('#shippingAddressesContainer').append(html);
if(presupuestoId !== null){
container.append(html);
}
else{
$('#shippingAddressesContainer').append(html);
}
$(document).trigger('updateCart');
return true;
}
hideLoader();
return false;
}
hideLoader();
return false;
}

View File

@ -0,0 +1,11 @@
export function showLoader() {
const el = document.getElementById('sectionLoader');
el.classList.remove('d-none');
el.classList.add('d-flex');
}
export function hideLoader() {
const el = document.getElementById('sectionLoader');
el.classList.remove('d-flex');
el.classList.add('d-none');
}