This commit is contained in:
2024-11-14 21:14:03 -08:00
2 changed files with 82 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,16 +1,14 @@
(ns auto-ap.ledger (ns auto-ap.ledger
(:require (:require
[auto-ap.datomic :refer [conn pull-id pull-ref transact-with-backoff]] [auto-ap.datomic :refer [conn pull-id pull-ref]]
[auto-ap.utils :refer [by dollars= heartbeat]]
[auto-ap.logging :as alog] [auto-ap.logging :as alog]
[auto-ap.utils :refer [by dollars= heartbeat]]
[clj-time.coerce :as c] [clj-time.coerce :as c]
[clj-time.core :as t] [clj-time.core :as t]
[iol-ion.query :refer [account-sets]]
[com.brunobonacci.mulog :as mu] [com.brunobonacci.mulog :as mu]
[com.unbounce.dogstatsd.core :as statsd] [com.unbounce.dogstatsd.core :as statsd]
[datomic.api :as dc] [datomic.api :as dc]
[manifold.deferred :as de] [iol-ion.query :refer [account-sets Line]]
[manifold.stream :as s]
[mount.core :as mount] [mount.core :as mount]
[yang.scheduler :as scheduler])) [yang.scheduler :as scheduler]))
@@ -387,6 +385,17 @@
:client/last-running-balance #inst "2000-01-01"}) :client/last-running-balance #inst "2000-01-01"})
{:user/name "backfill-client and dates"})) {:user/name "backfill-client and dates"}))
(defn mark-client-dirty [client-code]
(auto-ap.datomic/audit-transact-batch
(for [[c] (dc/q '[:find ?c
:in $ ?cd
:where [?c :client/code ?cd]]
(dc/db conn)
client-code)]
{:db/id c :client/ledger-last-change (c/to-date (t/now))
:client/last-running-balance #inst "2000-01-01"})
{:user/name "backfill-client and dates"}))
(defn refresh-bank-account-balances [client-ids] (defn refresh-bank-account-balances [client-ids]
(mu/with-context {:source "current-balance-refresh"} (mu/with-context {:source "current-balance-refresh"}
(let [db (dc/db conn) (let [db (dc/db conn)
@@ -430,16 +439,16 @@
running-balance-change (->> running-balance-set running-balance-change (->> running-balance-set
(reduce (reduce
(fn [{:keys [changes last-running-balance]} (fn [{:keys [changes last-running-balance]}
{id :e ^Line line-item]
[current-client current-account current-location current-date debit credit running-balance] #_(if (= 0 (rand-int 1000))
:v}] (println (.-account-id line-item) (.-debit line-item) (.-credit line-item)))
(let [delta (if (#{:account-type/asset (let [delta (if (#{:account-type/asset
:account-type/dividend :account-type/dividend
:account-type/expense} (:account_type (account-lookup current-account))) :account-type/expense} (:account_type (account-lookup (.-account-id line-item))))
(- (or debit 0.0) (or credit 0.0)) (- (or (.-debit line-item) 0.0) (or (.-credit line-item) 0.0))
(- (or credit 0.0) (or debit 0.0))) (- (or (.-credit line-item) 0.0) (or (.-debit line-item) 0.0)))
correct-running-balance (+ last-running-balance delta) correct-running-balance (+ last-running-balance delta)
running-balance-changed? (not (dollars= correct-running-balance (or running-balance 0.0)))] running-balance-changed? (not (dollars= correct-running-balance (or (.-running-balance line-item) 0.0)))]
(when running-balance-changed? (when running-balance-changed?
(swap! client-change-stats update (:client/code c) (fnil inc 0))) (swap! client-change-stats update (:client/code c) (fnil inc 0)))
(cond-> {:last-account-lookup account-lookup (cond-> {:last-account-lookup account-lookup
@@ -447,7 +456,7 @@
:changes changes} :changes changes}
running-balance-changed? running-balance-changed?
(update :changes conj {:db/id id (update :changes conj {:db/id (.-id line-item)
:journal-entry-line/running-balance correct-running-balance})))) :journal-entry-line/running-balance correct-running-balance}))))
{:last-running-balance 0.0}) {:last-running-balance 0.0})
:changes)] :changes)]
@@ -468,12 +477,70 @@
(comment (comment
(pull-id (dc/db conn) [:client/code "SCCB"]) (pull-id (dc/db conn) [:client/code "SCCB"])
#_(do
(mu/with-context {:service "upsert-running-balance"
:source "upsert-running-balance" }
(mu/trace ::updating-balance
[:service "upsert-running-balance"
:source "upsert-running-balance" ]
(let [db (dc/db conn)
starting-at (c/to-date (t/now))
_ (mark-client-dirty "NGA1")
clients (clients-needing-refresh db)
_ (alog/info ::clients-needing-update :clients clients :count (count clients))
client-change-stats (atom {})
changes (for [c clients
:let [client-id (:db/id c)
account-lookup (build-account-lookup client-id)]
running-balance-set (account-sets db client-id)
running-balance-change (->> running-balance-set
(reduce
(fn [{:keys [changes last-running-balance]}
line-item]
(if (= 0 (rand-int 1000))
(println (.-account-id line-item) (.-debit line-item) (.-credit line-item)))
(let [delta (if (#{:account-type/asset
:account-type/dividend
:account-type/expense} (:account_type (account-lookup (.-account-id line-item))))
(- (or (.-debit line-item) 0.0) (or (.-credit line-item) 0.0))
(- (or (.-credit line-item) 0.0) (or (.-debit line-item) 0.0)))
correct-running-balance (+ last-running-balance delta)
running-balance-changed? (not (dollars= correct-running-balance (or (.-running-balance line-item) 0.0)))]
(when running-balance-changed?
(swap! client-change-stats update (:client/code c) (fnil inc 0)))
(cond-> {:last-account-lookup account-lookup
:last-running-balance correct-running-balance
:changes changes}
running-balance-changed?
(update :changes conj {:db/id (.-id line-item)
:journal-entry-line/running-balance correct-running-balance}))))
{:last-running-balance 0.0})
:changes)]
running-balance-change)]
(mu/trace ::update-running-balance []
(auto-ap.datomic/audit-transact-batch changes
{:user/name "running balance updater"}))
(auto-ap.datomic/audit-transact (mapv (fn [c]
{:db/id (:db/id c)
:client/last-running-balance starting-at})
clients)
{:user/name "running balance updater"})
(alog/info ::change-stats :stats @client-change-stats)
(refresh-bank-account-balances (map :db/id clients))
(count changes)))))
(mark-client-dirty "NGA1")
(mark-all-clients-dirty) (mark-all-clients-dirty)
<<<<<<< HEAD
(clients-needing-refresh (dc/db conn)) (clients-needing-refresh (dc/db conn))
=======
(count (clients-needing-refresh (dc/db conn)))
>>>>>>> master
(upsert-running-balance) (upsert-running-balance)
@@ -501,7 +568,6 @@
#_(dc/q '[:find (pull ?je [*]) (pull ?jel [*]) #_(dc/q '[:find (pull ?je [*]) (pull ?jel [*])
:where [?je :journal-entry/line-items ?jel] :where [?je :journal-entry/line-items ?jel]
(not [?jel :journal-entry-line/running-balance-tuple])] (not [?jel :journal-entry-line/running-balance-tuple])]