From 4c9a3343d214f6e50c4a92ea3c4fe8f2e5ab8729 Mon Sep 17 00:00:00 2001 From: Johannes Kantz <67144859+JohannesKantz@users.noreply.github.com> Date: Sat, 4 Feb 2023 19:24:08 +0100 Subject: [PATCH] add: export invoice to txt --- .gitignore | 3 +- .../InvoiceController.java | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 1165112..7deba91 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,5 @@ build/ ### Mac OS ### .DS_Store -/database.db \ No newline at end of file +/database.db +/Rechnungen \ No newline at end of file diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java b/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java index 58af018..0a319db 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/InvoiceController.java @@ -9,6 +9,10 @@ import javafx.scene.control.Spinner; import javafx.scene.input.MouseEvent; import javafx.scene.text.Text; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Calendar; import java.util.List; @@ -30,13 +34,14 @@ public class InvoiceController { jahrSpinner.getValueFactory().setValue(Calendar.getInstance().get(Calendar.YEAR)); List childList = AccountMgr.getAllChildren(); - ObservableList childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); + ObservableList childOptions = FXCollections.observableArrayList(childList.stream() + .map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); childChoiceBox.setItems(childOptions); } @FXML void onRechnungErstellenClick(MouseEvent mouseEvent) { - if(childChoiceBox.getValue() == null){ + if (childChoiceBox.getValue() == null) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Es wurde kein Kind ausgewählt"); alert.setHeaderText("Bitte wählen sie ein Kind aus"); @@ -44,7 +49,8 @@ public class InvoiceController { } String childId = childChoiceBox.getValue().toString().split(":")[0]; - String date = String.format("%d-%02d", Integer.parseInt(jahrSpinner.getValue().toString()), monthToInt(monatChoiceBox.getValue().toString())); + String date = String.format("%d-%02d", Integer.parseInt(jahrSpinner.getValue().toString()), + monthToInt(monatChoiceBox.getValue().toString())); System.out.println("Invoice (" + date + ") from child: " + childId); @@ -52,8 +58,9 @@ public class InvoiceController { responseText.setText(invoice.get(invoice.size() - 1)); - //TODO: show invoice - //TODO: export invoice as word or pdf + // TODO: show invoice + Child child = AccountMgr.getChildById(Long.parseLong(childId)); + exportInvoice("Rechnung_" + date + "_" + childId + "_" + child.getName() +"_" + child.getFirstname(), invoice); } private int monthToInt(String month) { @@ -91,4 +98,21 @@ public class InvoiceController { default -> ""; }; } + + private void exportInvoice(String filename, List invoice) { + try { + Files.createDirectories(Path.of(Path.of("").toAbsolutePath() + "/Rechnungen")); + + // TODO: save invoice to pdf or word + File file = new File(Path.of("").toAbsolutePath() + "/Rechnungen/" + filename + ".txt"); + if (file.createNewFile()) { + System.out.println("File created: " + file.getName()); + } else { + System.out.println("File already exists."); + } + Files.write(Path.of(file.getAbsolutePath()), invoice); + } catch (IOException e) { + throw new RuntimeException(e); + } + } }