Adds totals

This commit is contained in:
Bryce
2024-04-11 13:18:45 -07:00
parent 2fb49d3331
commit 6031f15246
3 changed files with 55 additions and 18 deletions

View File

@@ -635,7 +635,8 @@
(:per-page args)
default-pagination-size))
(map last))
:count (count results)})
:count (count results)
:all-ids (map last results)})
(defn audit-transact-batch [txes id]
(let [batch-id (.toString (java.util.UUID/randomUUID))]

View File

@@ -83,8 +83,9 @@
:sort sort)
(let [start (or start 0)
per-page (or per-page 25)
[entities total] ((:fetch-page grid-spec)
request)]
[entities total :as page-results] ((:fetch-page grid-spec)
request)
request (assoc request :page-results page-results)]
(com/data-grid-card {:id (:id grid-spec)
:title (if (string? (:title grid-spec))

View File

@@ -251,12 +251,42 @@
(map first))]
refunds))
(defn sum-outstanding [ids]
(->>
(dc/q {:find ['?id '?o]
:in ['$ '[?id ...]]
:where ['[?id :invoice/outstanding-balance ?o]]}
(dc/db conn)
ids)
(map last)
(reduce
+
0.0)))
(defn sum-total-amount [ids]
(->>
(dc/q {:find ['?id '?o]
:in ['$ '[?id ...]]
:where ['[?id :invoice/total ?o]]
}
(dc/db conn)
ids)
(map last)
(reduce
+
0.0)))
(defn fetch-page [request]
(let [db (dc/db conn)
{ids-to-retrieve :ids matching-count :count} (fetch-ids db request)]
{ids-to-retrieve :ids matching-count :count
all-ids :all-ids} (fetch-ids db request)]
[(->> (hydrate-results ids-to-retrieve db request))
matching-count]))
matching-count
(sum-outstanding all-ids)
(sum-total-amount all-ids)]))
(def query-schema (mc/schema
[:maybe [:map {:date-range [:date-range :start-date :end-date]}
@@ -378,7 +408,6 @@
(:query-params request))})))
;; TODO test as a real user
(def grid-page
(helper/build {:id "entity-table"
@@ -395,18 +424,24 @@
(alog/peek ::PARSE
(mc/decode query-schema p main-transformer)))
:action-buttons (fn [request]
[(when (can? (:identity request) {:subject :invoice :activity :bulk-delete})
(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes ::route/bulk-delete))
"x-bind:hx-vals" "JSON.stringify({selected: $data.selected, 'all-selected': $data.all_selected})"
"hx-include" "#invoice-filters"
:color :red}
"Void selected"))
(when (can? (:identity request) {:subject :invoice :activity :pay})
(pay-button* {:ids (selected->ids request
(:query-params request))}))
(when (can? (:identity request) {:subject :invoice :activity :create})
(com/button {:hx-get (bidi/path-for ssr-routes/only-routes ::route/new-wizard)}
"New invoice"))])
(let [[_ _ outstanding total] (:page-results request)]
[(com/pill {:color :primary} "Outstanding: "
(format "$%,.2f" outstanding))
(com/pill {:color :secondary} "Total: "
(format "$%,.2f" total))
(when (can? (:identity request) {:subject :invoice :activity :bulk-delete})
(com/button {:hx-get (str (bidi/path-for ssr-routes/only-routes ::route/bulk-delete))
"x-bind:hx-vals" "JSON.stringify({selected: $data.selected, 'all-selected': $data.all_selected})"
"hx-include" "#invoice-filters"
:color :red}
"Void selected"))
(when (can? (:identity request) {:subject :invoice :activity :pay})
(pay-button* {:ids (selected->ids request
(:query-params request))}))
(when (can? (:identity request) {:subject :invoice :activity :create})
(com/button {:hx-get (bidi/path-for ssr-routes/only-routes ::route/new-wizard)}
"New invoice"))]))
:row-buttons (fn [request entity]
[(when (and (= :invoice-status/unpaid (:invoice/status entity))
(can? (:identity request) {:subject :invoice :activity :delete}))