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