mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
trabajando en la vista del pedido
This commit is contained in:
@ -34,7 +34,6 @@ public class CartService {
|
||||
private final CartItemRepository itemRepo;
|
||||
private final MessageSource messageSource;
|
||||
private final PresupuestoRepository presupuestoRepo;
|
||||
private final Utils utils;
|
||||
private final DireccionService direccionService;
|
||||
private final skApiClient skApiClient;
|
||||
private final PedidoService pedidoService;
|
||||
@ -43,14 +42,13 @@ public class CartService {
|
||||
public CartService(CartRepository cartRepo, CartItemRepository itemRepo,
|
||||
CartDireccionRepository cartDireccionRepo, MessageSource messageSource,
|
||||
PresupuestoFormatter presupuestoFormatter, PresupuestoRepository presupuestoRepo,
|
||||
Utils utils, DireccionService direccionService, skApiClient skApiClient,
|
||||
DireccionService direccionService, skApiClient skApiClient,
|
||||
PedidoService pedidoService, PresupuestoService presupuestoService) {
|
||||
this.cartRepo = cartRepo;
|
||||
this.itemRepo = itemRepo;
|
||||
this.cartDireccionRepo = cartDireccionRepo;
|
||||
this.messageSource = messageSource;
|
||||
this.presupuestoRepo = presupuestoRepo;
|
||||
this.utils = utils;
|
||||
this.direccionService = direccionService;
|
||||
this.skApiClient = skApiClient;
|
||||
this.pedidoService = pedidoService;
|
||||
@ -89,7 +87,7 @@ public class CartService {
|
||||
|
||||
Presupuesto p = item.getPresupuesto();
|
||||
|
||||
Map<String, Object> elemento = getElementoCart(p, locale);
|
||||
Map<String, Object> elemento = presupuestoService.getPresupuestoInfoForCard(p, locale);
|
||||
elemento.put("cartItemId", item.getId());
|
||||
resultados.add(elemento);
|
||||
}
|
||||
@ -159,38 +157,6 @@ public class CartService {
|
||||
return itemRepo.findByCartId(cart.getId()).size();
|
||||
}
|
||||
|
||||
private Map<String, Object> getElementoCart(Presupuesto presupuesto, Locale locale) {
|
||||
|
||||
Map<String, Object> resumen = new HashMap<>();
|
||||
|
||||
resumen.put("titulo", presupuesto.getTitulo());
|
||||
|
||||
resumen.put("imagen",
|
||||
"/assets/images/imprimelibros/presupuestador/" + presupuesto.getTipoEncuadernacion() + ".png");
|
||||
resumen.put("imagen_alt",
|
||||
messageSource.getMessage("presupuesto." + presupuesto.getTipoEncuadernacion(), null, locale));
|
||||
|
||||
resumen.put("presupuestoId", presupuesto.getId());
|
||||
|
||||
if (presupuesto.getServiciosJson() != null && presupuesto.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
resumen.put("hasSample", true);
|
||||
} else {
|
||||
resumen.put("hasSample", false);
|
||||
}
|
||||
Map<String, Object> detalles = utils.getTextoPresupuesto(presupuesto, locale);
|
||||
|
||||
resumen.put("tirada", presupuesto.getSelectedTirada());
|
||||
|
||||
resumen.put("baseTotal", Utils.formatCurrency(presupuesto.getBaseImponible(), locale));
|
||||
resumen.put("base", presupuesto.getBaseImponible());
|
||||
resumen.put("iva4", presupuesto.getIvaImporte4());
|
||||
resumen.put("iva21", presupuesto.getIvaImporte21());
|
||||
|
||||
resumen.put("resumen", detalles);
|
||||
|
||||
return resumen;
|
||||
}
|
||||
|
||||
public Map<String, Object> getCartSummaryRaw(Cart cart, Locale locale) {
|
||||
|
||||
double base = 0.0;
|
||||
|
||||
@ -361,6 +361,14 @@ public class Utils {
|
||||
return dateTime.format(formatter);
|
||||
}
|
||||
|
||||
public static String formatDate(LocalDateTime dateTime, Locale locale) {
|
||||
if (dateTime == null) {
|
||||
return "";
|
||||
}
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", locale);
|
||||
return dateTime.format(formatter);
|
||||
}
|
||||
|
||||
public static String formatInstant(Instant instant, Locale locale) {
|
||||
if (instant == null) {
|
||||
return "";
|
||||
|
||||
@ -229,6 +229,7 @@ public class skApiClient {
|
||||
Long id = ((Integer) responseBody.get("id")).longValue();
|
||||
|
||||
if (success != null && id != null && success) {
|
||||
|
||||
return Map.of("data", id);
|
||||
} else {
|
||||
// Tu lógica actual: si success es true u otra cosa → error 2
|
||||
|
||||
@ -52,6 +52,9 @@ public class PedidoLinea {
|
||||
@Column(name = "estado_manual", nullable = false)
|
||||
private Boolean estadoManual;
|
||||
|
||||
@Column(name = "fecha_entrega")
|
||||
private LocalDateTime fechaEntrega;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@ -100,6 +103,14 @@ public class PedidoLinea {
|
||||
this.estadoManual = estadoManual;
|
||||
}
|
||||
|
||||
public LocalDateTime getFechaEntrega() {
|
||||
return fechaEntrega;
|
||||
}
|
||||
|
||||
public void setFechaEntrega(LocalDateTime fechaEntrega) {
|
||||
this.fechaEntrega = fechaEntrega;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import java.util.List;
|
||||
public interface PedidoLineaRepository extends JpaRepository<PedidoLinea, Long> {
|
||||
|
||||
List<PedidoLinea> findByPedidoId(Long pedidoId);
|
||||
List<PedidoLinea> findByPedidoIdOrderByIdAsc(Long pedidoId);
|
||||
|
||||
List<PedidoLinea> findByPresupuestoId(Long presupuestoId);
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Instant;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
@ -19,5 +19,5 @@ public interface PedidoRepository extends JpaRepository<Pedido, Long>, JpaSpecif
|
||||
AND p.createdAt >= :afterDate
|
||||
""")
|
||||
Double sumTotalByCreatedByAndCreatedAtAfter(@Param("userId") Long userId,
|
||||
@Param("afterDate") LocalDateTime afterDate);
|
||||
@Param("afterDate") Instant afterDate);
|
||||
}
|
||||
|
||||
@ -2,8 +2,10 @@ package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -44,7 +46,7 @@ public class PedidoService {
|
||||
public int getDescuentoFidelizacion(Long userId) {
|
||||
// descuento entre el 1% y el 6% para clientes fidelidad (mas de 1500€ en el
|
||||
// ultimo año)
|
||||
LocalDateTime haceUnAno = LocalDateTime.now().minusYears(1);
|
||||
Instant haceUnAno = Instant.now().minusSeconds(365 * 24 * 60 * 60);
|
||||
double totalGastado = pedidoRepository.sumTotalByCreatedByAndCreatedAtAfter(userId, haceUnAno);
|
||||
if (totalGastado < 1200) {
|
||||
return 0;
|
||||
@ -127,6 +129,32 @@ public class PedidoService {
|
||||
return saved;
|
||||
}
|
||||
|
||||
/** Lista de los items del pedido preparados para la vista*/
|
||||
@Transactional
|
||||
public List<Map<String, Object>> getLineas(Long pedidoId, Locale locale) {
|
||||
Pedido p = pedidoRepository.findById(pedidoId).orElse(null);
|
||||
if (p == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
List<Map<String, Object>> resultados = new ArrayList<>();
|
||||
List<PedidoLinea> items = pedidoLineaRepository.findByPedidoIdOrderByIdAsc(p.getId());
|
||||
for (PedidoLinea item : items) {
|
||||
|
||||
Presupuesto presupuesto = item.getPresupuesto();
|
||||
Map<String, Object> elemento = presupuestoService.getPresupuestoInfoForCard(presupuesto, locale);
|
||||
elemento.put("estado", item.getEstado());
|
||||
elemento.put("fechaEntrega", item.getFechaEntrega() != null ?
|
||||
Utils.formatDate(item.getFechaEntrega(), locale) : "");
|
||||
elemento.put("lineaId", item.getId());
|
||||
resultados.add(elemento);
|
||||
}
|
||||
return resultados;
|
||||
}
|
||||
|
||||
/***************************
|
||||
* MÉTODOS PRIVADOS
|
||||
***************************/
|
||||
@Transactional
|
||||
private void saveDireccionesPedidoLinea(
|
||||
Map<String, Map<String, Object>> direcciones,
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.security.Principal;
|
||||
@ -12,6 +13,7 @@ import java.util.Map;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
import com.imprimelibros.erp.common.Utils;
|
||||
import com.imprimelibros.erp.datatables.DataTable;
|
||||
@ -31,13 +33,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
public class PedidosController {
|
||||
|
||||
private final PedidoRepository repoPedido;
|
||||
private final PedidoService pedidoService;
|
||||
private final UserDao repoUser;
|
||||
private final MessageSource messageSource;
|
||||
private final PedidoLineaRepository repoPedidoLinea;
|
||||
|
||||
public PedidosController(PedidoRepository repoPedido, UserDao repoUser, MessageSource messageSource,
|
||||
public PedidosController(PedidoRepository repoPedido, PedidoService pedidoService, UserDao repoUser, MessageSource messageSource,
|
||||
PedidoLineaRepository repoPedidoLinea) {
|
||||
this.repoPedido = repoPedido;
|
||||
this.pedidoService = pedidoService;
|
||||
this.repoUser = repoUser;
|
||||
this.messageSource = messageSource;
|
||||
this.repoPedidoLinea = repoPedidoLinea;
|
||||
@ -162,4 +166,21 @@ public class PedidosController {
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/view/{id}")
|
||||
public String verPedido(
|
||||
@PathVariable(name = "id", required = true) Long id,
|
||||
Model model, Locale locale) {
|
||||
|
||||
Boolean isAdmin = Utils.isCurrentUserAdmin();
|
||||
if (isAdmin) {
|
||||
model.addAttribute("isAdmin", true);
|
||||
} else {
|
||||
model.addAttribute("isAdmin", false);
|
||||
}
|
||||
List<Map<String, Object>> lineas = pedidoService.getLineas(id, locale);
|
||||
model.addAttribute("lineas", lineas);
|
||||
model.addAttribute("id", id);
|
||||
return "imprimelibros/pedidos/pedidos-view";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import com.imprimelibros.erp.common.Utils;
|
||||
import com.imprimelibros.erp.common.web.IpUtils;
|
||||
import com.imprimelibros.erp.configurationERP.VariableService;
|
||||
import com.imprimelibros.erp.presupuesto.GeoIpService;
|
||||
@ -71,14 +72,16 @@ public class PresupuestoService {
|
||||
private final skApiClient apiClient;
|
||||
private final GeoIpService geoIpService;
|
||||
private final UserDao userRepo;
|
||||
private final Utils utils;
|
||||
|
||||
public PresupuestoService(PresupuestadorItems presupuestadorItems, PresupuestoFormatter presupuestoFormatter,
|
||||
skApiClient apiClient, GeoIpService geoIpService, UserDao userRepo) {
|
||||
skApiClient apiClient, GeoIpService geoIpService, UserDao userRepo, Utils utils) {
|
||||
this.presupuestadorItems = presupuestadorItems;
|
||||
this.presupuestoFormatter = presupuestoFormatter;
|
||||
this.apiClient = apiClient;
|
||||
this.geoIpService = geoIpService;
|
||||
this.userRepo = userRepo;
|
||||
this.utils = utils;
|
||||
}
|
||||
|
||||
public boolean validateDatosGenerales(int[] tiradas) {
|
||||
@ -1321,6 +1324,39 @@ public class PresupuestoService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<String, Object> getPresupuestoInfoForCard(Presupuesto presupuesto, Locale locale) {
|
||||
|
||||
Map<String, Object> resumen = new HashMap<>();
|
||||
|
||||
resumen.put("titulo", presupuesto.getTitulo());
|
||||
|
||||
resumen.put("imagen",
|
||||
"/assets/images/imprimelibros/presupuestador/" + presupuesto.getTipoEncuadernacion() + ".png");
|
||||
resumen.put("imagen_alt",
|
||||
messageSource.getMessage("presupuesto." + presupuesto.getTipoEncuadernacion(), null, locale));
|
||||
|
||||
resumen.put("presupuestoId", presupuesto.getId());
|
||||
|
||||
if (presupuesto.getServiciosJson() != null && presupuesto.getServiciosJson().contains("ejemplar-prueba")) {
|
||||
resumen.put("hasSample", true);
|
||||
} else {
|
||||
resumen.put("hasSample", false);
|
||||
}
|
||||
Map<String, Object> detalles = utils.getTextoPresupuesto(presupuesto, locale);
|
||||
|
||||
resumen.put("tirada", presupuesto.getSelectedTirada());
|
||||
|
||||
resumen.put("baseTotal", Utils.formatCurrency(presupuesto.getBaseImponible(), locale));
|
||||
resumen.put("base", presupuesto.getBaseImponible());
|
||||
resumen.put("iva4", presupuesto.getIvaImporte4());
|
||||
resumen.put("iva21", presupuesto.getIvaImporte21());
|
||||
resumen.put("total", Utils.formatCurrency(presupuesto.getTotalConIva(), locale));
|
||||
|
||||
resumen.put("resumen", detalles);
|
||||
|
||||
return resumen;
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// Métodos privados
|
||||
// =======================================================================
|
||||
|
||||
Reference in New Issue
Block a user