-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeco-t.expenses-sum.user.js
More file actions
212 lines (180 loc) · 7.25 KB
/
geco-t.expenses-sum.user.js
File metadata and controls
212 lines (180 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// ==UserScript==
// @name GECO-T - Expenses Sum Row
// @namespace https://geco.reply.com/
// @version 1.0.0
// @description Show a row with the sum for expenses
// @author Roman Allenstein <r.allenstein@reply.de>
// @match https://geco.reply.com/ExpenseAccounts/*
// @grant none
// @run-at document-end
// @downloadURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-t.expenses-sum.user.js
// @updateURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-t.expenses-sum.user.js
// ==/UserScript==
// == Changelog ========================================================================================================
// 1.0 Initial release
(function () {
"use strict";
const BASE_SUM_ROW_ID = "tm-sum-row-expenses";
// Konfiguration für mehrere Tabellen/Seiten
// - resultExpToApprove: Liste (Approve Travelling Allowances) -> meist nur "Tot."
// - tbDetailsBody (+ tbDetailsHeader): Details-Modal -> "Travelling Allowance", "Allowance", ggf. "Reb."
const TABLES = [
{
key: "approve-list",
bodyTableId: "resultExpToApprove",
headerTableId: "resultExpToApprove", // Header sitzt im selben Table
bodyRowSelector: "tbody tr.trUpperRow",
labelCellIndex: 0,
sumColumns: [
// Bei manchen Seiten existiert "Ref." / "Reb." zusätzlich – wenn nicht vorhanden, wird einfach übersprungen.
{ key: "ref", matchers: [/^\s*ref\.?\s*$/i, /^\s*reb\.?\s*$/i] },
{ key: "tot", matchers: [/^\s*tot\.?/i, /total/i] }
]
},
{
key: "details-modal",
bodyTableId: "tbDetailsBody",
headerTableId: "tbDetailsHeader", // Header ist ein separates Table
bodyRowSelector: "tbody tr",
labelCellIndex: 0,
sumColumns: [
{ key: "travel", matchers: [/travelling\s*allowance/i] },
{ key: "allowance", matchers: [/^\s*allowance\s*$/i] },
{ key: "reb", matchers: [/^\s*reb\.?\s*$/i, /reimb/i] }
]
}
];
function normalizeText(s) {
return (s || "")
.toString()
.replace(/\s+/g, " ")
.trim()
.toLowerCase();
}
function parseGermanNumber(text) {
const s = (text || "")
.toString()
.replace(/\s/g, "")
.replace("€", "")
.replace(/\./g, "")
.replace(",", ".")
.trim();
const n = Number(s);
return Number.isFinite(n) ? n : 0;
}
function formatGermanNumber(n) {
const fixed = (Number.isFinite(n) ? n : 0).toFixed(2);
const [intPart, decPart] = fixed.split(".");
const withThousands = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return `${withThousands},${decPart}`;
}
function getHeaderThs(headerTable) {
// robust: thead th, sonst alle th
const ths = headerTable.querySelectorAll("thead th");
return ths.length ? Array.from(ths) : Array.from(headerTable.querySelectorAll("th"));
}
function headerIndexByMatchers(headerThs, matchers) {
for (let i = 0; i < headerThs.length; i++) {
const t = normalizeText(headerThs[i].textContent);
for (const re of matchers) {
if (re.test(t)) return i;
}
}
return -1;
}
function ensureSumRow(tbody, colCount, sumRowId, labelCellIndex, labelText) {
let tr = tbody.querySelector(`#${CSS.escape(sumRowId)}`);
if (tr) return tr;
tr = document.createElement("tr");
tr.id = sumRowId;
tr.style.fontWeight = "700";
tr.style.borderTop = "2px solid #ccc";
for (let i = 0; i < colCount; i++) {
const td = document.createElement("td");
td.style.whiteSpace = "nowrap";
td.style.paddingTop = "8px";
td.style.paddingBottom = "8px";
tr.appendChild(td);
}
if (tr.children[labelCellIndex]) tr.children[labelCellIndex].textContent = labelText;
tbody.appendChild(tr);
return tr;
}
function updateOne(cfg) {
const bodyTable = document.getElementById(cfg.bodyTableId);
const headerTable = document.getElementById(cfg.headerTableId);
if (!bodyTable || !headerTable) return;
const tbody = bodyTable.querySelector("tbody");
if (!tbody) return;
const headerThs = getHeaderThs(headerTable);
const colCount = headerThs.length;
if (!colCount) return;
// Spaltenindizes ermitteln (wenn nicht vorhanden -> -1)
const colMap = {};
for (const sc of cfg.sumColumns) {
colMap[sc.key] = headerIndexByMatchers(headerThs, sc.matchers);
}
// Wenn keine der Zielspalten existiert: nichts tun
const anyColExists = Object.values(colMap).some((idx) => idx >= 0);
if (!anyColExists) return;
// Body-rows auswählen (SumRow ausschließen)
const sumRowId = `${BASE_SUM_ROW_ID}-${cfg.key}-${cfg.bodyTableId}`;
const rows = Array.from(bodyTable.querySelectorAll(cfg.bodyRowSelector)).filter(
(r) => r.id !== sumRowId
);
// Leere/Placeholder-Zeilen vermeiden (Details können auch leer sein)
const dataRows = rows.filter((r) => r.querySelectorAll("td").length >= colCount);
const sums = {};
for (const key of Object.keys(colMap)) sums[key] = 0;
for (const row of dataRows) {
const tds = row.querySelectorAll("td");
for (const [key, idx] of Object.entries(colMap)) {
if (idx < 0) continue;
sums[key] += parseGermanNumber(tds[idx]?.innerText || tds[idx]?.textContent || "");
}
}
const sumRow = ensureSumRow(
tbody,
colCount,
sumRowId,
cfg.labelCellIndex,
"Summe"
);
const cells = sumRow.querySelectorAll("td");
for (const [key, idx] of Object.entries(colMap)) {
if (idx < 0) continue;
if (!cells[idx]) continue;
cells[idx].textContent = `${formatGermanNumber(sums[key])} €`;
}
}
function updateAll() {
for (const cfg of TABLES) updateOne(cfg);
}
function hookAspNetAjax() {
const prm = window.Sys?.WebForms?.PageRequestManager?.getInstance?.();
if (!prm) return false;
prm.add_endRequest(function () {
// ASP.NET AJAX: DOM kann nach EndRequest noch kurz "nachziehen"
window.setTimeout(updateAll, 0);
window.setTimeout(updateAll, 150);
});
return true;
}
// Init
updateAll();
// Primär: ASP.NET AJAX Hook
const hooked = hookAspNetAjax();
// Fallback: beobachte grob (Modal + ContentPanel + Page Content)
if (!hooked) {
let queued = false;
const mo = new MutationObserver(() => {
if (queued) return;
queued = true;
requestAnimationFrame(() => {
queued = false;
updateAll();
});
});
mo.observe(document.body, { childList: true, subtree: true, attributes: false });
}
})();