From 3cd83bc3d09fd6584d6dd0476d7a44aa2d9e71e7 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sun, 5 Jul 2020 17:18:03 +0200
Subject: [PATCH] Fixed #529 - don't set template ID to null in controller, use
 ftl instead

---
 .../budgetmaster/templates/TemplateController.java            | 2 --
 .../resources/templates/transactions/newTransactionNormal.ftl | 4 +++-
 .../templates/transactions/newTransactionTransfer.ftl         | 4 +++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java b/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
index a29f6371b..617a0a21b 100644
--- a/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
+++ b/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
@@ -126,8 +126,6 @@ public class TemplateController extends BaseController
 
 		final Template template = templateOptional.get();
 
-		// set ID to null, otherwise the transaction form is prefilled with the template ID and saving the transaction
-		// may then override an existing transactions if the ID is also already used in transactions table
 		template.setID(null);
 
 		templateService.prepareTemplateForNewTransaction(template, true);
diff --git a/src/main/resources/templates/transactions/newTransactionNormal.ftl b/src/main/resources/templates/transactions/newTransactionNormal.ftl
index 6cbfe5e44..57aac21ec 100644
--- a/src/main/resources/templates/transactions/newTransactionNormal.ftl
+++ b/src/main/resources/templates/transactions/newTransactionNormal.ftl
@@ -26,7 +26,9 @@
                     <#import "../helpers/validation.ftl" as validation>
                     <form name="NewTransaction" action="<@s.url '/transactions/newTransaction/normal'/>" method="post" onsubmit="return validateForm()">
                         <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
-                        <input type="hidden" name="ID" value="<#if transaction.getID()??>${transaction.getID()?c}</#if>">
+                        <!-- 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>">
 
                         <#-- isPayment switch -->
                         <@newTransactionMacros.isExpenditureSwitch transaction isPayment/>
diff --git a/src/main/resources/templates/transactions/newTransactionTransfer.ftl b/src/main/resources/templates/transactions/newTransactionTransfer.ftl
index 6ae040977..66329a093 100644
--- a/src/main/resources/templates/transactions/newTransactionTransfer.ftl
+++ b/src/main/resources/templates/transactions/newTransactionTransfer.ftl
@@ -26,7 +26,9 @@
                     <#import "../helpers/validation.ftl" as validation>
                     <form name="NewTransaction" action="<@s.url '/transactions/newTransaction/transfer'/>" method="post" onsubmit="return validateForm()">
                         <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
-                        <input type="hidden" name="ID" value="<#if transaction.getID()??>${transaction.getID()?c}</#if>">
+                        <!-- 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="isPayment" value="true">
 
                         <#-- name -->
-- 
GitLab