From 7c112a8bf83c326d8dc187f6f5226f9edbe4db72 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Wed, 10 Jun 2020 22:09:43 +0200
Subject: [PATCH] Fixed #520 - templates: don't save and prefill empty amount

---
 .../deadlocker8/budgetmaster/templates/Template.java | 10 ++++++++++
 .../budgetmaster/templates/TemplateController.java   | 12 +++++++++---
 src/main/resources/static/js/transactions.js         |  2 +-
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/main/java/de/deadlocker8/budgetmaster/templates/Template.java b/src/main/java/de/deadlocker8/budgetmaster/templates/Template.java
index 52de03249..74e4478ce 100644
--- a/src/main/java/de/deadlocker8/budgetmaster/templates/Template.java
+++ b/src/main/java/de/deadlocker8/budgetmaster/templates/Template.java
@@ -184,6 +184,16 @@ public class Template implements TransactionBase
 		return transferAccount != null;
 	}
 
+	public boolean isPayment()
+	{
+		if(this.amount == null)
+		{
+			return true;
+		}
+
+		return this.amount <= 0;
+	}
+
 	@Override
 	public String toString()
 	{
diff --git a/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java b/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
index f42e236ba..e47499375 100644
--- a/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
+++ b/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java
@@ -76,7 +76,10 @@ public class TemplateController extends BaseController
 									  @RequestParam(value = "includeCategory") Boolean includeCategory,
 									  @RequestParam(value = "includeAccount") Boolean includeAccount)
 	{
-		transactionService.handleAmount(transaction, isPayment);
+		if(transaction.getAmount() != null)
+		{
+			transactionService.handleAmount(transaction, isPayment);
+		}
 		transactionService.handleTags(transaction);
 
 		if(templateName == null || templateName.isEmpty())
@@ -172,7 +175,10 @@ public class TemplateController extends BaseController
 		TemplateValidator templateValidator = new TemplateValidator(previousTemplateName, templateService.getExistingTemplateNames());
 		templateValidator.validate(template, bindingResult);
 
-		transactionService.handleAmount(template, isPayment);
+		if(template.getAmount() != null)
+		{
+			transactionService.handleAmount(template, isPayment);
+		}
 		transactionService.handleTags(template);
 
 		if(bindingResult.hasErrors())
@@ -207,7 +213,7 @@ public class TemplateController extends BaseController
 
 		Template template = templateOptional.get();
 		templateService.prepareTemplateForNewTransaction(template, false);
-		templateService.prepareModelNewOrEdit(model, true, template, template.getAmount() <= 0, accountService.getAllAccountsAsc());
+		templateService.prepareModelNewOrEdit(model, true, template, template.isPayment(), accountService.getAllAccountsAsc());
 
 		return "templates/newTemplate";
 	}
diff --git a/src/main/resources/static/js/transactions.js b/src/main/resources/static/js/transactions.js
index 04e6ee14b..b24b599c2 100644
--- a/src/main/resources/static/js/transactions.js
+++ b/src/main/resources/static/js/transactions.js
@@ -249,7 +249,7 @@ function validateAmount(text, allowEmpty=false)
     if(allowEmpty && text.length === 0)
     {
         removeTooltip(id);
-        document.getElementById("hidden-" + id).value = 0;
+        document.getElementById("hidden-" + id).value = "";
         return true;
     }
 
-- 
GitLab