aboutsummaryrefslogtreecommitdiff
path: root/src/cashflow/frontend/transactions/recurring.clj
blob: 2d85bf89125ce2f1607d89118a68216c979e5cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(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)))