mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
falta el update carrito del backend
This commit is contained in:
@ -19,6 +19,9 @@ import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/cart")
|
||||
@ -48,7 +51,13 @@ public class CartController {
|
||||
"cart.shipping.select-placeholder",
|
||||
"cart.shipping.new-address",
|
||||
"cart.shipping.errors.noAddressSelected",
|
||||
"cart.shipping.enter-units",
|
||||
"cart.shipping.units-label",
|
||||
"cart.shipping.errors.units-error",
|
||||
"cart.shipping.ud",
|
||||
"cart.shipping.uds",
|
||||
"app.yes",
|
||||
"app.aceptar",
|
||||
"app.cancelar");
|
||||
|
||||
Map<String, String> translations = translationService.getTranslations(locale, keys);
|
||||
@ -56,6 +65,8 @@ public class CartController {
|
||||
|
||||
var items = service.listItems(Utils.currentUserId(principal), locale);
|
||||
model.addAttribute("items", items);
|
||||
var summary = service.getCartSummary(items, locale);
|
||||
model.addAttribute("cartSummary", summary);
|
||||
|
||||
model.addAttribute("cartId", service.getOrCreateActiveCart(Utils.currentUserId(principal)));
|
||||
return "imprimelibros/cart/cart"; // crea esta vista si quieres (tabla simple)
|
||||
@ -126,4 +137,14 @@ public class CartController {
|
||||
|
||||
return "imprimelibros/direcciones/direccionCard :: direccionCard(direccion=${direccion})";
|
||||
}
|
||||
|
||||
@PostMapping("/update/{id}")
|
||||
public String postMethodName(@PathVariable Long id, @RequestBody String entity) {
|
||||
|
||||
|
||||
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,6 @@ public class CartService {
|
||||
private final CartRepository cartRepo;
|
||||
private final CartItemRepository itemRepo;
|
||||
private final MessageSource messageSource;
|
||||
private final PresupuestoFormatter presupuestoFormatter;
|
||||
private final PresupuestoRepository presupuestoRepo;
|
||||
private final Utils utils;
|
||||
|
||||
@ -33,7 +32,6 @@ public class CartService {
|
||||
this.cartRepo = cartRepo;
|
||||
this.itemRepo = itemRepo;
|
||||
this.messageSource = messageSource;
|
||||
this.presupuestoFormatter = presupuestoFormatter;
|
||||
this.presupuestoRepo = presupuestoRepo;
|
||||
this.utils = utils;
|
||||
}
|
||||
@ -152,4 +150,29 @@ public class CartService {
|
||||
|
||||
return resumen;
|
||||
}
|
||||
|
||||
public Map<String, Object> getCartSummary(List<Map<String, Object>> cartItems, Locale locale) {
|
||||
|
||||
double base = 0.0;
|
||||
double iva4 = 0.0;
|
||||
double iva21 = 0.0;
|
||||
|
||||
for (Map<String, Object> item : cartItems) {
|
||||
Presupuesto p = presupuestoRepo.findById((Long) item.get("presupuestoId"))
|
||||
.orElseThrow(() -> new IllegalStateException("Presupuesto no encontrado: " + item.get("presupuestoId")));
|
||||
base += p.getBaseImponible().doubleValue();
|
||||
iva4 += p.getIvaImporte4().doubleValue();
|
||||
iva21 += p.getIvaImporte21().doubleValue();
|
||||
}
|
||||
|
||||
double total = base + iva4 + iva21;
|
||||
|
||||
Map<String, Object> summary = new HashMap<>();
|
||||
summary.put("base", Utils.formatCurrency(base, locale));
|
||||
summary.put("iva4", Utils.formatCurrency(iva4, locale));
|
||||
summary.put("iva21", Utils.formatCurrency(iva21, locale));
|
||||
summary.put("total", Utils.formatCurrency(total, locale));
|
||||
|
||||
return summary;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package com.imprimelibros.erp.pedido;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PedidoService {
|
||||
|
||||
public int hasDescuentoFidelidad() {
|
||||
// descuento entre el 1% y el 6% para clientes fidelidad (mas de 1500€ en el ultimo año)
|
||||
double totalGastado = 1600.0; // Ejemplo, deberías obtenerlo del historial del cliente
|
||||
if(totalGastado < 1200) {
|
||||
return 0;
|
||||
} else if(totalGastado >= 1200 && totalGastado < 1999) {
|
||||
return 1;
|
||||
} else if(totalGastado >= 2000 && totalGastado < 2999) {
|
||||
return 2;
|
||||
} else if(totalGastado >= 3000 && totalGastado < 3999) {
|
||||
return 3;
|
||||
} else if(totalGastado >= 4000 && totalGastado < 4999) {
|
||||
return 4;
|
||||
} else if(totalGastado >= 5000 && totalGastado < 9999) {
|
||||
return 5;
|
||||
} else if(totalGastado >= 10000) {
|
||||
return 6;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 0005-add-carts-onlyoneshipment
|
||||
author: jjo
|
||||
preConditions:
|
||||
onFail: MARK_RAN
|
||||
not:
|
||||
columnExists:
|
||||
tableName: carts
|
||||
columnName: only_one_shipment
|
||||
|
||||
changes:
|
||||
- addColumn:
|
||||
tableName: carts
|
||||
columns:
|
||||
- column:
|
||||
name: only_one_shipment
|
||||
type: TINYINT(1)
|
||||
defaultValueNumeric: 1
|
||||
remarks: "Si 1, el carrito tiene la misma direccion para todos los items"
|
||||
afterColumn: currency
|
||||
constraints:
|
||||
nullable: false
|
||||
|
||||
rollback:
|
||||
- dropColumn:
|
||||
tableName: carts
|
||||
columnName: only_one_shipment
|
||||
@ -0,0 +1,81 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 0006-add-cart-direcciones
|
||||
author: jjo
|
||||
preConditions:
|
||||
onFail: MARK_RAN
|
||||
not:
|
||||
tableExists:
|
||||
tableName: cart_direcciones
|
||||
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: cart_direcciones
|
||||
remarks: "Relación de direcciones y unidades por carrito/direcciones_envio"
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: BIGINT UNSIGNED AUTO_INCREMENT
|
||||
constraints:
|
||||
primaryKey: true
|
||||
primaryKeyName: pk_cart_direcciones
|
||||
|
||||
- column:
|
||||
name: direccion_id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
nullable: false
|
||||
|
||||
- column:
|
||||
name: presupuesto_id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
nullable: true
|
||||
|
||||
- column:
|
||||
name: unidades
|
||||
type: INT
|
||||
constraints:
|
||||
nullable: true
|
||||
|
||||
- createIndex:
|
||||
indexName: idx_cart_dir_direccion_id
|
||||
tableName: cart_direcciones
|
||||
columns:
|
||||
- column:
|
||||
name: direccion_id
|
||||
|
||||
- createIndex:
|
||||
indexName: idx_cart_dir_presupuesto_id
|
||||
tableName: cart_direcciones
|
||||
columns:
|
||||
- column:
|
||||
name: presupuesto_id
|
||||
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: cart_direcciones
|
||||
baseColumnNames: direccion_id
|
||||
constraintName: fk_cart_dir_direccion
|
||||
referencedTableName: direcciones
|
||||
referencedColumnNames: id
|
||||
onDelete: CASCADE
|
||||
onUpdate: CASCADE
|
||||
|
||||
- addForeignKeyConstraint:
|
||||
baseTableName: cart_direcciones
|
||||
baseColumnNames: presupuesto_id
|
||||
constraintName: fk_cart_dir_presupuesto
|
||||
referencedTableName: presupuesto
|
||||
referencedColumnNames: id
|
||||
onDelete: SET NULL
|
||||
onUpdate: CASCADE
|
||||
|
||||
rollback:
|
||||
- dropForeignKeyConstraint:
|
||||
baseTableName: cart_direcciones
|
||||
constraintName: fk_cart_dir_direccion
|
||||
- dropForeignKeyConstraint:
|
||||
baseTableName: cart_direcciones
|
||||
constraintName: fk_cart_dir_presupuesto
|
||||
- dropTable:
|
||||
tableName: cart_direcciones
|
||||
@ -6,4 +6,8 @@ databaseChangeLog:
|
||||
- include:
|
||||
file: db/changelog/changesets/0003-create-paises.yml
|
||||
- include:
|
||||
file: db/changelog/changesets/0004-create-direcciones.yml
|
||||
file: db/changelog/changesets/0004-create-direcciones.yml
|
||||
- include:
|
||||
file: db/changelog/changesets/0005-add-carts-onlyoneshipment.yml
|
||||
- include:
|
||||
file: db/changelog/changesets/0006-add-cart-direcciones.yml
|
||||
@ -13,14 +13,19 @@ cart.shipping.select-placeholder=Buscar en direcciones...
|
||||
cart.shipping.new-address=Nueva dirección
|
||||
cart.shipping.info=Todos los pedidos incluyen un envío gratuito a la Península y Baleares por línea de pedido.
|
||||
cart.shipping.order=Envío del pedido
|
||||
cart.shipping.samples=Envío de pruebas
|
||||
cart.shipping.onlyOneShipping=Todo el pedido se envía a una única dirección.
|
||||
cart.shipping.samples=Envío de prueba
|
||||
cart.shipping.onlyOneShipment=Todo el pedido se envía a una única dirección.
|
||||
cart.shipping.tirada=Tirada:
|
||||
cart.shipping.unidades=unidades
|
||||
cart.shipping.ud=ud.
|
||||
cart.shipping.uds=uds.
|
||||
cart.shipping.enter-units=Introduzca el número de unidades para esta dirección:
|
||||
cart.shipping.units-label=Número de unidades (máximo {max})
|
||||
|
||||
|
||||
cart.shipping.errors.units-error=Por favor, introduzca un número válido entre 1 y {max}.
|
||||
cart.shipping.errors.noAddressSelected=Debe seleccionar una dirección de envío para el pedido.
|
||||
cart.shipping.errors.fillAddressesItems=Debe seleccionar una dirección de envío para cada artículo de la cesta.
|
||||
|
||||
cart.resumen.title=Resumen de la cesta
|
||||
cart.resumen.base=Base imponible:
|
||||
|
||||
@ -6,7 +6,7 @@ checkout.payment=Método de pago
|
||||
checkout.shipping.info=Todos los pedidos incluyen un envío gratuito a la Península y Baleares por línea de pedido.
|
||||
checkout.shipping.order=Envío del pedido
|
||||
checkout.shipping.samples=Envío de pruebas
|
||||
checkout.shipping.onlyOneShipping=Todo el pedido se envía a una única dirección.
|
||||
checkout.shipping.onlyOneShipment=Todo el pedido se envía a una única dirección.
|
||||
|
||||
|
||||
checkout.summary.presupuesto=#Presupuesto
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
<div th:fragment="cartContent(items, cartId)" class="cart-content container-fluid row gy-4">
|
||||
|
||||
<div id="sectionLoader" class="position-absolute top-0 start-0 w-100 h-100 d-none justify-content-center align-items-center
|
||||
bg-body bg-opacity-75" style="z-index:10;">
|
||||
<div class="spinner-border" role="status" style="width:2.5rem;height:2.5rem;">
|
||||
<span class="visually-hidden">Cargando…</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div th:if="${items.isEmpty()}">
|
||||
<div class="alert alert-info" role="alert" th:text="#{cart.empty}"></div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-danger alert-shipment d-none" role="alert"
|
||||
th:text="#{cart.shipping.errors.fillAddressesItems}"></div>
|
||||
|
||||
<form id="cartForm" th:action="${'/cart/update/' + cartId}" method="POST" class="col-xl-8 col-12">
|
||||
|
||||
<input type="hidden" name="id" th:value="${cartId}" />
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<p th:text="#{cart.shipping.info}"></p>
|
||||
<div
|
||||
class="form-check form-switch form-switch-custom form-switch-presupuesto mb-3 d-flex align-items-center">
|
||||
<input type="checkbox" class="form-check-input datos-generales-data me-2" id="onlyOneShipment"
|
||||
name="only_one_shipment" checked />
|
||||
<label for="onlyOneShipment" class="form-label d-flex align-items-center mb-0">
|
||||
<span th:text="#{cart.shipping.onlyOneShipment}" class="me-2"></span>
|
||||
</label>
|
||||
</div>
|
||||
<button type="button" class="btn btn-secondary" id="addOrderAddress"
|
||||
th:text="#{cart.shipping.add}">Añadir dirección</button>
|
||||
|
||||
<div id="shippingAddressesContainer" class="shipping-order-address d-flex flex-wrap gap-3 mt-4"></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div th:each="item : ${items}" th:insert="~{imprimelibros/cart/_cartItem :: cartItem(${item})}">
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div th:replace="~{imprimelibros/cart/_cartSummary :: cartSummary(${cartSummary})}"></div>
|
||||
|
||||
</div>
|
||||
@ -27,6 +27,8 @@
|
||||
<i
|
||||
class="ri-truck-line fs-5 p-1 bg-soft-primary text-primary rounded-circle align-middle me-2"></i>
|
||||
<label class="fs-13 my-2" th:text="#{cart.tabs.envio}">Envío</label>
|
||||
<i
|
||||
class="ri-error-warning-line fs-5 p-1 bg-soft-danger rounded-circle text-danger align-middle me-2 d-none alert-icon-shipment"></i>
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
@ -143,7 +145,12 @@
|
||||
</div>
|
||||
|
||||
<div class="ribbon-content mt-4">
|
||||
<div class="px-2 mb-2">
|
||||
<button type="button" class="btn btn-secondary btn-add-shipping-sample"
|
||||
th:text="#{cart.shipping.add}">Añadir dirección</button>
|
||||
|
||||
<div class="shipping-addresses-sample d-flex flex-wrap gap-3 mt-4"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
<div th:fragment="cartSummary(summary)" class="col-xl-4">
|
||||
<div class="sticky-side-div">
|
||||
<div class="card">
|
||||
<div class="card-header border-bottom-dashed">
|
||||
<h5 th:text="#{cart.resumen.title}" class="card-title mb-0"></h5>
|
||||
</div>
|
||||
<div class="card-body pt-2">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-borderless mb-0">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span th:text="#{cart.resumen.base}"></span></td>
|
||||
<td class="text-end" id="base-cesta" th:text="${summary.base}"></td>
|
||||
</tr>
|
||||
<tr id="tr-iva-4">
|
||||
<td><span th:text="#{cart.resumen.iva-4}"></span> : </td>
|
||||
<td class="text-end" id="iva-4-cesta" th:text="${summary.iva4}"></td>
|
||||
</tr>
|
||||
<tr id="tr-iva-21">
|
||||
<td><span th:text="#{cart.resumen.iva-21}"></span> : </td>
|
||||
<td class="text-end" id="iva-21-cesta" th:text="${summary.iva21}"></td>
|
||||
</tr>
|
||||
<tr class="table-active">
|
||||
<th><span th:text="#{cart.resumen.total}"></span>:</th>
|
||||
<td class="text-end">
|
||||
<span id="total-cesta" class="fw-semibold" th:text="${summary.total}"></span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="button" class="btn btn-secondary w-100 mt-2" id="btn-checkout"
|
||||
th:onclick="location.href='/checkout'" th:text="#{cart.resumen.tramitar}">Checkout</button>
|
||||
</div>
|
||||
<!-- end table-responsive -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert border-dashed alert-danger" role="alert">
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="ms-2">
|
||||
<h5 class="fs-14 text-danger fw-semibold" th:text="#{cart.resumen.fidelizacion}"></h5>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- end stickey -->
|
||||
|
||||
</div>
|
||||
@ -36,100 +36,7 @@
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<div class="container-fluid row gy-4">
|
||||
|
||||
<div id="sectionLoader" class="position-absolute top-0 start-0 w-100 h-100 d-none justify-content-center align-items-center
|
||||
bg-body bg-opacity-75" style="z-index:10;">
|
||||
<div class="spinner-border" role="status" style="width:2.5rem;height:2.5rem;">
|
||||
<span class="visually-hidden">Cargando…</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div th:if="${items.isEmpty()}">
|
||||
<div class="alert alert-info" role="alert" th:text="#{cart.empty}"></div>
|
||||
</div>
|
||||
|
||||
<form id="cartForm" th:action="${'/cart/update/' + cartId}" method="POST" class="col-xl-8 col-12">
|
||||
|
||||
<input type="hidden" name="id" th:value="${cartId}" />
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<p th:text="#{cart.shipping.info}"></p>
|
||||
<div
|
||||
class="form-check form-switch form-switch-custom form-switch-presupuesto mb-3 d-flex align-items-center">
|
||||
<input type="checkbox" class="form-check-input datos-generales-data me-2"
|
||||
id="onlyOneShipping" name="onlyOneShipping" checked />
|
||||
<label for="onlyOneShipping" class="form-label d-flex align-items-center mb-0">
|
||||
<span th:text="#{cart.shipping.onlyOneShipping}" class="me-2"></span>
|
||||
</label>
|
||||
</div>
|
||||
<button type="button" class="btn btn-secondary" id="addOrderAddress"
|
||||
th:text="#{cart.shipping.add}">Añadir dirección</button>
|
||||
|
||||
<div id="shippingAddressesContainer" class="shipping-order-address d-flex flex-wrap gap-3 mt-4"></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div th:each="item : ${items}" th:insert="~{imprimelibros/cart/_cartItem :: cartItem(${item})}">
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="col-xl-4">
|
||||
<div class="sticky-side-div">
|
||||
<div class="card">
|
||||
<div class="card-header border-bottom-dashed">
|
||||
<h5 th:text="#{cart.resumen.title}" class="card-title mb-0"></h5>
|
||||
</div>
|
||||
<div class="card-body pt-2">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-borderless mb-0">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><span th:text="#{cart.resumen.base}"></span></td>
|
||||
<td class="text-end" id="base-cesta"></td>
|
||||
</tr>
|
||||
<tr id="tr-iva-4">
|
||||
<td><span th:text="#{cart.resumen.iva-4}"></span> : </td>
|
||||
<td class="text-end" id="iva-4-cesta"></td>
|
||||
</tr>
|
||||
<tr id="tr-iva-21">
|
||||
<td><span th:text="#{cart.resumen.iva-21}"></span> : </td>
|
||||
<td class="text-end" id="iva-21-cesta"></td>
|
||||
</tr>
|
||||
<tr class="table-active">
|
||||
<th><span th:text="#{cart.resumen.total}"></span>:</th>
|
||||
<td class="text-end">
|
||||
<span id="total-cesta" class="fw-semibold">
|
||||
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="button" class="btn btn-secondary w-100 mt-2"
|
||||
th:onclick="location.href='/checkout'"
|
||||
th:text="#{cart.resumen.tramitar}">Checkout</button>
|
||||
</div>
|
||||
<!-- end table-responsive -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert border-dashed alert-danger" role="alert">
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="ms-2">
|
||||
<h5 class="fs-14 text-danger fw-semibold"
|
||||
th:text="#{cart.resumen.fidelizacion}"></h5>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- end stickey -->
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div th:insert="~{imprimelibros/cart/_cartContent :: cartContent(${items}, ${cartId})}"></div>
|
||||
|
||||
</th:block>
|
||||
|
||||
|
||||
@ -12,10 +12,10 @@
|
||||
<p th:text="#{checkout.shipping.info}"></p>
|
||||
<div
|
||||
class="form-check form-switch form-switch-custom form-switch-presupuesto mb-3 d-flex align-items-center">
|
||||
<input type="checkbox" class="form-check-input datos-generales-data me-2" id="onlyOneShipping"
|
||||
name="onlyOneShipping" checked />
|
||||
<label for="onlyOneShipping" class="form-label d-flex align-items-center mb-0">
|
||||
<span th:text="#{checkout.shipping.onlyOneShipping}" class="me-2"></span>
|
||||
<input type="checkbox" class="form-check-input datos-generales-data me-2" id="onlyOneShipment"
|
||||
name="onlyOneShipment" checked />
|
||||
<label for="onlyOneShipment" class="form-label d-flex align-items-center mb-0">
|
||||
<span th:text="#{checkout.shipping.onlyOneShipment}" class="me-2"></span>
|
||||
</label>
|
||||
</div>
|
||||
<button type="button" class="btn btn-secondary" id="addOrderAddress"
|
||||
|
||||
@ -8,31 +8,36 @@
|
||||
<input type="hidden" class="direccion-pais-code3" th:value="${direccion.pais.code3}" />
|
||||
<input type="hidden" class="item-tirada" th:value="${unidades != null ? unidades : ''}" />
|
||||
|
||||
<div class="row g-3 align-items-start">
|
||||
<div class="row g-3 align-items-start flex-nowrap">
|
||||
<div class="col">
|
||||
<span class="mb-2 fw-semibold d-block text-muted text-uppercase" th:text="${direccion.alias}"></span>
|
||||
<span class="fs-14 mb-1 d-block" th:text="${direccion.att}"></span>
|
||||
<span class="text-muted fw-normal text-wrap mb-1 d-block" th:text="${direccion.direccion}"></span>
|
||||
<span class="text-muted fw-normal d-block" th:text="${pais}"></span>
|
||||
<span class="text-muted fw-normal d-block"
|
||||
<span class="mb-2 fw-semibold d-block text-muted text-uppercase text-break" th:text="${direccion.alias}"></span>
|
||||
<span class="fs-14 mb-1 d-block text-break" th:text="${direccion.att}"></span>
|
||||
<span class="text-muted fw-normal text-wrap mb-1 d-block text-break" th:text="${direccion.direccion}"></span>
|
||||
<span class="text-muted fw-normal d-block text-break" th:text="${pais}"></span>
|
||||
<span class="text-muted fw-normal d-block text-break"
|
||||
th:text="#{'direcciones.telefono'} + ': ' + ${direccion.telefono}"></span>
|
||||
</div>
|
||||
<div th:if="${unidades != null}" class="col-auto ms-auto text-end">
|
||||
<span class="mb-2 fw-semibold d-block text-muted text-uppercase" th:if="${unidades == 1}"
|
||||
<span id="units-text" class="mb-2 fw-semibold d-block text-muted text-uppercase" th:if="${unidades == 1}"
|
||||
th:text="|${unidades} #{cart.shipping.ud}|"></span>
|
||||
|
||||
<!-- plural -->
|
||||
<span class="mb-2 fw-semibold d-block text-muted text-uppercase" th:unless="${unidades == 1}"
|
||||
<span id="units-text" class="mb-2 fw-semibold d-block text-muted text-uppercase" th:unless="${unidades == 1}"
|
||||
th:text="|${unidades} #{cart.shipping.uds}|"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="d-flex flex-wrap align-items-center gap-2 px-2 py-1 bg-light rounded-bottom border-top mt-auto actions-row">
|
||||
class="d-flex flex-wrap align-items-center gap-2 py-1 bg-light rounded-bottom border-top mt-auto actions-row">
|
||||
<a href="javascript:void(0)" class="d-block text-body p-1 px-2 btn-delete-direccion"
|
||||
data-id="${this._esc(d.id ?? '')}">
|
||||
<i class="ri-delete-bin-fill text-muted align-bottom me-1"></i>
|
||||
<span th:text="#{'direcciones.btn.delete'}">Eliminar</span>
|
||||
</a>
|
||||
<a th:if="${unidades != null}" href="javascript:void(0)" class="d-block text-body p-1 px-2 btn-edit-direccion"
|
||||
data-id="${this._esc(d.id ?? '')}">
|
||||
<i class="ri-pencil-fill text-muted align-bottom me-1"></i>
|
||||
<span th:text="#{'direcciones.btn.edit'}">Editar</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user