home · contact · privacy
Add "fill sink" button.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 23 Mar 2025 11:38:19 +0000 (12:38 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 23 Mar 2025 11:38:19 +0000 (12:38 +0100)
src/templates/edit_structured.tmpl

index 32816e3a4bed1f2c1bfd7f38dc806d2ba529cb5f..676f49a3b9c2bdc5387b3ac893604ad424cf16c4 100644 (file)
@@ -202,6 +202,45 @@ function mirror() {
     update_form();
 }
 
+function fill_sink() {
+    let sink_indices = [];
+    const sum_per_currency = {};
+    for (let i = 0; i < dat_lines.length; i++) {
+        const dat_line = dat_lines[i];
+        if (!dat_line.is_intro && !dat_line.error) {
+            const currency = dat_line.booked.currency || '€';
+            if (dat_line.booked.amount == 'None') {
+                sink_indices.push(i);
+            } else {
+               if (!Object.hasOwn(sum_per_currency, currency)) {
+                   sum_per_currency[currency] = 0;
+               }
+               sum_per_currency[currency] += parseFloat(dat_line.booked.amount);
+            }
+        }
+    }
+    const sink_amounts_per_currency = {};
+    for (const [currency, amount] of Object.entries(sum_per_currency)) {
+        if (amount != 0) {
+            sink_amounts_per_currency[currency] = -amount;
+        }
+    }
+    for (i = 0; i < Object.keys(sink_amounts_per_currency).length - sink_indices.length; i++) {
+        sink_indices.push(dat_lines.length);
+        new_line = {error: '', comment: '', booked: {account: '?', amount: 'None', currency: ''}};
+        dat_lines.push(new_line);
+    }
+    let sink_indices_index = 0;
+    for (const [currency, amount] of Object.entries(sink_amounts_per_currency)) {
+        const dat_line = dat_lines[sink_indices[sink_indices_index]];
+        sink_indices_index++;
+        dat_line.booked.currency = currency;
+        dat_line.booked.amount = (-amount).toString();
+    }
+    taint();
+    update_form();
+}
+
 window.onload = update_form;
 {% endblock %}
 
@@ -210,6 +249,7 @@ window.onload = update_form;
 <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="fill_sink()" value="fill sink" class="disable_on_change">
 |
 <input type="button" onclick="replace()" value="replace string" class="disable_on_change">
 from