mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
falta mover la cesta a cliente
This commit is contained in:
@ -20,6 +20,7 @@ import com.imprimelibros.erp.cart.dto.UpdateCartRequest;
|
||||
import com.imprimelibros.erp.common.Utils;
|
||||
import com.imprimelibros.erp.direcciones.DireccionService;
|
||||
import com.imprimelibros.erp.externalApi.skApiClient;
|
||||
import com.imprimelibros.erp.pedido.PedidoService;
|
||||
import com.imprimelibros.erp.presupuesto.PresupuestoRepository;
|
||||
|
||||
@Service
|
||||
@ -32,11 +33,12 @@ public class CartService {
|
||||
private final Utils utils;
|
||||
private final DireccionService direccionService;
|
||||
private final skApiClient skApiClient;
|
||||
private final PedidoService pedidoService;
|
||||
|
||||
public CartService(CartRepository cartRepo, CartItemRepository itemRepo,
|
||||
MessageSource messageSource, PresupuestoFormatter presupuestoFormatter,
|
||||
PresupuestoRepository presupuestoRepo, Utils utils, DireccionService direccionService,
|
||||
skApiClient skApiClient) {
|
||||
skApiClient skApiClient, PedidoService pedidoService) {
|
||||
this.cartRepo = cartRepo;
|
||||
this.itemRepo = itemRepo;
|
||||
this.messageSource = messageSource;
|
||||
@ -44,6 +46,7 @@ public class CartService {
|
||||
this.utils = utils;
|
||||
this.direccionService = direccionService;
|
||||
this.skApiClient = skApiClient;
|
||||
this.pedidoService = pedidoService;
|
||||
}
|
||||
|
||||
/** Devuelve el carrito activo o lo crea si no existe. */
|
||||
@ -180,6 +183,7 @@ public class CartService {
|
||||
|
||||
for (CartItem item : items) {
|
||||
Presupuesto p = item.getPresupuesto();
|
||||
Double peso = p.getPeso() != null ? p.getPeso().doubleValue() : 0.0;
|
||||
base += p.getBaseImponible().doubleValue();
|
||||
iva4 += p.getIvaImporte4().doubleValue();
|
||||
iva21 += p.getIvaImporte21().doubleValue();
|
||||
@ -190,43 +194,79 @@ public class CartService {
|
||||
Boolean freeShipment = direccionService.checkFreeShipment(cd.getDireccion().getCp(),
|
||||
cd.getDireccion().getPaisCode3()) && !cd.getIsPalets();
|
||||
if (!freeShipment) {
|
||||
try {
|
||||
Map<String, Object> data = Map.of(
|
||||
"cp", cd.getDireccion().getCp(),
|
||||
"pais_code3", cd.getDireccion().getPaisCode3(),
|
||||
"peso", p.getPeso() != null ? p.getPeso() : 0,
|
||||
"unidades", p.getSelectedTirada(),
|
||||
"palets", cd.getIsPalets() ? 1 : 0);
|
||||
var shipmentCost = skApiClient.getCosteEnvio(data, locale);
|
||||
if (shipmentCost != null && shipmentCost.get("data") != null) {
|
||||
shipment += (Double) shipmentCost.get("data");
|
||||
iva21 += ((Double) shipmentCost.get("data")) * 0.21;
|
||||
} else {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Integer unidades = p.getSelectedTirada();
|
||||
Map<String, Object> res = getShippingCost(cd, peso, unidades, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
|
||||
}
|
||||
// si tiene prueba de envio, hay que añadir el coste
|
||||
if (p.getServiciosJson() != null && p.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
try {
|
||||
Map<String, Object> data = Map.of(
|
||||
"cp", cd.getDireccion().getCp(),
|
||||
"pais_code3", cd.getDireccion().getPaisCode3(),
|
||||
"peso", p.getPeso() != null ? p.getPeso() : 0,
|
||||
"unidades", 1,
|
||||
"palets", cd.getIsPalets() ? 1 : 0);
|
||||
var shipmentCost = skApiClient.getCosteEnvio(data, locale);
|
||||
if (shipmentCost != null && shipmentCost.get("data") != null) {
|
||||
shipment += (Double) shipmentCost.get("data");
|
||||
iva21 += ((Double) shipmentCost.get("data")) * 0.21;
|
||||
} else {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Map<String, Object> res = getShippingCost(cd, peso, 1, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// envio por cada presupuesto
|
||||
// buscar la direccion asignada a este presupuesto
|
||||
if (direcciones == null)
|
||||
continue;
|
||||
List<CartDireccion> cd_presupuesto = direcciones.stream()
|
||||
.filter(d -> d.getPresupuesto() != null && d.getPresupuesto().getId().equals(p.getId())
|
||||
&& d.getUnidades() != null && d.getUnidades() != null && d.getUnidades() > 0)
|
||||
.toList();
|
||||
Boolean firstDirection = true;
|
||||
for (CartDireccion cd : cd_presupuesto) {
|
||||
Integer unidades = cd.getUnidades();
|
||||
if (firstDirection) {
|
||||
Boolean freeShipment = direccionService.checkFreeShipment(cd.getDireccion().getCp(),
|
||||
cd.getDireccion().getPaisCode3()) && !cd.getIsPalets();
|
||||
if (!freeShipment && unidades != null && unidades > 0) {
|
||||
Map<String, Object> res = getShippingCost(cd, peso, unidades, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
} else {
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
}
|
||||
firstDirection = false;
|
||||
} else {
|
||||
Map<String, Object> res = getShippingCost(cd, peso, unidades, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
} else {
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
}
|
||||
}
|
||||
// ejemplar de prueba
|
||||
CartDireccion cd_prueba = direcciones.stream()
|
||||
.filter(d -> d.getPresupuesto() != null && d.getPresupuesto().getId().equals(p.getId())
|
||||
&& d.getUnidades() == null)
|
||||
.findFirst().orElse(null);
|
||||
if (cd_prueba != null) {
|
||||
|
||||
Map<String, Object> res = getShippingCost(cd_prueba, peso, 1, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -234,11 +274,17 @@ public class CartService {
|
||||
|
||||
double total = base + iva4 + iva21 + shipment;
|
||||
|
||||
int fidelizacion = pedidoService.getDescuentoFidelizacion();
|
||||
double descuento = (total) * fidelizacion / 100.0;
|
||||
total -= descuento;
|
||||
|
||||
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("shipment", Utils.formatCurrency(shipment, locale));
|
||||
summary.put("fidelizacion", fidelizacion + "%");
|
||||
summary.put("descuento", Utils.formatCurrency(-descuento, locale));
|
||||
summary.put("total", Utils.formatCurrency(total, locale));
|
||||
summary.put("errorShipmentCost", errorShipementCost);
|
||||
|
||||
@ -308,4 +354,44 @@ public class CartService {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> getShippingCost(
|
||||
CartDireccion cd,
|
||||
Double peso,
|
||||
Integer unidades,
|
||||
Locale locale) {
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
try {
|
||||
Map<String, Object> data = Map.of(
|
||||
"cp", cd.getDireccion().getCp(),
|
||||
"pais_code3", cd.getDireccion().getPaisCode3(),
|
||||
"peso", peso != null ? peso : 0.0,
|
||||
"unidades", unidades,
|
||||
"palets", Boolean.TRUE.equals(cd.getIsPalets()) ? 1 : 0);
|
||||
|
||||
var shipmentCost = skApiClient.getCosteEnvio(data, locale);
|
||||
|
||||
if (shipmentCost != null && shipmentCost.get("data") != null) {
|
||||
Number n = (Number) shipmentCost.get("data");
|
||||
double cost = n.doubleValue();
|
||||
|
||||
result.put("success", true);
|
||||
result.put("shipment", cost);
|
||||
result.put("iva21", cost * 0.21);
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("shipment", 0.0);
|
||||
result.put("iva21", 0.0);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("shipment", 0.0);
|
||||
result.put("iva21", 0.0);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -61,7 +61,8 @@ public class DireccionService {
|
||||
String alias = opt.get("alias").toLowerCase();
|
||||
String text = opt.get("text").toLowerCase();
|
||||
String direccion = opt.get("direccion").toLowerCase();
|
||||
return text.contains(q) || cp.contains(q) || ciudad.contains(q) || att.contains(q) || alias.contains(q) || direccion.contains(q);
|
||||
return text.contains(q) || cp.contains(q) || ciudad.contains(q) || att.contains(q)
|
||||
|| alias.contains(q) || direccion.contains(q);
|
||||
})
|
||||
.sorted(Comparator.comparing(m -> m.get("text"), Collator.getInstance()))
|
||||
.collect(Collectors.toList());
|
||||
@ -81,15 +82,16 @@ public class DireccionService {
|
||||
}
|
||||
|
||||
public Boolean checkFreeShipment(Integer cp, String paisCode3) {
|
||||
if (paisCode3 != null && paisCode3.equals("ESP") && cp != null) {
|
||||
// Excluir Canarias (35xxx y 38xxx), Baleares (07xxx), Ceuta (51xxx), Melilla (52xxx)
|
||||
int provincia = cp / 1000;
|
||||
if (paisCode3 != null && paisCode3.toLowerCase().equals("esp") && cp != null) {
|
||||
// Excluir Canarias (35xxx y 38xxx), Baleares (07xxx), Ceuta (51xxx), Melilla
|
||||
// (52xxx)
|
||||
int provincia = cp / 1000;
|
||||
|
||||
if (provincia != 7 && provincia != 35 && provincia != 38 && provincia != 51 && provincia != 52) {
|
||||
return true; // España peninsular
|
||||
if (provincia != 7 && provincia != 35 && provincia != 38 && provincia != 51 && provincia != 52) {
|
||||
return true; // España peninsular
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -4,8 +4,8 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PedidoService {
|
||||
|
||||
public int hasDescuentoFidelidad() {
|
||||
|
||||
public int getDescuentoFidelizacion() {
|
||||
// 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) {
|
||||
@ -18,10 +18,8 @@ public class PedidoService {
|
||||
return 3;
|
||||
} else if(totalGastado >= 4000 && totalGastado < 4999) {
|
||||
return 4;
|
||||
} else if(totalGastado >= 5000 && totalGastado < 9999) {
|
||||
} else if(totalGastado >= 5000) {
|
||||
return 5;
|
||||
} else if(totalGastado >= 10000) {
|
||||
return 6;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user