(cloud) deleting old approach
This commit is contained in:
@@ -401,3 +401,25 @@
|
|||||||
:invoice/status (if (> new-outstanding-balance 0)
|
:invoice/status (if (> new-outstanding-balance 0)
|
||||||
:invoice-status/unpaid
|
:invoice-status/unpaid
|
||||||
:invoice-status/paid)})))
|
:invoice-status/paid)})))
|
||||||
|
|
||||||
|
|
||||||
|
;; TODO expected-deposit ledger entry
|
||||||
|
#_(defmethod entity-change->ledger :expected-deposit
|
||||||
|
[db [type id]]
|
||||||
|
(let [{:expected-deposit/keys [total client date]} (d/pull db '[:expected-deposit/total :expected-deposit/client :expected-deposit/date] id)]
|
||||||
|
#:journal-entry
|
||||||
|
{:source "expected-deposit"
|
||||||
|
:original-entity id
|
||||||
|
|
||||||
|
:client client
|
||||||
|
:date date
|
||||||
|
:amount total
|
||||||
|
:vendor :vendor/ccp-square
|
||||||
|
:line-items [#:journal-entry-line
|
||||||
|
{:credit total
|
||||||
|
:location "A"
|
||||||
|
:account :account/receipts-split}
|
||||||
|
#:journal-entry-line
|
||||||
|
{:debit total
|
||||||
|
:location "A"
|
||||||
|
:account :account/ccp}]}))
|
||||||
|
|||||||
@@ -500,7 +500,7 @@
|
|||||||
(let [invoice (:invoice-payment/invoice x)
|
(let [invoice (:invoice-payment/invoice x)
|
||||||
new-balance (+ (:invoice/outstanding-balance invoice)
|
new-balance (+ (:invoice/outstanding-balance invoice)
|
||||||
(:invoice-payment/amount x))]
|
(:invoice-payment/amount x))]
|
||||||
[[:db.fn/retractEntity (:db/id x)]
|
[[:db/retractEntity (:db/id x)]
|
||||||
`(upsert-invoice ~{:db/id (:db/id invoice)
|
`(upsert-invoice ~{:db/id (:db/id invoice)
|
||||||
:invoice/outstanding-balance new-balance
|
:invoice/outstanding-balance new-balance
|
||||||
:invoice/status (if (dollars-0? new-balance)
|
:invoice/status (if (dollars-0? new-balance)
|
||||||
|
|||||||
@@ -1,162 +1,18 @@
|
|||||||
(ns auto-ap.ledger
|
(ns auto-ap.ledger
|
||||||
(:require
|
(:require
|
||||||
[auto-ap.datomic
|
[auto-ap.datomic :refer [conn pull-id pull-ref transact-with-backoff]]
|
||||||
:refer [audit-transact
|
[auto-ap.utils :refer [by dollars= heartbeat]]
|
||||||
conn
|
|
||||||
pull-id
|
|
||||||
pull-ref
|
|
||||||
remove-nils
|
|
||||||
transact-with-backoff]]
|
|
||||||
[auto-ap.utils :refer [by dollars-0? dollars= heartbeat]]
|
|
||||||
[clj-time.coerce :as c]
|
[clj-time.coerce :as c]
|
||||||
[clj-time.core :as t]
|
[clj-time.core :as t]
|
||||||
[clojure.tools.logging :as log]
|
|
||||||
[com.brunobonacci.mulog :as mu]
|
[com.brunobonacci.mulog :as mu]
|
||||||
[com.unbounce.dogstatsd.core :as statsd]
|
[com.unbounce.dogstatsd.core :as statsd]
|
||||||
[datomic.client.api :as dc]
|
[datomic.client.api :as dc]
|
||||||
[iol-ion.tx :refer [upsert-ledger]]
|
[iol-ion.tx :refer [upsert-invoice upsert-ledger upsert-transaction]]
|
||||||
[manifold.deferred :as de]
|
[manifold.deferred :as de]
|
||||||
[manifold.stream :as s]
|
[manifold.stream :as s]
|
||||||
[mount.core :as mount]
|
[mount.core :as mount]
|
||||||
[yang.scheduler :as scheduler]))
|
[yang.scheduler :as scheduler]))
|
||||||
|
|
||||||
(defn datums->impacted-entity [db [e changes]]
|
|
||||||
(let [entity (dc/pull db '[{:invoice/_expense-accounts [:db/id] :transaction/_accounts [:db/id]}] e)
|
|
||||||
namespaces (->> changes
|
|
||||||
(map #(:db/ident (dc/pull db '[:db/ident] (:a %))))
|
|
||||||
(map namespace)
|
|
||||||
set)]
|
|
||||||
(cond (namespaces "invoice" ) [[:invoice e]]
|
|
||||||
(namespaces "invoice-expense-account" ) [[:invoice (:db/id (:invoice/_expense-accounts entity))]]
|
|
||||||
(namespaces "transaction-account" ) [[:transaction (:db/id (:transaction/_accounts entity))]]
|
|
||||||
(namespaces "transaction" ) [[:transaction e]]
|
|
||||||
#_#_(namespaces "expected-deposit" ) [[:expected-deposit e]]
|
|
||||||
:else nil)))
|
|
||||||
|
|
||||||
(defmulti entity-change->ledger (fn [_ [type]]
|
|
||||||
type))
|
|
||||||
|
|
||||||
(defmethod entity-change->ledger :invoice
|
|
||||||
[db [_ id]]
|
|
||||||
(when id
|
|
||||||
(let [entity (dc/pull db ['* {:invoice/vendor '[*]
|
|
||||||
:invoice/payment '[*]
|
|
||||||
:invoice/status '[:db/ident]
|
|
||||||
:invoice/import-status '[:db/ident]}] id)
|
|
||||||
credit-invoice? (< (:invoice/total entity 0.0) 0.0)]
|
|
||||||
(when-not (or
|
|
||||||
(not (:invoice/total entity))
|
|
||||||
(= true (:invoice/exclude-from-ledger entity))
|
|
||||||
(= :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))
|
|
||||||
:journal-entry/date (:invoice/date entity)
|
|
||||||
:journal-entry/original-entity (:db/id entity)
|
|
||||||
:journal-entry/vendor (:db/id (:invoice/vendor entity))
|
|
||||||
:journal-entry/amount (Math/abs (:invoice/total entity))
|
|
||||||
|
|
||||||
:journal-entry/line-items (into [(cond-> {:db/id (str (:db/id entity) "-" 0)
|
|
||||||
:journal-entry-line/account :account/accounts-payable
|
|
||||||
:journal-entry-line/location "A"
|
|
||||||
}
|
|
||||||
credit-invoice? (assoc :journal-entry-line/debit (Math/abs (:invoice/total entity)))
|
|
||||||
(not credit-invoice?) (assoc :journal-entry-line/credit (Math/abs (:invoice/total entity))))]
|
|
||||||
(map-indexed (fn [i ea]
|
|
||||||
(cond->
|
|
||||||
{:db/id (str (:db/id entity) "-" (inc i))
|
|
||||||
:journal-entry-line/account (:db/id (:invoice-expense-account/account ea))
|
|
||||||
:journal-entry-line/location (or (:invoice-expense-account/location ea) "HQ")
|
|
||||||
}
|
|
||||||
credit-invoice? (assoc :journal-entry-line/credit (Math/abs (:invoice-expense-account/amount ea)))
|
|
||||||
(not credit-invoice?) (assoc :journal-entry-line/debit (Math/abs (:invoice-expense-account/amount ea)))))
|
|
||||||
(:invoice/expense-accounts entity)))
|
|
||||||
:journal-entry/cleared (and (< (:invoice/outstanding-balance entity) 0.01)
|
|
||||||
(every? #(= :payment-status/cleared (:payment/status %)) (:invoice/payments entity))
|
|
||||||
)})))))
|
|
||||||
|
|
||||||
(defmethod entity-change->ledger :transaction
|
|
||||||
[db [_ id]]
|
|
||||||
(when id
|
|
||||||
(let [entity (dc/pull db ['* {:transaction/vendor '[*]
|
|
||||||
:transaction/client '[*]
|
|
||||||
:transaction/approval-status '[*]
|
|
||||||
:transaction/bank-account '[* {:bank-account/type [:db/ident]}]
|
|
||||||
:transaction/accounts '[*
|
|
||||||
{:transaction-account/account [*]}] }] id)
|
|
||||||
decreasing? (< (or (:transaction/amount entity) 0.0) 0.0)
|
|
||||||
credit-from-bank? decreasing?
|
|
||||||
debit-from-bank? (not decreasing?)]
|
|
||||||
(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/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
|
|
||||||
:db/id (str (:db/id entity) "-" 0)
|
|
||||||
: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-indexed
|
|
||||||
(fn [i a]
|
|
||||||
(remove-nils{
|
|
||||||
:db/id (str (:db/id entity) "-" (inc i))
|
|
||||||
: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]]
|
|
||||||
(let [{:expected-deposit/keys [total client date]} (d/pull db '[:expected-deposit/total :expected-deposit/client :expected-deposit/date] id)]
|
|
||||||
#:journal-entry
|
|
||||||
{:source "expected-deposit"
|
|
||||||
:original-entity id
|
|
||||||
|
|
||||||
:client client
|
|
||||||
:date date
|
|
||||||
:amount total
|
|
||||||
:vendor :vendor/ccp-square
|
|
||||||
:line-items [#:journal-entry-line
|
|
||||||
{:credit total
|
|
||||||
:location "A"
|
|
||||||
:account :account/receipts-split}
|
|
||||||
#:journal-entry-line
|
|
||||||
{:debit total
|
|
||||||
:location "A"
|
|
||||||
:account :account/ccp}]}))
|
|
||||||
|
|
||||||
(defmethod entity-change->ledger :invoice-expense-account
|
|
||||||
[_ _]
|
|
||||||
nil
|
|
||||||
)
|
|
||||||
|
|
||||||
(defmethod entity-change->ledger nil
|
|
||||||
[_ _]
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defn reconcile-ledger
|
(defn reconcile-ledger
|
||||||
([] (reconcile-ledger (-> (t/now)
|
([] (reconcile-ledger (-> (t/now)
|
||||||
(t/plus (t/months -6))
|
(t/plus (t/months -6))
|
||||||
@@ -174,7 +30,8 @@
|
|||||||
]}
|
]}
|
||||||
:args [(dc/db conn) start-date]})
|
:args [(dc/db conn) start-date]})
|
||||||
(map first)
|
(map first)
|
||||||
(mapv #(entity-change->ledger (dc/db conn) [:transaction %])))
|
(mapv (fn [t]
|
||||||
|
`(upsert-transaction ~{:db/id t}))))
|
||||||
|
|
||||||
|
|
||||||
invoices-missing-ledger-entries (->> (dc/q {:query {:find ['?t ]
|
invoices-missing-ledger-entries (->> (dc/q {:query {:find ['?t ]
|
||||||
@@ -190,7 +47,8 @@
|
|||||||
]}
|
]}
|
||||||
:args [(dc/db conn) start-date]})
|
:args [(dc/db conn) start-date]})
|
||||||
(map first)
|
(map first)
|
||||||
(mapv #(entity-change->ledger (dc/db conn) [:invoice %])))
|
(mapv (fn [i]
|
||||||
|
`(upsert-invoice ~{:db/id i}))))
|
||||||
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
|
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
|
||||||
(when (seq repairs)
|
(when (seq repairs)
|
||||||
(mu/log ::ledger-repairs-needed
|
(mu/log ::ledger-repairs-needed
|
||||||
@@ -203,36 +61,14 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn touch-transaction [e]
|
(defn touch-transaction [e]
|
||||||
(when-let [change (entity-change->ledger (dc/db conn)
|
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
|
||||||
[:transaction e])]
|
:db/doc "touching transaction to update ledger"}
|
||||||
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
|
`(upsert-transaction ~{:db/id e})]}))
|
||||||
:db/doc "touching transaction to update ledger"}
|
|
||||||
`(upsert-ledger ~change)]})))
|
|
||||||
|
|
||||||
(defn touch-invoice [e]
|
(defn touch-invoice [e]
|
||||||
(when-let [change (entity-change->ledger (dc/db conn)
|
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
|
||||||
[:invoice e])]
|
:db/doc "touching invoice to update ledger"}
|
||||||
(dc/transact conn {:tx-data [{:db/id "datomic.tx"
|
`(upsert-invoice ~{:db/id e})]}))
|
||||||
:db/doc "touching invoice to update ledger"}
|
|
||||||
`(upsert-ledger ~change)]})))
|
|
||||||
|
|
||||||
(defn lazy-tx-range
|
|
||||||
([start end xf] (lazy-tx-range start end xf 0))
|
|
||||||
([start end xf o]
|
|
||||||
(let [next-results (dc/tx-range conn {:start start
|
|
||||||
:end end
|
|
||||||
:offset o
|
|
||||||
:limit 200})]
|
|
||||||
(lazy-seq
|
|
||||||
(if (seq next-results)
|
|
||||||
(concat (sequence (comp (mapcat :data)
|
|
||||||
xf) next-results) (lazy-tx-range start
|
|
||||||
end
|
|
||||||
xf
|
|
||||||
(+ (count next-results)
|
|
||||||
o)))
|
|
||||||
next-results)))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,12 @@ upsertledger - matching transaction rule might not assign an account. Other thin
|
|||||||
Double check each job still functions in the new system
|
Double check each job still functions in the new system
|
||||||
Make reports just be based on running-balances
|
Make reports just be based on running-balances
|
||||||
Test exports
|
Test exports
|
||||||
Move pay into iol-ion.tx, make sure to use upsert-invoice
|
|
||||||
|
|
||||||
Some jobs just aren't so big they need to be jobs anymore:
|
Some jobs just aren't so big they need to be jobs anymore:
|
||||||
Refreshing running balance for journal entry lines
|
Refreshing running balance for journal entry lines
|
||||||
Refreshing current balance for bank accounts
|
Refreshing current balance for bank accounts
|
||||||
Closing auto invoices
|
Closing auto invoices
|
||||||
|
|
||||||
|
|
||||||
Running Balance Cache
|
Running Balance Cache
|
||||||
* Add tests for upsert-ledger
|
* Add tests for upsert-ledger
|
||||||
* Make a new way to reset the entire cache for a client
|
* Make a new way to reset the entire cache for a client
|
||||||
|
|||||||
Reference in New Issue
Block a user