much cleaner matching solution... won't rematch
This commit is contained in:
@@ -51,7 +51,7 @@
|
||||
nil))
|
||||
nil))
|
||||
|
||||
(defn transactions->txs [transactions transaction->client transaction->bank-account-id apply-rules existing]
|
||||
(defn transactions->txs [transactions transaction->bank-account apply-rules existing]
|
||||
(into []
|
||||
|
||||
(for [transaction transactions
|
||||
@@ -72,8 +72,11 @@
|
||||
(- amount)
|
||||
amount)
|
||||
check-number (extract-check-number transaction)
|
||||
client-id (transaction->client transaction)
|
||||
bank-account-id (transaction->bank-account-id transaction)
|
||||
bank-account (transaction->bank-account transaction)
|
||||
bank-account-id (:db/id bank-account)
|
||||
client (:client/_bank-accounts bank-account)
|
||||
client-id (:db/id client)
|
||||
valid-locations (:client/locations client)
|
||||
check (transaction->payment transaction check-number client-id bank-account-id amount id)]
|
||||
:when (and client-id
|
||||
(not (existing (sha-256 (str id)))))]
|
||||
@@ -94,11 +97,10 @@
|
||||
:status status
|
||||
:client client-id
|
||||
:check-number check-number
|
||||
:bank-account (transaction->bank-account-id transaction)
|
||||
:bank-account bank-account-id
|
||||
:payment (when check
|
||||
{:db/id (:db/id check)
|
||||
:payment/status :payment-status/cleared}
|
||||
)
|
||||
:payment/status :payment-status/cleared})
|
||||
|
||||
:vendor (when check
|
||||
(:db/id (:payment/vendor check)))
|
||||
@@ -109,7 +111,7 @@
|
||||
:location "A"
|
||||
:amount (Math/abs (double amount))}])}
|
||||
|
||||
apply-rules
|
||||
(apply-rules valid-locations)
|
||||
remove-nils))))
|
||||
|
||||
|
||||
@@ -162,19 +164,6 @@
|
||||
(:db/id bank-account))
|
||||
true))))
|
||||
|
||||
(defn more-specific? [left right key]
|
||||
(cond (and (get left key)
|
||||
(get right key))
|
||||
nil
|
||||
(get left key)
|
||||
true
|
||||
|
||||
(get right key)
|
||||
false
|
||||
|
||||
:else
|
||||
nil))
|
||||
|
||||
(defn rule-priority [rule]
|
||||
(or
|
||||
(->> [[:transaction-rule/bank-account 0]
|
||||
@@ -209,21 +198,34 @@
|
||||
|
||||
(defn rule-applying-fn [rules]
|
||||
(let [rules-by-priority (group-rules-by-priority rules)]
|
||||
(fn [transaction]
|
||||
(let [matching-rules (get-matching-rules-by-priority rules-by-priority transaction )]
|
||||
(if-let [top-match (and (= (count matching-rules) 1) (first matching-rules))]
|
||||
(assoc transaction
|
||||
:transaction/matched-rule (:db/id top-match)
|
||||
:transaction/approval-status (:transaction-rule/transaction-approval-status top-match)
|
||||
:transaction/accounts (map
|
||||
(fn [tra]
|
||||
{:transaction-account/account (:db/id (:transaction-rule-account/account tra))
|
||||
:transaction-account/amount (Math/abs (* (:transaction-rule-account/percentage tra)
|
||||
(:transaction/amount transaction)))
|
||||
:transaction-account/location (:transaction-rule-account/location tra)})
|
||||
(:transaction-rule/accounts top-match))
|
||||
:transaction/vendor (:db/id (:transaction-rule/vendor top-match)))
|
||||
transaction)))))
|
||||
(fn [transaction valid-locations]
|
||||
(if (:transaction/payment transaction)
|
||||
transaction
|
||||
(let [matching-rules (get-matching-rules-by-priority rules-by-priority transaction )]
|
||||
(if-let [top-match (and (= (count matching-rules) 1) (first matching-rules))]
|
||||
(assoc transaction
|
||||
:transaction/matched-rule (:db/id top-match)
|
||||
:transaction/approval-status (:transaction-rule/transaction-approval-status top-match)
|
||||
:transaction/accounts (mapcat
|
||||
(fn [tra]
|
||||
(if (= "Shared" (:transaction-rule-account/location tra))
|
||||
(map
|
||||
(fn [location]
|
||||
{:transaction-account/account (:db/id (:transaction-rule-account/account tra))
|
||||
:transaction-account/amount (Math/abs (* (/ 1.0 (count valid-locations))
|
||||
(:transaction-rule-account/percentage tra)
|
||||
(:transaction/amount transaction)))
|
||||
:transaction-account/location location})
|
||||
|
||||
|
||||
valid-locations)
|
||||
[{:transaction-account/account (:db/id (:transaction-rule-account/account tra))
|
||||
:transaction-account/amount (Math/abs (* (:transaction-rule-account/percentage tra)
|
||||
(:transaction/amount transaction)))
|
||||
:transaction-account/location (:transaction-rule-account/location tra)}]))
|
||||
(:transaction-rule/accounts top-match))
|
||||
:transaction/vendor (:db/id (:transaction-rule/vendor top-match)))
|
||||
transaction))))))
|
||||
|
||||
(defn get-existing []
|
||||
(transduce (map first) conj #{}
|
||||
@@ -232,6 +234,17 @@
|
||||
:where ['[_ :transaction/id ?tid]]}
|
||||
:args [(d/db (d/connect uri))]})))
|
||||
|
||||
(defn get-all-bank-accounts []
|
||||
(->> (d-clients/get-all)
|
||||
(mapcat (fn [client]
|
||||
(->> client
|
||||
:client/bank-accounts
|
||||
(filter :bank-account/yodlee-account-id)
|
||||
(filter :db/id)
|
||||
(map (fn [{:keys [:db/id :bank-account/yodlee-account-id] :as bank-account}]
|
||||
(assoc bank-account
|
||||
:client/_bank-accounts client))))))))
|
||||
|
||||
(defn manual-import [manual-transactions]
|
||||
(let [transformed-transactions (->> manual-transactions
|
||||
(filter #(= "posted" (:status %)))
|
||||
@@ -241,7 +254,6 @@
|
||||
(map
|
||||
(fn [index {:keys [date description-original high-level-category amount bank-account-id client-id] :as transaction}]
|
||||
{:id (str date "-" bank-account-id "-" description-original "-" amount "-" index "-" client-id)
|
||||
:client-id client-id
|
||||
:bank-account-id bank-account-id
|
||||
:date (time/unparse date "YYYY-MM-dd")
|
||||
:amount {:amount amount}
|
||||
@@ -250,27 +262,19 @@
|
||||
:status "POSTED"})
|
||||
(range)
|
||||
transaction-group))))
|
||||
all-rules (tr/get-all)]
|
||||
all-rules (tr/get-all)
|
||||
all-bank-accounts (get-all-bank-accounts)
|
||||
transaction->bank-account (comp (by :db/id all-bank-accounts) :bank-account-id)]
|
||||
(println "importing manual transactions" transformed-transactions)
|
||||
(batch-transact
|
||||
(transactions->txs transformed-transactions :client-id :bank-account-id (rule-applying-fn all-rules) (get-existing)))))
|
||||
|
||||
|
||||
(transactions->txs transformed-transactions transaction->bank-account (rule-applying-fn all-rules) (get-existing)))))
|
||||
|
||||
(defn do-import
|
||||
([]
|
||||
(do-import (client/get-transactions)))
|
||||
([transactions]
|
||||
(let [all-bank-accounts (mapcat (fn [c] (map
|
||||
(fn [{:keys [:db/id :bank-account/yodlee-account-id]}]
|
||||
(when (and id yodlee-account-id)
|
||||
{:bank-account-id id
|
||||
:client-id (:db/id c)
|
||||
:yodlee-account-id yodlee-account-id}))
|
||||
(:client/bank-accounts c)))
|
||||
(d-clients/get-all))
|
||||
transaction->client (comp (by :yodlee-account-id :client-id all-bank-accounts) :accountId)
|
||||
transaction->bank-account-id (comp (by :yodlee-account-id :bank-account-id all-bank-accounts) :accountId)
|
||||
(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->client transaction->bank-account-id (rule-applying-fn all-rules) (get-existing))))))
|
||||
(batch-transact (transactions->txs transactions transaction->bank-account (rule-applying-fn all-rules) (get-existing))))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user