mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
haciendo pruebas en SK
This commit is contained in:
@ -57,14 +57,11 @@ public class CartService {
|
||||
this.presupuestoService = presupuestoService;
|
||||
}
|
||||
|
||||
|
||||
public Cart findById(Long cartId) {
|
||||
return cartRepo.findById(cartId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Carrito no encontrado"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Devuelve el carrito activo o lo crea si no existe. */
|
||||
@Transactional
|
||||
public Cart getOrCreateActiveCart(Long userId) {
|
||||
@ -223,8 +220,7 @@ public class CartService {
|
||||
Map<String, Object> res = getShippingCost(cd, peso, unidades, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
@ -236,8 +232,7 @@ public class CartService {
|
||||
Map<String, Object> res = getShippingCost(cd, peso, 1, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
@ -288,8 +283,7 @@ public class CartService {
|
||||
Map<String, Object> res = getShippingCost(cd_prueba, peso, 1, locale);
|
||||
if (res.get("success").equals(Boolean.FALSE)) {
|
||||
errorShipementCost = true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
shipment += (Double) res.get("shipment");
|
||||
iva21 += (Double) res.get("iva21");
|
||||
}
|
||||
@ -408,10 +402,7 @@ public class CartService {
|
||||
// delete cart directions by direccion id in ACTIVE carts
|
||||
@Transactional
|
||||
public void deleteCartDireccionesByDireccionId(Long direccionId) {
|
||||
/*List<CartDireccion> cartDirecciones = cartDireccionRepo.findByDireccion_IdAndCart_Status(direccionId, Cart.Status.ACTIVE);
|
||||
for (CartDireccion cd : cartDirecciones) {
|
||||
cartDireccionRepo.deleteById(cd.getId());
|
||||
}*/
|
||||
|
||||
cartDireccionRepo.deleteByDireccionIdAndCartStatus(direccionId, Cart.Status.ACTIVE);
|
||||
}
|
||||
|
||||
@ -423,10 +414,27 @@ public class CartService {
|
||||
|
||||
List<Map<String, Object>> presupuestoRequests = new ArrayList<>();
|
||||
|
||||
for (CartItem item : items) {
|
||||
for (Integer i = 0; i < items.size(); i++) {
|
||||
CartItem item = items.get(i);
|
||||
Presupuesto p = item.getPresupuesto();
|
||||
|
||||
Map<String, Object> data_to_send = presupuestoService.toSkApiRequest(p, true);
|
||||
data_to_send.put("createPedido", 0);
|
||||
if (items.size() > 1) {
|
||||
// Recuperar el mapa anidado datosCabecera
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> datosCabecera = (Map<String, Object>) data_to_send.get("datosCabecera");
|
||||
if (datosCabecera != null) {
|
||||
Object tituloOriginal = datosCabecera.get("titulo");
|
||||
datosCabecera.put(
|
||||
"titulo",
|
||||
"[" + (i + 1) + "/" + items.size() + "] " + (tituloOriginal != null ? tituloOriginal : "")
|
||||
);
|
||||
}
|
||||
}
|
||||
Map<String, Object> direcciones_presupuesto = this.getDireccionesPresupuesto(cart, p);
|
||||
data_to_send.put("direcciones", direcciones_presupuesto.get("direcciones"));
|
||||
data_to_send.put("direccionesFP1", direcciones_presupuesto.get("direccionesFP1"));
|
||||
|
||||
Map<String, Object> result = skApiClient.savePresupuesto(data_to_send);
|
||||
|
||||
@ -449,36 +457,90 @@ public class CartService {
|
||||
presupuestoRequests.add(dataMap);
|
||||
}
|
||||
|
||||
// Aquí ya tienes todos los presupuestos SK en presupuestoRequests
|
||||
// TODO: crear el pedido de verdad y devolver su ID
|
||||
// Long pedidoId = pedidoService.crearPedidoDesdePresupuestos(cart,
|
||||
// presupuestoRequests);
|
||||
// return pedidoId;
|
||||
|
||||
return 1L;
|
||||
// Crear el pedido en base a los presupuestos guardados
|
||||
if(presupuestoRequests.isEmpty()) {
|
||||
throw new IllegalStateException("No se pudieron guardar los presupuestos en SK.");
|
||||
}
|
||||
else{
|
||||
List<Long> presupuestoIds = new ArrayList<>();
|
||||
for (Map<String, Object> presData : presupuestoRequests) {
|
||||
Long presId = ((Number) presData.get("id")).longValue();
|
||||
presupuestoIds.add(presId);
|
||||
}
|
||||
Map<String, Object> ids = new HashMap<>();
|
||||
ids.put("presupuesto_ids", presupuestoIds);
|
||||
Long pedidoId = skApiClient.crearPedido(ids);
|
||||
return pedidoId;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getDireccionesPresupuesto(Cart cart, Presupuesto presupuesto) {
|
||||
|
||||
public Map<String, Object> getDireccionesPresupuesto(Cart cart, Presupuesto presupuesto) {
|
||||
|
||||
List<Map<String, Object>> direccionesPresupuesto = new ArrayList<>();
|
||||
List<Map<String, Object>> direccionesPrueba = new ArrayList<>();
|
||||
List<CartDireccion> direcciones = cart.getDirecciones().stream()
|
||||
.filter(d -> d.getPresupuesto() != null && d.getPresupuesto().getId().equals(presupuesto.getId()))
|
||||
.toList();
|
||||
if(cart.getOnlyOneShipment()){
|
||||
if (cart.getOnlyOneShipment()) {
|
||||
direcciones = direcciones.stream().limit(1).toList();
|
||||
Direccion dir = direcciones.size() > 0 ? direcciones.get(0).getDireccion() : null;
|
||||
if(dir != null){
|
||||
asdaslkdjasldjaslkdjaslkdjaslkdjaslkjljaslkd
|
||||
if (!direcciones.isEmpty()) {
|
||||
direccionesPresupuesto.add(direcciones.get(0).toSkMap(
|
||||
presupuesto.getSelectedTirada(),
|
||||
presupuesto.getPeso(),
|
||||
direcciones.get(0).getIsPalets(),
|
||||
false));
|
||||
if (presupuesto.getServiciosJson() != null
|
||||
&& presupuesto.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
direccionesPrueba.add(direcciones.get(0).toSkMap(
|
||||
1,
|
||||
presupuesto.getPeso(),
|
||||
false,
|
||||
true));
|
||||
}
|
||||
if (presupuesto.getServiciosJson() != null
|
||||
&& presupuesto.getServiciosJson().contains("deposito-legal")) {
|
||||
direccionesPresupuesto.add(direcciones.get(0).toSkMapDepositoLegal());
|
||||
}
|
||||
Map<String, Object> direccionesRet = new HashMap<>();
|
||||
direccionesRet.put("direcciones", direccionesPresupuesto);
|
||||
direccionesRet.put("direccionesFP1", direccionesPrueba.get(0));
|
||||
return direccionesRet;
|
||||
}
|
||||
} else {
|
||||
for (CartDireccion cd : cart.getDirecciones()) {
|
||||
|
||||
// direccion de ejemplar de prueba
|
||||
if(cd.getPresupuesto() == null || !cd.getPresupuesto().getId().equals(presupuesto.getId())) {
|
||||
continue;
|
||||
}
|
||||
if (cd.getUnidades() == null || cd.getUnidades() <= 0) {
|
||||
direccionesPrueba.add(cd.toSkMap(
|
||||
1,
|
||||
presupuesto.getPeso(),
|
||||
false,
|
||||
true));
|
||||
} else {
|
||||
direccionesPresupuesto.add(cd.toSkMap(
|
||||
cd.getUnidades(),
|
||||
presupuesto.getPeso(),
|
||||
cd.getIsPalets(),
|
||||
false));
|
||||
}
|
||||
}
|
||||
if (presupuesto.getServiciosJson() != null
|
||||
&& presupuesto.getServiciosJson().contains("deposito-legal")) {
|
||||
CartDireccion cd = new CartDireccion();
|
||||
direccionesPresupuesto.add(cd.toSkMapDepositoLegal());
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> resultado = new ArrayList<>();
|
||||
for (CartDireccion cd : direcciones) {
|
||||
Map<String, Object> dirMap = new HashMap<>();
|
||||
dirMap.put("direccion", cd.getDireccion());
|
||||
dirMap.put("isPalets", cd.getIsPalets());
|
||||
dirMap.put("unidades", cd.getUnidades());
|
||||
resultado.add(dirMap);
|
||||
Map<String, Object> direccionesRet = new HashMap<>();
|
||||
direccionesRet.put("direcciones", direccionesPresupuesto);
|
||||
if(!direccionesPrueba.isEmpty())
|
||||
direccionesRet.put("direccionesFP1", direccionesPrueba.get(0));
|
||||
else{
|
||||
direccionesRet.put("direccionesFP1", new ArrayList<>());
|
||||
}
|
||||
return resultado;
|
||||
return direccionesRet;
|
||||
}
|
||||
|
||||
/***************************************
|
||||
@ -524,6 +586,4 @@ public class CartService {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user