fixes running balances
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
(ns auto-ap.ledger
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn pull-id pull-ref transact-with-backoff]]
|
||||
[auto-ap.utils :refer [by dollars= heartbeat]]
|
||||
[auto-ap.datomic :refer [conn pull-id pull-ref]]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.utils :refer [by dollars= heartbeat]]
|
||||
[clj-time.coerce :as c]
|
||||
[clj-time.core :as t]
|
||||
[iol-ion.query :refer [account-sets]]
|
||||
[com.brunobonacci.mulog :as mu]
|
||||
[com.unbounce.dogstatsd.core :as statsd]
|
||||
[datomic.api :as dc]
|
||||
[manifold.deferred :as de]
|
||||
[manifold.stream :as s]
|
||||
[iol-ion.query :refer [account-sets Line]]
|
||||
[mount.core :as mount]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
@@ -387,6 +385,17 @@
|
||||
:client/last-running-balance #inst "2000-01-01"})
|
||||
{: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]
|
||||
(mu/with-context {:source "current-balance-refresh"}
|
||||
(let [db (dc/db conn)
|
||||
@@ -430,16 +439,16 @@
|
||||
running-balance-change (->> running-balance-set
|
||||
(reduce
|
||||
(fn [{:keys [changes last-running-balance]}
|
||||
{id :e
|
||||
[current-client current-account current-location current-date debit credit running-balance]
|
||||
:v}]
|
||||
^Line 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 current-account)))
|
||||
(- (or debit 0.0) (or credit 0.0))
|
||||
(- (or credit 0.0) (or debit 0.0)))
|
||||
: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 0.0)))]
|
||||
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
|
||||
@@ -447,7 +456,7 @@
|
||||
:changes changes}
|
||||
|
||||
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}))))
|
||||
{:last-running-balance 0.0})
|
||||
:changes)]
|
||||
@@ -468,11 +477,65 @@
|
||||
(comment
|
||||
(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)
|
||||
|
||||
(clients-needing-refresh (dc/db conn))
|
||||
(count (clients-needing-refresh (dc/db conn)))
|
||||
|
||||
(upsert-running-balance)
|
||||
|
||||
@@ -500,7 +563,6 @@
|
||||
|
||||
|
||||
|
||||
|
||||
#_(dc/q '[:find (pull ?je [*]) (pull ?jel [*])
|
||||
:where [?je :journal-entry/line-items ?jel]
|
||||
(not [?jel :journal-entry-line/running-balance-tuple])]
|
||||
|
||||
Reference in New Issue
Block a user