no need for ledger automation.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
(ns auto-ap.ledger
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn remove-nils]]
|
||||
[auto-ap.datomic :refer [audit-transact conn remove-nils]]
|
||||
[auto-ap.logging :refer [info-event]]
|
||||
[auto-ap.utils :refer [dollars-0? dollars=]]
|
||||
[clj-time.coerce :as c]
|
||||
@@ -42,6 +42,7 @@
|
||||
(= :import-status/pending (:db/ident (:invoice/import-status entity)))
|
||||
(= :invoice-status/voided (:db/ident (:invoice/status entity)))
|
||||
(dollars-0? (:invoice/total entity)))
|
||||
|
||||
(remove-nils
|
||||
{:journal-entry/source "invoice"
|
||||
:journal-entry/client (:db/id (:invoice/client entity))
|
||||
@@ -76,42 +77,43 @@
|
||||
:transaction/bank-account '[* {:bank-account/type [:db/ident]}]
|
||||
:transaction/accounts '[*
|
||||
{:transaction-account/account [*]}] }] id)
|
||||
decreasing? (< (:transaction/amount entity) 0.0)
|
||||
decreasing? (< (or (:transaction/amount entity) 0.0) 0.0)
|
||||
credit-from-bank? decreasing?
|
||||
debit-from-bank? (not decreasing?)]
|
||||
(when-not (or (= :transaction-approval-status/excluded (:db/ident (:transaction/approval-status entity)))
|
||||
(= :transaction-approval-status/suppressed (:db/ident (:transaction/approval-status entity)))
|
||||
(dollars-0? (:transaction/amount entity)))
|
||||
(when (and (not (= :transaction-approval-status/excluded (:db/ident (:transaction/approval-status entity))))
|
||||
(not (= :transaction-approval-status/suppressed (:db/ident (:transaction/approval-status entity))))
|
||||
(:transaction/amount entity)
|
||||
(not (dollars-0? (:transaction/amount entity))))
|
||||
(remove-nils
|
||||
{:journal-entry/source "transaction"
|
||||
:journal-entry/client (:db/id (:transaction/client entity))
|
||||
:journal-entry/date (:transaction/date entity)
|
||||
:journal-entry/original-entity (:db/id entity)
|
||||
:journal-entry/alternate-description (:transaction/description-original entity)
|
||||
:journal-entry/vendor (:db/id (:transaction/vendor entity))
|
||||
:journal-entry/amount (Math/abs (:transaction/amount entity))
|
||||
:journal-entry/cleared-against (:transaction/cleared-against entity)
|
||||
{:journal-entry/source "transaction"
|
||||
:journal-entry/client (:db/id (:transaction/client entity))
|
||||
:journal-entry/date (:transaction/date entity)
|
||||
:journal-entry/original-entity (:db/id entity)
|
||||
:journal-entry/alternate-description (:transaction/description-original entity)
|
||||
:journal-entry/vendor (:db/id (:transaction/vendor entity))
|
||||
:journal-entry/amount (Math/abs (:transaction/amount entity))
|
||||
:journal-entry/cleared-against (:transaction/cleared-against entity)
|
||||
|
||||
:journal-entry/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
|
||||
:journal-entry-line/location "A"
|
||||
:journal-entry-line/credit (when credit-from-bank?
|
||||
(Math/abs (:transaction/amount entity)))
|
||||
:journal-entry-line/debit (when debit-from-bank?
|
||||
(Math/abs (:transaction/amount entity)))})
|
||||
]
|
||||
(map
|
||||
(fn [a]
|
||||
(remove-nils{:journal-entry-line/account (:db/id (:transaction-account/account a))
|
||||
:journal-entry-line/location (:transaction-account/location a)
|
||||
:journal-entry-line/debit (when credit-from-bank?
|
||||
(Math/abs (:transaction-account/amount a)))
|
||||
:journal-entry-line/credit (when debit-from-bank?
|
||||
(Math/abs (:transaction-account/amount a)))}))
|
||||
(if (seq (:transaction/accounts entity))
|
||||
(:transaction/accounts entity)
|
||||
[{:transaction-account/amount (:transaction/amount entity)}])))
|
||||
|
||||
:journal-entry/cleared true})))))
|
||||
:journal-entry/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
|
||||
:journal-entry-line/location "A"
|
||||
:journal-entry-line/credit (when credit-from-bank?
|
||||
(Math/abs (:transaction/amount entity)))
|
||||
:journal-entry-line/debit (when debit-from-bank?
|
||||
(Math/abs (:transaction/amount entity)))})
|
||||
]
|
||||
(map
|
||||
(fn [a]
|
||||
(remove-nils{:journal-entry-line/account (:db/id (:transaction-account/account a))
|
||||
:journal-entry-line/location (:transaction-account/location a)
|
||||
:journal-entry-line/debit (when credit-from-bank?
|
||||
(Math/abs (:transaction-account/amount a)))
|
||||
:journal-entry-line/credit (when debit-from-bank?
|
||||
(Math/abs (:transaction-account/amount a)))}))
|
||||
(if (seq (:transaction/accounts entity))
|
||||
(:transaction/accounts entity)
|
||||
[{:transaction-account/amount (:transaction/amount entity)}])))
|
||||
|
||||
:journal-entry/cleared true})))))
|
||||
|
||||
#_(defmethod entity-change->ledger :expected-deposit
|
||||
[db [type id]]
|
||||
@@ -142,45 +144,6 @@
|
||||
[_ _]
|
||||
nil)
|
||||
|
||||
(mount/defstate tx-report-queue
|
||||
:start (d/tx-report-queue conn)
|
||||
:stop (d/remove-tx-report-queue conn))
|
||||
|
||||
|
||||
(defn process-one []
|
||||
(lc/with-context {:source "process-txes"}
|
||||
(try
|
||||
(let [transaction (.take tx-report-queue)
|
||||
_ (log/info "Converting tranasction to ledger")
|
||||
db (:db-after transaction)
|
||||
affected-entities (->> (:tx-data transaction)
|
||||
(map (fn [^datomic.db.Datum x]
|
||||
{:e (:e x)
|
||||
:a (d/ident db (:a x))
|
||||
:v (:v x)
|
||||
:added (:added x)}))
|
||||
(group-by :e)
|
||||
(mapcat #(datums->impacted-entity db %))
|
||||
(set))
|
||||
_ (info-event (str "Found " (count affected-entities) " affected entities")
|
||||
{:affected-entities (count affected-entities)})
|
||||
d-txs (->> affected-entities
|
||||
(map #(entity-change->ledger db %))
|
||||
(filter seq))
|
||||
retractions (map (fn [[_ e]] [:db/retractEntity [:journal-entry/original-entity e]]) affected-entities)]
|
||||
|
||||
(when (seq retractions)
|
||||
@(d/transact conn retractions))
|
||||
|
||||
(doseq [d-tx d-txs]
|
||||
@(d/transact conn [d-tx]))
|
||||
(log/info "Succesfully process transaction"))
|
||||
(catch Exception e
|
||||
(log/error e)))))
|
||||
|
||||
(mount/defstate process-txes-worker
|
||||
:start (scheduler/run-fun process-one 1)
|
||||
:stop (-> process-txes-worker :running? (reset! false)))
|
||||
|
||||
(defn reconcile-ledger
|
||||
([] (reconcile-ledger (-> (t/now)
|
||||
@@ -459,3 +422,43 @@
|
||||
:text "This process looks for unbalance ledger entries, or missing ledger entries"
|
||||
:priority :low}
|
||||
nil))
|
||||
|
||||
(defn transact-with-ledger [transaction id]
|
||||
(let [db (d/db conn)
|
||||
tx (d/with db transaction)
|
||||
affected-entities (->> (:tx-data tx)
|
||||
(map (fn [^datomic.db.Datum x]
|
||||
{:e (:e x)
|
||||
:a (d/ident db (:a x))
|
||||
:v (:v x)
|
||||
:added (:added x)}))
|
||||
(group-by :e)
|
||||
(mapcat #(datums->impacted-entity db %))
|
||||
(set))
|
||||
ledger-txs (->> affected-entities
|
||||
(map #(entity-change->ledger (:db-after tx) %))
|
||||
(filter seq))
|
||||
retractions (map (fn [[_ e]] [:db/retractEntity [:journal-entry/original-entity e]]) affected-entities)]
|
||||
(audit-transact retractions id)
|
||||
(audit-transact (into transaction ledger-txs) id)))
|
||||
|
||||
|
||||
(defn transact-batch-with-ledger [txes id]
|
||||
(let [batch-id (.toString (java.util.UUID/randomUUID))]
|
||||
(reduce
|
||||
(fn [full-tx batch]
|
||||
(let [batch (conj (vec batch) {:db/id "datomic.tx"
|
||||
:audit/batch batch-id})
|
||||
_ (log/info "transacting batch " batch-id " " (count batch))
|
||||
tx-result (transact-with-ledger batch id)
|
||||
_ (Thread/sleep 1000)]
|
||||
|
||||
(cond-> full-tx
|
||||
(:tx-data full-tx) (update :tx-data #(into % (:tx-data tx-result)))
|
||||
(not (:tx-data full-tx)) (assoc :tx-data (vec (:tx-data tx-result)))
|
||||
(not (:db-before full-tx)) (assoc :db-before (:db-before tx-result))
|
||||
true (assoc :db-after (:db-after tx-result))
|
||||
true (update :tempids merge (:tempids tx-result)))))
|
||||
|
||||
{}
|
||||
(partition-all 50 txes))))
|
||||
|
||||
Reference in New Issue
Block a user