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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user