mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
draft de impresion presupuesto, añadiendo posibilidades de iva
This commit is contained in:
@ -5,92 +5,129 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.awt.Desktop;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
class PdfSmokeTest {
|
||||
|
||||
@Autowired
|
||||
PdfService pdfService;
|
||||
@Autowired
|
||||
PdfService pdfService;
|
||||
@Autowired
|
||||
PdfTemplateEngine templateEngine;
|
||||
@Autowired
|
||||
TemplateRegistry templateRegistry;
|
||||
|
||||
@Test
|
||||
void generaPresupuesto() throws Exception {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("numero", "2025-00123");
|
||||
model.put("fecha", LocalDate.of(2025, 10, 12));
|
||||
model.put("validezDias", 30);
|
||||
@Test
|
||||
void generaPresupuesto() throws Exception {
|
||||
|
||||
model.put("empresa", Map.of(
|
||||
"nombre", "ImprimeLibros ERP",
|
||||
"direccion", "C/ Dirección 123, 28000 Madrid",
|
||||
"telefono", "+34 600 000 000",
|
||||
"email", "info@imprimelibros.com",
|
||||
"cif", "B-12345678",
|
||||
"web", "www.imprimelibros.com"));
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("numero", "2025-00123");
|
||||
model.put("fecha", LocalDate.of(2025, 10, 12));
|
||||
model.put("validezDias", 30);
|
||||
|
||||
model.put("cliente", Map.of(
|
||||
"nombre", "Editorial Ejemplo S.L.",
|
||||
"cif", "B-00000000",
|
||||
"direccion", "Av. de los Libros, 45",
|
||||
"cp", "28001",
|
||||
"poblacion", "Madrid",
|
||||
"provincia", "Madrid",
|
||||
"email", "compras@editorial.com"));
|
||||
model.put("empresa", Map.of(
|
||||
"nombre", "ImprimeLibros ERP",
|
||||
"direccion", "C/ Dirección 123, 28000 Madrid",
|
||||
"telefono", "+34 600 000 000",
|
||||
"email", "info@imprimelibros.com",
|
||||
"cif", "B-12345678",
|
||||
"cp", "28000",
|
||||
"poblacion", "Madrid",
|
||||
"web", "www.imprimelibros.com"));
|
||||
|
||||
model.put("titulo", "Libro de Ejemplo");
|
||||
model.put("autor", "Autora Demo");
|
||||
model.put("isbn", "978-1-2345-6789-0");
|
||||
model.put("ancho", 150);
|
||||
model.put("alto", 210);
|
||||
model.put("formatoPersonalizado", true);
|
||||
model.put("paginasNegro", "300");
|
||||
model.put("paginasColor", null);
|
||||
model.put("tiradas", List.of(300, 500, 1000));
|
||||
model.put("cliente", Map.of(
|
||||
"nombre", "Editorial Ejemplo S.L.",
|
||||
"cif", "B-00000000",
|
||||
"direccion", "Av. de los Libros, 45",
|
||||
"cp", "28001",
|
||||
"poblacion", "Madrid",
|
||||
"provincia", "Madrid",
|
||||
"email", "compras@editorial.com"));
|
||||
|
||||
model.put("lineas", List.of(
|
||||
Map.of("descripcion", "Impresión interior B/N offset 80 g",
|
||||
"meta", "300 páginas · tinta negra · papel 80 g",
|
||||
"uds", 1000,
|
||||
"precio", 2.15,
|
||||
"dto", 0,
|
||||
"importe", 2150.0),
|
||||
Map.of("descripcion", "Cubierta color 300 g laminado mate",
|
||||
"meta", "Lomo 15 mm · 4/0 · laminado mate",
|
||||
"uds", 1000,
|
||||
"precio", 0.38,
|
||||
"dto", 5.0,
|
||||
"importe", 361.0)));
|
||||
model.put("titulo", "Libro de Ejemplo");
|
||||
model.put("autor", "Autora Demo");
|
||||
model.put("isbn", "978-1-2345-6789-0");
|
||||
model.put("ancho", 150);
|
||||
model.put("alto", 210);
|
||||
model.put("formatoPersonalizado", true);
|
||||
model.put("paginasNegro", "300");
|
||||
model.put("paginasColor", null);
|
||||
model.put("tiradas", List.of(300, 500, 1000));
|
||||
|
||||
model.put("servicios", List.of(
|
||||
Map.of("descripcion", "Transporte península", "unidades", 1, "precio", 90.00)));
|
||||
model.put("lineas", List.of(
|
||||
Map.of("descripcion", "Impresión interior B/N offset 80 g",
|
||||
"meta", "300 páginas · tinta negra · papel 80 g",
|
||||
"uds", 1000,
|
||||
"precio", 2.15,
|
||||
"dto", 0,
|
||||
"importe", 2150.0),
|
||||
Map.of("descripcion", "Cubierta color 300 g laminado mate",
|
||||
"meta", "Lomo 15 mm · 4/0 · laminado mate",
|
||||
"uds", 1000,
|
||||
"precio", 0.38,
|
||||
"dto", 5.0,
|
||||
"importe", 361.0)));
|
||||
|
||||
model.put("baseImponible", 2601.0);
|
||||
model.put("ivaTipo", 21);
|
||||
model.put("ivaImporte", 546.21);
|
||||
model.put("totalConIva", 3147.21);
|
||||
model.put("peso", 120.0);
|
||||
model.put("servicios", List.of(
|
||||
Map.of("descripcion", "Transporte península", "unidades", 1, "precio", 90.00)));
|
||||
|
||||
model.put("observaciones", "Presupuesto válido 30 días.<br/>Incluye embalaje estándar.");
|
||||
model.put("condiciones", "Entrega 7-10 días laborables tras confirmación de artes finales.");
|
||||
model.put("baseImponible", 2601.0);
|
||||
model.put("ivaTipo", 21);
|
||||
model.put("ivaImporte", 546.21);
|
||||
model.put("totalConIva", 3147.21);
|
||||
model.put("peso", 120.0);
|
||||
|
||||
var spec = new DocumentSpec(
|
||||
DocumentType.PRESUPUESTO,
|
||||
"presupuesto-a4",
|
||||
Locale.forLanguageTag("es-ES"),
|
||||
model);
|
||||
model.put("observaciones", "Presupuesto válido 30 días.<br/>Incluye embalaje estándar.");
|
||||
model.put("condiciones", "Entrega 7-10 días laborables tras confirmación de artes finales.");
|
||||
|
||||
byte[] pdf = pdfService.generate(spec);
|
||||
Map<String, Object> pricing = new HashMap<>();
|
||||
pricing.put("tiradas", List.of(100, 200, 300));
|
||||
pricing.put("impresion", List.of(152.15, 276.12, 377.36));
|
||||
pricing.put("iva", List.of(7.68, 12.60, 16.72));
|
||||
pricing.put("total", List.of(159.99, 287.91, 395.03));
|
||||
pricing.put("unidad", List.of(1.52, 1.38, 1.26));
|
||||
model.put("pricing", pricing);
|
||||
|
||||
Path out = Path.of("target/presupuesto-test.pdf");
|
||||
Files.createDirectories(out.getParent());
|
||||
Files.write(out, pdf);
|
||||
var spec = new DocumentSpec(
|
||||
DocumentType.PRESUPUESTO,
|
||||
"presupuesto-a4",
|
||||
Locale.forLanguageTag("es-ES"),
|
||||
model);
|
||||
|
||||
System.out.println("✅ PDF generado en: " + out.toAbsolutePath());
|
||||
}
|
||||
byte[] pdf = pdfService.generate(spec);
|
||||
|
||||
// HTML
|
||||
|
||||
String templateName = templateRegistry.resolve(DocumentType.PRESUPUESTO, "presupuesto-a4");
|
||||
String html = templateEngine.render(templateName, Locale.forLanguageTag("es-ES"), model);
|
||||
String css = Files.readString(Path.of("src/main/resources/static/assets/css/presupuestopdf.css"));
|
||||
String htmlWithCss = html.replaceFirst(
|
||||
"(?i)</head>",
|
||||
"<style>\n" + css + "\n</style>\n</head>");
|
||||
Path htmlPath = Path.of("target/presupuesto-test.html");
|
||||
Files.writeString(htmlPath, htmlWithCss, StandardCharsets.UTF_8);
|
||||
|
||||
System.out.println("✅ HTML exportado: target/presupuesto-test.html");
|
||||
|
||||
// 🟢 Abrir en el navegador (si está soportado)
|
||||
if (Desktop.isDesktopSupported()) {
|
||||
Desktop.getDesktop().browse(htmlPath.toUri());
|
||||
}
|
||||
|
||||
Path out = Path.of("target/presupuesto-test.pdf");
|
||||
Files.createDirectories(out.getParent());
|
||||
Files.write(out, pdf);
|
||||
|
||||
System.out.println("✅ PDF generado en: " + out.toAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user