Recalculates running balance as report is run

This commit is contained in:
2025-02-08 22:14:24 -08:00
parent d078fb8a15
commit 38ed981cba
3 changed files with 79 additions and 70 deletions

View File

@@ -352,8 +352,8 @@
(do (println (count a)) (do (println (count a))
(count a))))) (count a)))))
(defn clients-needing-refresh [db] (defn clients-needing-refresh [db available]
(->> (let [clients (->>
(dc/q '[:find (pull ?c [:client/code :db/id :client/ledger-last-change :client/last-running-balance]) (dc/q '[:find (pull ?c [:client/code :db/id :client/ledger-last-change :client/last-running-balance])
:in $ :in $
:where :where
@@ -363,7 +363,12 @@
[(> ?last-change ?last-running-balance)]] [(> ?last-change ?last-running-balance)]]
db) db)
(map (fn [[client]] (map (fn [[client]]
client)))) client)))]
(if (and (set? available) (seq available))
(filter (comp available :db/id) clients)
clients)))
#_(clients-needing-refresh (dc/db conn) #{ 17592273679867})
#_(comment [17592334354011 #inst "0024-08-03T07:52:58.000-00:00"] #_(comment [17592334354011 #inst "0024-08-03T07:52:58.000-00:00"]
[17592302554688 #inst "0023-07-20T07:52:58.000-00:00"] [17592302554688 #inst "0023-07-20T07:52:58.000-00:00"]
@@ -422,7 +427,9 @@
:bank-account/current-balance running-balance}]))))))) :bank-account/current-balance running-balance}])))))))
;; TODO using iol-ion query as the base, building running balance sets ;; TODO using iol-ion query as the base, building running balance sets
(defn upsert-running-balance [] (defn upsert-running-balance
([] (upsert-running-balance nil))
([clients]
(mu/with-context {:service "upsert-running-balance" (mu/with-context {:service "upsert-running-balance"
:source "upsert-running-balance"} :source "upsert-running-balance"}
(mu/trace ::updating-balance (mu/trace ::updating-balance
@@ -430,7 +437,7 @@
:source "upsert-running-balance"] :source "upsert-running-balance"]
(let [db (dc/db conn) (let [db (dc/db conn)
starting-at (c/to-date (t/now)) starting-at (c/to-date (t/now))
clients (clients-needing-refresh db) clients (clients-needing-refresh db clients)
_ (alog/info ::clients-needing-update :clients clients :count (count clients)) _ (alog/info ::clients-needing-update :clients clients :count (count clients))
client-change-stats (atom {}) client-change-stats (atom {})
changes (for [c clients changes (for [c clients
@@ -473,7 +480,7 @@
{:user/name "running balance updater"}) {:user/name "running balance updater"})
(alog/info ::change-stats :stats @client-change-stats) (alog/info ::change-stats :stats @client-change-stats)
(refresh-bank-account-balances (map :db/id clients)) (refresh-bank-account-balances (map :db/id clients))
(count changes))))) (count changes))))))
(comment (comment
(pull-id (dc/db conn) [:client/code "SCCB"]) (pull-id (dc/db conn) [:client/code "SCCB"])

View File

@@ -4,7 +4,7 @@
[auto-ap.datomic [auto-ap.datomic
:refer [conn pull-many]] :refer [conn pull-many]]
[auto-ap.graphql.utils :refer [assert-can-see-client]] [auto-ap.graphql.utils :refer [assert-can-see-client]]
[auto-ap.ledger :refer [build-account-lookup]] [auto-ap.ledger :refer [build-account-lookup upsert-running-balance]]
[auto-ap.ledger.reports :as l-reports] [auto-ap.ledger.reports :as l-reports]
[auto-ap.logging :as alog] [auto-ap.logging :as alog]
[auto-ap.pdf.ledger :refer [table->pdf]] [auto-ap.pdf.ledger :refer [table->pdf]]
@@ -72,6 +72,8 @@
_ (doseq [client-id client-ids] _ (doseq [client-id client-ids]
(assert-can-see-client (:identity request) client-id)) (assert-can-see-client (:identity request) client-id))
_ (upsert-running-balance (into #{} client-ids))
lookup-account (->> client-ids lookup-account (->> client-ids
(map (fn build-lookup [client-id] (map (fn build-lookup [client-id]
[client-id (build-account-lookup client-id)])) [client-id (build-account-lookup client-id)]))

View File

@@ -4,7 +4,7 @@
[auto-ap.datomic [auto-ap.datomic
:refer [conn pull-many]] :refer [conn pull-many]]
[auto-ap.graphql.utils :refer [assert-can-see-client]] [auto-ap.graphql.utils :refer [assert-can-see-client]]
[auto-ap.ledger :refer [build-account-lookup]] [auto-ap.ledger :refer [build-account-lookup upsert-running-balance]]
[auto-ap.ledger.reports :as l-reports] [auto-ap.ledger.reports :as l-reports]
[auto-ap.logging :as alog] [auto-ap.logging :as alog]
[auto-ap.pdf.ledger :refer [table->pdf]] [auto-ap.pdf.ledger :refer [table->pdf]]
@@ -33,8 +33,7 @@
[config.core :refer [env] :as env] [config.core :refer [env] :as env]
[datomic.api :as dc] [datomic.api :as dc]
[iol-ion.utils :refer [by]] [iol-ion.utils :refer [by]]
[malli.core :as mc] [malli.core :as mc])
[medley.core :refer [map-vals]])
(:import (:import
[java.util UUID] [java.util UUID]
[org.apache.commons.io.output ByteArrayOutputStream])) [org.apache.commons.io.output ByteArrayOutputStream]))
@@ -76,6 +75,7 @@
(when (and (seq periods) client) (when (and (seq periods) client)
(let [client (if (= :all client) (take 5 (:clients request)) client) (let [client (if (= :all client) (take 5 (:clients request)) client)
client-ids (map :db/id client) client-ids (map :db/id client)
_ (upsert-running-balance (into #{} client-ids))
_ (doseq [client-id client-ids] _ (doseq [client-id client-ids]
(assert-can-see-client (:identity request) client-id)) (assert-can-see-client (:identity request) client-id))