Umsatz-modul eingebaut, buchen noch nicht getestet

This commit is contained in:
2026-03-24 17:28:21 +01:00
parent 126c2fc7d7
commit 2d2eb2fa1c
10 changed files with 428 additions and 70 deletions
+69
View File
@@ -0,0 +1,69 @@
# R/sync_hibiscus.R
sync_hibiscus <- function(conn) {
ok <- TRUE
error_f <- NULL
# ── Einstellungen ────────────────────────────────────────────────────────────
SYNC_AB <- as.Date("2024-01-01")
if (as.character(Sys.info())[1] == "Darwin") {
h2jar <- "/Users/cosw/bin/jameica.app/lib/h2/migration-h2/disabled/h2-1.4.199.jar"
url <- "jdbc:h2:/Users/cosw/insync/Projekte/Gemeindefinanzen/jameica/hibiscus/h2db/hibiscus;IFEXISTS=TRUE;CIPHER=AES"
}
if (as.character(Sys.info())[1] == "Linux") {
h2jar <- "..."
url <- "jdbc:h2:/home/cosw/.jameica/hibiscus/h2db/hibiscus;IFEXISTS=TRUE;CIPHER=AES"
}
user <- "hibiscus"
pw <- "FbvGoL+yJlH1GtUojnC8ZajYuTA= FbvGoL+yJlH1GtUojnC8ZajYuTA="
# ── Hibiscus H2 lesen ────────────────────────────────────────────────────────
if (ok) {
tryCatch({
drv <- JDBC("org.h2.Driver", h2jar, identifier.quote = "`")
con_j <- dbConnect(drv, url, user, pw)
originaldaten <- dbReadTable(con_j, "UMSATZ")
dbDisconnect(con_j)
ok <- nrow(originaldaten) > 0
error_f <- fehler_add(paste("Hibiscus Daten gelesen:", nrow(originaldaten)), ok, error_f)
}, error = function(e) {
ok <<- FALSE
error_f <<- fehler_add(paste("H2 Fehler:", e$message), FALSE, error_f)
})
}
# ── Bereits vorhandene IDs ────────────────────────────────────────────────────
if (ok) {
vh <- dbxSelect(conn, "SELECT id FROM hibiscus_transactions") %>% pull()
error_f <- fehler_add(paste("Vorhandene IDs:", length(vh)), TRUE, error_f)
}
# ── Neue Datensätze bestimmen ─────────────────────────────────────────────────
if (ok) {
neue_daten <- originaldaten %>%
filter(!ID %in% vh) %>%
filter(DATUM >= SYNC_AB) %>%
rename_with(tolower) %>%
mutate(
id = as.integer(id),
datum = as.character(datum),
valuta = as.character(valuta)
)
ok <- nrow(neue_daten) > 0
error_f <- fehler_add(paste("Neue Datensätze:", nrow(neue_daten)), ok, error_f)
}
# ── In SQLite schreiben ───────────────────────────────────────────────────────
if (ok) {
dbWriteTable(conn, "hibiscus_transactions", neue_daten, append = TRUE)
error_f <- fehler_add(
paste(nrow(neue_daten), "Umsätze gespeichert"), TRUE, error_f
)
}
error_f
}
View File
+28
View File
@@ -0,0 +1,28 @@
# R/read_functions.R — Hilfsfunktion
# R/read_functions.R — read_hibiscus bereinigen
read_hibiscus <- function(conn) {
tbl(conn, "hibiscus_transactions") %>%
select(id, konto_id, empfaenger_name, empfaenger_konto,
betrag, zweck, datum, valuta, saldo) %>%
left_join(
tbl(conn, "postings") %>%
select(id, bank_transaction_id, entry_id) %>%
rename(posting_id = id),
by = c("id" = "bank_transaction_id")
) %>%
collect() %>%
mutate(
datum = as.Date(datum), # einfach as.Date reicht da Text "2023-12-25"
valuta = as.Date(valuta),
gebucht = !is.na(posting_id)
)
}
# R/read_functions.R — Kontakt aus bank_connections auflösen
resolve_contact <- function(conn, empfaenger_name, empfaenger_konto) {
bc <- tbl(conn, "bank_connections") %>%
filter(iban == empfaenger_konto | remote_name == empfaenger_name) %>%
select(contact_id) %>%
collect()
if (nrow(bc) > 0) bc$contact_id[1] else NA_integer_
}
+232
View File
@@ -0,0 +1,232 @@
umsatzUI <- function(id) {
ns <- NS(id)
tagList(
useShinyjs(),
div(
style = "display: flex; justify-content: flex-end; margin-bottom: 8px;",
actionBttn(ns("sync"), "Sync Hibiscus",
size = "xs", style = "minimal",
icon = icon("rotate"), color = "primary")
),
reactableOutput(ns("umsatz_table")),
hr(),
uiOutput(ns("buchungs_panel"))
)
}
umsatzServer <- function(id, conn, r_global) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
# ── Daten ──────────────────────────────────────────────────────────────────
refresh <- reactiveVal(0)
zeige_alle <- reactiveVal(FALSE)
umsatz_data <- reactive({
r_global$umsatz_refresh
d <- read_hibiscus(conn)
if (!r_global$umsatz_zeige_alle) d <- d %>% filter(!gebucht)
d %>% arrange(desc(valuta))
})
# ── Filter-Buttons ─────────────────────────────────────────────────────────
observeEvent(input$filter_ungebucht, { zeige_alle(FALSE) })
observeEvent(input$filter_alle, { zeige_alle(TRUE) })
# ── Sync ───────────────────────────────────────────────────────────────────
observeEvent(input$sync, {
showNotification("Sync läuft...", type = "message", duration = 3)
tryCatch({
result <- sync_hibiscus(conn)
r_global$umsatz_refresh <- isolate(r_global$umsatz_refresh) + 1
showNotification("✓ Sync abgeschlossen", type = "message")
}, error = function(e) {
showNotification(paste("Fehler:", e$message), type = "error")
})
})
# ── Tabelle ────────────────────────────────────────────────────────────────
output$umsatz_table <- renderReactable({
reactable(
umsatz_data() %>%
select(id, valuta, empfaenger_name, empfaenger_konto,
betrag, zweck, gebucht, posting_id, entry_id),
striped = TRUE,
highlight = TRUE,
filterable = TRUE, # ← das reicht
selection = "single",
onClick = "select",
defaultSorted = list(valuta = "desc"),
columns = list(
id = colDef(show = FALSE),
posting_id = colDef(show = FALSE),
entry_id = colDef(show = FALSE),
valuta = colDef(name = "Datum", maxWidth = 120),
empfaenger_name = colDef(name = "Empfänger", minWidth = 150),
empfaenger_konto = colDef(name = "IBAN", minWidth = 150),
betrag = colDef(
name = "Betrag",
maxWidth = 110,
align = "right",
format = colFormat(currency = "EUR", separators = TRUE, locales = "de-DE")
),
zweck = colDef(name = "Zweck", minWidth = 200),
gebucht = colDef(
name = "Gebucht",
maxWidth = 80,
align = "center",
cell = function(value) if (value) "✓" else "—"
)
),
rowStyle = function(index) {
if (umsatz_data()$gebucht[index])
list(color = "gray", opacity = "0.5")
}
)
})
# ── Selektierter Umsatz ────────────────────────────────────────────────────
sel <- reactive(getReactableState("umsatz_table", "selected"))
selected_umsatz <- reactive({
req(sel())
umsatz_data()[sel(), ]
})
# ── Buchungs-Panel ─────────────────────────────────────────────────────────
output$buchungs_panel <- renderUI({
req(selected_umsatz())
u <- selected_umsatz()
if (u$gebucht) {
tagList(
h4("Bereits gebucht"),
actionBttn(ns("goto_buchung"),
paste0("→ Zur Buchung (Entry #", u$entry_id, ")"),
size = "sm", style = "minimal", color = "primary")
)
} else {
tagList(
h4("Buchen"),
fluidRow(
column(6,
selectizeInput(ns("konto"), "Konto:",
choices = get_account_choices(conn),
selected = 0,
width = "100%")
),
column(6,
selectizeInput(ns("projekt"), "Projekt:",
choices = get_project_choices(conn),
width = "100%")
)
),
fluidRow(
column(6,
selectizeInput(ns("kontakt"), "Kontakt:",
choices = get_contact_choices(conn),
selected = resolve_contact(conn, u$empfaenger_name, u$empfaenger_konto),
width = "100%")
),
column(6,
br(),
actionBttn(ns("new_contact"), "Neuer Kontakt",
size = "xs", style = "minimal", icon = icon("plus"))
)
),
actionBttn(ns("buchen"), "Buchen",
size = "sm", style = "minimal", color = "success",
icon = icon("check"))
)
}
})
# ── Zur Buchung springen ───────────────────────────────────────────────────
observeEvent(input$goto_buchung, {
req(selected_umsatz())
r_global$nav_history <- c(r_global$nav_history, list(list(tab = "umsatz")))
r_global$jump_to_entry_id <- selected_umsatz()$entry_id
r_global$active_tab <- "buchungen"
})
# ── Buchen ────────────────────────────────────────────────────────────────
observeEvent(input$buchen, {
req(selected_umsatz(), input$konto)
u <- selected_umsatz()
gegenkonto_id <- dbReadTable(conn, "accounts") %>%
filter(hibiscus_account_id == u$konto_id) %>%
pull(id)
req(length(gegenkonto_id) > 0, input$konto != 0)
new_t_id <- max_id(conn, "entries") + 1L
dbxInsert(conn, "entries", data.frame(
id = new_t_id,
contact_id = as.integer(input$kontakt),
note = u$zweck
))
p_id1 <- max_id(conn, "postings") + 1L
dbxInsert(conn, "postings", data.frame(
id = c(p_id1, p_id1 + 1L),
entry_id = c(new_t_id, new_t_id),
account_id = c(as.integer(input$konto), gegenkonto_id),
project_id = c(as.integer(input$projekt), NA_integer_),
amount = c(u$betrag, -u$betrag),
valuta = c(as.character(u$valuta), as.character(u$valuta)),
booking_date = c(as.character(u$datum), as.character(u$datum)),
bank_transaction_id = c(u$id, NA_integer_)
))
refresh(refresh() + 1)
showNotification("✓ Gebucht", type = "message")
})
# ── Neuer Kontakt ──────────────────────────────────────────────────────────
observeEvent(input$new_contact, {
req(selected_umsatz())
u <- selected_umsatz()
showModal(modalDialog(
title = "Neuer Kontakt",
textInput(ns("new_contact_name"), "Name", value = u$empfaenger_name),
textInput(ns("new_contact_iban"), "IBAN", value = u$empfaenger_konto),
footer = tagList(
modalButton("Abbrechen"),
actionBttn(ns("save_contact"), "Speichern",
style = "minimal", color = "success")
)
))
})
observeEvent(input$save_contact, {
req(input$new_contact_name)
u <- selected_umsatz()
new_c_id <- max_id(conn, "contacts") + 1L
dbxInsert(conn, "contacts", data.frame(
id = new_c_id,
display_name = input$new_contact_name
))
new_bc_id <- max_id(conn, "bank_connections") + 1L
dbxInsert(conn, "bank_connections", data.frame(
id = new_bc_id,
contact_id = new_c_id,
iban = input$new_contact_iban,
remote_name = u$empfaenger_name
))
removeModal()
updateSelectizeInput(session, "kontakt",
choices = get_contact_choices(conn),
selected = new_c_id
)
showNotification("✓ Kontakt angelegt", type = "message")
})
})
}