feat/kindErstellen #9

Merged
PBS2H21ASH merged 23 commits from feat/kindErstellen into stable 2023-02-05 20:40:12 +01:00
2 changed files with 31 additions and 6 deletions
Showing only changes of commit 4c9a3343d2 - Show all commits

1
.gitignore vendored
View File

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

View File

@ -9,6 +9,10 @@ import javafx.scene.control.Spinner;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text; 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.Calendar;
import java.util.List; import java.util.List;
@ -30,13 +34,14 @@ public class InvoiceController {
jahrSpinner.getValueFactory().setValue(Calendar.getInstance().get(Calendar.YEAR)); jahrSpinner.getValueFactory().setValue(Calendar.getInstance().get(Calendar.YEAR));
List<Child> childList = AccountMgr.getAllChildren(); 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); childChoiceBox.setItems(childOptions);
} }
@FXML @FXML
void onRechnungErstellenClick(MouseEvent mouseEvent) { void onRechnungErstellenClick(MouseEvent mouseEvent) {
if(childChoiceBox.getValue() == null){ if (childChoiceBox.getValue() == null) {
Alert alert = new Alert(Alert.AlertType.ERROR); Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Es wurde kein Kind ausgewählt"); alert.setTitle("Es wurde kein Kind ausgewählt");
alert.setHeaderText("Bitte wählen sie ein Kind aus"); alert.setHeaderText("Bitte wählen sie ein Kind aus");
@ -44,7 +49,8 @@ public class InvoiceController {
} }
String childId = childChoiceBox.getValue().toString().split(":")[0]; 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); System.out.println("Invoice (" + date + ") from child: " + childId);
@ -52,8 +58,9 @@ public class InvoiceController {
responseText.setText(invoice.get(invoice.size() - 1)); responseText.setText(invoice.get(invoice.size() - 1));
//TODO: show invoice // 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) { private int monthToInt(String month) {
@ -91,4 +98,21 @@ public class InvoiceController {
default -> ""; 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);
}
}
} }