Adds expands expense account report
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -9,6 +9,7 @@
|
|||||||
[auto-ap.ssr.ui :refer [base-page]]
|
[auto-ap.ssr.ui :refer [base-page]]
|
||||||
[auto-ap.ssr.utils :refer [apply-middleware-to-all-handlers
|
[auto-ap.ssr.utils :refer [apply-middleware-to-all-handlers
|
||||||
html-response wrap-schema-enforce]]
|
html-response wrap-schema-enforce]]
|
||||||
|
[auto-ap.time :as atime]
|
||||||
[bidi.bidi :as bidi]
|
[bidi.bidi :as bidi]
|
||||||
[cemerick.url :as url]
|
[cemerick.url :as url]
|
||||||
[clj-time.coerce :as coerce]
|
[clj-time.coerce :as coerce]
|
||||||
@@ -17,37 +18,61 @@
|
|||||||
[hiccup2.core :as hiccup]))
|
[hiccup2.core :as hiccup]))
|
||||||
|
|
||||||
(defn lookup-data [request]
|
(defn lookup-data [request]
|
||||||
(let [query (cond-> {:query '{:find [?an ?user-date (sum ?amt)]
|
(let [query (cond-> {:query '{:find [?cn ?user-date (sum ?amt)]
|
||||||
:in [$ [?clients ?start ?end]]
|
:in [$ [?clients ?start ?end]]
|
||||||
:where
|
:where
|
||||||
[[(iol-ion.query/scan-invoices $ ?clients ?start ?end) [[?e _ ?sort-default] ...]]
|
[[(iol-ion.query/scan-invoices $ ?clients ?start ?end) [[?e _ ?sort-default] ...]]
|
||||||
[?e :invoice/date ?d]
|
[?e :invoice/date ?d]
|
||||||
|
[?e :invoice/client ?c]
|
||||||
[?e :invoice/expense-accounts ?iea]
|
[?e :invoice/expense-accounts ?iea]
|
||||||
[?iea :invoice-expense-account/amount ?amt]
|
[?iea :invoice-expense-account/amount ?amt]
|
||||||
[?iea :invoice-expense-account/account ?a]
|
[?c :client/name ?cn]
|
||||||
[?a :account/name ?an]
|
[(clj-time.coerce/to-date-time ?d) ?user-date]]}
|
||||||
[(iol-ion.query/iso-date ?d) ?user-date]]}
|
|
||||||
:args
|
:args
|
||||||
[(dc/db conn)
|
[(dc/db conn)
|
||||||
[(extract-client-ids (:clients request)
|
[(extract-client-ids (:clients request)
|
||||||
(:client-id request)
|
(:client-id request)
|
||||||
(when (:client-code request)
|
(when (:client-code request)
|
||||||
[:client/code (:client-code request)]))
|
[:client/code (:client-code request)]))
|
||||||
(some-> (time/plus (time/now) (time/days -14)) coerce/to-date)
|
(some-> (time/plus (time/now) (time/days -120)) coerce/to-date)
|
||||||
(some-> (time/now) coerce/to-date)]]}
|
(some-> (time/now) coerce/to-date)]]}
|
||||||
|
|
||||||
(:vendor-id (:query-params request))
|
(:vendor-id (:query-params request))
|
||||||
(merge-query {:query '{:in [?v]
|
(merge-query {:query '{:in [?v]
|
||||||
:where [ [?e :invoice/vendor ?v]]}
|
:where [ [?e :invoice/vendor ?v]]}
|
||||||
:args [ (:db/id (:vendor-id (:query-params request)))]}))]
|
:args [ (:db/id (:vendor-id (:query-params request)))]})
|
||||||
|
|
||||||
|
(:account-id (:query-params request))
|
||||||
|
(merge-query {:query '{:in [?a]
|
||||||
|
:where [ [?iea :invoice-expense-account/account ?a]]}
|
||||||
|
:args [ (:db/id (:account-id (:query-params request)))]}))]
|
||||||
|
|
||||||
(dc/query query)))
|
(dc/query query)))
|
||||||
|
|
||||||
|
(defn week-seq
|
||||||
|
([c] (week-seq c (atime/last-monday)))
|
||||||
|
([c starting] (reverse (for [n (range c)
|
||||||
|
:let [start (time/minus starting (time/weeks n))
|
||||||
|
end (time/minus starting (time/weeks (dec n)))]]
|
||||||
|
[(atime/as-local-time (coerce/to-date-time start)) (atime/as-local-time (coerce/to-date-time end))]))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn- best-week [d weeks]
|
||||||
|
(reduce
|
||||||
|
(fn [acc [start end]]
|
||||||
|
(if (and (time/after? d start)
|
||||||
|
(time/before? d end))
|
||||||
|
(reduced [start end])
|
||||||
|
nil))
|
||||||
|
nil
|
||||||
|
weeks))
|
||||||
|
|
||||||
|
|
||||||
(defn card* [request]
|
(defn card* [request]
|
||||||
(com/content-card {:class "w-full" :id "report"}
|
(com/content-card {:class "w-full" :id "report"}
|
||||||
[:div {:class "flex flex-col px-8 py-8 space-y-3"}
|
[:div {:class "flex flex-col px-8 py-8 space-y-3"}
|
||||||
[:div
|
[:div
|
||||||
[:h1.text-2xl.mb-3.font-bold "Expenses by day, 14 days"]
|
[:h1.text-2xl.mb-3.font-bold "Expense report, last 8 weeks"]
|
||||||
[:form {:hx-get (bidi.bidi/path-for ssr-routes/only-routes :company-expense-report-card )
|
[:form {:hx-get (bidi.bidi/path-for ssr-routes/only-routes :company-expense-report-card )
|
||||||
:hx-trigger "change"
|
:hx-trigger "change"
|
||||||
:hx-target "#report"
|
:hx-target "#report"
|
||||||
@@ -64,7 +89,16 @@
|
|||||||
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
||||||
:value (fc/field-value)
|
:value (fc/field-value)
|
||||||
:value-fn :db/id
|
:value-fn :db/id
|
||||||
:content-fn :vendor/name})))])]
|
:content-fn :vendor/name})))
|
||||||
|
(fc/with-field :account-id
|
||||||
|
(com/validated-field {:label "Account"
|
||||||
|
:errors (fc/field-errors)}
|
||||||
|
(com/typeahead {:name (fc/field-name)
|
||||||
|
:class "w-64"
|
||||||
|
:url (bidi/path-for ssr-routes/only-routes :account-search)
|
||||||
|
:value (fc/field-value)
|
||||||
|
:value-fn :db/id
|
||||||
|
:content-fn :account/name})))])]
|
||||||
[:div
|
[:div
|
||||||
(let [data (lookup-data request)
|
(let [data (lookup-data request)
|
||||||
distinct-accounts (take 5 (->> data
|
distinct-accounts (take 5 (->> data
|
||||||
@@ -76,26 +110,34 @@
|
|||||||
(reverse)
|
(reverse)
|
||||||
(map first)
|
(map first)
|
||||||
(take 5)))
|
(take 5)))
|
||||||
|
weeks (week-seq 10 (time/plus (time/now) (time/days -60)))
|
||||||
|
x-axis (for [[start end] weeks]
|
||||||
|
(str (iol-ion.query/excel-date (coerce/to-date start))
|
||||||
|
" - "
|
||||||
|
(iol-ion.query/excel-date (coerce/to-date end))))
|
||||||
|
|
||||||
lookup (->>
|
lookup (->>
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc [a d v]]
|
(fn [acc [a d v]]
|
||||||
(assoc-in acc [a d] v))
|
(update-in acc [a (best-week d weeks)] (fnil + 0.0) v))
|
||||||
{}
|
{}
|
||||||
data))
|
data))
|
||||||
dates (for [d (range -14 0)]
|
|
||||||
(iol-ion.query/iso-date (coerce/to-date (time/plus (time/now) (time/days d)))))
|
_ (alog/peek ::Lookup lookup)
|
||||||
series (for [ea distinct-accounts]
|
series (for [ea distinct-accounts]
|
||||||
(for [d dates]
|
(for [d weeks]
|
||||||
(get-in lookup [ea d] 0)))]
|
(get-in lookup [ea d] 0)))]
|
||||||
[:div {:class "w-full h-64"
|
(list
|
||||||
:id "client-chart"
|
[:div.flex.gap-2.my-4.flex-wrap
|
||||||
:x-data (hx/json {:chart {:labels dates
|
|
||||||
#_["2 years ago" "1 year ago" "today"],
|
|
||||||
:series (alog/peek ::test series)}})
|
|
||||||
:x-init (hiccup/raw "new Chartist.Bar($el, chart, {seriesBarDistance:10}); ")}
|
|
||||||
[:div.flex.space-x-2.my-4
|
|
||||||
(for [[d color] (map vector distinct-accounts ["bg-green-500" "bg-red-500" "bg-blue-500" "bg-yellow-500" "bg-purple-500"])]
|
(for [[d color] (map vector distinct-accounts ["bg-green-500" "bg-red-500" "bg-blue-500" "bg-yellow-500" "bg-purple-500"])]
|
||||||
[:span.px-2.py-1.rounded.rounded-full.text-sm.text-gray-100 {:class color} d])]])]]]))
|
[:span.px-2.py-1.rounded.rounded-full.text-sm.text-gray-100 {:class color} d])]
|
||||||
|
[:div {:class "w-full h-64"
|
||||||
|
:id "client-chart"
|
||||||
|
:x-data (hx/json {:chart {:labels x-axis
|
||||||
|
#_["2 years ago" "1 year ago" "today"],
|
||||||
|
:series (alog/peek ::test series)}})
|
||||||
|
:x-init (hiccup/raw "new Chartist.Bar($el, chart, {seriesBarDistance:10}); ")}
|
||||||
|
]))]]]))
|
||||||
|
|
||||||
(defn page [request]
|
(defn page [request]
|
||||||
(base-page
|
(base-page
|
||||||
@@ -118,9 +160,13 @@
|
|||||||
"My Company"))
|
"My Company"))
|
||||||
|
|
||||||
(defn card [request]
|
(defn card [request]
|
||||||
(html-response
|
(html-response
|
||||||
(card* request)
|
(card* request)
|
||||||
:headers {"hx-push-url" (str "?" (url/map->query (update (:query-params request) :vendor-id :db/id))) }))
|
:headers {"hx-push-url" (str "?" (-> request
|
||||||
|
:query-params
|
||||||
|
(update :vendor-id :db/id)
|
||||||
|
(update :account-id :db/id)
|
||||||
|
url/map->query))}))
|
||||||
|
|
||||||
(def key->handler
|
(def key->handler
|
||||||
(apply-middleware-to-all-handlers
|
(apply-middleware-to-all-handlers
|
||||||
@@ -132,4 +178,7 @@
|
|||||||
[:map {:default {}}
|
[:map {:default {}}
|
||||||
[:vendor-id {:optional true}
|
[:vendor-id {:optional true}
|
||||||
[:maybe
|
[:maybe
|
||||||
[:entity-map {:pull [:vendor/name :db/id]}]]]] )))))
|
[:entity-map {:pull [:vendor/name :db/id]}]]]
|
||||||
|
[:account-id {:optional true}
|
||||||
|
[:maybe
|
||||||
|
[:entity-map {:pull [:account/name :db/id]}]]]] )))))
|
||||||
@@ -59,7 +59,7 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn last-monday []
|
(defn last-monday []
|
||||||
(loop [current (local-now)]
|
(loop [current (local-today)]
|
||||||
(if (= 1 (time/day-of-week current))
|
(if (= 1 (time/day-of-week current))
|
||||||
current
|
current
|
||||||
(recur (time/minus current (time/days 1))))))
|
(recur (time/minus current (time/days 1))))))
|
||||||
|
|||||||
Reference in New Issue
Block a user