Adds ability to find expected deposits and link them

This commit is contained in:
2021-11-24 16:50:13 -08:00
parent 42bd65357f
commit 06ea51a168
11 changed files with 250 additions and 27 deletions

View File

@@ -32,6 +32,11 @@
:where ['[?e :expected-deposit/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:exact-match-id args)
(merge-query {:query {:in ['?e]
:where []}
:args [(:exact-match-id args)]})
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :expected-deposit/client ?client-id]]}

View File

@@ -195,10 +195,15 @@
:add-sales-date {:txes [[{:db/ident :expected-deposit/sales-date
:db/doc "The date of sales the deposit was for"
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one}]]}})
:db/cardinality :db.cardinality/one}]]}
:add-expected-deposit-status {:txes [[{:db/ident :expected-deposit/status
:db/doc "Whether the deposit has been cleared"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :expected-deposit-status/pending}
{:db/ident :expected-deposit-status/cleared}
{:db/ident :transaction/expected-deposit
:db/doc "If this transaction is a deposit, the deposit that we anticipated"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}]]}})

View File

@@ -158,6 +158,7 @@
:transaction/vendor [:db/id :vendor/name]
:transaction/matched-rule [:db/id :transaction-rule/note]
:transaction/payment [:db/id :payment/date]
:transaction/expected-deposit [:db/id :expected-deposit/date]
:transaction/accounts [:transaction-account/amount
:db/id
:transaction-account/location
@@ -167,9 +168,10 @@
(map #(update % :transaction/date c/from-date))
(map #(update % :transaction/post-date c/from-date))
(map (fn [transaction]
(if (:transaction/payment transaction)
(update-in transaction [:transaction/payment :payment/date] c/from-date)
transaction)))
(cond-> transaction
(:transaction/payment transaction) (update-in [:transaction/payment :payment/date] c/from-date)
(:transaction/expected-deposit transaction) (update-in [:transaction/expected-deposit :expected-deposit/date] c/from-date))
))
(map #(dissoc % :transaction/id))
(group-by :db/id))]

View File

@@ -339,6 +339,7 @@
:client {:type :client}
:accounts {:type '(list :invoices_expense_accounts)}
:payment {:type :payment}
:expected_deposit {:type :expected_deposit}
:vendor {:type :vendor}
:bank_account {:type :bank_account}
:date {:type 'String}
@@ -651,6 +652,7 @@
:expected_deposit_page {:type :expected_deposit_page
:args {:client_id {:type :id}
:exact_match_id {:type :id}
:date_range {:type :date_range}
:total_lte {:type :money}
:total_gte {:type :money}

View File

@@ -155,9 +155,24 @@
nil))
nil))
(defn find-expected-deposit [client-id amount date]
(when date
(-> (d/q
'[:find ?ed
:in $ ?c ?a ?d-start
:where
[?ed :expected-deposit/client ?c]
(not [?ed :expected-deposit/status :expected-deposit-status/cleared])
[?ed :expected-deposit/date ?d]
[(>= ?d ?d-start)]
[?ed :expected-deposit/total ?a2]
[(auto-ap.utils/dollars= ?a2 ?a)]
]
(d/db conn) client-id amount (coerce/to-date (t/plus date (t/days -10))))
first
first)))
(defn transactions->txs [transactions transaction->bank-account apply-rules existing]
(log/info transactions)
(into []
(for [transaction transactions
@@ -190,13 +205,15 @@
(= "POSTED" status)
(or (not (:start-date bank-account))
(t/after? date (:start-date bank-account)))
)]
(t/after? date (:start-date bank-account))))]
(let [existing-check (transaction->existing-payment transaction check-number client-id bank-account-id amount id)
autopay-invoices-matches (when-not existing-check
(match-transaction-to-unfulfilled-autopayments amount client-id ))
unpaid-invoices-matches (when-not existing-check
(match-transaction-to-unpaid-invoices amount client-id ))]
(match-transaction-to-unpaid-invoices amount client-id ))
expected-deposit (when (and (> amount 0.0)
(not existing-check))
(find-expected-deposit client-id amount date))]
(cond->
[#:transaction
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
@@ -225,10 +242,13 @@
;; temporarily removed to automatically match autopaid invoices
#_(and (not existing-check)
(seq autopay-invoices-matches)) #_(add-new-payment autopay-invoices-matches bank-account-id client-id)
expected-deposit (update 0 #(assoc % :transaction/expected-deposit {:db/id expected-deposit
:expected-deposit/status :expected-deposit-status/cleared}))
(and (not (seq autopay-invoices-matches))
(not (seq unpaid-invoices-matches))) (update 0 #(apply-rules % valid-locations))
(not (seq unpaid-invoices-matches))
(not expected-deposit)) (update 0 #(apply-rules % valid-locations))
true (update 0 remove-nils))))))