From 57d65074e5b33d8f395c3a46d6e0b925da8affaf Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 23 Jun 2022 13:26:10 -0700 Subject: [PATCH] Makes reconciling the ledger a bit faster --- src/clj/auto_ap/ledger.clj | 68 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index 062e0985..e557e605 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -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])]))