read_buch_tabelle <- function(conn, trans_id = NULL){ if(!is.null(trans_id)){ postings <- tbl(conn, "postings") %>% filter(entry_id == trans_id) } else { postings <- tbl(conn, "postings") } entries <- tbl(conn, "entries") accounts <- tbl(conn, "accounts") projects <- tbl(conn, "projects") contacts <- tbl(conn, "contacts") attachments <- tbl(conn, "attachments") # Anzahl Attachments pro entry_id vorberechnen attachment_counts <- attachments %>% group_by(entry_id) %>% summarise(n_attachments = n(), .groups = "drop") result <- postings |> left_join(entries, by = c("entry_id" = "id")) |> left_join(contacts, by = c("contact_id" = "id")) |> left_join(accounts, by = c("account_id" = "id")) |> left_join(projects, by = c("project_id" = "id")) |> left_join(attachment_counts, by = "entry_id") |> select(id, valuta, account_name, projektname, display_name, amount, entry_id, n_attachments, account_id) |> collect() %>% mutate( saldo = 0, n_attachments = tidyr::replace_na(n_attachments, 0L) ) %>% arrange(valuta, entry_id) } read_posting <- function(conn, id){ dbxSelect(conn, paste0("SELECT * FROM postings WHERE id =", id)) } coldef_entries_tabelle <- list( id = colDef(name = "ID", width = 80), valuta = colDef(name = "Wertstellung", width = 120), account_name = colDef(name = "Kontoname", width = 200), projektname = colDef(name = "Projektname", width = 150), display_name = colDef(name = "Kontakt", width = 250), account_id = colDef(show = FALSE), entry_id = colDef(show = FALSE), amount = colDef(name = "Betrag", width = 120, format = colFormat(prefix = "", separators = TRUE, digits = 2), style = function(value) { color <- if (value < 0) "#e06666" else "#2b2b2b" # Rot bei negativ, sonst dunkelgrau list(color = color, fontWeight = "bold") # Gibt CSS-Styles zurück } ), n_attachments = colDef( name = "Anhänge", filterMethod = JS("function(rows, columnId, filterValue) { const val = parseFloat(filterValue); if (isNaN(val)) return rows; // kein gültiger Wert → alles zeigen return rows.filter(row => row.values[columnId] > val); }") ), entry_id = colDef(name ="trans_id", width = 120), saldo = colDef( name = "Saldo", width = 140, html = TRUE, cell = JS(" function(cellInfo, state) { const page = state.page || 0 const pageSize = state.pageSize || state.sortedData.length const globalIndex = page * pageSize + cellInfo.viewIndex let total = 0 for (let i = 0; i <= globalIndex && i < state.sortedData.length; i++) { total += Number(state.sortedData[i].amount || 0) } const color = total < 0 ? '#e06666' : '#2b2b2b' return ` ${total.toLocaleString('de-DE', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} ` } ") ) )