Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Mautsch committed Nov 12, 2024
1 parent 03ff2b5 commit 4afda06
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,96 @@
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;

@Component
public class InvoiceCreatorLogic {
public record Organization(String name, java.util.List<String> address) {}
public record Patient(String givenName, String familyName, java.util.List<String> address) {}

public record InvoiceData(
java.util.List<String> patientAddress
Organization organization,
Patient patient,

String textBlockIntro,
String textBlockOutro
) {
}

public byte[] createInvoice(InvoiceData invoiceData) {
var document = new Document(PageSize.A4, 50, 50, 50, 50);
var fontNormal = FontFactory.getFont(FontFactory.HELVETICA, 12); // Regular font for other text


// Create a PDF writer instance and pass output stream
try (var out = new ByteArrayOutputStream()) {
PdfWriter.getInstance(document, out);

document.open();
addTable(invoiceData, document);
addTextSection(document);

addHeader(document, invoiceData);
addTable(document, invoiceData);
addDiagnosis(document);

document.add(new Paragraph(invoiceData.textBlockIntro(), fontNormal));
document.add(new Paragraph(invoiceData.textBlockOutro(), fontNormal));

document.close();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return new byte[0]; // Return empty byte array on error
}

private static void addTable(InvoiceData invoiceData, Document document) {
private static void addHeader(Document document, InvoiceData invoiceData) throws DocumentException {
// Fonts for bold and regular text
var fontBold = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 14);
var fontNormal = FontFactory.getFont(FontFactory.HELVETICA, 12);

// Paragraph for the bold title, centered
var titleParagraph = new Paragraph(invoiceData.organization().name(), fontBold);
titleParagraph.setAlignment(Element.ALIGN_CENTER);

// Paragraph for the address, also centered
var addressParagraph = new Paragraph(String.join("\n", invoiceData.organization().address()), fontNormal);
addressParagraph.setAlignment(Element.ALIGN_CENTER);

// Add the title and address to the document
document.add(titleParagraph);
document.add(addressParagraph);

// Add a separator line as a long line below the header
var separator = new Paragraph(new Chunk(new LineSeparator()));
separator.setSpacingBefore(10f); // Adjust spacing if needed
separator.setSpacingAfter(10f);
document.add(separator);
}

private static void addTable(Document document, InvoiceData invoiceData) {
var table = new PdfPTable(2);
table.setWidthPercentage(100); // Set table width to 100%
table.addCell(addAddress(invoiceData));
table.addCell(addInvoiceNumber());
table.addCell(addLocationDate());

document.add(table);
}
table.addCell(createCell(String.join("\n", invoiceData.patient().address), Element.ALIGN_LEFT));
table.addCell(createCell("Invoicenumber 29022024-165", Element.ALIGN_RIGHT));
table.addCell(createCell("Springfield, 01.01.2030", Element.ALIGN_RIGHT));

private static PdfPCell addAddress(InvoiceData invoiceData) {
var font = FontFactory.getFont(FontFactory.HELVETICA, 12);
var cell = new PdfPCell(new Paragraph(String.join("\n", invoiceData.patientAddress), font));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_TOP);
return cell;
document.add(table);
}

private static PdfPCell addInvoiceNumber() {
var font = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
var cell = new PdfPCell(new Paragraph("Invoicenumber 29022024-165", font));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setVerticalAlignment(Element.ALIGN_TOP);
return cell;
}

private static PdfPCell addLocationDate() {
private static PdfPCell createCell(String string, int allignment) {
var font = FontFactory.getFont(FontFactory.HELVETICA, 12);
var cell = new PdfPCell(new Paragraph("Springfield, 01.01.2030", font));
var cell = new PdfPCell(new Paragraph(string, font));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setHorizontalAlignment(allignment);
cell.setVerticalAlignment(Element.ALIGN_TOP);
return cell;
}

private static void addTextSection(Document document) throws DocumentException {
private static void addDiagnosis(Document document) throws DocumentException {
var fontBold = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12); // Bold font for diagnosis
var fontNormal = FontFactory.getFont(FontFactory.HELVETICA, 12); // Regular font for other text

Expand All @@ -84,15 +106,6 @@ private static void addTextSection(Document document) throws DocumentException {
// Add the diagnosis to the document
document.add(diagnosisParagraph);
document.add(new Paragraph("\n"));

// Add the greeting and text
var greetingParagraph = new Paragraph("Dear Mr. Simpson,\n", fontNormal);
var servicesParagraph = new Paragraph("for my work i would like to charge you with the following", fontNormal);

// Add the other text sections to the document
document.add(greetingParagraph);
document.add(servicesParagraph);
}


}
11 changes: 10 additions & 1 deletion src/test/java/org/goafabric/calleeservice/logic/InvoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ public class InvoiceTest {

@Test
public void createInvoice() throws IOException {
var organization = new InvoiceCreatorLogic.Organization("Practice Dr. Hibbert",
Arrays.asList("Hibbertstreet 123", "443 Shelbyville"));

var patient = new InvoiceCreatorLogic.Patient("Homer", "Simpson",
Arrays.asList("Mr", "Homer Simpson", "Evergreen Terrace 742", "443 Springfield"));

var textBlockIntro = "Dear Mr. Simpson,\nfor my work i would like to charge you with the following items:";
var textBlockOutro = "Please pay the until the 01.01.2031 refering the invoice numbers";


var patientAddress = Arrays.asList("Mr", "Homer Simpson", "Evergreen Terrace 742", "443 Springfield");
var invoiceData = new InvoiceCreatorLogic.InvoiceData(patientAddress);
var invoiceData = new InvoiceCreatorLogic.InvoiceData(organization, patient, textBlockIntro, textBlockOutro);
Files.write(new File("invoice.pdf").toPath(), invoiceCreatorLogic.createInvoice(invoiceData));
}

Expand Down

0 comments on commit 4afda06

Please sign in to comment.