diff --git a/src/clj/auto_ap/datomic.clj b/src/clj/auto_ap/datomic.clj index 4729db20..e5516ac0 100644 --- a/src/clj/auto_ap/datomic.clj +++ b/src/clj/auto_ap/datomic.clj @@ -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))] diff --git a/src/clj/auto_ap/ssr/grid_page_helper.clj b/src/clj/auto_ap/ssr/grid_page_helper.clj index 3fb9e12c..fa36881f 100644 --- a/src/clj/auto_ap/ssr/grid_page_helper.clj +++ b/src/clj/auto_ap/ssr/grid_page_helper.clj @@ -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)) diff --git a/src/clj/auto_ap/ssr/invoices.clj b/src/clj/auto_ap/ssr/invoices.clj index 2e9369e5..8edd2207 100644 --- a/src/clj/auto_ap/ssr/invoices.clj +++ b/src/clj/auto_ap/ssr/invoices.clj @@ -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}))