Makes reconciling the ledger a bit faster
This commit is contained in:
@@ -195,37 +195,45 @@
|
||||
:start (scheduler/run-fun process-one 1)
|
||||
:stop (-> process-txes-worker :running? (reset! false)))
|
||||
|
||||
(defn reconcile-ledger []
|
||||
(let [txes-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
|
||||
:in ['$]
|
||||
:where ['[?t :transaction/date]
|
||||
'[?t :transaction/amount ?amt]
|
||||
'[(not= 0.0 ?amt)]
|
||||
'(not [?t :transaction/approval-status :transaction-approval-status/excluded])
|
||||
'(not [?t :transaction/approval-status :transaction-approval-status/suppressed])
|
||||
'(not-join [?t] [?e :journal-entry/original-entity ?t])]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
(mapv #(entity-change->ledger (d/db conn) [:transaction %])))
|
||||
(defn reconcile-ledger
|
||||
([] (reconcile-ledger (-> (t/now)
|
||||
(t/plus (t/months -6))
|
||||
(c/to-date))))
|
||||
([start-date]
|
||||
(let [txes-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
|
||||
:in ['$ '?sd]
|
||||
:where [
|
||||
'[?t :transaction/date ?d]
|
||||
'[(>= ?d ?sd)]
|
||||
'(not [_ :journal-entry/original-entity ?t])
|
||||
'(not [?t :transaction/amount 0.0])
|
||||
'(not [?t :transaction/approval-status :transaction-approval-status/excluded])
|
||||
'(not [?t :transaction/approval-status :transaction-approval-status/suppressed])
|
||||
]}
|
||||
:args [(d/db conn) start-date]})
|
||||
(map first)
|
||||
(mapv #(entity-change->ledger (d/db conn) [:transaction %])))
|
||||
|
||||
|
||||
invoices-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
|
||||
:in ['$]
|
||||
:where ['[?t :invoice/date]
|
||||
'[?t :invoice/total ?amt]
|
||||
'[(not= 0.0 ?amt)]
|
||||
'(not [?t :invoice/status :invoice-status/voided])
|
||||
'(not [?t :invoice/import-status :import-status/pending])
|
||||
'(not [?t :invoice/exclude-from-ledger true])
|
||||
'(not-join [?t] [?e :journal-entry/original-entity ?t])]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
(mapv #(entity-change->ledger (d/db conn) [:invoice %])))
|
||||
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
|
||||
(when (seq repairs)
|
||||
(log/info (take 3 repairs))
|
||||
(log/warn "repairing " (count txes-missing-ledger-entries) " missing transactions, " (count invoices-missing-ledger-entries) " missing invoices that were missing ledger entries")
|
||||
@(d/transact conn repairs))))
|
||||
invoices-missing-ledger-entries (->> (d/query {:query {:find ['?t ]
|
||||
:in ['$ '?sd]
|
||||
:where ['[?t :invoice/date ?d]
|
||||
'[(>= ?d ?sd)]
|
||||
'(not [_ :journal-entry/original-entity ?t])
|
||||
'[?t :invoice/total ?amt]
|
||||
'[(not= 0.0 ?amt)]
|
||||
'(not [?t :invoice/status :invoice-status/voided])
|
||||
'(not [?t :invoice/import-status :import-status/pending])
|
||||
'(not [?t :invoice/exclude-from-ledger true])
|
||||
]}
|
||||
:args [(d/db conn) start-date]})
|
||||
(map first)
|
||||
(mapv #(entity-change->ledger (d/db conn) [:invoice %])))
|
||||
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
|
||||
(when (seq repairs)
|
||||
(log/info (take 3 repairs))
|
||||
(log/warn "repairing " (count txes-missing-ledger-entries) " missing transactions, " (count invoices-missing-ledger-entries) " missing invoices that were missing ledger entries")
|
||||
@(d/transact conn repairs)))))
|
||||
|
||||
(mount/defstate reconciliation-frequency :start (* 1000 60 60))
|
||||
|
||||
@@ -236,7 +244,7 @@
|
||||
|
||||
(defn touch-transaction [e]
|
||||
@(d/transact conn [[:db/retractEntity [:journal-entry/original-entity e]]])
|
||||
@(d/transact conn [{:db/id "datomic.tx"
|
||||
@(d/transact conn [{:db/id "datomic.tx"
|
||||
:db/doc "touching transaction to update ledger"}
|
||||
(entity-change->ledger (d/db conn)
|
||||
[:transaction e])]))
|
||||
|
||||
Reference in New Issue
Block a user