Added better invoice generation
This commit is contained in:
parent
bcc377117d
commit
b52519979c
|
|
@ -1,6 +1,9 @@
|
||||||
package com.denniseckerskorn.services.finance_service;
|
package com.denniseckerskorn.services.finance_service;
|
||||||
|
|
||||||
import com.denniseckerskorn.entities.finance.Invoice;
|
import com.denniseckerskorn.entities.finance.Invoice;
|
||||||
|
import com.denniseckerskorn.entities.finance.InvoiceLine;
|
||||||
|
import com.denniseckerskorn.entities.finance.ProductService;
|
||||||
|
import com.denniseckerskorn.entities.user_managment.users.Student;
|
||||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
import com.itextpdf.layout.Document;
|
import com.itextpdf.layout.Document;
|
||||||
|
|
@ -8,6 +11,7 @@ import com.itextpdf.layout.element.Paragraph;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class InvoicePdfGenerator {
|
public class InvoicePdfGenerator {
|
||||||
|
|
@ -19,24 +23,65 @@ public class InvoicePdfGenerator {
|
||||||
PdfDocument pdf = new PdfDocument(writer);
|
PdfDocument pdf = new PdfDocument(writer);
|
||||||
Document doc = new Document(pdf);
|
Document doc = new Document(pdf);
|
||||||
|
|
||||||
doc.add(new Paragraph("Factura #" + invoice.getId()));
|
// Cabecera
|
||||||
|
doc.add(new Paragraph("FACTURA #" + invoice.getId()).setFontSize(16).setBold());
|
||||||
doc.add(new Paragraph("Fecha: " + invoice.getDate()));
|
doc.add(new Paragraph("Fecha: " + invoice.getDate()));
|
||||||
doc.add(new Paragraph("Cliente: " + invoice.getUser().getName() + " " + invoice.getUser().getSurname()));
|
|
||||||
doc.add(new Paragraph("Estado: " + invoice.getStatus()));
|
doc.add(new Paragraph("Estado: " + invoice.getStatus()));
|
||||||
|
doc.add(new Paragraph(" "));
|
||||||
|
|
||||||
|
// Información del cliente
|
||||||
|
var user = invoice.getUser();
|
||||||
|
doc.add(new Paragraph("Cliente: " + user.getName() + " " + user.getSurname()));
|
||||||
|
doc.add(new Paragraph("Email: " + user.getEmail()));
|
||||||
|
doc.add(new Paragraph("Teléfono: " + user.getPhoneNumber()));
|
||||||
|
doc.add(new Paragraph("Dirección: " + user.getAddress()));
|
||||||
|
|
||||||
|
// Si es estudiante, mostrar info básica (sin tutor ni cinturón)
|
||||||
|
Student student = user.getStudent();
|
||||||
|
if (student != null) {
|
||||||
|
doc.add(new Paragraph("DNI: " + student.getDni()));
|
||||||
|
doc.add(new Paragraph("Fecha de nacimiento: " + student.getBirthdate()));
|
||||||
|
}
|
||||||
|
|
||||||
doc.add(new Paragraph(" "));
|
doc.add(new Paragraph(" "));
|
||||||
doc.add(new Paragraph("Detalle de productos:"));
|
|
||||||
|
|
||||||
invoice.getInvoiceLines().forEach(line -> {
|
// Detalle de productos
|
||||||
String product = line.getProductService().getName();
|
doc.add(new Paragraph("Detalle de productos:").setBold());
|
||||||
int qty = line.getQuantity();
|
doc.add(new Paragraph("Producto | Cant. | Precio | IVA % | Subtotal (sin IVA) | IVA"));
|
||||||
var price = line.getUnitPrice();
|
|
||||||
var total = line.getSubtotal();
|
BigDecimal totalSubtotal = BigDecimal.ZERO;
|
||||||
doc.add(new Paragraph("- " + product + " x" + qty + " - " + price + " € = " + total + " €"));
|
BigDecimal totalIVA = BigDecimal.ZERO;
|
||||||
});
|
|
||||||
|
for (InvoiceLine line : invoice.getInvoiceLines()) {
|
||||||
|
ProductService product = line.getProductService();
|
||||||
|
String nombre = product.getName();
|
||||||
|
int cantidad = line.getQuantity();
|
||||||
|
BigDecimal unitPrice = line.getUnitPrice(); // sin IVA
|
||||||
|
BigDecimal subtotalSinIVA = unitPrice.multiply(BigDecimal.valueOf(cantidad));
|
||||||
|
BigDecimal porcentajeIVA = product.getIvaType().getPercentage();
|
||||||
|
BigDecimal iva = subtotalSinIVA.multiply(porcentajeIVA).divide(BigDecimal.valueOf(100));
|
||||||
|
|
||||||
|
totalSubtotal = totalSubtotal.add(subtotalSinIVA);
|
||||||
|
totalIVA = totalIVA.add(iva);
|
||||||
|
|
||||||
|
doc.add(new Paragraph(
|
||||||
|
nombre + " | " +
|
||||||
|
cantidad + " | " +
|
||||||
|
unitPrice + " € | " +
|
||||||
|
porcentajeIVA + "% | " +
|
||||||
|
subtotalSinIVA + " € | " +
|
||||||
|
iva + " €"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Totales
|
||||||
|
doc.add(new Paragraph(" "));
|
||||||
|
doc.add(new Paragraph("Subtotal (sin IVA): " + totalSubtotal + " €").setBold());
|
||||||
|
doc.add(new Paragraph("IVA total: " + totalIVA + " €").setBold());
|
||||||
|
doc.add(new Paragraph("TOTAL: " + totalSubtotal.add(totalIVA) + " €").setFontSize(14).setBold());
|
||||||
|
|
||||||
doc.add(new Paragraph(" "));
|
doc.add(new Paragraph(" "));
|
||||||
doc.add(new Paragraph("TOTAL: " + invoice.getTotal() + " €"));
|
doc.add(new Paragraph("Gracias por su compra.").setItalic());
|
||||||
|
|
||||||
doc.close();
|
doc.close();
|
||||||
return out.toByteArray();
|
return out.toByteArray();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue