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