From 3af2e382884ebca4f7f7e0a1c22039c86e71a6d0 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 26 Feb 2025 17:37:37 +0100
Subject: [PATCH] Add "mirror" button to /edit_structured.

---
 src/templates/edit_structured.tmpl | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/templates/edit_structured.tmpl b/src/templates/edit_structured.tmpl
index de2a226..c3431d3 100644
--- a/src/templates/edit_structured.tmpl
+++ b/src/templates/edit_structured.tmpl
@@ -175,6 +175,26 @@ function replace() {
   update_form();
 }
 
+function mirror() {
+  dat_lines.slice(1).forEach((dat_line) => {
+      let inverted_amount = `-${dat_line.booking_line.amount}`;
+      if (inverted_amount.startsWith('--')) {
+          inverted_amount = inverted_amount.slice(2);
+      }
+      dat_lines.push({
+          error: '',
+          comment: '',
+          booking_line: {
+              account: '?',
+              amount: inverted_amount,
+              currency: dat_line.booking_line.currency
+          }
+      });
+  })
+  taint();
+  update_form();
+}
+
 window.onload = update_form;
 {% endblock %}
 
@@ -182,6 +202,8 @@ window.onload = update_form;
 {% block content %}
 <form action="/edit_structured/{{id}}" method="POST">
 {{ macros.edit_bar("raw", id) }}
+<input type="button" onclick="mirror()" value="mirror" class="disable_on_change">
+|
 <input type="button" onclick="replace()" value="replace string" class="disable_on_change">
 from
 <input id="replace_from" />
-- 
2.30.2