mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-22 00:30:23 +00:00
añadidas las direcciones de pedido
This commit is contained in:
211
src/main/java/com/imprimelibros/erp/pedidos/PedidoDireccion.java
Normal file
211
src/main/java/com/imprimelibros/erp/pedidos/PedidoDireccion.java
Normal file
@ -0,0 +1,211 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import com.imprimelibros.erp.direcciones.Direccion.TipoIdentificacionFiscal;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "pedidos_direcciones")
|
||||
public class PedidoDireccion {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
// FK a pedidos_lineas.id (nullable, on delete set null)
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "pedido_linea_id")
|
||||
private PedidoLinea pedidoLinea;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "pedido_id")
|
||||
private Pedido pedido;
|
||||
|
||||
@Column(name = "unidades")
|
||||
private Integer unidades;
|
||||
|
||||
@Column(name = "is_facturacion", nullable = false)
|
||||
private boolean facturacion = false;
|
||||
|
||||
@Column(name = "is_ejemplar_prueba", nullable = false)
|
||||
private boolean ejemplarPrueba = false;
|
||||
|
||||
@Column(name = "att", nullable = false, length = 150)
|
||||
private String att;
|
||||
|
||||
@Column(name = "direccion", nullable = false, length = 255)
|
||||
private String direccion;
|
||||
|
||||
@Column(name = "cp", nullable = false)
|
||||
private Integer cp;
|
||||
|
||||
@Column(name = "ciudad", nullable = false, length = 100)
|
||||
private String ciudad;
|
||||
|
||||
@Column(name = "provincia", nullable = false, length = 100)
|
||||
private String provincia;
|
||||
|
||||
@Column(name = "pais_code3", nullable = false, length = 3)
|
||||
private String paisCode3 = "esp";
|
||||
|
||||
@Column(name = "telefono", nullable = false, length = 30)
|
||||
private String telefono;
|
||||
|
||||
@Column(name = "instrucciones", length = 255)
|
||||
private String instrucciones;
|
||||
|
||||
@Column(name = "razon_social", length = 150)
|
||||
private String razonSocial;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "tipo_identificacion_fiscal", nullable = false, length = 20)
|
||||
private TipoIdentificacionFiscal tipoIdentificacionFiscal = TipoIdentificacionFiscal.DNI;
|
||||
|
||||
@Column(name = "identificacion_fiscal", length = 50)
|
||||
private String identificacionFiscal;
|
||||
|
||||
@CreationTimestamp
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
// ===== GETTERS & SETTERS =====
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public PedidoLinea getPedidoLinea() {
|
||||
return pedidoLinea;
|
||||
}
|
||||
|
||||
public void setPedidoLinea(PedidoLinea pedidoLinea) {
|
||||
this.pedidoLinea = pedidoLinea;
|
||||
}
|
||||
|
||||
public Pedido getPedido() {
|
||||
return pedido;
|
||||
}
|
||||
|
||||
public void setPedido(Pedido pedido) {
|
||||
this.pedido = pedido;
|
||||
}
|
||||
|
||||
public Integer getUnidades() {
|
||||
return unidades;
|
||||
}
|
||||
|
||||
public void setUnidades(Integer unidades) {
|
||||
this.unidades = unidades;
|
||||
}
|
||||
|
||||
public boolean isFacturacion() {
|
||||
return facturacion;
|
||||
}
|
||||
|
||||
public void setFacturacion(boolean facturacion) {
|
||||
this.facturacion = facturacion;
|
||||
}
|
||||
|
||||
public boolean isEjemplarPrueba() {
|
||||
return ejemplarPrueba;
|
||||
}
|
||||
|
||||
public void setEjemplarPrueba(boolean ejemplarPrueba) {
|
||||
this.ejemplarPrueba = ejemplarPrueba;
|
||||
}
|
||||
|
||||
public String getAtt() {
|
||||
return att;
|
||||
}
|
||||
|
||||
public void setAtt(String att) {
|
||||
this.att = att;
|
||||
}
|
||||
|
||||
public String getDireccion() {
|
||||
return direccion;
|
||||
}
|
||||
|
||||
public void setDireccion(String direccion) {
|
||||
this.direccion = direccion;
|
||||
}
|
||||
|
||||
public Integer getCp() {
|
||||
return cp;
|
||||
}
|
||||
|
||||
public void setCp(Integer cp) {
|
||||
this.cp = cp;
|
||||
}
|
||||
|
||||
public String getCiudad() {
|
||||
return ciudad;
|
||||
}
|
||||
|
||||
public void setCiudad(String ciudad) {
|
||||
this.ciudad = ciudad;
|
||||
}
|
||||
|
||||
public String getProvincia() {
|
||||
return provincia;
|
||||
}
|
||||
|
||||
public void setProvincia(String provincia) {
|
||||
this.provincia = provincia;
|
||||
}
|
||||
|
||||
public String getPaisCode3() {
|
||||
return paisCode3;
|
||||
}
|
||||
|
||||
public void setPaisCode3(String paisCode3) {
|
||||
this.paisCode3 = paisCode3;
|
||||
}
|
||||
|
||||
public String getTelefono() {
|
||||
return telefono;
|
||||
}
|
||||
|
||||
public void setTelefono(String telefono) {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
public String getInstrucciones() {
|
||||
return instrucciones;
|
||||
}
|
||||
|
||||
public void setInstrucciones(String instrucciones) {
|
||||
this.instrucciones = instrucciones;
|
||||
}
|
||||
|
||||
public String getRazonSocial() {
|
||||
return razonSocial;
|
||||
}
|
||||
|
||||
public void setRazonSocial(String razonSocial) {
|
||||
this.razonSocial = razonSocial;
|
||||
}
|
||||
|
||||
public TipoIdentificacionFiscal getTipoIdentificacionFiscal() {
|
||||
return tipoIdentificacionFiscal;
|
||||
}
|
||||
|
||||
public void setTipoIdentificacionFiscal(TipoIdentificacionFiscal tipoIdentificacionFiscal) {
|
||||
this.tipoIdentificacionFiscal = tipoIdentificacionFiscal;
|
||||
}
|
||||
|
||||
public String getIdentificacionFiscal() {
|
||||
return identificacionFiscal;
|
||||
}
|
||||
|
||||
public void setIdentificacionFiscal(String identificacionFiscal) {
|
||||
this.identificacionFiscal = identificacionFiscal;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PedidoDireccionRepository extends JpaRepository<PedidoDireccion, Long> {
|
||||
|
||||
// Todas las direcciones de una línea de pedido
|
||||
List<PedidoDireccion> findByPedidoLinea_Id(Long pedidoLineaId);
|
||||
|
||||
// Si en tu código sueles trabajar con el objeto:
|
||||
List<PedidoDireccion> findByPedidoLinea(PedidoLinea pedidoLinea);
|
||||
}
|
||||
|
||||
@ -9,6 +9,24 @@ import com.imprimelibros.erp.presupuesto.dto.Presupuesto;
|
||||
@Table(name = "pedidos_lineas")
|
||||
public class PedidoLinea {
|
||||
|
||||
public enum Estado {
|
||||
aprobado("pedido.estado.aprobado"),
|
||||
maquetacion("pedido.estado.maquetacion"),
|
||||
haciendo_ferro("pedido.estado.haciendo_ferro"),
|
||||
produccion("pedido.estado.produccion"),
|
||||
cancelado("pedido.estado.cancelado");
|
||||
|
||||
private final String messageKey;
|
||||
|
||||
Estado(String messageKey) {
|
||||
this.messageKey = messageKey;
|
||||
}
|
||||
|
||||
public String getMessageKey() {
|
||||
return messageKey;
|
||||
}
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
@ -21,6 +39,13 @@ public class PedidoLinea {
|
||||
@JoinColumn(name = "presupuesto_id", nullable = false)
|
||||
private Presupuesto presupuesto;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "estado", nullable = false)
|
||||
private Estado estado = Estado.aprobado;
|
||||
|
||||
@Column(name = "estado_manual", nullable = false)
|
||||
private Boolean estadoManual;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@ -53,6 +78,22 @@ public class PedidoLinea {
|
||||
this.presupuesto = presupuesto;
|
||||
}
|
||||
|
||||
public Estado getEstado() {
|
||||
return estado;
|
||||
}
|
||||
|
||||
public void setEstado(Estado estado) {
|
||||
this.estado = estado;
|
||||
}
|
||||
|
||||
public Boolean getEstadoManual() {
|
||||
return estadoManual;
|
||||
}
|
||||
|
||||
public void setEstadoManual(Boolean estadoManual) {
|
||||
this.estadoManual = estadoManual;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
package com.imprimelibros.erp.pedidos;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.imprimelibros.erp.direcciones.Direccion;
|
||||
import com.imprimelibros.erp.presupuesto.PresupuestoRepository;
|
||||
import com.imprimelibros.erp.presupuesto.dto.Presupuesto;
|
||||
import com.imprimelibros.erp.direcciones.DireccionService;
|
||||
|
||||
@Service
|
||||
public class PedidoService {
|
||||
@ -16,12 +19,17 @@ public class PedidoService {
|
||||
private final PedidoRepository pedidoRepository;
|
||||
private final PedidoLineaRepository pedidoLineaRepository;
|
||||
private final PresupuestoRepository presupuestoRepository;
|
||||
private final PedidoDireccionRepository pedidoDireccionRepository;
|
||||
private final DireccionService direccionService;
|
||||
|
||||
public PedidoService(PedidoRepository pedidoRepository, PedidoLineaRepository pedidoLineaRepository,
|
||||
PresupuestoRepository presupuestoRepository) {
|
||||
PresupuestoRepository presupuestoRepository, PedidoDireccionRepository pedidoDireccionRepository,
|
||||
DireccionService direccionService) {
|
||||
this.pedidoRepository = pedidoRepository;
|
||||
this.pedidoLineaRepository = pedidoLineaRepository;
|
||||
this.presupuestoRepository = presupuestoRepository;
|
||||
this.pedidoDireccionRepository = pedidoDireccionRepository;
|
||||
this.direccionService = direccionService;
|
||||
}
|
||||
|
||||
public int getDescuentoFidelizacion() {
|
||||
@ -52,7 +60,10 @@ public class PedidoService {
|
||||
* - usuario que crea el pedido
|
||||
*/
|
||||
@Transactional
|
||||
public Pedido crearPedido(List<Long> presupuestoIds,
|
||||
public Pedido crearPedido(
|
||||
List<Long> presupuestoIds,
|
||||
Map<String, Object> presupuestoDirecciones,
|
||||
Long direccionFacturacionId,
|
||||
Map<String, Object> cartSummaryRaw,
|
||||
String proveedor,
|
||||
String proveedorRef,
|
||||
@ -91,11 +102,124 @@ public class PedidoService {
|
||||
linea.setPresupuesto(presupuesto);
|
||||
linea.setCreatedBy(userId);
|
||||
linea.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
linea.setEstado(PedidoLinea.Estado.aprobado);
|
||||
linea.setEstadoManual(false);
|
||||
pedidoLineaRepository.save(linea);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Map<String, Object>> direcciones = (Map<String, Map<String, Object>>) presupuestoDirecciones
|
||||
.get(presupuesto.getId().toString());
|
||||
if (direcciones != null) {
|
||||
saveDireccionesPedidoLinea(direcciones, saved, linea, direccionFacturacionId);
|
||||
}
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void saveDireccionesPedidoLinea(
|
||||
Map<String, Map<String, Object>> direcciones,
|
||||
Pedido pedido,
|
||||
PedidoLinea linea, Long direccionFacturacionId) {
|
||||
// direccion prueba
|
||||
if (direcciones.containsKey("direccionesFP1")) {
|
||||
try {
|
||||
Map<String, Object> fp1 = (Map<String, Object>) direcciones.get("direccionesFP1");
|
||||
@SuppressWarnings("unchecked")
|
||||
PedidoDireccion direccion = saveDireccion(
|
||||
(HashMap<String, Object>) fp1.get("direccion"),
|
||||
pedido,
|
||||
linea, true,
|
||||
false);
|
||||
pedidoDireccionRepository.save(direccion);
|
||||
} catch (Exception e) {
|
||||
// Viene vacio
|
||||
}
|
||||
}
|
||||
if (direcciones.containsKey("direcciones")) {
|
||||
List<?> dirs = (List<?>) direcciones.get("direcciones");
|
||||
for (Object dir : dirs) {
|
||||
@SuppressWarnings("unchecked")
|
||||
HashMap<String, Object> direccionEnvio = (HashMap<String, Object>) ((HashMap<String, Object>) dir)
|
||||
.get("direccion");
|
||||
if (direccionEnvio.get("cantidad") != null && (Integer) direccionEnvio.get("cantidad") == 4
|
||||
&& direccionEnvio.get("att").toString().contains("Depósito Legal")) {
|
||||
continue; // Saltar la dirección de depósito legal
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
PedidoDireccion direccion = saveDireccion(
|
||||
(HashMap<String, Object>) ((HashMap<String, Object>) dir).get("direccion"),
|
||||
pedido,
|
||||
linea, false,
|
||||
false);
|
||||
pedidoDireccionRepository.save(direccion);
|
||||
}
|
||||
}
|
||||
if (direccionFacturacionId != null) {
|
||||
Direccion dirFact = direccionService.findById(direccionFacturacionId).orElse(null);
|
||||
if (dirFact != null) {
|
||||
HashMap<String, Object> dirFactMap = new HashMap<>();
|
||||
dirFactMap.put("att", dirFact.getAtt());
|
||||
dirFactMap.put("direccion", dirFact.getDireccion());
|
||||
dirFactMap.put("cp", dirFact.getCp());
|
||||
dirFactMap.put("municipio", dirFact.getCiudad());
|
||||
dirFactMap.put("provincia", dirFact.getProvincia());
|
||||
dirFactMap.put("pais_code3", dirFact.getPaisCode3());
|
||||
dirFactMap.put("telefono", dirFact.getTelefono());
|
||||
dirFactMap.put("instrucciones", dirFact.getInstrucciones());
|
||||
dirFactMap.put("razon_social", dirFact.getRazonSocial());
|
||||
dirFactMap.put("tipo_identificacion_fiscal", dirFact.getTipoIdentificacionFiscal().name());
|
||||
dirFactMap.put("identificacion_fiscal", dirFact.getIdentificacionFiscal());
|
||||
|
||||
PedidoDireccion direccion = saveDireccion(
|
||||
dirFactMap,
|
||||
pedido,
|
||||
linea, false,
|
||||
true);
|
||||
pedidoDireccionRepository.save(direccion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PedidoDireccion saveDireccion(HashMap<String, Object> dir, Pedido pedido, PedidoLinea linea,
|
||||
Boolean isEjemplarPrueba,
|
||||
Boolean isFacturacion) {
|
||||
|
||||
PedidoDireccion direccion = new PedidoDireccion();
|
||||
direccion.setPedidoLinea(isFacturacion ? null : linea);
|
||||
if (isFacturacion) {
|
||||
direccion.setUnidades(null);
|
||||
direccion.setFacturacion(true);
|
||||
direccion.setPedido(pedido);
|
||||
|
||||
} else {
|
||||
if (isEjemplarPrueba) {
|
||||
direccion.setUnidades(1);
|
||||
direccion.setEjemplarPrueba(true);
|
||||
} else {
|
||||
direccion.setUnidades((Integer) dir.getOrDefault("cantidad", 1));
|
||||
direccion.setEjemplarPrueba(false);
|
||||
}
|
||||
}
|
||||
|
||||
direccion.setFacturacion(false);
|
||||
direccion.setAtt((String) dir.getOrDefault("att", ""));
|
||||
direccion.setDireccion((String) dir.getOrDefault("direccion", ""));
|
||||
direccion.setCp((Integer) dir.getOrDefault("cp", 0));
|
||||
direccion.setCiudad((String) dir.getOrDefault("municipio", ""));
|
||||
direccion.setProvincia((String) dir.getOrDefault("provincia", ""));
|
||||
direccion.setPaisCode3((String) dir.getOrDefault("pais_code3", "esp"));
|
||||
direccion.setTelefono((String) dir.getOrDefault("telefono", ""));
|
||||
direccion.setInstrucciones((String) dir.getOrDefault("instrucciones", ""));
|
||||
direccion.setRazonSocial((String) dir.getOrDefault("razon_social", ""));
|
||||
direccion.setTipoIdentificacionFiscal(Direccion.TipoIdentificacionFiscal
|
||||
.valueOf((String) dir.getOrDefault("tipo_identificacion_fiscal",
|
||||
Direccion.TipoIdentificacionFiscal.DNI.name())));
|
||||
direccion.setIdentificacionFiscal((String) dir.getOrDefault("identificacion_fiscal", ""));
|
||||
|
||||
return direccion;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user