From f279e20468fb5323c33cbf43346c35ddef7f96e0 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 22 Feb 2026 15:25:50 +0100 Subject: Initial commit --- src/cashflow/frontend/transactions/one_time.clj | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/cashflow/frontend/transactions/one_time.clj (limited to 'src/cashflow/frontend/transactions/one_time.clj') diff --git a/src/cashflow/frontend/transactions/one_time.clj b/src/cashflow/frontend/transactions/one_time.clj new file mode 100644 index 0000000..d658dad --- /dev/null +++ b/src/cashflow/frontend/transactions/one_time.clj @@ -0,0 +1,139 @@ +(ns cashflow.frontend.transactions.one-time + (:require [ring.util.response :as ruresp] + [cashflow.utils :as cutils] + [cashflow.frontend.utils :as cfutils] + [cashflow.database :as cdatabase] + [clojure.string :as cstr] + [dotenv :as env] + [cashflow.frontend.navigation :as cfnavigation]) + (:import java.lang.Integer + java.lang.Double + java.time.LocalDate)) + +(defn- gen-table [year month] + [:div {:class ["scroll" "surface"] :id "transaction-table" :style {:height "30em"}} + [:table {:class ["stripes" "border" "large-space"]} + [:thead {:class "fixed"} + [:tr + [:th "Datum"] + [:th "Beschreibung"] + [:th "Betrag"] + [:th]]] + [:tbody + [:tr + [:td] + [:td "wiederkehrende Umsätze"] + [:td (cfutils/render-amount (cdatabase/recurring-transactions-applying-total year month))]] + (for [transaction (cdatabase/list-one-time-transactions year month)] + [:tr + [:td (cstr/join "." (-> transaction :from (cstr/split #"-") reverse))] + [:td (:description transaction)] + [:td (cfutils/render-amount (:amount transaction))] + [:td [:button {:class ["transparent" "circle"] + :hx-delete (str "/transactions/one-time/" (:db/id transaction) "/") + :hx-target "#transaction-table" + :hx-swap "outerHTML"} + [:i "delete"]]]])] + [:tfoot {:class "fixed"} + [:tr + [:th {:scope "row" :colspan "2"} "Gesamt: "] + [:td (-> (cdatabase/one-time-transactions-total year month) cfutils/render-amount)]]]]]) + +(defn- gen-month-switcher [year month] + (let [actual-date (LocalDate/now)] + [:div {:style {:display :flex} + :_ "on change go to url `/transactions/one-time/${#month-switcher-year.value}/${#month-switcher-month.value}/`"} + [:p {:style {:margin-top :auto + :margin-right "1em" + :font-weight :bold}} "Monat: "] + [:div {:class ["field" "border" "small"]} + [:input {:id "month-switcher-month" + :type :number :value month}]] + + [:p {:style {:margin-top :auto + :font-weight :bold + :margin-left "1em" + :margin-right "1em"}} "Jahr: "] + [:div {:class ["field" "border" "small"]} + [:input {:id "month-switcher-year" + :type :number :value year}]] + (if (or (not= (.getMonthValue actual-date) month) + (not= (.getYear actual-date) year)) + [:button {:style {:margin-top :auto} + :_ "on click go to url /transactions/one-time/"} + [:i "today"] + [:span "Springe zum aktuellen Monat"]])])) + +(defn- gen [req] + (let [year-param (-> req + (get-in [:path-params :year]) + (cutils/string-min-length 2 "0" :before)) + month-param (-> req + (get-in [:path-params :month]) + (cutils/string-min-length 2 "0" :before)) + default-day (or (try (LocalDate/parse (str year-param "-" month-param "-01")) + (catch Exception _ nil)) + (LocalDate/now)) + year (.getYear default-day) + month (.getMonthValue default-day)] + (list [:h1 "cashflow - Umsätze"] + (cfnavigation/gen :one-time) + (gen-month-switcher year month) + (gen-table year month) + [:article {:style {:width :fit-content :margin "3em auto"}} + [:form {:class "add-transaction" :hx-post "/transactions/one-time/" :hx-target "#transaction-table" :hx-swap "outerHTML"} + [:div {:class ["field" "label" "round" "border"] :style {:width :fit-content}} + [:input {:type :date :placeholder " " :name "date" :value (str default-day)}] + [:label "Datum"]] + [: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 :number :placeholder " " :step "0.01" :name "amount"}] + [:label "Betrag"]] + [:button {:class ["small-round" "fill"] :style {:margin-top "1em"} :type :submit} "Hinzufügen"]]]))) + +(defn handle-get [req] + (->> req + gen + (cfutils/render-page "cashflow - eimalige Umsätze") + ruresp/response)) + +(defn- table-response [year month] + (-> (gen-table year month) + cfutils/render-component + ruresp/response)) + +(defn handle-post [req] + (let [params (:form-params req) + description (get params "description") + amount (-> params + (get "amount") + Double/parseDouble) + date (get params "date") + split-date (cstr/split date #"-") + year (-> split-date + first + Integer/parseInt) + month (-> split-date + second + Integer/parseInt) + day (-> split-date + last + Integer/parseInt)] + (cdatabase/create-transaction! {:type :month + :description description + :amount amount + :year year + :month month + :day day}) + (table-response year month))) + +(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 (:year transaction) (:month transaction)))) + -- cgit v1.2.3