add: export invoice to txt
This commit is contained in:
		| @@ -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<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); | ||||
|     } | ||||
|  | ||||
|     @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<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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Johannes Kantz
					Johannes Kantz