aboutsummaryrefslogtreecommitdiff
path: root/src/cashflow/frontend/transactions/one_time.clj
blob: d658dad1c737d645e14004df12075d65e3cf613d (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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))))