From 270d2e4f4b2554c20ab4c4c3170adb563e42bcca Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sun, 3 Oct 2021 12:20:37 +0200
Subject: [PATCH] #596 - added button to add recurring options to new transfer
 page too + combined post routes

---
 .../transactions/TransactionController.java   | 52 +++++++------------
 .../transactions/newTransactionNormal.ftl     |  4 +-
 .../transactions/newTransactionTransfer.ftl   |  6 ++-
 3 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionController.java b/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionController.java
index 5789be4d4..7b49534fa 100644
--- a/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionController.java
+++ b/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionController.java
@@ -131,15 +131,15 @@ public class TransactionController extends BaseController
 		return "transactions/newTransaction" + StringUtils.capitalize(type);
 	}
 
-	@PostMapping(value = "/newTransaction/normal")
-	public String postRepeating(Model model, @CookieValue("currentDate") String cookieDate,
-								@ModelAttribute("NewTransaction") Transaction transaction, BindingResult bindingResult,
-								@RequestParam(value = "isRepeating", required = false) boolean isRepeating,
-								@RequestParam(value = "previousType", required = false) TransactionType previousType,
-								@RequestParam(value = "repeatingModifierNumber", required = false, defaultValue = "0") int repeatingModifierNumber,
-								@RequestParam(value = "repeatingModifierType", required = false) String repeatingModifierType,
-								@RequestParam(value = "repeatingEndType", required = false) String repeatingEndType,
-								@RequestParam(value = "repeatingEndValue", required = false) String repeatingEndValue)
+	@PostMapping(value = "/newTransaction")
+	public String post(Model model, @CookieValue("currentDate") String cookieDate,
+					   @ModelAttribute("NewTransaction") Transaction transaction, BindingResult bindingResult,
+					   @RequestParam(value = "isRepeating", required = false) boolean isRepeating,
+					   @RequestParam(value = "previousType", required = false) TransactionType previousType,
+					   @RequestParam(value = "repeatingModifierNumber", required = false, defaultValue = "0") int repeatingModifierNumber,
+					   @RequestParam(value = "repeatingModifierType", required = false) String repeatingModifierType,
+					   @RequestParam(value = "repeatingEndType", required = false) String repeatingEndType,
+					   @RequestParam(value = "repeatingEndValue", required = false) String repeatingEndValue)
 	{
 		DateTime date = dateService.getDateTimeFromCookie(cookieDate);
 
@@ -161,7 +161,17 @@ public class TransactionController extends BaseController
 			transaction.setRepeatingOption(null);
 		}
 
-		return handleRedirect(model, transaction.getID() != null, transaction, bindingResult, date, "transactions/newTransaction");
+		String redirectUrl;
+		if(transaction.isTransfer())
+		{
+			redirectUrl = "transactions/newTransactionTransfer";
+		}
+		else
+		{
+			redirectUrl = "transactions/newTransactionNormal";
+		}
+
+		return handleRedirect(model, transaction.getID() != null, transaction, bindingResult, date, redirectUrl);
 	}
 
 	private void handlePreviousType(Transaction transaction, boolean isRepeating)
@@ -197,28 +207,6 @@ public class TransactionController extends BaseController
 		return new RepeatingOption(startDate, repeatingModifier, repeatingEnd);
 	}
 
-	@PostMapping(value = "/newTransaction/transfer")
-	public String postTransfer(Model model,
-							   @CookieValue("currentDate") String cookieDate,
-							   @ModelAttribute("NewTransaction") Transaction transaction, BindingResult bindingResult,
-							   @RequestParam(value = "previousType", required = false) TransactionType previousType)
-	{
-		DateTime date = dateService.getDateTimeFromCookie(cookieDate);
-
-//		TODO
-//		handlePreviousType(transaction, isRepeating);
-
-		TransactionValidator transactionValidator = new TransactionValidator();
-		transactionValidator.validate(transaction, bindingResult);
-
-		transactionService.handleAmount(transaction);
-		transactionService.handleTags(transaction);
-
-		transaction.setRepeatingOption(null);
-
-		return handleRedirect(model, transaction.getID() != null, transaction, bindingResult, date, "transactions/newTransactionTransfer");
-	}
-
 	private String handleRedirect(Model model, boolean isEdit, @ModelAttribute("NewTransaction") Transaction transaction, BindingResult bindingResult, DateTime date, String url)
 	{
 		if(bindingResult.hasErrors())
diff --git a/src/main/resources/templates/transactions/newTransactionNormal.ftl b/src/main/resources/templates/transactions/newTransactionNormal.ftl
index 87de8e513..9bbab097b 100644
--- a/src/main/resources/templates/transactions/newTransactionNormal.ftl
+++ b/src/main/resources/templates/transactions/newTransactionNormal.ftl
@@ -33,7 +33,7 @@
                 <@header.content>
                     <div class="container">
                         <#import "../helpers/validation.ftl" as validation>
-                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction/normal'/>" method="post" onsubmit="return validateForm()">
+                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
                             <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                             <!-- only set ID for transactions not templates, otherwise the input is filled with the template ID and saving the transaction
                             may then override an existing transactions if the ID is also already used in transactions table -->
@@ -77,7 +77,7 @@
                             <@newTransactionMacros.transactionRepeating transaction currentDate/>
 
                             <#-- buttons -->
-                            <@newTransactionMacros.buttons "/transactions"/>
+                            <@newTransactionMacros.buttons '/transactions'/>
                             <@newTransactionMacros.buttonTransactionActions isEdit true previousType??/>
                         </form>
 
diff --git a/src/main/resources/templates/transactions/newTransactionTransfer.ftl b/src/main/resources/templates/transactions/newTransactionTransfer.ftl
index b44cc7a1c..36ca67f35 100644
--- a/src/main/resources/templates/transactions/newTransactionTransfer.ftl
+++ b/src/main/resources/templates/transactions/newTransactionTransfer.ftl
@@ -33,13 +33,14 @@
                 <@header.content>
                     <div class="container">
                         <#import "../helpers/validation.ftl" as validation>
-                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction/transfer'/>" method="post" onsubmit="return validateForm()">
+                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
                             <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                             <!-- only set ID for transactions not templates, otherwise the input is filled with the template ID and saving the transaction
                             may then override an existing transactions if the ID is also already used in transactions table -->
                             <input type="hidden" name="ID" value="<#if transaction.class.simpleName == "Transaction" && transaction.getID()??>${transaction.getID()?c}</#if>">
                             <input type="hidden" name="isExpenditure" value="true">
                             <input type="hidden" name="previousType" value="<#if previousType??>${previousType.name()}</#if>">
+                            <input type="hidden" name="isRepeating" value="${transaction.isRepeating()?c}">
 
                             <#assign hint=helpers.getHintByLocalizationKey("hint.transaction.save")/>
                             <@header.hint hint=hint/>
@@ -78,6 +79,9 @@
                             </#if>
                             <@customSelectMacros.customAccountSelect "transfer-account-select-wrapper" "transferAccount" accounts selectedTransferAccount "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.transfer.account") "transaction-destination-account"/>
 
+                            <#-- repeating options -->
+                            <@newTransactionMacros.transactionRepeating transaction currentDate/>
+
                             <#-- buttons -->
                             <@newTransactionMacros.buttons '/transactions'/>
                             <@newTransactionMacros.buttonTransactionActions isEdit true previousType??/>
-- 
GitLab