(cloud) experimental approach to ensure the ledgers do not get out of sync
This commit is contained in:
@@ -220,6 +220,82 @@
|
||||
result
|
||||
nil)))
|
||||
|
||||
|
||||
|
||||
|
||||
(defn invoice->journal-entry
|
||||
([db invoice-id]
|
||||
(invoice->journal-entry db invoice-id invoice-id))
|
||||
;; the 3-arity version allows you to pass a potential tempid in instead of the invoice-id,
|
||||
;; which would be a temporary value after the transaction
|
||||
([db invoice-id raw-invoice-id]
|
||||
(let [entity (dc/pull db
|
||||
'[:invoice/total
|
||||
:invoice/exclude-from-ledger
|
||||
:invoice/outstanding-balance
|
||||
:invoice/date
|
||||
{:invoice/vendor [:db/id :vendor/name]
|
||||
:invoice/client [:db/id :client/code]
|
||||
:invoice/payment [:db/id {:payment/status [:db/ident]}]
|
||||
:invoice/status [:db/ident]
|
||||
:invoice/import-status [:db/ident]
|
||||
:invoice/expense-accounts [:invoice-expense-account/account
|
||||
:invoice-expense-account/amount
|
||||
:invoice-expense-account/location]}]
|
||||
invoice-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)))
|
||||
(< -0.001 (:invoice/total entity) 0.001))
|
||||
|
||||
(remove-nils
|
||||
{:journal-entry/source "invoice"
|
||||
:journal-entry/client (:db/id (:invoice/client entity))
|
||||
:journal-entry/date (:invoice/date entity)
|
||||
:journal-entry/original-entity raw-invoice-id
|
||||
: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))
|
||||
)})))))
|
||||
|
||||
(defn upsert-invoice [db invoice]
|
||||
(let [
|
||||
upserted-entity (upsert-entity db invoice)
|
||||
with-invoice (try (dc/with db {:tx-data upserted-entity})
|
||||
(catch ClassCastException e
|
||||
(println "Dev local does not support with in tx functions. :(")
|
||||
(dc/with (dc/with-db @(resolve 'auto-ap.datomic/conn)) {:tx-data upserted-entity})
|
||||
))
|
||||
invoice-id (or (-> with-invoice :tempids (get (:db/id invoice)))
|
||||
(:db/id invoice))
|
||||
journal-entry (invoice->journal-entry (:db-after with-invoice)
|
||||
invoice-id
|
||||
(:db/id invoice))]
|
||||
(into upserted-entity
|
||||
(if journal-entry
|
||||
(upsert-ledger db journal-entry)
|
||||
[[:db/retractEntity [:journal-entry/original-entity (:db/id invoice)]]]))))
|
||||
|
||||
(defn propose-invoice [db invoice]
|
||||
(let [existing? (boolean (seq (dc/q '[:find ?i
|
||||
:in $ ?invoice-number ?client ?vendor
|
||||
@@ -228,10 +304,10 @@
|
||||
[?i :invoice/client ?client]
|
||||
[?i :invoice/vendor ?vendor]
|
||||
(not [?i :invoice/status :invoice-status/voided])]
|
||||
db
|
||||
(:invoice/invoice-number invoice)
|
||||
(:invoice/client invoice)
|
||||
(:invoice/vendor invoice))))]
|
||||
db
|
||||
(:invoice/invoice-number invoice)
|
||||
(:invoice/client invoice)
|
||||
(:invoice/vendor invoice))))]
|
||||
(if existing?
|
||||
[]
|
||||
[(remove-nils invoice)])))
|
||||
(upsert-invoice db invoice))))
|
||||
|
||||
Reference in New Issue
Block a user