Files
integreat/scratch-sessions/pnl-export.repl
2023-06-13 22:05:58 -07:00

84 lines
6.2 KiB
Plaintext

;; This buffer is for Clojure experiments and evaluation.
;; Press C-j to evaluate the last expression.
;; You can also press C-u C-j to evaluate the expression and pretty-print its result.
(dc/pull (dc/db conn) [:db/id] [:client/code "NGAK"])
(user/init-repl)
(require '[auto-ap.datomic :refer [pull-attr pull-many]])
(csv/write-csv *out*
(let [clients (pull-many (dc/db conn) '[:db/id :client/code :client/locations] [[:client/code "NGAK"] [:client/code "NGOP"] #_#_[:client/code "NGRV"] [:client/code "NGE1"]])
account->numeric-code (into {} (seq (dc/q '[:find ?i ?n
:in $ [?a ...]
:where [?i ?a ?n]]
(dc/db conn) [:account/numeric-code :bank-account/numeric-code])))
account->name (let [lookup (into {} (seq (dc/q '[:find ?i ?n
:in $
:where [?i :account/name ?n]]
(dc/db conn))))]
(fn [x]
(or (lookup x) "Bank Account")))
account->location (into {} (seq (dc/q '[:find ?i ?l
:in $
:where [?i :account/location ?l]]
(dc/db conn))))
used-accounts (into #{} (for [c clients
d (dc/index-range (dc/db conn)
:journal-entry-line/client+account+location+date
[(:db/id c)]
[(inc (:db/id c))])
:let [[_ account] (:v d)]]
account))]
(for [client clients
:let [all-entries (->> (dc/index-pull (dc/db conn)
{:index :avet
:selector [:db/id :journal-entry-line/running-balance :journal-entry-line/client+account+location+date]
:start [:journal-entry-line/client+account+location+date [(:db/id client)]]
:reverse false
:limit 1})
(take-while (fn [curr]
(= (first (:journal-entry-line/client+account+location+date curr))
(:db/id client)))))
account-lookup (->> all-entries
(reduce
(fn [acc curr]
(let [[client account location date] (:journal-entry-line/client+account+location+date curr)
numeric-code (account->numeric-code account)]
(assoc acc (format "%d-%d-%s-%s" client numeric-code location (atime/unparse-local (clj-time.coerce/to-date-time date) atime/iso-date)) (:journal-entry-line/running-balance curr))))
(sorted-map)))]
a (sort-by account->numeric-code used-accounts)
:let [numeric-code (account->numeric-code a)]
l (or (account->location a) (:client/locations client))
delta (range -60 0)
:let [date (t/plus (c/to-date-time #inst "2023-06-12T08:00:00") (t/days delta))
date-str (atime/unparse-local date atime/iso-date)]
:when (and numeric-code (>= numeric-code 40000))]
(conj [(:client/code client) l numeric-code (account->name a) date-str] (with-precision 2
(double (.setScale (bigdec (or
(some->> (subseq account-lookup >= (format "%d-%d-%s-%s" (:db/id client) numeric-code l "2000-01-01") <= (format "%d-%d-%s-%s" (:db/id client) numeric-code l date-str))
last
last)
0.0))
2 java.math.RoundingMode/HALF_UP))))))
)
(dc/q '[:find (count ?x)
:where [?x :journal-entry-line/dirty true]]
(dc/db conn))
(let [clients (map first
(dc/q '[:find (pull ?c [:client/code :db/id])
:where [?c :client/code]]
(dc/db conn)))]
(doseq [[c i] (map vector clients (range))]
(let [db (dc/db conn)
accounts-needing-rebuild (auto-ap.ledger/accounts-needing-rebuild db (:db/id c))]
(when (seq accounts-needing-rebuild)
(println "C" c "needs" (count accounts-needing-rebuild) "built")
#_(refresh-running-balance-accounts accounts-needing-rebuild clients c i db)
#_(mu/log ::client-completed)))))
,