mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-24 09:40:21 +00:00
falta el update carrito del backend
This commit is contained in:
@ -27,7 +27,12 @@
|
||||
}
|
||||
|
||||
.direccion-card {
|
||||
flex: 1 1 250px; /* ancho mínimo 250px, crece si hay espacio */
|
||||
max-width: 250px; /* opcional, para que no se estiren demasiado */
|
||||
min-width: 240px; /* protege el ancho mínimo */
|
||||
}
|
||||
flex: 1 1 350px; /* ancho mínimo 350px, crece si hay espacio */
|
||||
max-width: 350px; /* opcional, para que no se estiren demasiado */
|
||||
min-width: 340px; /* protege el ancho mínimo */
|
||||
}
|
||||
|
||||
.shipping-addresses-item { align-items: stretch; justify-content: center;}
|
||||
.shipping-addresses-sample { align-items: stretch; justify-content: center;}
|
||||
.direccion-card { display: flex; flex-direction: column; }
|
||||
.direccion-card .card-body { display: flex; flex-direction: column; }
|
||||
@ -8,15 +8,20 @@ $(() => {
|
||||
$(document).on('updateCart', () => {
|
||||
// get form and submit
|
||||
const form = $('#cartForm');
|
||||
const container = $("#onlyOneShipping").is(':checked') ? $('#shippingAddressesContainer') : $('.product');
|
||||
const container = $("#onlyOneShipment").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
|
||||
$(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() !== "")
|
||||
$(this).find('.presupuesto-id').attr('name', 'direcciones[' + i + '].presupuestoId');
|
||||
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 + '].tirada');
|
||||
});
|
||||
$.post(form.attr('action'), form.serialize(), (response) => {
|
||||
// handle response
|
||||
@ -24,12 +29,65 @@ $(() => {
|
||||
hideLoader();
|
||||
});
|
||||
updateTotal();
|
||||
checkAddressesForItems();
|
||||
});
|
||||
|
||||
updateTotal();
|
||||
|
||||
function checkAddressesForItems(){
|
||||
if($('#onlyOneShipment').is(':checked')){
|
||||
if($("#shippingAddressesContainer .direccion-card").length === 0){
|
||||
$(".alert-shipment").removeClass("d-none");
|
||||
$('#btn-checkout').prop('disabled', true);
|
||||
return;
|
||||
}
|
||||
$(".alert-shipment").addClass("d-none");
|
||||
$('#btn-checkout').prop('disabled', false);
|
||||
}
|
||||
else{
|
||||
const items = $(".product");
|
||||
let errorFound = false;
|
||||
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(){
|
||||
const unidades = parseInt($(this).find(".item-tirada").val()) || 0;
|
||||
totalUnidades += unidades;
|
||||
});
|
||||
if(totalUnidades < tirada){
|
||||
errorFoundItem = true;
|
||||
}
|
||||
|
||||
if(item.find(".shipping-addresses-sample")){
|
||||
const container = item.find(".shipping-addresses-sample");
|
||||
if(container.find('.direccion-card').toArray().length === 0){
|
||||
errorFoundItem = true;
|
||||
}
|
||||
}
|
||||
if(errorFoundItem){
|
||||
errorFound = true;
|
||||
item.find(".alert-icon-shipment").removeClass("d-none");
|
||||
}
|
||||
else{
|
||||
item.find(".alert-icon-shipment").addClass("d-none");
|
||||
}
|
||||
}
|
||||
if(errorFound){
|
||||
$(".alert-shipment").removeClass("d-none");
|
||||
$('#btn-checkout').prop('disabled', true);
|
||||
}
|
||||
else{
|
||||
$(".alert-shipment").addClass("d-none");
|
||||
$('#btn-checkout').prop('disabled', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function updateTotal() {
|
||||
const items = $(".product");
|
||||
/*const items = $(".product");
|
||||
let iva4 = 0;
|
||||
let iva21 = 0;
|
||||
let base = 0;
|
||||
@ -56,7 +114,7 @@ $(() => {
|
||||
$("#tr-iva-21").hide();
|
||||
}
|
||||
const total = base + iva4 + iva21;
|
||||
$("#total-cesta").text(formateaMoneda(total));
|
||||
$("#total-cesta").text(formateaMoneda(total));*/
|
||||
}
|
||||
|
||||
$(document).on("click", ".delete-item", async function (event) {
|
||||
|
||||
@ -5,7 +5,7 @@ $(() => {
|
||||
// Si usas jQuery AJAX:
|
||||
$(document).ajaxStart(showLoader).ajaxStop(hideLoader);
|
||||
|
||||
$("#onlyOneShipping").on('change', function () {
|
||||
$("#onlyOneShipment").on('change', function () {
|
||||
if ($(this).is(':checked')) {
|
||||
$('.nav-product').hide();
|
||||
document.querySelectorAll('.card.product').forEach(card => {
|
||||
@ -13,19 +13,19 @@ $(() => {
|
||||
if (detailsBtn) new bootstrap.Tab(detailsBtn).show();
|
||||
});
|
||||
$('#shippingAddressesContainer').empty().show();
|
||||
$('.div-shipping-product').toArray().forEach(element => {
|
||||
$('.shipping-addresses-item').toArray().forEach(element => {
|
||||
$(element).empty().hide();
|
||||
});
|
||||
$('#addOrderAddress').show();
|
||||
} else {
|
||||
$('.nav-product').show();
|
||||
$('#shippingAddressesContainer').empty().hide();
|
||||
$('.div-shipping-product').toArray().forEach(element => {
|
||||
$('.shipping-addresses-item').toArray().forEach(element => {
|
||||
$(element).empty().show();
|
||||
});
|
||||
$('.div-shipping-product').empty().show();
|
||||
$('#addOrderAddress').hide();
|
||||
}
|
||||
$(document).trigger('updateCart');
|
||||
});
|
||||
|
||||
$(document).on('click', '.btn-delete-direccion', function (e) {
|
||||
@ -33,9 +33,39 @@ $(() => {
|
||||
const $card = $(this).closest('.direccion-card');
|
||||
const $div = $card.parent();
|
||||
$card.remove();
|
||||
if($div.hasClass('shipping-order-address')){
|
||||
if ($div.hasClass('shipping-order-address')) {
|
||||
$('#addOrderAddress').show();
|
||||
}
|
||||
else {
|
||||
$div.trigger('direcciones:actualizadas');
|
||||
}
|
||||
$(document).trigger('updateCart');
|
||||
});
|
||||
|
||||
//btn-edit-direccion
|
||||
$(document).on('click', '.btn-edit-direccion', function (e) {
|
||||
e.preventDefault();
|
||||
const $card = $(this).closest('.direccion-card');
|
||||
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) + parseInt($card.find('.item-tirada').val() || 0);
|
||||
const units = getUnitsFromUser(remainingTirada);
|
||||
units.then(unidades => {
|
||||
if (unidades) {
|
||||
$card.find('.item-tirada').val(unidades);
|
||||
$card.find('#units-text').each(function () {
|
||||
if (unidades == 1) {
|
||||
$(this).text(`${unidades} ${window.languageBundle['cart.shipping.ud'] || 'unidad'}`);
|
||||
} else {
|
||||
$(this).text(`${unidades} ${window.languageBundle['cart.shipping.uds'] || 'unidades'}`);
|
||||
}
|
||||
});
|
||||
container.trigger('direcciones:actualizadas');
|
||||
$(document).trigger('updateCart');
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
const language = document.documentElement.lang || 'es-ES';
|
||||
@ -55,8 +85,8 @@ $(() => {
|
||||
});
|
||||
|
||||
$('#addOrderAddress').on('click', async () => {
|
||||
if ($('#onlyOneShipping').is(':checked')) {
|
||||
if(await seleccionarDireccionEnvio()){
|
||||
if ($('#onlyOneShipment').is(':checked')) {
|
||||
if (await seleccionarDireccionEnvio()) {
|
||||
$('#addOrderAddress').hide();
|
||||
}
|
||||
}
|
||||
@ -71,6 +101,14 @@ $(() => {
|
||||
seleccionarDireccionEnvio(presupuestoId, remainingTirada, container);
|
||||
});
|
||||
|
||||
$(document).on('click', '.btn-add-shipping-sample', function () {
|
||||
const presupuestoId = $(this).closest('.product').find('.item-presupuesto-id').val();
|
||||
const container = $(this).closest('.product').find('.shipping-addresses-sample');
|
||||
seleccionarDireccionEnvio(presupuestoId, null, container);
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function seleccionarDireccionEnvio(presupuestoId = null, tirada = null, container = null) {
|
||||
|
||||
const { value: direccionId, isDenied } = await Swal.fire({
|
||||
@ -144,11 +182,6 @@ $(() => {
|
||||
},
|
||||
escapeMarkup: m => m
|
||||
});
|
||||
|
||||
// (Opcional) Prefijar valor si ya tienes una dirección elegida:
|
||||
// const preselected = { id: '123', text: 'Oficina Central — Madrid' };
|
||||
// const option = new Option(preselected.text, preselected.id, true, true);
|
||||
// $select.append(option).trigger('change');
|
||||
},
|
||||
|
||||
preConfirm: () => {
|
||||
@ -182,29 +215,9 @@ $(() => {
|
||||
}
|
||||
|
||||
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 (tirada !== null && tirada >= 1 && direccionId) {
|
||||
|
||||
const unidadesValue = await getUnitsFromUser(tirada);
|
||||
if (unidadesValue) {
|
||||
unidades = parseInt(unidadesValue);
|
||||
} else {
|
||||
@ -216,16 +229,19 @@ $(() => {
|
||||
// Obtén el objeto completo seleccionado
|
||||
showLoader();
|
||||
let uri = `/cart/get-address/${direccionId}`;
|
||||
if (presupuestoId !== null && unidades !== null) {
|
||||
uri += `?presupuestoId=${presupuestoId}&unidades=${unidades}`;
|
||||
if (presupuestoId !== null) {
|
||||
uri += `?presupuestoId=${presupuestoId}`;
|
||||
if (tirada !== null) {
|
||||
uri += `&unidades=${unidades}`;
|
||||
}
|
||||
}
|
||||
const response = await fetch(uri);
|
||||
if (response.ok) {
|
||||
const html = await response.text();
|
||||
if(presupuestoId !== null){
|
||||
container.append(html);
|
||||
if (presupuestoId !== null) {
|
||||
container.append(html).trigger('direcciones:actualizadas');
|
||||
}
|
||||
else{
|
||||
else {
|
||||
$('#shippingAddressesContainer').append(html);
|
||||
}
|
||||
$(document).trigger('updateCart');
|
||||
@ -238,6 +254,78 @@ $(() => {
|
||||
return false;
|
||||
}
|
||||
|
||||
async function getUnitsFromUser(tirada) {
|
||||
|
||||
// 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,
|
||||
},
|
||||
inputValue: tirada,
|
||||
showCancelButton: true,
|
||||
buttonsStyling: false,
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-secondary me-2',
|
||||
cancelButton: 'btn btn-light',
|
||||
},
|
||||
confirmButtonText: window.languageBundle['app.aceptar'] || 'Aceptar',
|
||||
cancelButtonText: window.languageBundle['app.cancelar'] || 'Cancelar',
|
||||
inputValidator: (value) => {
|
||||
if (!value || isNaN(value) || value < 1 || value > tirada) {
|
||||
return window.languageBundle['cart.shipping.errors.units-error']?.replace('{max}', tirada) || `Por favor, introduzca un número válido entre 1 y ${tirada}.`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
return unidadesValue;
|
||||
}
|
||||
|
||||
function checkTotalUnits(container, tirada) {
|
||||
|
||||
const totalUnits = container.find('.direccion-card').toArray().reduce((acc, el) => {
|
||||
const unidades = parseInt($(el).find('.item-tirada').val()) || 0;
|
||||
return acc + unidades;
|
||||
}, 0);
|
||||
if (totalUnits < tirada) {
|
||||
return false;
|
||||
}
|
||||
if(container.find('.product').closest('.shipping-addresses-sample')){
|
||||
if(container.find('.direccion-card').toArray().length === 0){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$(document).on('direcciones:actualizadas', '.shipping-addresses-item', function (e) {
|
||||
|
||||
const tirada = $(this).closest('.product').find('.item-tirada').val();
|
||||
const container = $(this);
|
||||
|
||||
if (!checkTotalUnits(container, tirada)) {
|
||||
container.closest('.px-2').find('.btn-add-shipping').show();
|
||||
} else {
|
||||
container.closest('.px-2').find('.btn-add-shipping').hide();
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('direcciones:actualizadas', '.shipping-addresses-sample', function (e) {
|
||||
|
||||
const container = $(this);
|
||||
|
||||
if (container.find('.direccion-card').toArray().length === 0) {
|
||||
container.closest('.px-2').find('.btn-add-shipping-sample').show();
|
||||
}
|
||||
else {
|
||||
container.closest('.px-2').find('.btn-add-shipping-sample').hide();
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('submit', '#direccionForm', function (e) {
|
||||
e.preventDefault();
|
||||
const $form = $(this);
|
||||
|
||||
Reference in New Issue
Block a user