(ns cashflow.frontend.transactions.recurring (:require [cashflow.frontend.utils :as cfutils] [cashflow.database :as cdatabase] [ring.util.response :as ruresp] [clojure.string :as cstr] [cashflow.frontend.navigation :as cfnavigation]) (:import java.time.LocalDate)) (defn- gen-table [] [:div {:class ["scroll" "surface"] :id "transaction-table" :style {:height "30em"}} [:table {:class ["stripes" "border" "large-space"]} [:thead {:class "fixed"} [:tr [:th "Beschreibung"] [:th "von"] [:th "bis"] [:th "Betrag"] [:th "Interval"] [:th]]] [:tbody (for [transaction (cdatabase/list-recurring-transactions)] [:tr [:td (:description transaction)] [:td (cstr/join "." (-> transaction :from (cstr/split #"-") reverse))] [:td (cstr/join "." (-> transaction :to (cstr/split #"-") reverse))] [:td (cfutils/render-amount (:amount transaction))] [:td (:month-interval transaction) " Monate"] [:td [:button {:class ["transparent" "circle"] :hx-delete (str "/transactions/recurring/" (:db/id transaction) "/") :hx-target "#transaction-table" :hx-swap "outerHTML"} [:i "delete"]]]])]]]) (defn- gen [_req] (list [:h1 "cashflow - Umsätze"] (cfnavigation/gen :recurring) (gen-table) [:article {:style {:width :fit-content :margin "3em auto"}} [:form {:class "add-transaction" :hx-post "/transactions/recurring/" :hx-target "#transaction-table" :hx-swap "outerHTML"} [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} [:input {:type :text :placeholder " " :name "description"}] [:label "Beschreibung"]] [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} [:input {:type :date :placeholder " " :name "from"}] [:label "von"]] [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} [:input {:type :date :placeholder " " :name "to"}] [:label "bis"]] [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} [:input {:type :number :placeholder " " :step "0.01" :name "amount"}] [:label "Betrag"]] [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} [:input {:type :number :placeholder " " :name "month-interval"}] [:label "Interval (monate)"]] [:button {:class ["small-round" "fill"] :style {:margin-top "1em"} :type :submit} "Hinzufügen"]]])) (defn handle-get [req] (->> req gen (cfutils/render-page "cashflow - wiederkehrende Umsätze") ruresp/response)) (defn- table-response [] (-> (gen-table) cfutils/render-component ruresp/response)) (defn handle-post [req] (let [params (:form-params req) description (get params "description") from (get params "from") to (get params "to") amount (-> params (get "amount") Double/parseDouble) month-interval (-> params (get "month-interval") Integer/parseInt bigint)] (cdatabase/create-transaction! {:type :recurring :description description :amount amount :from from :to to :month-interval month-interval}) (table-response))) (defn handle-delete [req] (let [id (-> req (get-in [:path-params :id]) Integer/parseInt) transaction (cdatabase/id->transaction id)] (cdatabase/delete-transaction! id) (table-response)))