Addes expense report card

This commit is contained in:
2024-04-29 20:08:11 -07:00
parent cb5028948e
commit 8afbc124c4
4 changed files with 111 additions and 87 deletions

View File

@@ -99,74 +99,87 @@
(defn expense-breakdown-card* [request]
(com/content-card {:class "w-full" :id "expense-breakdown-report"}
[:div {:class "flex flex-col px-8 py-8 space-y-3"}
[:div
[:h1.text-2xl.mb-3.font-bold "Expense breakdown report, last 8 weeks"]
[:form {:hx-get (bidi.bidi/path-for ssr-routes/only-routes :company-expense-report-breakdown-card )
:hx-trigger "change"
:hx-target "#expense-breakdown-report"
:hx-swap "outerHTML"}
(fc/start-form
(:query-params request)
(:form-errors request)
[:div
(fc/with-field :vendor-id
(com/validated-field {:label "Vendor"
:errors (fc/field-errors)}
(com/typeahead {:name (fc/field-name)
:class "w-64"
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
:value (fc/field-value)
:value-fn :db/id
: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
(let [data (lookup-breakdown-data request)
distinct-accounts (take 5 (->> data
(reduce
(fn [acc [an _ amount]]
(update acc an (fnil + 0.0) amount))
{})
(sort-by last)
(reverse)
(map first)
(take 5)))
weeks (week-seq 8)
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 (->>
(reduce
(fn [acc [a d v]]
(update-in acc [a (best-week d weeks)] (fnil + 0.0) v))
{}
data))
_ (alog/peek ::Lookup lookup)
series (for [ea distinct-accounts]
(for [d weeks]
(get-in lookup [ea d] 0)))]
(list
[:div.flex.gap-2.my-4.flex-wrap
(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])]
[: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}); ")}]))]]]))
(com/card {:class "w-full h-full" :id "expense-breakdown-report"}
[:div {:class "flex flex-col px-8 py-8 space-y-3 w-full h-full"}
[:form {:hx-get (bidi.bidi/path-for ssr-routes/only-routes :company-expense-report-breakdown-card)
:hx-trigger "change"
:hx-target "#expense-breakdown-report"
:hx-swap "outerHTML"}
(fc/start-form
(:query-params request)
(:form-errors request)
[:div.flex.justify-between
[:h1.text-2xl.mb-3.font-bold "Expense breakdown report, last 8 weeks"]
[:div.flex.gap-2
(fc/with-field :vendor-id
(com/validated-field {:label "Vendor"
:errors (fc/field-errors)}
(com/typeahead {:name (fc/field-name)
:class "w-64"
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
:value (fc/field-value)
:value-fn :db/id
: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.flex-grow
(let [data (lookup-breakdown-data request)
distinct-accounts (->> data
(reduce
(fn [acc [an _ amount]]
(update acc an (fnil + 0.0) amount))
{})
(sort-by last)
(reverse)
(map first)
(take 20))
weeks (week-seq 8)
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 (->>
(reduce
(fn [acc [a d v]]
(update-in acc [a (best-week d weeks)] (fnil + 0.0) v))
{}
data))
series (for [ea distinct-accounts]
(for [d weeks]
(get-in lookup [ea d] 0)))]
[:canvas {:x-data (hx/json {:chart nil
:labels x-axis
:datasets (map (fn [s a] {:label a
:data s
:borderWidth 1})
series
distinct-accounts)})
:x-init
"new Chart($el, {
type: 'bar',
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
}
});"}])]]))
(defn vendor-invoice-total-card* [request]
(com/content-card {:class "w-full" :id "invoice-totals-report"}