add: export invoice to txt

This commit is contained in:
Johannes Kantz 2023-02-04 19:24:08 +01:00
parent 745eddea31
commit 4c9a3343d2
2 changed files with 31 additions and 6 deletions

1
.gitignore vendored
View File

@ -38,3 +38,4 @@ build/
.DS_Store
/database.db
/Rechnungen

View File

@ -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,7 +34,8 @@ public class InvoiceController {
jahrSpinner.getValueFactory().setValue(Calendar.getInstance().get(Calendar.YEAR));
List<Child> childList = AccountMgr.getAllChildren();
ObservableList<Object> childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0]));
ObservableList<Object> childOptions = FXCollections.observableArrayList(childList.stream()
.map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0]));
childChoiceBox.setItems(childOptions);
}
@ -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);
@ -53,7 +59,8 @@ public class InvoiceController {
responseText.setText(invoice.get(invoice.size() - 1));
// TODO: show invoice
//TODO: export invoice as word or pdf
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<String> 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);
}
}
}