mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
cargando carrito desde backend
This commit is contained in:
@ -13,10 +13,12 @@ import java.util.Map;
|
||||
|
||||
import com.imprimelibros.erp.presupuesto.classes.PresupuestoFormatter;
|
||||
import com.imprimelibros.erp.presupuesto.dto.Presupuesto;
|
||||
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.presupuesto.PresupuestoRepository;
|
||||
|
||||
|
||||
@Service
|
||||
public class CartService {
|
||||
|
||||
@ -25,15 +27,20 @@ public class CartService {
|
||||
private final MessageSource messageSource;
|
||||
private final PresupuestoRepository presupuestoRepo;
|
||||
private final Utils utils;
|
||||
private final DireccionService direccionService;
|
||||
private final skApiClient skApiClient;
|
||||
|
||||
public CartService(CartRepository cartRepo, CartItemRepository itemRepo,
|
||||
MessageSource messageSource, PresupuestoFormatter presupuestoFormatter,
|
||||
PresupuestoRepository presupuestoRepo, Utils utils) {
|
||||
PresupuestoRepository presupuestoRepo, Utils utils, DireccionService direccionService,
|
||||
skApiClient skApiClient) {
|
||||
this.cartRepo = cartRepo;
|
||||
this.itemRepo = itemRepo;
|
||||
this.messageSource = messageSource;
|
||||
this.presupuestoRepo = presupuestoRepo;
|
||||
this.utils = utils;
|
||||
this.direccionService = direccionService;
|
||||
this.skApiClient = skApiClient;
|
||||
}
|
||||
|
||||
/** Devuelve el carrito activo o lo crea si no existe. */
|
||||
@ -48,6 +55,11 @@ public class CartService {
|
||||
});
|
||||
}
|
||||
|
||||
public Cart getCartById(Long cartId) {
|
||||
return cartRepo.findById(cartId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Carrito no encontrado"));
|
||||
}
|
||||
|
||||
/** Lista items (presupuestos) del carrito activo del usuario. */
|
||||
@Transactional
|
||||
public List<Map<String, Object>> listItems(Long userId, Locale locale) {
|
||||
@ -57,13 +69,14 @@ public class CartService {
|
||||
for (CartItem item : items) {
|
||||
|
||||
Presupuesto p = presupuestoRepo.findById(item.getPresupuestoId())
|
||||
.orElseThrow(() -> new IllegalStateException("Presupuesto no encontrado: " + item.getPresupuestoId()));
|
||||
.orElseThrow(
|
||||
() -> new IllegalStateException("Presupuesto no encontrado: " + item.getPresupuestoId()));
|
||||
|
||||
Map<String, Object> elemento = getElementoCart(p, locale);
|
||||
elemento.put("cartItemId", item.getId());
|
||||
resultados.add(elemento);
|
||||
}
|
||||
//System.out.println("Cart items: " + resultados);
|
||||
// System.out.println("Cart items: " + resultados);
|
||||
return resultados;
|
||||
}
|
||||
|
||||
@ -120,9 +133,9 @@ public class CartService {
|
||||
}
|
||||
|
||||
private Map<String, Object> getElementoCart(Presupuesto presupuesto, Locale locale) {
|
||||
|
||||
|
||||
Map<String, Object> resumen = new HashMap<>();
|
||||
|
||||
|
||||
resumen.put("titulo", presupuesto.getTitulo());
|
||||
|
||||
resumen.put("imagen",
|
||||
@ -132,7 +145,7 @@ public class CartService {
|
||||
|
||||
resumen.put("presupuestoId", presupuesto.getId());
|
||||
|
||||
if(presupuesto.getServiciosJson() != null && presupuesto.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
if (presupuesto.getServiciosJson() != null && presupuesto.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
resumen.put("hasSample", true);
|
||||
} else {
|
||||
resumen.put("hasSample", false);
|
||||
@ -151,18 +164,40 @@ public class CartService {
|
||||
return resumen;
|
||||
}
|
||||
|
||||
public Map<String, Object> getCartSummary(List<Map<String, Object>> cartItems, Locale locale) {
|
||||
public Map<String, Object> getCartSummary(Cart cart, 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")));
|
||||
List<CartItem> items = cart.getItems();
|
||||
List<CartDireccion> direcciones = cart.getDirecciones();
|
||||
|
||||
for (CartItem item : items) {
|
||||
Presupuesto p = presupuestoRepo.findById(item.getPresupuestoId())
|
||||
.orElseThrow(() -> new IllegalStateException("Presupuesto no encontrado: " + item.getPresupuestoId()));
|
||||
base += p.getBaseImponible().doubleValue();
|
||||
iva4 += p.getIvaImporte4().doubleValue();
|
||||
iva21 += p.getIvaImporte21().doubleValue();
|
||||
if(cart.getOnlyOneShipment() != null && cart.getOnlyOneShipment()) {
|
||||
// Si es envío único, que es a españa y no ha canarias
|
||||
if(direcciones != null && direcciones.size() > 0) {
|
||||
CartDireccion cd = direcciones.get(0);
|
||||
Boolean freeShipment = direccionService.checkFreeShipment(cd.getDireccion().getCp(), cd.getDireccion().getPaisCode3()) && !cd.getIsPalets();
|
||||
if(!freeShipment) {
|
||||
Map<String, Object> data =
|
||||
Map.of(
|
||||
"cp", cd.getDireccion().getCp(),
|
||||
"pais_code3", cd.getDireccion().getPaisCode3(),
|
||||
"peso", p.getPeso() != null ? p.getPeso() : 0,
|
||||
"unidades", cd.getUnidades(),
|
||||
"palets", cd.getIsPalets() ? 1 : 0
|
||||
);
|
||||
var shipmentCost = skApiClient.getCosteEnvio(data, locale);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double total = base + iva4 + iva21;
|
||||
@ -175,4 +210,18 @@ public class CartService {
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Boolean updateCart(Long cartId, UpdateCartRequest request) {
|
||||
|
||||
try{
|
||||
Cart cart = cartRepo.findById(cartId).orElseThrow(() -> new IllegalArgumentException("Carrito no encontrado"));
|
||||
cart.setOnlyOneShipment(request.isOnlyOneShipment());
|
||||
cartRepo.save(cart);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// Manejo de excepciones
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user