Automatically tries to fix transactions that don't balance debits and credits
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[auto-ap.datomic :refer [uri remove-nils conn]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[auto-ap.utils :refer [dollars-0?] ]
|
||||
[auto-ap.utils :refer [dollars-0? dollars=] ]
|
||||
[clojure.tools.logging :as log]
|
||||
[auto-ap.logging :refer [info-event]]
|
||||
[unilog.context :as lc]))
|
||||
@@ -260,6 +260,27 @@
|
||||
(fn [[e accounts]] (not= accounts (get jel-accounts e)))
|
||||
transaction-accounts)))
|
||||
|
||||
(defn unbalanced-transactions []
|
||||
(->> (d/query {:query {:find ['?je '?a '(sum ?debit) '(sum ?credit)]
|
||||
:with ['?jel]
|
||||
:in '[$]
|
||||
: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 [transaction?]
|
||||
(:transaction/amount transaction?)))
|
||||
(map :db/id)))
|
||||
|
||||
(defn mismatched-invoices []
|
||||
(let [jel-accounts (reduce
|
||||
(fn [acc [e lia]]
|
||||
@@ -303,6 +324,13 @@
|
||||
(log/warn (count mismatched-ts) " transactions exist but don't match ledger ")
|
||||
(doseq [[m] mismatched-ts]
|
||||
(touch-transaction m)))))
|
||||
(log/info "Attempting to fix transactions that are in the ledger but debits/credits don't add up")
|
||||
(let [unbalanced-ts (unbalanced-transactions)]
|
||||
(if (seq unbalanced-ts)
|
||||
(do
|
||||
(log/warn (count unbalanced-ts) " transactions exist but don't have matching debits/credits ")
|
||||
(doseq [m unbalanced-ts]
|
||||
(touch-transaction m)))))
|
||||
(log/info "Finished fixing transactions that are in the ledger but are wrong")
|
||||
(let [mismatched-is (mismatched-invoices)]
|
||||
(if (seq mismatched-is)
|
||||
@@ -310,6 +338,8 @@
|
||||
(log/warn (count mismatched-is) " invoice exist but don't match ledger ")
|
||||
(doseq [[m] mismatched-is]
|
||||
(touch-invoice m)))))
|
||||
|
||||
|
||||
(log/info "Finished fixing invoices that are in the ledger but are wrong")
|
||||
(catch Exception e
|
||||
(log/error e)))))
|
||||
|
||||
Reference in New Issue
Block a user