diff --git a/R/_funktionen/sync_hibiscus.R b/R/_funktionen/sync_hibiscus.R new file mode 100644 index 0000000..c2338f3 --- /dev/null +++ b/R/_funktionen/sync_hibiscus.R @@ -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 +} \ No newline at end of file diff --git a/R/entry_edit_modal.R b/R/entries/entry_edit_modal.R similarity index 100% rename from R/entry_edit_modal.R rename to R/entries/entry_edit_modal.R diff --git a/R/todo.R b/R/entries/todo.R similarity index 100% rename from R/todo.R rename to R/entries/todo.R diff --git a/R/buchungen_mod.R b/R/postings/buchungen_mod.R similarity index 100% rename from R/buchungen_mod.R rename to R/postings/buchungen_mod.R diff --git a/R/umsatz/bank_connections.R b/R/umsatz/bank_connections.R new file mode 100644 index 0000000..6ede76c --- /dev/null +++ b/R/umsatz/bank_connections.R @@ -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_ +} \ No newline at end of file diff --git a/R/umsatz/module_umsatz.R b/R/umsatz/module_umsatz.R new file mode 100644 index 0000000..8e38b77 --- /dev/null +++ b/R/umsatz/module_umsatz.R @@ -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") + }) + }) +} diff --git a/db/data_transfer.R b/db/data_transfer.R index c834855..dd14e31 100644 --- a/db/data_transfer.R +++ b/db/data_transfer.R @@ -416,23 +416,7 @@ if (ok) { dbWriteTable(con_s, "postings", postings, append = TRUE) } -# Transfer Hibiscus transactions ---- - -if (ok) { - spalten <- dbxSelect(con_f, "SELECT * FROM Umsatz WHERE id=1") %>% - names %>% - paste(collapse = ",") - query <- paste("SELECT", spalten, "FROM Umsatz") - hib <- dbxSelect(con_f, query) %>% - select(-fz_jahr) - dbWriteTable(con_s, "hibiscus_transactions", hib, append = TRUE) -} - - - - -# Transfer Attachments -## * Daten aus Attachments Tabelle übertragen ---- +# Daten aus Attachments Tabelle übertragen ---- if(ok){ att <- dbxSelect(con_f, "SELECT id, trans_id, quittung_id, wiso_id, btisch_id, adress_id, ft_dateiname, beschreibung, created_at, updated_at, ft_extension FROM Attachments") %>% @@ -457,6 +441,31 @@ if(ok){ } ## ------------------------------------------------------- 2026-03-19 16:38 +# Transfer Bankverbindungen ---- +if (ok) { + bank_connections <- dbxSelect(con_f, " + SELECT id, adress_id, kontakt, iban, bic, kreditinstitut, + remote_name, created_at, updated_at + FROM Bankverbindungen + ") %>% + transmute( + id = as.integer(id), + contact_id = as.integer(adress_id), + contact_text = kontakt, + iban = iban, + bic = bic, + bank_name = kreditinstitut, + remote_name = remote_name, + created_at = as.character(created_at), + updated_at = as.character(updated_at) + ) + + dbWriteTable(con_s, "bank_connections", bank_connections, append = TRUE) + error_f <- fehler_add( + paste(nrow(bank_connections), "Bankverbindungen übertragen"), TRUE, error_f + ) +} + # Close ---- dbDisconnect(con_s) diff --git a/db/development.sqlite b/db/development.sqlite index 643836f..5c93b04 100644 Binary files a/db/development.sqlite and b/db/development.sqlite differ diff --git a/server.R b/server.R index a34d638..5eedd76 100644 --- a/server.R +++ b/server.R @@ -1,13 +1,13 @@ +# server.R server <- function(input, output, session) { # ── Globaler Navigations-Bus ─────────────────────────────────────────────── r_global <- reactiveValues( - # Navigation active_tab = NULL, - nav_history = list(), # ← Stack der letzten Positionen - - # Filter + Sprungziele + nav_history = list(), buchungen_filter = "alle", + umsatz_zeige_alle = FALSE, + umsatz_refresh = 0, jump_to_entry_id = NULL, jump_to_account_id = NULL, jump_to_contact_id = NULL, @@ -17,33 +17,41 @@ server <- function(input, output, session) { # ── Tab-Wechsel ──────────────────────────────────────────────────────────── observeEvent(r_global$active_tab, ignoreInit = TRUE, { req(r_global$active_tab) - cat("Tab-Wechsel zu:", r_global$active_tab, "\n") updateTabItems(session, "tabs", selected = r_global$active_tab) r_global$active_tab <- NULL }) + # ── Back-Button ──────────────────────────────────────────────────────────── observeEvent(input$back_btn, { req(length(r_global$nav_history) > 0) - - # Letzten Zustand holen und Stack kürzen - last <- tail(r_global$nav_history, 1)[[1]] + last <- tail(r_global$nav_history, 1)[[1]] r_global$nav_history <- head(r_global$nav_history, -1) - - # Zurückspringen updateTabItems(session, "tabs", selected = last$tab) - - # Zustand wiederherstellen — je nach Tab - if (last$tab == "konten") { - r_global$jump_to_account_name <- last$account_name - } }) - # ── Filter-Buttons (falls in der Navbar oder UI definiert) ───────────────── + # ── Filter Buchungen ─────────────────────────────────────────────────────── observeEvent(input$filter_alle, { r_global$buchungen_filter <- "alle" }) observeEvent(input$filter_giro, { r_global$buchungen_filter <- "giro" }) observeEvent(input$filter_monat, { r_global$buchungen_filter <- "monat" }) + # ── Filter Umsätze ───────────────────────────────────────────────────────── + observeEvent(input$umsatz_filter_ungebucht, { r_global$umsatz_zeige_alle <- FALSE }) + observeEvent(input$umsatz_filter_alle, { r_global$umsatz_zeige_alle <- TRUE }) + + # ── Sync Hibiscus ────────────────────────────────────────────────────────── + observeEvent(input$umsatz_sync, { + showNotification("Sync läuft...", type = "message", duration = 3) + tryCatch({ + source("sync_hibiscus.R", local = TRUE) + 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") + }) + }) + # ── Module ───────────────────────────────────────────────────────────────── - accountsServer("accounts_tab", conn, r_global) + accountsServer("accounts_tab", conn, r_global) buchungenServer("buchungen_tab", conn, r_global) + umsatzServer("umsatz_tab", conn, r_global) } \ No newline at end of file diff --git a/ui.R b/ui.R index 902b48f..fb736fe 100644 --- a/ui.R +++ b/ui.R @@ -1,57 +1,69 @@ -## ui.R ## +# ui.R dashboardPage( dashboardHeader( title = "GemFin", + + # Back-Button tags$li( class = "dropdown", style = "padding: 8px 10px;", actionBttn("back_btn", "Zurück", - size = "xs", - style = "minimal", - icon = icon("arrow-left")) - ) - ), - ## Sidebar content - dashboardSidebar( - sidebarMenu(id = "tabs", - menuItem("buchungen", tabName = "buchungen", icon = icon("list")), - menuItem("konten", tabName = "konten", icon = icon("building-columns")) + size = "xs", style = "minimal", + icon = icon("arrow-left")) ), - conditionalPanel( - condition = "input.tabs == 'buchungen'", - tags$ul( - class = "sidebar-menu", - tags$li( - class = "treeview", - tags$a(href = "#", - tags$i(class = "fa fa-filter"), - tags$span("Filter"), - tags$span(class = "pull-right-container", - tags$i(class = "fa fa-angle-left pull-right") - ) - ), - tags$ul( - class = "treeview-menu", - style = "padding: 5px 0;", - tags$li(actionBttn("filter_alle", "Alle", size = "xs", style = "minimal")), - tags$li(actionBttn("filter_giro", "Girokonto", size = "xs", style = "minimal")), - tags$li(actionBttn("filter_monat", "Dieser Monat", size = "xs", style = "minimal")) - ) + + # Filter Buchungen + tags$li( + class = "dropdown", + conditionalPanel( + condition = "input.tabs == 'buchungen'", + div( + style = "display: flex; align-items: center; gap: 4px; padding: 8px 4px;", + actionBttn("filter_alle", "Alle", size = "xs", style = "minimal"), + actionBttn("filter_giro", "Girokonto", size = "xs", style = "minimal"), + actionBttn("filter_monat", "Dieser Monat", size = "xs", style = "minimal") + ) + ) + ), + + # Filter Umsätze + tags$li( + class = "dropdown", + conditionalPanel( + condition = "input.tabs == 'umsatz'", + div( + style = "display: flex; align-items: center; gap: 4px; padding: 8px 4px;", + actionBttn("umsatz_filter_ungebucht", "Ungebucht", + size = "xs", style = "minimal", color = "warning"), + actionBttn("umsatz_filter_alle", "Alle", + size = "xs", style = "minimal"), + actionBttn("umsatz_sync", "Sync", + size = "xs", style = "minimal", + icon = icon("rotate"), color = "primary") ) ) ) ), - dashboardBody( + + dashboardSidebar( + sidebarMenu(id = "tabs", + menuItem("Buchungen", tabName = "buchungen", icon = icon("list")), + menuItem("Umsätze", tabName = "umsatz", icon = icon("bank")), + menuItem("Konten", tabName = "konten", icon = icon("building-columns")) + ) + ), + + dashboardBody( tabItems( - # First tab content tabItem(tabName = "buchungen", buchungenUI("buchungen_tab") ), - # Second tab content + tabItem(tabName = "umsatz", + umsatzUI("umsatz_tab") + ), tabItem(tabName = "konten", accountsUI("accounts_tab") ) ) ) -) - +) \ No newline at end of file