Adds transaction->autopay linking
This commit is contained in:
@@ -793,7 +793,6 @@
|
||||
|
||||
(let [sort-bys (conj (:sort args)
|
||||
{:sort-key "default" :asc true})
|
||||
_ (log/info sort-bys)
|
||||
length (count sort-bys)
|
||||
comparator (fn [xs ys]
|
||||
(reduce
|
||||
|
||||
@@ -130,7 +130,6 @@
|
||||
:where ['[?e :payment/date ?sort-default]]}}))]
|
||||
|
||||
|
||||
(log/info "query" query)
|
||||
(cond->> query
|
||||
true (d/query)
|
||||
true (apply-sort-3 args)
|
||||
|
||||
@@ -105,6 +105,11 @@
|
||||
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
|
||||
:args [(:invoice-number-like args)]})
|
||||
|
||||
(:scheduled-payments args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['[?e :invoice/scheduled-payment]]}
|
||||
:args []})
|
||||
|
||||
(:unresolved args)
|
||||
(merge-query {:query {:in []
|
||||
:where ['(or-join [?e]
|
||||
|
||||
@@ -532,6 +532,7 @@
|
||||
:due_range {:type :date_range}
|
||||
:status {:type :invoice_status}
|
||||
:unresolved {:type 'Boolean}
|
||||
:scheduled_payments {:type 'Boolean}
|
||||
:client_id {:type :id}
|
||||
:vendor_id {:type :id}
|
||||
:amount_lte {:type :money}
|
||||
|
||||
@@ -95,24 +95,57 @@
|
||||
{:transaction/payment [{:payment/status [:db/ident]} :db/id]} ]
|
||||
transaction-id)
|
||||
payment (-> transaction :transaction/payment )
|
||||
is-autopay-payment? (some->> (doto (d/query {:query {:find ['?sp]
|
||||
:in ['$ '?payment]
|
||||
:where ['[?ip :invoice-payment/payment ?payment]
|
||||
'[?ip :invoice-payment/invoice ?i]
|
||||
'[(get-else $ ?i :invoice/scheduled-payment "N/A") ?sp]]}
|
||||
:args [(d/db conn) (:db/id payment)]})
|
||||
log/info)
|
||||
seq
|
||||
(map first)
|
||||
(every? #(instance? java.util.Date %)))
|
||||
]
|
||||
|
||||
(log/info "Unlinking " transaction-id " from payment " payment)
|
||||
(log/info (:db/id payment))
|
||||
|
||||
(log/info "Unlinking " transaction-id " from payment " payment " - Deleting the payment due to autopay? " is-autopay-payment?)
|
||||
(when (not= :payment-status/cleared (-> payment :payment/status :db/ident))
|
||||
(throw (ex-info "Payment can't be undone because it isn't cleared." {:validation-error "Payment can't be undone because it isn't cleared."})))
|
||||
(audit-transact
|
||||
(into [{:db/id (:db/id payment)
|
||||
:payment/status :payment-status/pending}
|
||||
{:db/id transaction-id
|
||||
:transaction/approval-status :transaction-approval-status/unapproved}
|
||||
[:db/retract transaction-id :transaction/payment (:db/id payment)]
|
||||
[:db/retract transaction-id :transaction/vendor (:db/id (:transaction/vendor transaction))]
|
||||
[:db/retract transaction-id :transaction/location (:transaction/location transaction)]]
|
||||
(if is-autopay-payment?
|
||||
(audit-transact
|
||||
(->> [{:db/id (:db/id payment)
|
||||
:payment/status :payment-status/pending}
|
||||
{:db/id transaction-id
|
||||
:transaction/approval-status :transaction-approval-status/unapproved}
|
||||
|
||||
(map (fn [a]
|
||||
[:db/retract transaction-id :transaction/accounts (:db/id a)])
|
||||
(:transaction/accounts transaction)))
|
||||
(:id context))
|
||||
[:db/retractEntity (:db/id payment) ]
|
||||
[:db/retract transaction-id :transaction/payment (:db/id payment)]
|
||||
[:db/retract transaction-id :transaction/vendor (:db/id (:transaction/vendor transaction))]
|
||||
[:db/retract transaction-id :transaction/location (:transaction/location transaction)]]
|
||||
(into (map (fn [a]
|
||||
[:db/retract transaction-id :transaction/accounts (:db/id a)])
|
||||
(:transaction/accounts transaction)))
|
||||
(into (map (fn [[invoice-payment]]
|
||||
[:db/retractEntity invoice-payment])
|
||||
(d/query {:query {:find ['?ip]
|
||||
:in ['$]
|
||||
:where ['[?ip :invoice-payment/payment ?p]]}
|
||||
:args [(d/db conn) (:db/id payment)]} ))))
|
||||
(:id context))
|
||||
(audit-transact
|
||||
(into [{:db/id (:db/id payment)
|
||||
:payment/status :payment-status/pending}
|
||||
{:db/id transaction-id
|
||||
:transaction/approval-status :transaction-approval-status/unapproved}
|
||||
[:db/retract transaction-id :transaction/payment (:db/id payment)]
|
||||
[:db/retract transaction-id :transaction/vendor (:db/id (:transaction/vendor transaction))]
|
||||
[:db/retract transaction-id :transaction/location (:transaction/location transaction)]]
|
||||
|
||||
(map (fn [a]
|
||||
[:db/retract transaction-id :transaction/accounts (:db/id a)])
|
||||
(:transaction/accounts transaction)))
|
||||
(:id context)))
|
||||
(-> (d-transactions/get-by-id transaction-id)
|
||||
approval-status->graphql
|
||||
->graphql)))
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
(ns auto-ap.yodlee.import
|
||||
(:require [auto-ap.yodlee.core :as client]
|
||||
[auto-ap.utils :refer [by]]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [uri remove-nils]]
|
||||
(:require [auto-ap.datomic :refer [audit-transact conn remove-nils uri]]
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[clj-time.coerce :as coerce]
|
||||
[digest :refer [sha-256]]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.time :as time]
|
||||
[auto-ap.datomic.transaction-rules :as tr]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
[auto-ap.rule-matching :as rm]
|
||||
[clojure.string :as str]
|
||||
[unilog.context :as lc]
|
||||
[clojure.tools.logging :as log]
|
||||
[auto-ap.time :as time]
|
||||
[auto-ap.utils :refer [by dollars=]]
|
||||
[auto-ap.yodlee.core :as client]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as t]
|
||||
[clojure.string :as str]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.api :as d]
|
||||
[digest :refer [sha-256]]
|
||||
[mount.core :as mount]
|
||||
[unilog.context :as lc]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
|
||||
(defn rough-match [client-id bank-account-id amount]
|
||||
(if (and client-id bank-account-id amount)
|
||||
(let [[matching-checks] (d-checks/get-graphql {:client-id client-id
|
||||
@@ -32,7 +31,7 @@
|
||||
nil))
|
||||
|
||||
|
||||
(defn transaction->payment [_ check-number client-id bank-account-id amount id]
|
||||
(defn transaction->existing-payment [_ check-number client-id bank-account-id amount id]
|
||||
(log/info "Searching for a matching check for "
|
||||
{:client-id client-id
|
||||
:check-number check-number
|
||||
@@ -57,6 +56,61 @@
|
||||
:else
|
||||
(rough-match client-id bank-account-id amount)))
|
||||
|
||||
|
||||
|
||||
(defn match-transaction-to-unfulfilled-autopayments [amount client-id]
|
||||
(log/info "trying to find uncleared autopay invoices")
|
||||
(let [candidate-invoices-vendor-groups (->> (d/query {:query {:find ['?vendor-id '?e '?total '?sd]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/scheduled-payment ?sd]
|
||||
'[?e :invoice/status :invoice-status/paid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/total ?total]]}
|
||||
:args [(d/db conn) client-id]})
|
||||
(sort-by last) ;; sort by scheduled payment date
|
||||
(group-by first) ;; group by vendors
|
||||
vals)
|
||||
considerations (for [candidate-invoices candidate-invoices-vendor-groups
|
||||
invoice-count (range 1 3)
|
||||
consideration (partition invoice-count 1 candidate-invoices)
|
||||
:when (dollars= (reduce (fn [acc [_ _ amount]]
|
||||
(+ acc amount)) 0.0 consideration)
|
||||
(- amount))]
|
||||
consideration)]
|
||||
(log/info "Found " (count considerations) "considerations for transaction of" amount)
|
||||
(if (= 1 (count considerations))
|
||||
(first considerations)
|
||||
[])))
|
||||
|
||||
(defn add-new-payment [[transaction :as tx] [[vendor] :as invoice-payments] bank-account-id client-id]
|
||||
(log/info "Adding a new payment for transaction " (:transaction/id transaction) " and invoices " invoice-payments)
|
||||
(let [payment-id (d/tempid :db.part/user)]
|
||||
(-> tx
|
||||
(conj {:payment/bank-account bank-account-id
|
||||
:payment/client client-id
|
||||
:payment/amount (- (:transaction/amount transaction))
|
||||
:payment/vendor vendor
|
||||
:payment/date (:transaction/date transaction)
|
||||
:payment/type :payment-type/debit
|
||||
:payment/status :payment-status/cleared
|
||||
:db/id payment-id})
|
||||
(into (map (fn [[vendor invoice-id invoice-amount]]
|
||||
{:invoice-payment/invoice invoice-id
|
||||
:invoice-payment/payment payment-id
|
||||
:invoice-payment/amount invoice-amount})
|
||||
invoice-payments))
|
||||
(update 0 assoc
|
||||
:transaction/payment payment-id
|
||||
:transaction/approval-status :transaction-approval-status/approved
|
||||
:transaction/vendor vendor
|
||||
:transaction/location "A"
|
||||
:transaction/accounts [#:transaction-account
|
||||
{:account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
|
||||
:location "A"
|
||||
:amount (Math/abs (:transaction/amount transaction))}]))))
|
||||
|
||||
(defn extract-check-number [{{description-original :original} :description}]
|
||||
|
||||
(if-let [[_ _ check-number] (re-find #"(?i)check(card|[^0-9]+([0-9]*))" description-original)]
|
||||
@@ -67,6 +121,7 @@
|
||||
nil))
|
||||
|
||||
(defn transactions->txs [transactions transaction->bank-account apply-rules existing]
|
||||
|
||||
(into []
|
||||
|
||||
(for [transaction transactions
|
||||
@@ -101,39 +156,40 @@
|
||||
(or (not (:start-date bank-account))
|
||||
(t/after? date (:start-date bank-account)))
|
||||
)]
|
||||
(let [check (transaction->payment transaction check-number client-id bank-account-id amount id)]
|
||||
(->
|
||||
#:transaction
|
||||
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
|
||||
:id (sha-256 (str id))
|
||||
:account-id account-id
|
||||
:date (coerce/to-date date)
|
||||
:amount (double amount)
|
||||
:description-original (some-> description-original (str/replace #"\s+" " "))
|
||||
:description-simple (some-> description-simple (str/replace #"\s+" " "))
|
||||
:approval-status (if check
|
||||
:transaction-approval-status/approved
|
||||
:transaction-approval-status/unapproved)
|
||||
:type type
|
||||
:status status
|
||||
:client client-id
|
||||
:check-number check-number
|
||||
:bank-account bank-account-id
|
||||
:payment (when check
|
||||
{:db/id (:db/id check)
|
||||
:payment/status :payment-status/cleared})
|
||||
(let [existing-check (transaction->existing-payment transaction check-number client-id bank-account-id amount id)
|
||||
invoices-matches (when-not existing-check
|
||||
(match-transaction-to-unfulfilled-autopayments amount client-id ))]
|
||||
(cond->
|
||||
[#:transaction
|
||||
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
|
||||
:id (sha-256 (str id))
|
||||
:account-id account-id
|
||||
:date (coerce/to-date date)
|
||||
:amount (double amount)
|
||||
:description-original (some-> description-original (str/replace #"\s+" " "))
|
||||
:description-simple (some-> description-simple (str/replace #"\s+" " "))
|
||||
:approval-status :transaction-approval-status/unapproved
|
||||
:type type
|
||||
:status status
|
||||
:client client-id
|
||||
:check-number check-number
|
||||
:bank-account bank-account-id}]
|
||||
existing-check (update 0 #(assoc % :transaction/approval-status :transaction-approval-status/approved
|
||||
:transaction/payment {:db/id (:db/id existing-check)
|
||||
:payment/status :payment-status/cleared}
|
||||
:transaction/vendor (:db/id (:payment/vendor existing-check))
|
||||
:transaction/location "A"
|
||||
:transaction/accounts [#:transaction-account
|
||||
{:account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
|
||||
:location "A"
|
||||
:amount (Math/abs (double amount))}]))
|
||||
|
||||
:vendor (when check
|
||||
(:db/id (:payment/vendor check)))
|
||||
:location (when check
|
||||
"A")
|
||||
:accounts (when check
|
||||
[#:transaction-account {:account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
|
||||
:location "A"
|
||||
:amount (Math/abs (double amount))}])}
|
||||
(and (not existing-check)
|
||||
(seq invoices-matches)) (add-new-payment invoices-matches bank-account-id client-id)
|
||||
|
||||
|
||||
(apply-rules valid-locations)
|
||||
remove-nils)))))
|
||||
true (update 0 #(apply-rules % valid-locations))
|
||||
true (update 0 remove-nils))))))
|
||||
|
||||
|
||||
(defn batch-transact [transactions]
|
||||
@@ -203,7 +259,27 @@
|
||||
(let [all-bank-accounts (get-all-bank-accounts)
|
||||
transaction->bank-account (comp (by :bank-account/yodlee-account-id all-bank-accounts) :accountId)
|
||||
all-rules (tr/get-all)]
|
||||
(batch-transact (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))))))))
|
||||
(doseq [tx (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))]
|
||||
(audit-transact tx {:user/name "Yodlee import"
|
||||
:user/role ":admin"})))))))
|
||||
|
||||
(defn do-import2
|
||||
([]
|
||||
(do-import2 (client2/get-transactions "AFH")))
|
||||
([transactions]
|
||||
(lc/with-context {:source "Import yodlee transactions"}
|
||||
|
||||
(do
|
||||
(log/info "importing from yodlee2")
|
||||
(let [all-bank-accounts (get-all-bank-accounts)
|
||||
transaction->bank-account (comp (by (comp :yodlee-account/id :bank-account/yodlee-account) all-bank-accounts) :accountId)
|
||||
all-rules (tr/get-all)]
|
||||
|
||||
(log/info "COUNT" (count (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))))
|
||||
(doseq [tx (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))]
|
||||
(log/info "transacting" tx)
|
||||
(audit-transact tx {:user/name "Yodlee import"
|
||||
:user/role ":admin"})))))))
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
:amount-lte (:amount-lte (:amount-range params))
|
||||
:location (:location params)
|
||||
:unresolved (:unresolved params)
|
||||
:scheduled-payments (:scheduled-payments params)
|
||||
:invoice-number-like (:invoice-number-like params)
|
||||
:client-id (:id @(re-frame/subscribe [::subs/client]))
|
||||
:import-status (:import-status params)
|
||||
|
||||
@@ -76,6 +76,19 @@
|
||||
{:on-change-event [::data-page/filter-changed data-page :due-range]
|
||||
:value @(re-frame/subscribe [::data-page/filter data-page :due-range])}]]
|
||||
|
||||
[:p.menu-label ""]
|
||||
[:div
|
||||
[switch-field {:id "unresolved-only"
|
||||
:checked (boolean @(re-frame/subscribe [::data-page/filter data-page :scheduled-payments]))
|
||||
|
||||
:on-change (fn [e]
|
||||
(re-frame/dispatch [::data-page/filter-changed data-page :scheduled-payments (.-checked (.-target e))])
|
||||
|
||||
|
||||
)
|
||||
:label "Scheduled payments"
|
||||
:type "checkbox"}]]
|
||||
|
||||
[:p.menu-label "Amount"]
|
||||
[:div
|
||||
[number-filter
|
||||
|
||||
Reference in New Issue
Block a user