movidos los ficheros por funciones

This commit is contained in:
Jaime Jiménez
2025-08-06 12:40:08 +02:00
parent a593a1af78
commit a1b8fb01fc
18 changed files with 36 additions and 46 deletions

View File

@ -0,0 +1,372 @@
package com.imprimelibros.erp.presupuesto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import com.imprimelibros.erp.presupuesto.validation.ConsistentTiradas;
import com.imprimelibros.erp.presupuesto.validation.Par;
import com.imprimelibros.erp.presupuesto.validation.PresupuestoValidationGroups;
import com.imprimelibros.erp.presupuesto.validation.Tamanio;
import jakarta.persistence.*;
@ConsistentTiradas(groups = PresupuestoValidationGroups.DatosGenerales.class)
@Tamanio(groups = PresupuestoValidationGroups.DatosGenerales.class)
@Entity
@Table(name = "presupuesto")
public class Presupuesto {
public enum TipoEncuadernacion {
fresado, cosido, grapado, espiral, wireo
}
public enum TipoImpresion {
negro, negrohq, color, colorhq
}
public enum TipoCubierta {
tapaBlanda, tapaDura, tapaDuraLomoRedondo
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "{presupuesto.errores.tipo-encuadernacion}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "tipo_encuadernacion")
private TipoEncuadernacion tipoEncuadernacion = TipoEncuadernacion.fresado;
@NotBlank(message = "{presupuesto.errores.titulo}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "titulo")
private String titulo;
@Column(name = "autor")
private String autor;
@Column(name = "isbn")
private String isbn;
@NotNull(message = "{presupuesto.errores.tirada1}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "tirada1")
private Integer tirada1;
@Column(name = "tirada2")
private Integer tirada2;
@Column(name = "tirada3")
private Integer tirada3;
@Column(name = "tirada4")
private Integer tirada4;
@NotNull(message = "{presupuesto.errores.ancho}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "ancho")
private Integer ancho;
@NotNull(message = "{presupuesto.errores.alto}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "alto")
private Integer alto;
@Column(name = "formatoPersonalizado")
private Boolean formatoPersonalizado;
@Par(message = "{presupuesto.errores.paginasNegro.par}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@NotNull(message = "{presupuesto.errores.paginasNegro.required}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "paginas_negro")
private Integer paginasNegro;
@Par(message = "{presupuesto.errores.paginasColor.par}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@NotNull(message = "{presupuesto.errores.paginasColor.required}", groups = PresupuestoValidationGroups.DatosGenerales.class)
@Column(name = "paginas_color")
private Integer paginasColor;
@Column(name = "posicion_paginas_color")
private String posicionPaginasColor;
@Column(name = "paginas_color_total")
private Integer paginasColorTotal;
@NotNull(message = "{presupuesto.errores.tipo-impresion}", groups = PresupuestoValidationGroups.Interior.class)
@Column(name = "tipo_impresion")
private TipoImpresion tipoImpresion = TipoImpresion.negro;
@NotNull(message = "{presupuesto.errores.papel-interior}", groups = PresupuestoValidationGroups.Interior.class)
@Column(name = "papel_interior_id")
private Integer papelInteriorId;
@NotNull(message = "{presupuesto.errores.gramaje-interior}", groups = PresupuestoValidationGroups.Interior.class)
@Column(name = "gramaje_interior")
private Integer gramajeInterior;
@NotNull(message = "{presupuesto.errores.tipo-cubierta}", groups = PresupuestoValidationGroups.Cubierta.class)
@Column(name = "tipo_cubierta")
private TipoCubierta tipoCubierta = TipoCubierta.tapaBlanda;
@Column(name = "solapas_cubierta")
private Boolean solapasCubierta = false;
@Column(name = "tamanio_solapas_cubierta")
private Integer tamanioSolapasCubierta;
@Column(name = "cubierta_caras")
private Integer cubiertaCaras;
@Column(name = "papel_guardas_id")
private Integer papelGuardasId;
@Column(name = "gramaje_guardas")
private Integer gramajeGuardas;
@Column(name = "guardas_impresas")
private Integer guardasImpresas;
@Column(name = "cabezada")
private String cabezada;
@NotNull(message = "{presupuesto.errores.papel-cubierta}", groups = PresupuestoValidationGroups.Cubierta.class)
@Column(name = "papel_cubierta_id")
private Integer papelCubiertaId = 2;
@NotNull(message = "{presupuesto.errores.gramaje-cubierta}", groups = PresupuestoValidationGroups.Cubierta.class)
@Column(name = "gramaje_cubierta")
private Integer gramajeCubierta = 240;
// Getters y Setters
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Integer getTirada1() {
return tirada1;
}
public void setTirada1(Integer tirada1) {
this.tirada1 = tirada1;
}
public Integer getTirada2() {
return tirada2;
}
public void setTirada2(Integer tirada2) {
this.tirada2 = tirada2;
}
public Integer getTirada3() {
return tirada3;
}
public void setTirada3(Integer tirada3) {
this.tirada3 = tirada3;
}
public Integer getTirada4() {
return tirada4;
}
public Integer[] getTiradas() {
return new Integer[] { tirada1, tirada2, tirada3, tirada4 };
}
public void setTirada4(Integer tirada4) {
this.tirada4 = tirada4;
}
public Integer getAncho() {
return ancho;
}
public void setAncho(Integer ancho) {
this.ancho = ancho;
}
public Integer getAlto() {
return alto;
}
public void setAlto(Integer alto) {
this.alto = alto;
}
public Boolean getFormatoPersonalizado() {
return formatoPersonalizado;
}
public void setFormatoPersonalizado(Boolean formatoPersonalizado) {
this.formatoPersonalizado = formatoPersonalizado;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getPaginasNegro() {
return paginasNegro;
}
public void setPaginasNegro(Integer paginasNegro) {
this.paginasNegro = paginasNegro;
}
public Integer getPaginasColor() {
return paginasColor;
}
public void setPaginasColor(Integer paginasColor) {
this.paginasColor = paginasColor;
}
public TipoEncuadernacion getTipoEncuadernacion() {
return tipoEncuadernacion;
}
public void setTipoEncuadernacion(TipoEncuadernacion tipoEncuadernacion) {
this.tipoEncuadernacion = tipoEncuadernacion;
}
public String getPosicionPaginasColor() {
return posicionPaginasColor;
}
public void setPosicionPaginasColor(String posicionPaginasColor) {
this.posicionPaginasColor = posicionPaginasColor;
}
public Integer getPaginasColorTotal() {
return paginasColorTotal;
}
public void setPaginasColorTotal(Integer paginasColorTotal) {
this.paginasColorTotal = paginasColorTotal;
}
public TipoImpresion getTipoImpresion() {
return tipoImpresion;
}
public void setTipoImpresion(TipoImpresion tipoImpresion) {
this.tipoImpresion = tipoImpresion;
}
public Integer getPapelInteriorId() {
return papelInteriorId;
}
public void setPapelInteriorId(Integer papelInteriorId) {
this.papelInteriorId = papelInteriorId;
}
public Integer getGramajeInterior() {
return gramajeInterior;
}
public void setGramajeInterior(Integer gramajeInterior) {
this.gramajeInterior = gramajeInterior;
}
public TipoCubierta getTipoCubierta() {
return tipoCubierta;
}
public void setTipoCubierta(TipoCubierta tipoCubierta) {
this.tipoCubierta = tipoCubierta;
}
public Boolean getSolapasCubierta() {
return solapasCubierta;
}
public void setSolapasCubierta(Boolean solapasCubierta) {
this.solapasCubierta = solapasCubierta;
}
public Integer getTamanioSolapasCubierta() {
return tamanioSolapasCubierta;
}
public void setTamanioSolapasCubierta(Integer tamanioSolapasCubierta) {
this.tamanioSolapasCubierta = tamanioSolapasCubierta;
}
public Integer getCubiertaCaras() {
return cubiertaCaras;
}
public void setCubiertaCaras(Integer cubiertaCaras) {
this.cubiertaCaras = cubiertaCaras;
}
public Integer getPapelGuardasId() {
return papelGuardasId;
}
public void setPapelGuardasId(Integer papelGuardasId) {
this.papelGuardasId = papelGuardasId;
}
public Integer getGramajeGuardas() {
return gramajeGuardas;
}
public void setGramajeGuardas(Integer gramajeGuardas) {
this.gramajeGuardas = gramajeGuardas;
}
public Integer getGuardasImpresas() {
return guardasImpresas;
}
public void setGuardasImpresas(Integer guardasImpresas) {
this.guardasImpresas = guardasImpresas;
}
public String getCabezada() {
return cabezada;
}
public void setCabezada(String cabezada) {
this.cabezada = cabezada;
}
public Integer getPapelCubiertaId() {
return papelCubiertaId;
}
public void setPapelCubiertaId(Integer papelCubiertaId) {
this.papelCubiertaId = papelCubiertaId;
}
public Integer getGramajeCubierta() {
return gramajeCubierta;
}
public void setGramajeCubierta(Integer gramajeCubierta) {
this.gramajeCubierta = gramajeCubierta;
}
}

View File

@ -0,0 +1,204 @@
package com.imprimelibros.erp.presupuesto;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.imprimelibros.erp.externalApi.skApiClient;
import com.imprimelibros.erp.presupuesto.classes.ImagenPresupuesto;
import com.imprimelibros.erp.presupuesto.validation.PresupuestoValidationGroups;
@RestController
@RequestMapping("/presupuesto")
public class PresupuestoController {
@Autowired
protected PresupuestoService presupuestoService;
@Autowired
protected skApiClient apiClient;
@PostMapping("/public/validar/datos-generales")
public ResponseEntity<?> validarDatosGenerales(
@Validated(PresupuestoValidationGroups.DatosGenerales.class) Presupuesto presupuesto,
BindingResult result, Locale locale) {
Map<String, String> errores = new HashMap<>();
// errores de campos individuales
result.getFieldErrors().forEach(error -> errores.put(error.getField(), error.getDefaultMessage()));
// errores globales (@ConsistentTiradas...)
result.getGlobalErrors().forEach(error -> errores.put("global", error.getDefaultMessage()));
if (!errores.isEmpty()) {
return ResponseEntity.badRequest().body(errores);
}
// opciones color
Map<String, Object> resultado = presupuestoService.obtenerOpcionesColor(presupuesto, locale);
// opciones papel interior
resultado.putAll(presupuestoService.obtenerOpcionesPapelInterior(presupuesto, locale));
// opciones gramaje interior
resultado.putAll(presupuestoService.obtenerOpcionesGramajeInterior(presupuesto));
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/validar/interior")
public ResponseEntity<?> validarInterior(
@Validated(PresupuestoValidationGroups.Interior.class) Presupuesto presupuesto,
BindingResult result, Locale locale) {
Map<String, String> errores = new HashMap<>();
// errores de campos individuales
result.getFieldErrors().forEach(error -> errores.put(error.getField(), error.getDefaultMessage()));
// errores globales (@ConsistentTiradas...)
result.getGlobalErrors().forEach(error -> errores.put("global", error.getDefaultMessage()));
if (!errores.isEmpty()) {
return ResponseEntity.badRequest().body(errores);
}
Map<String, Object> resultado = new HashMap<>();
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/get-papel-interior")
public ResponseEntity<?> getPapelInterior(
@Validated(PresupuestoValidationGroups.Interior.class) Presupuesto presupuesto,
BindingResult result, Locale locale) {
Map<String, String> errores = new HashMap<>();
// errores de campos individuales
result.getFieldErrors().forEach(error -> errores.put(error.getField(), error.getDefaultMessage()));
if (!errores.isEmpty()) {
return ResponseEntity.badRequest().body(errores);
}
// opciones color
Map<String, Object> resultado = presupuestoService.obtenerOpcionesPapelInterior(presupuesto, locale);
// opciones gramaje interior
resultado.putAll(presupuestoService.obtenerOpcionesGramajeInterior(presupuesto));
List<String> opciones = (List<String>) resultado.get("opciones_gramaje_interior");
if (opciones != null && !opciones.isEmpty()) {
String gramajeActual = presupuesto.getGramajeInterior().toString();
if (!opciones.contains(gramajeActual)) {
presupuesto.setGramajeInterior(Integer.parseInt(opciones.get(0))); // Asignar primera opción
}
}
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/get-gramaje-interior")
public ResponseEntity<?> getGramajeInterior(
@Validated(PresupuestoValidationGroups.Interior.class) Presupuesto presupuesto,
BindingResult result) {
Map<String, String> errores = new HashMap<>();
// errores de campos individuales
result.getFieldErrors().forEach(error -> errores.put(error.getField(), error.getDefaultMessage()));
if (!errores.isEmpty()) {
return ResponseEntity.badRequest().body(errores);
}
Map<String, Object> resultado = presupuestoService.obtenerOpcionesGramajeInterior(presupuesto);
List<String> opciones = (List<String>) resultado.get("opciones_gramaje_interior");
if (opciones != null && !opciones.isEmpty()) {
String gramajeActual = presupuesto.getGramajeInterior().toString();
if (!opciones.contains(gramajeActual)) {
presupuesto.setGramajeInterior(Integer.parseInt(opciones.get(0))); // Asignar primera opción
}
}
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/get-max-solapas")
public ResponseEntity<?> getMaxSolapas(
@Validated(PresupuestoValidationGroups.Interior.class) Presupuesto presupuesto,
BindingResult result) {
Map<String, String> errores = new HashMap<>();
// errores de campos individuales
result.getFieldErrors().forEach(error -> errores.put(error.getField(), error.getDefaultMessage()));
if (!errores.isEmpty()) {
return ResponseEntity.badRequest().body(errores);
}
Map<String, Object> resultado = new HashMap<>();
resultado.put("solapas", apiClient.getMaxSolapas(presupuestoService.toSkApiRequest(presupuesto)));
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/get-papel-cubierta")
public ResponseEntity<?> getPapelCubierta(
Presupuesto presupuesto,
BindingResult result, Locale locale) {
Map<String, Object> resultado = new HashMap<>();
Map<String, Object> papelesCubierta = presupuestoService.obtenerOpcionesPapelCubierta(presupuesto, locale);
List<ImagenPresupuesto> opciones = (List<ImagenPresupuesto>) presupuestoService
.obtenerOpcionesPapelCubierta(presupuesto, locale)
.get("opciones_papel_cubierta");
if (opciones != null && opciones.stream().noneMatch(
o -> o.getExtra_data().get("sk-id").equals(String.valueOf(presupuesto.getPapelCubiertaId())))) {
presupuesto.setPapelCubiertaId(Integer.valueOf(opciones.get(0).getExtra_data().get("sk-id")));
}
resultado.putAll(papelesCubierta);
resultado.putAll(presupuestoService.obtenerOpcionesGramajeCubierta(presupuesto));
List<String> gramajesCubierta = (List<String>) resultado.get("opciones_gramaje_cubierta");
if (gramajesCubierta != null && !gramajesCubierta.isEmpty()) {
String gramajeActual = presupuesto.getGramajeCubierta().toString();
if (!gramajesCubierta.contains(gramajeActual)) {
presupuesto.setGramajeCubierta(Integer.parseInt(gramajesCubierta.get(0))); // Asignar primera opción
}
}
return ResponseEntity.ok(resultado);
}
@PostMapping("/public/get-gramaje-cubierta")
public ResponseEntity<?> getGramajeCubierta(
Presupuesto presupuesto,
BindingResult result) {
Map<String, Object> resultado = presupuestoService.obtenerOpcionesGramajeCubierta(presupuesto);
List<String> gramajesCubierta = (List<String>) resultado.get("opciones_gramaje_cubierta");
if (gramajesCubierta != null && !gramajesCubierta.isEmpty()) {
String gramajeActual = presupuesto.getGramajeCubierta().toString();
if (!gramajesCubierta.contains(gramajeActual)) {
presupuesto.setGramajeCubierta(Integer.parseInt(gramajesCubierta.get(0))); // Asignar primera opción
}
}
return ResponseEntity.ok(resultado);
}
}

View File

@ -0,0 +1,348 @@
package com.imprimelibros.erp.presupuesto;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imprimelibros.erp.configurationERP.VariableService;
import com.imprimelibros.erp.presupuesto.Presupuesto.TipoCubierta;
import com.imprimelibros.erp.presupuesto.classes.ImagenPresupuesto;
import com.imprimelibros.erp.presupuesto.classes.PresupuestadorItems;
@Service
public class PresupuestoService {
@Autowired
protected VariableService variableService;
private final PresupuestadorItems presupuestadorItems;
public PresupuestoService(PresupuestadorItems presupuestadorItems) {
this.presupuestadorItems = presupuestadorItems;
}
public boolean validateDatosGenerales(int[] tiradas) {
for (int tirada : tiradas) {
if (tirada <= 0) {
return false; // Invalid tirada found
}
}
return true;
}
public Boolean isPOD(Presupuesto presupuesto) {
int pod_value = variableService.getValorEntero("POD");
return (presupuesto.getTirada1() != null && presupuesto.getTirada1() <= pod_value) ||
(presupuesto.getTirada2() != null && presupuesto.getTirada2() <= pod_value) ||
(presupuesto.getTirada3() != null && presupuesto.getTirada3() <= pod_value) ||
(presupuesto.getTirada4() != null && presupuesto.getTirada4() <= pod_value);
}
public Map<String, Object> obtenerOpcionesColor(Presupuesto presupuesto, Locale locale) {
List<ImagenPresupuesto> opciones = new ArrayList<>();
if (presupuesto.getPaginasColor() > 0) {
if (!this.isPOD(presupuesto)) {
// POD solo color foto
ImagenPresupuesto opcionColor = this.presupuestadorItems.getImpresionColor(locale);
opcionColor.setSelected(Presupuesto.TipoImpresion.color.equals(presupuesto.getTipoImpresion()));
opciones.add(opcionColor);
}
ImagenPresupuesto opcionColorHq = this.presupuestadorItems.getImpresionColorPremium(locale);
if(Presupuesto.TipoImpresion.colorhq.equals(presupuesto.getTipoImpresion()))
opcionColorHq.setSelected(true);
opciones.add(opcionColorHq);
} else {
ImagenPresupuesto opcionNegro = this.presupuestadorItems.getImpresionNegro(locale);
if(Presupuesto.TipoImpresion.negro.equals(presupuesto.getTipoImpresion()))
opcionNegro.setSelected(true);
opciones.add(opcionNegro);
ImagenPresupuesto opcionNegroHq = this.presupuestadorItems.getImpresionNegroPremium(locale);
if(Presupuesto.TipoImpresion.negrohq.equals(presupuesto.getTipoImpresion()))
opcionNegroHq.setSelected(true);
opciones.add(opcionNegroHq);
}
boolean opcionSeleccionada = opciones.stream().anyMatch(ImagenPresupuesto::isSelected);
if (!opcionSeleccionada) {
opciones.get(0).setSelected(true);
presupuesto.setTipoImpresion(Presupuesto.TipoImpresion.valueOf(opciones.get(0).getId()));
}
Map<String, Object> response = new HashMap<>();
response.put("opciones_color", opciones);
return response;
}
public Map<String, Object> obtenerOpcionesPapelInterior(Presupuesto presupuesto, Locale locale) {
List<ImagenPresupuesto> opciones = new ArrayList<>();
opciones.add(this.presupuestadorItems.getPapelOffsetBlanco(locale));
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negro ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.color) {
opciones.add(this.presupuestadorItems.getPapelOffsetBlancoVolumen(locale));
}
opciones.add(this.presupuestadorItems.getPapelOffsetAhuesado(locale));
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negro ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.color) {
opciones.add(this.presupuestadorItems.getPapelOffsetAhuesadoVolumen(locale));
}
opciones.add(this.presupuestadorItems.getPapelEstucadoMate(locale));
for (ImagenPresupuesto imagenPresupuesto : opciones) {
imagenPresupuesto.setSelected(
presupuesto.getPapelInteriorId() != null
&& imagenPresupuesto.getExtra_data().get("sk-id").equals(
String.valueOf(presupuesto.getPapelInteriorId())));
}
boolean yaSeleccionado = opciones.stream().anyMatch(ImagenPresupuesto::isSelected);
if (!yaSeleccionado && !opciones.isEmpty()) {
ImagenPresupuesto primeraOpcion = opciones.get(0);
primeraOpcion.setSelected(true);
presupuesto.setPapelInteriorId(Integer.parseInt(primeraOpcion.getExtra_data().get("sk-id")));
}
Map<String, Object> response = new HashMap<>();
response.put("opciones_papel_interior", opciones);
return response;
}
public Map<String, Object> obtenerOpcionesGramajeInterior(Presupuesto presupuesto) {
List<String> gramajes = new ArrayList<>();
final int BLANCO_OFFSET_ID = 3;
final int BLANCO_OFFSET_VOLUMEN_ID = 7;
final int AHUESADO_OFFSET_ID = 4;
final int AHUESADO_OFFSET_VOLUMEN_ID = 6;
final int ESTUCADO_MATE_ID = 2;
if (presupuesto.getPapelInteriorId() != null && presupuesto.getPapelInteriorId() == BLANCO_OFFSET_ID) {
gramajes.add("80");
gramajes.add("90");
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negrohq ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.colorhq) {
gramajes.add("100");
gramajes.add("120");
gramajes.add("150");
gramajes.add("170");
}
} else if (presupuesto.getPapelInteriorId() != null
&& presupuesto.getPapelInteriorId() == BLANCO_OFFSET_VOLUMEN_ID) {
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negro ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.color) {
gramajes.add("80");
}
} else if (presupuesto.getPapelInteriorId() != null && presupuesto.getPapelInteriorId() == AHUESADO_OFFSET_ID) {
gramajes.add("80");
gramajes.add("90");
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negrohq ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.colorhq) {
gramajes.add("100");
}
} else if (presupuesto.getPapelInteriorId() != null
&& presupuesto.getPapelInteriorId() == AHUESADO_OFFSET_VOLUMEN_ID) {
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negro ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.color) {
gramajes.add("70");
gramajes.add("80");
}
} else if (presupuesto.getPapelInteriorId() != null && presupuesto.getPapelInteriorId() == ESTUCADO_MATE_ID) {
if (presupuesto.getTipoImpresion() != Presupuesto.TipoImpresion.color) {
gramajes.add("90");
}
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negrohq ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.colorhq) {
gramajes.add("100");
gramajes.add("115");
}
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negro ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.color) {
gramajes.add("120");
}
if (presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.negrohq ||
presupuesto.getTipoImpresion() == Presupuesto.TipoImpresion.colorhq) {
gramajes.add("125");
gramajes.add("135");
gramajes.add("150");
gramajes.add("170");
gramajes.add("200");
}
}
Map<String, Object> response = new HashMap<>();
response.put("opciones_gramaje_interior", gramajes);
return response;
}
public Map<String, Object> obtenerOpcionesPapelCubierta(Presupuesto presupuesto, Locale locale) {
List<ImagenPresupuesto> opciones = new ArrayList<>();
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaBlanda) {
opciones.add(this.presupuestadorItems.getCartulinaGraficaCubierta(locale));
}
opciones.add(this.presupuestadorItems.getEstucadoMateCubierta(locale));
for (ImagenPresupuesto imagenPresupuesto : opciones) {
imagenPresupuesto.setSelected(
presupuesto.getPapelCubiertaId() != null
&& imagenPresupuesto.getExtra_data().get("sk-id").equals(
String.valueOf(presupuesto.getPapelCubiertaId())));
}
Map<String, Object> response = new HashMap<>();
response.put("opciones_papel_cubierta", opciones);
return response;
}
public Map<String, Object> obtenerOpcionesGramajeCubierta(Presupuesto presupuesto) {
List<String> gramajes = new ArrayList<>();
final int CARTULINA_GRAFICA_ID = 3;
final int ESTUCADO_MATE_ID = 2;
if (presupuesto.getPapelCubiertaId() != null && presupuesto.getPapelCubiertaId() == CARTULINA_GRAFICA_ID) {
gramajes.add("240");
gramajes.add("270");
gramajes.add("300");
gramajes.add("350");
} else if (presupuesto.getPapelCubiertaId() != null && presupuesto.getPapelCubiertaId() == ESTUCADO_MATE_ID) {
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaBlanda) {
gramajes.add("250");
gramajes.add("300");
gramajes.add("350");
} else {
gramajes.add("170");
}
}
Map<String, Object> response = new HashMap<>();
response.put("opciones_gramaje_cubierta", gramajes);
return response;
}
public Map<String, Object> toSkApiRequest(Presupuesto presupuesto) {
final int SK_CLIENTE_ID = 1284;
final int SK_PAGINAS_CUADERNILLO = 32;
Map<String, Object> tamanio = Map.of(
"ancho", presupuesto.getAncho(),
"alto", presupuesto.getAlto());
Map<String, Object> interior = Map.of(
"papelInterior", presupuesto.getPapelInteriorId(),
"gramajeInterior", presupuesto.getGramajeInterior());
Map<String, Object> cubierta = Map.of(
"tipoCubierta", presupuesto.getTipoCubierta().name(),
"papelCubierta", presupuesto.getPapelCubiertaId(),
"gramajeCubierta", presupuesto.getGramajeCubierta(),
"carasImpresion", presupuesto.getCubiertaCaras(),
"solapas", presupuesto.getSolapasCubierta() ? presupuesto.getTamanioSolapasCubierta() : 0,
"acabado", 0, //// Añadir acabados
"cabezada", presupuesto.getCabezada(),
"lomoRedondo", presupuesto.getTipoCubierta() == TipoCubierta.tapaDuraLomoRedondo ? 1 : 0);
/*
* Map<String, Object> sobrecubierta = new HashMap<>();
* sobrecubierta.put("papel", "2");
* sobrecubierta.put("gramaje", 170);
* sobrecubierta.put("solapas", 80);
* sobrecubierta.put("acabado", null);
*
* Map<String, Object> servicios = Map.of(
* "retractilado", 0,
* "retractilado5", 0,
* "ferro", 0,
* "ferroDigital", 0,
* "marcapaginas", 0,
* "prototipo", 0);
*/
Map<String, Object> body = new HashMap<>();
body.put("tipo_impresion_id", this.getTipoImpresionId(presupuesto));
body.put("tirada", Arrays.stream(presupuesto.getTiradas())
.filter(Objects::nonNull)
.collect(Collectors.toList()));
body.put("tamanio", tamanio);
body.put("tipo", presupuesto.getTipoEncuadernacion());
body.put("clienteId", SK_CLIENTE_ID);
body.put("isColor", presupuesto.getTipoImpresion().name().contains("color") ? 1 : 0);
body.put("isHq", presupuesto.getTipoImpresion().name().contains("hq") ? 1 : 0);
body.put("paginas", presupuesto.getPaginasNegro() + presupuesto.getPaginasColor());
body.put("paginasColor", presupuesto.getPaginasColor());
body.put("paginasCuadernillo", SK_PAGINAS_CUADERNILLO);
body.put("interior", interior);
body.put("cubierta", cubierta);
// body.put("sobrecubierta", sobrecubierta);
body.put("guardas", null);
body.put("faja", false);
// body.put("servicios", servicios);
return body;
}
public Integer getTipoImpresionId(Presupuesto presupuesto) {
if (presupuesto.getTipoEncuadernacion() == Presupuesto.TipoEncuadernacion.fresado) {
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDura ||
presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDuraLomoRedondo) {
return 1; // Fresado tapa dura
} else {
return 2; // Fresado tapa blanda
}
} else if (presupuesto.getTipoEncuadernacion() == Presupuesto.TipoEncuadernacion.cosido) {
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDura ||
presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDuraLomoRedondo) {
return 3; // Cosido tapa dura
} else {
return 4; // Cosido tapa blanda
}
} else if (presupuesto.getTipoEncuadernacion() == Presupuesto.TipoEncuadernacion.espiral) {
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDura ||
presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDuraLomoRedondo) {
return 5; // Espiral tapa dura
} else {
return 6; // Espiral tapa blanda
}
} else if (presupuesto.getTipoEncuadernacion() == Presupuesto.TipoEncuadernacion.wireo) {
if (presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDura ||
presupuesto.getTipoCubierta() == Presupuesto.TipoCubierta.tapaDuraLomoRedondo) {
return 7; // Wireo tapa dura
} else {
return 8; // Wireo tapa blanda
}
} else if (presupuesto.getTipoEncuadernacion() == Presupuesto.TipoEncuadernacion.grapado) {
return 21; // Grapado
} else {
return 0; // Default case, no valid type
}
}
}

View File

@ -0,0 +1,85 @@
package com.imprimelibros.erp.presupuesto.classes;
import java.util.Map;
public class ImagenPresupuesto {
private String id;
private String imagen;
private String alt;
private String texto;
private boolean selected = false;
private Map<String, String> extra_data;
// Constructores
public ImagenPresupuesto() {}
public ImagenPresupuesto(String id, String imagen, String alt, String texto, boolean selected) {
this.id = id;
this.imagen = imagen;
this.alt = alt;
this.texto = texto;
this.selected = selected;
}
public ImagenPresupuesto(String id, String imagen, String alt, String texto, Map<String, String> extra_data,boolean selected) {
this.id = id;
this.imagen = imagen;
this.alt = alt;
this.texto = texto;
this.selected = selected;
this.extra_data = extra_data;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImagen() {
return imagen;
}
public void setImagen(String imagen) {
this.imagen = imagen;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getTexto() {
return texto;
}
public void setTexto(String texto) {
this.texto = texto;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Map<String, String> getExtra_data() {
return extra_data;
}
public void setExtra_data(Map<String, String> extra_data) {
this.extra_data = extra_data;
}
}

View File

@ -0,0 +1,129 @@
package com.imprimelibros.erp.presupuesto.classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import java.util.Locale;
import java.util.Map;
@Component
public class PresupuestadorItems {
@Autowired
private MessageSource messageSource;
public ImagenPresupuesto getImpresionNegro(Locale locale) {
return new ImagenPresupuesto(
"negro",
"/assets/images/imprimelibros/presupuestador/blancoYnegro.png",
"",
messageSource.getMessage("presupuesto.blanco-negro", null, locale),
false);
}
public ImagenPresupuesto getImpresionNegroPremium(Locale locale) {
return new ImagenPresupuesto(
"negrohq",
"/assets/images/imprimelibros/presupuestador/negroFoto.png",
"",
messageSource.getMessage("presupuesto.blanco-negro-premium", null, locale),
false);
}
public ImagenPresupuesto getImpresionColor(Locale locale) {
return new ImagenPresupuesto(
"color",
"/assets/images/imprimelibros/presupuestador/color.png",
"",
messageSource.getMessage("presupuesto.color", null, locale),
false);
}
public ImagenPresupuesto getImpresionColorPremium(Locale locale) {
return new ImagenPresupuesto(
"colorhq",
"/assets/images/imprimelibros/presupuestador/colorFoto.png",
"",
messageSource.getMessage("presupuesto.color-premium", null, locale),
false);
}
public ImagenPresupuesto getPapelOffsetBlanco(Locale locale) {
return new ImagenPresupuesto(
"offset-blanco",
"/assets/images/imprimelibros/presupuestador/offset-blanco.png",
"",
messageSource.getMessage("presupuesto.offset-blanco", null, locale),
Map.of("sk-id", "3"),
false);
}
public ImagenPresupuesto getPapelOffsetBlancoVolumen(Locale locale) {
return new ImagenPresupuesto(
"offset-blanco-volumen",
"/assets/images/imprimelibros/presupuestador/offset-blanco.png",
"",
messageSource.getMessage("presupuesto.offset-blanco-volumen", null, locale),
Map.of("sk-id", "7"),
false);
}
public ImagenPresupuesto getPapelOffsetAhuesado(Locale locale) {
return new ImagenPresupuesto(
"offset-ahuesado",
"/assets/images/imprimelibros/presupuestador/offset-ahuesado.png",
"",
messageSource.getMessage("presupuesto.offset-ahuesado", null, locale),
Map.of("sk-id", "4"),
false);
}
public ImagenPresupuesto getPapelOffsetAhuesadoVolumen(Locale locale) {
return new ImagenPresupuesto(
"offset-ahuesado-volumen",
"/assets/images/imprimelibros/presupuestador/offset-ahuesado-volumen.png",
"",
messageSource.getMessage("presupuesto.offset-ahuesado-volumen", null, locale),
Map.of("sk-id", "6"),
false);
}
public ImagenPresupuesto getPapelEstucadoMate(Locale locale) {
return new ImagenPresupuesto(
"estucado-mate",
"/assets/images/imprimelibros/presupuestador/estucado-mate.png",
"",
messageSource.getMessage("presupuesto.estucado-mate", null, locale),
Map.of("sk-id", "2"),
false);
}
public ImagenPresupuesto getCartulinaGraficaCubierta(Locale locale) {
return new ImagenPresupuesto(
"cartulina-grafica-cubierta",
"/assets/images/imprimelibros/presupuestador/cartulina-grafica.png",
"",
messageSource.getMessage("presupuesto.cartulina-grafica-cubierta", null, locale),
Map.of("sk-id", "3"),
false);
}
public ImagenPresupuesto getEstucadoMateCubierta(Locale locale) {
return new ImagenPresupuesto(
"estucado-mate-cubierta",
"/assets/images/imprimelibros/presupuestador/estucado-mate-cubierta.png",
"",
messageSource.getMessage("presupuesto.estucado-mate-cubierta", null, locale),
Map.of("sk-id", "2"),
false);
}
}

View File

@ -0,0 +1,16 @@
package com.imprimelibros.erp.presupuesto.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ConsistentTiradasValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConsistentTiradas {
String message() default "Las tiradas deben ser todas mayores o todas menores al valor POD";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -0,0 +1,53 @@
package com.imprimelibros.erp.presupuesto.validation;
import com.imprimelibros.erp.configurationERP.VariableService;
import com.imprimelibros.erp.presupuesto.Presupuesto;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
public class ConsistentTiradasValidator implements ConstraintValidator<ConsistentTiradas, Presupuesto> {
@Autowired
private VariableService variableService;
@Autowired
private MessageSource messageSource;
@Override
public boolean isValid(Presupuesto presupuesto, ConstraintValidatorContext context) {
if (presupuesto == null)
return true;
Integer[] tiradas = presupuesto.getTiradas();
Integer podValue = variableService.getValorEntero("POD");
boolean allAbove = true;
boolean allBelow = true;
for (Integer t : tiradas) {
if (t == null)
continue;
if (t <= podValue)
allAbove = false;
else // (t > podValue)
allBelow = false;
}
if (!(allAbove || allBelow)) {
String mensajeInterpolado = messageSource.getMessage(
"presupuesto.errores.tiradas.consistentes", // clave del mensaje
new Object[] { podValue }, // parámetros para {0}
LocaleContextHolder.getLocale() // respeta el idioma actual
);
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(mensajeInterpolado)
.addConstraintViolation();
return false;
}
return true;
}
}

View File

@ -0,0 +1,16 @@
package com.imprimelibros.erp.presupuesto.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ParValidator.class)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Par {
String message() default "El valor debe ser un número par";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -0,0 +1,14 @@
package com.imprimelibros.erp.presupuesto.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class ParValidator implements ConstraintValidator<Par, Integer> {
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
if (value == null) return true; // se permite null, usa @NotNull aparte si lo necesitas
return value % 2 == 0;
}
}

View File

@ -0,0 +1,9 @@
package com.imprimelibros.erp.presupuesto.validation;
public class PresupuestoValidationGroups {
public interface DatosGenerales {}
public interface Interior {}
public interface Cubierta {}
}

View File

@ -0,0 +1,19 @@
package com.imprimelibros.erp.presupuesto.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = TamanioValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Tamanio {
String message() default "{presupuesto.errores.tamanio.invalido}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -0,0 +1,55 @@
package com.imprimelibros.erp.presupuesto.validation;
import com.imprimelibros.erp.configurationERP.VariableService;
import com.imprimelibros.erp.presupuesto.Presupuesto;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
public class TamanioValidator implements ConstraintValidator<Tamanio, Presupuesto>{
@Autowired
private VariableService variableService;
@Autowired
private MessageSource messageSource;
@Override
public boolean isValid(Presupuesto presupuesto, ConstraintValidatorContext context) {
if (presupuesto == null)
return true;
Integer min = variableService.getValorEntero("ancho_alto_min");
Integer max = variableService.getValorEntero("ancho_alto_max");
if (presupuesto.getAncho() <= min || presupuesto.getAncho() >= max) {
String mensajeInterpolado = messageSource.getMessage(
"presupuesto.errores.ancho.min_max", // clave del mensaje
new Object[] { min, max }, // parámetros para {0}
LocaleContextHolder.getLocale() // respeta el idioma actual
);
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(mensajeInterpolado)
.addConstraintViolation();
return false;
}
else if (presupuesto.getAlto() <= min || presupuesto.getAlto() >= max) {
String mensajeInterpolado = messageSource.getMessage(
"presupuesto.errores.alto.min_max", // clave del mensaje
new Object[] { min, max }, // parámetros para {0}
LocaleContextHolder.getLocale() // respeta el idioma actual
);
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(mensajeInterpolado)
.addConstraintViolation();
return false;
}
return true;
}
}