# module_buchungen.R buchungenUI <- function(id) { ns <- NS(id) tagList( useShinyjs(), h3("Hauptbuchungen"), reactableOutput(ns("buchungen_table")), hr(), h3("Details / Gegenbuchungen"), uiOutput(ns("details_table")) , h3("Anhänge"), uiOutput(ns("attachments_ui")), fileInput(ns("anhang"), NULL, multiple = TRUE, accept = c(".pdf", ".jpg", ".png", ".xlsx", ".docx"), width = "100%"), br(), actionBttn(ns("add_trans"), "Transaktion hinzu", size = "sm", style = "material-flat", color = "warning"), actionBttn(ns("del_trans"), "Transaktion Löschen", size = "sm", style = "material-flat", color = "danger"), actionBttn(ns("add_detail"), "Detail hinzu", size = "sm", style = "material-flat", color = "success"), ) } buchungenServer <- function(id, conn, r_global) { moduleServer(id, function(input, output, session) { ns <- session$ns # ── Reactive Variablen ---- postings_data <- reactiveVal(read_buch_tabelle(conn)) sel_details <- reactiveVal(NULL) selected_trans_id <- reactiveVal(NULL) reset_trigger <- reactiveVal(NULL) current_main_idx <- reactiveVal(NULL) # ── Filter ---- aktiver_filter <- reactive(r_global$buchungen_filter) gefilterte_daten <- reactive({ d <- postings_data() f <- aktiver_filter() if (f == "alle") d else if (f == "giro") d |> filter(grepl("0130", account_name)) else if (f == "monat") d |> filter( floor_date(as.Date(valuta), "month") == floor_date(Sys.Date(), "month") ) else if (startsWith(f, "contact:")) d |> filter(contact_id == as.integer(sub("contact:", "", f))) else if (startsWith(f, "project:")) d |> filter(project_id == as.integer(sub("project:", "", f))) else if (startsWith(f, "account:")) d |> filter(account_id == as.integer(sub("account:", "", f))) else d }) # ── Haupttabelle rendern ---- output$buchungen_table <- renderReactable({ reset_trigger() f_reactable( daten = gefilterte_daten(), coldefs = coldef_entries_tabelle, selection = "single", hoehe = "60vh", filterable = TRUE, ) }) sel <- reactive(getReactableState("buchungen_table", "selected")) observeEvent(sel(), ignoreInit = T, { output$details_table <- renderUI({ sel_details(read_buch_entries(conn, gefilterte_daten()$entry_id[sel()])) rows <- lapply(1:nrow(sel_details()), function(ind) { fluidRow(class = "details-row", column(1, numericInput(ns("id"), label = NULL, width = "100%", value = sel_details()$id[ind])), column(2, dateInput( ns("valuta"), label = NULL, width = "100%",value = sel_details()$valuta[ind])), column(3, selectizeInput(ns("account"), label = NULL, choices = get_account_choices(conn), selected = sel_details()$account_id[ind], width = "100%") ), column(3, selectizeInput(ns("projekt"), label = NULL, choices = get_project_choices(conn), selected = sel_details()$projekt_id[ind], width = "100%") ), column(2, numericInput(ns("betrag"), label = NULL, width = "100%",value = sel_details()$amount[ind])) ) }) tagList( rows) }) }) observe({ req(sel_details()) lapply(1:nrow(sel_details()), function(ind) { # Alle Input-IDs dieser Zeile ids <- c(paste0("id_", ind), paste0("valuta_", ind), paste0("betrag_", ind), paste0("account_", ind), paste0("projekt_", ind)) # Observer nur für diese Zeile observeEvent( lapply(ids, function(nm) input[[nm]]), # Trigger: irgendein Input dieser Zeile ignoreInit = TRUE, { record <- list( id = input[[paste0("id_", ind)]], valuta = input[[paste0("valuta_", ind)]], betrag = input[[paste0("betrag_", ind)]], account = input[[paste0("account_", ind)]], projekt = input[[paste0("projekt_", ind)]] ) print( record) } ) }) }) # ── Anhänge ---- # output$attachments_ui <- renderUI({ # req(selected_trans_id()) # att <- dbxSelect(conn, paste0( # "SELECT * FROM attachments WHERE entry_id = ", selected_trans_id() # )) # if (nrow(att) == 0) return(p("Keine Anhänge vorhanden.")) # # tagList(lapply(seq_len(nrow(att)), function(i) { # ext <- tools::file_ext(att$original_name[i]) # filename <- paste0("attachments/", att$id[i], ".", ext) # # observeEvent(input[[paste0("open_att_", att$id[i])]], { # showModal(modalDialog( # tags$iframe(src = filename, width = "100%", height = "600px", # style = "border:none;"), # title = att$original_name[i], # size = "l", # easyClose = TRUE, # footer = modalButton("Schließen") # )) # }, ignoreInit = TRUE, once = FALSE) # # div( # actionLink(ns(paste0("open_att_", att$id[i])), att$original_name[i]), # actionLink(ns(paste0("del_att_", att$id[i])), "✕", # style = "color:red; margin-left:8px;") # ) # })) # }) }) }