making ledger reconciliation be more user friendly.

This commit is contained in:
2022-06-24 09:28:43 -07:00
parent 57d65074e5
commit 616798cebf

View File

@@ -255,57 +255,83 @@
:db/doc "touching invoice to update ledger"} :db/doc "touching invoice to update ledger"}
(entity-change->ledger (d/db conn) (entity-change->ledger (d/db conn)
[:invoice e])])) [:invoice e])]))
(defn mismatched-transactions [] (defn mismatched-transactions
(let [jel-accounts (reduce ([]
(mismatched-transactions (c/to-date (t/minus (t/now) (t/days 7)))
(c/to-date (t/minus (t/now) (t/hours 1)))) )
([changed-between-start changed-between-end]
(let [entities-to-consider (d/q '[:find [?t ...]
:in $ ?log ?start ?end
:where
[(tx-ids ?log ?start ?end) [?tx ...]]
[(tx-data ?log ?tx) [[?t]]]
[?t :transaction/date]]
(d/db auto-ap.datomic/conn)
(d/log auto-ap.datomic/conn)
changed-between-start
changed-between-end)
_ (log/info "checking" (count entities-to-consider) "transactions looking for mismatches between" changed-between-start changed-between-end)
jel-accounts (reduce
(fn [acc [e lia]] (fn [acc [e lia]]
(update acc e (fnil conj #{} ) lia)) (update acc e (fnil conj #{} ) lia))
{} {}
(d/query {:query {:find ['?e '?lia] (d/q '[:find ?e ?lia
:in ['$] :in $ [?e ...]
:where ['[?je :journal-entry/line-items ?li] :where
'[?je :journal-entry/original-entity ?e] [?je :journal-entry/original-entity ?e]
'[?li :journal-entry-line/account ?lia] [?je :journal-entry/line-items ?li]
'[?lia :account/name]]} [?li :journal-entry-line/account ?lia]
:args [(d/db auto-ap.datomic/conn)]})) [?lia :account/name]]
(d/db auto-ap.datomic/conn)
entities-to-consider))
transaction-accounts (reduce transaction-accounts (reduce
(fn [acc [e lia]] (fn [acc [e lia]]
(update acc e (fnil conj #{} ) lia)) (update acc e (fnil conj #{} ) lia))
{} {}
(d/query {:query {:find ['?e '?lia] (d/q '[:find ?e ?lia
:in ['$] :in $ [?e ...]
:where ['[?e :transaction/accounts ?li] :where
'(not [?e :transaction/approval-status :transaction-approval-status/excluded]) [?e :transaction/date ?d]
'(not [?e :transaction/approval-status :transaction-approval-status/suppressed]) [?e :transaction/accounts ?li]
'[?li :transaction-account/account ?lia] (not [?e :transaction/approval-status :transaction-approval-status/excluded])
(not [?e :transaction/approval-status :transaction-approval-status/suppressed])
'[?lia :account/name] [?li :transaction-account/account ?lia]
'[?e :transaction/amount ?amt] [?lia :account/name]
'[(not= ?amt 0.0)]]} [?e :transaction/amount ?amt]
:args [(d/db auto-ap.datomic/conn)]}))] [(not= ?amt 0.0)]]
(d/db auto-ap.datomic/conn)
entities-to-consider))]
(->> transaction-accounts (->> transaction-accounts
(filter (filter
(fn [[e accounts]] (not= accounts (get jel-accounts e)))) (fn [[e accounts]] (not= accounts (get jel-accounts e))))))))
(filter (fn [[e]]
(let [[tx-date] (d/q '[:find [(max ?d) ...] (defn unbalanced-transactions
:in $ ?t ([] (unbalanced-transactions (c/to-date (t/minus (t/now) (t/days 7)))
(c/to-date (t/minus (t/now) (t/hours 1)))))
([changed-between-start changed-between-end]
(let [entities-to-consider (d/q '[:find [?je ...]
:in $ ?log ?start ?end
:where :where
[?t _ _ ?tx] [(tx-ids ?log ?start ?end) [?tx ...]]
[?tx :db/txInstant ?d]] [(tx-data ?log ?tx) [[?je]]]
(d/history (d/db auto-ap.datomic/conn)) [?je :journal-entry/amount]
e)] [?je :journal-entry/original-entity ?i]
(t/before? (c/to-date-time tx-date) [?i :transaction/date]]
(t/minus (t/now) (t/hours 1))))))))) (d/db auto-ap.datomic/conn)
(d/log auto-ap.datomic/conn)
(defn unbalanced-transactions [] changed-between-start
(->> (d/query {:query {:find ['?je '?a '(sum ?debit) '(sum ?credit)] changed-between-end)]
:with ['?jel] (log/info "checking" (count entities-to-consider) "transaction journal entries looking for mismatches between" changed-between-start changed-between-end)
:in '[$] (->> (d/q '[:find ?je ?a (sum ?debit) (sum ?credit)
:where ['[?je :journal-entry/amount ?a] :with ?jel
'[?je :journal-entry/line-items ?jel] :in $ [?je ...]
'[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit] :where [?je :journal-entry/amount ?a]
'[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]] [?je :journal-entry/line-items ?jel]
} [(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit]
:args [(d/db auto-ap.datomic/conn)]}) [(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
]
(d/db auto-ap.datomic/conn)
entities-to-consider)
(filter (fn [[_ a d c]] (filter (fn [[_ a d c]]
(or (not (dollars= a d)) (or (not (dollars= a d))
(not (dollars= a c))))) (not (dollars= a c)))))
@@ -313,20 +339,35 @@
(map (fn [je] (map (fn [je]
(:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn) (:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn)
je)))) je))))
(filter (fn [transaction?] (map :db/id)))))
(:transaction/amount transaction?)))
(map :db/id)))
(defn unbalanced-invoices [] (defn unbalanced-invoices
(->> (d/query {:query {:find ['?je '?a '(sum ?debit) '(sum ?credit)] ([] (unbalanced-invoices (c/to-date (t/minus (t/now) (t/days 7)))
:with ['?jel] (c/to-date (t/minus (t/now) (t/hours 1)))))
:in '[$] ([changed-between-start changed-between-end]
:where ['[?je :journal-entry/amount ?a] (let [entities-to-consider (d/q '[:find [?je ...]
'[?je :journal-entry/line-items ?jel] :in $ ?log ?start ?end
'[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit] :where
'[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]] [(tx-ids ?log ?start ?end) [?tx ...]]
} [(tx-data ?log ?tx) [[?je]]]
:args [(d/db auto-ap.datomic/conn)]}) [?je :journal-entry/amount]
[?je :journal-entry/original-entity ?i]
[?i :invoice/date]]
(d/db auto-ap.datomic/conn)
(d/log auto-ap.datomic/conn)
changed-between-start
changed-between-end)]
(log/info "checking" (count entities-to-consider) "invoice journal entries looking for mismatches between" changed-between-start changed-between-end)
(->> (d/q '[:find ?je ?a (sum ?debit) (sum ?credit)
:with ?jel
:in $ [?je ...]
:where [?je :journal-entry/amount ?a]
[?je :journal-entry/line-items ?jel]
[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit]
[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
]
(d/db auto-ap.datomic/conn)
entities-to-consider)
(filter (fn [[_ a d c]] (filter (fn [[_ a d c]]
(or (not (dollars= a d)) (or (not (dollars= a d))
(not (dollars= a c))))) (not (dollars= a c)))))
@@ -334,43 +375,61 @@
(map (fn [je] (map (fn [je]
(:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn) (:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn)
je)))) je))))
(filter (fn [invoice?] (map :db/id)))))
(:invoice/total invoice?)))
(map :db/id)))
(defn mismatched-invoices [] (defn mismatched-invoices
(let [jel-accounts (reduce ([]
(mismatched-invoices (c/to-date (t/minus (t/now) (t/days 7)))
(c/to-date (t/minus (t/now) (t/hours 1)))) )
([changed-between-start changed-between-end]
(let [entities-to-consider (d/q '[:find [?i ...]
:in $ ?log ?start ?end
:where
[(tx-ids ?log ?start ?end) [?tx ...]]
[(tx-data ?log ?tx) [[?i]]]
[?i :invoice/date]]
(d/db auto-ap.datomic/conn)
(d/log auto-ap.datomic/conn)
changed-between-start
changed-between-end)
_ (log/info (count entities-to-consider) "invoices have changed between" changed-between-start "and" changed-between-end)
jel-accounts (reduce
(fn [acc [e lia]] (fn [acc [e lia]]
(update acc e (fnil conj #{} ) lia)) (update acc e (fnil conj #{} ) lia))
{} {}
(d/query {:query {:find ['?e '?lia] (d/q '[:find ?e ?lia
:in ['$] :in $ [?e ...]
:where ['[?je :journal-entry/line-items ?li] :where
'[?je :journal-entry/original-entity ?e] [?je :journal-entry/original-entity ?e]
'[?li :journal-entry-line/account ?lia] [?je :journal-entry/line-items ?li]
'(not [?lia :account/numeric-code 21000]) [?li :journal-entry-line/account ?lia]
'[?lia :account/name]]} (not [?lia :account/numeric-code 21000])
:args [(d/db auto-ap.datomic/conn)]})) [?lia :account/name]]
(d/db auto-ap.datomic/conn)
entities-to-consider))
invoice-accounts (reduce invoice-accounts (reduce
(fn [acc [e lia]] (fn [acc [e lia]]
(update acc e (fnil conj #{} ) lia)) (update acc e (fnil conj #{} ) lia))
{} {}
(d/query {:query {:find ['?e '?lia] (d/q '[:find ?e ?lia
:in ['$] :in $ [?e ...]
:where ['[?e :invoice/expense-accounts ?li] :where
'(not [?e :invoice/total 0.0]) [?e :invoice/expense-accounts ?li]
'[?li :invoice-expense-account/account ?lia] (not [?e :invoice/total 0.0])
'[?lia :account/name] [?li :invoice-expense-account/account ?lia]
'(not [?lia :account/numeric-code 21000]) [?lia :account/name]
'(not [?e :invoice/status :invoice-status/voided]) (not [?lia :account/numeric-code 21000])
'(not [?e :invoice/exclude-from-ledger true]) (not [?e :invoice/status :invoice-status/voided])
'[?e :invoice/import-status :import-status/imported]]} (not [?e :invoice/exclude-from-ledger true])
:args [(d/db auto-ap.datomic/conn)]})) [?e :invoice/import-status :import-status/imported]]
(d/db auto-ap.datomic/conn)
entities-to-consider))
] ]
(filter (filter
(fn [[e accounts]] (fn [[e accounts]]
(not= accounts (get jel-accounts e))) (not= accounts (get jel-accounts e)))
invoice-accounts))) invoice-accounts))))
(defn touch-broken-ledger [] (defn touch-broken-ledger []
(statsd/event {:title "Reconciling Ledger" (statsd/event {:title "Reconciling Ledger"