allows manually matching rules.
This commit is contained in:
@@ -10,7 +10,8 @@
|
||||
[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.transaction-rules :as tr]
|
||||
[auto-ap.rule-matching :as rm]))
|
||||
|
||||
|
||||
|
||||
@@ -125,107 +126,7 @@
|
||||
[]
|
||||
(partition-all 100 transactions)))
|
||||
|
||||
(defn rule-applies? [transaction {:keys [:transaction-rule/description
|
||||
:transaction-rule/dom-gte :transaction-rule/dom-lte
|
||||
:transaction-rule/amount-gte :transaction-rule/amount-lte
|
||||
:transaction-rule/client :transaction-rule/bank-account
|
||||
:transaction-rule/yodlee-merchant]} ]
|
||||
(let [transaction-dom (some-> transaction
|
||||
:transaction/date
|
||||
.toInstant
|
||||
(.atZone (java.time.ZoneId/of "US/Pacific"))
|
||||
(.get java.time.temporal.ChronoField/DAY_OF_MONTH))]
|
||||
(and
|
||||
(if description
|
||||
(re-find description (:transaction/description-original transaction))
|
||||
true)
|
||||
(if dom-gte
|
||||
(>= transaction-dom dom-gte)
|
||||
true)
|
||||
(if dom-lte
|
||||
(<= transaction-dom dom-lte)
|
||||
true)
|
||||
(if amount-gte
|
||||
(>= (:transaction/amount transaction) amount-gte)
|
||||
true)
|
||||
(if amount-lte
|
||||
(<= (:transaction/amount transaction) amount-lte)
|
||||
true)
|
||||
(if client
|
||||
(= (:transaction/client transaction)
|
||||
(:db/id client))
|
||||
true)
|
||||
(if yodlee-merchant
|
||||
(= (:yodlee-merchant/yodlee-id (:transaction/yodlee-merchant transaction))
|
||||
(:yodlee-merchant/yodlee-id yodlee-merchant))
|
||||
true)
|
||||
(if bank-account
|
||||
(= (:transaction/bank-account transaction)
|
||||
(:db/id bank-account))
|
||||
true))))
|
||||
|
||||
(defn rule-priority [rule]
|
||||
(or
|
||||
(->> [[:transaction-rule/bank-account 0]
|
||||
[:transaction-rule/client 1]
|
||||
[:transaction-rule/dom-lte 2]
|
||||
[:transaction-rule/dom-gte 2]
|
||||
[:transaction-rule/amount-lte 3]
|
||||
[:transaction-rule/amount-gte 3]
|
||||
[:transaction-rule/description 4]
|
||||
[:transaction-rule/yodlee-merchant 5]]
|
||||
(filter (fn [[key]]
|
||||
(get rule key)))
|
||||
(map second)
|
||||
first)
|
||||
6))
|
||||
|
||||
(defn get-matching-rules-by-priority [rules-by-priority transaction]
|
||||
(loop [[rule-set & rules] rules-by-priority]
|
||||
(if rule-set
|
||||
(let [matching-rules (into [] (filter #(rule-applies? transaction %) rule-set))]
|
||||
(if (seq matching-rules)
|
||||
matching-rules
|
||||
(recur rules)))
|
||||
[])))
|
||||
|
||||
(defn group-rules-by-priority [rules]
|
||||
(->> rules
|
||||
(map (fn [r] (update r :transaction-rule/description #(some-> % re-pattern))))
|
||||
(group-by rule-priority)
|
||||
(sort-by first)
|
||||
(map second)))
|
||||
|
||||
(defn rule-applying-fn [rules]
|
||||
(let [rules-by-priority (group-rules-by-priority rules)]
|
||||
(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 #{}
|
||||
@@ -267,7 +168,7 @@
|
||||
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 transaction->bank-account (rule-applying-fn all-rules) (get-existing)))))
|
||||
(transactions->txs transformed-transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing)))))
|
||||
|
||||
(defn do-import
|
||||
([]
|
||||
@@ -276,5 +177,5 @@
|
||||
(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 (rule-applying-fn all-rules) (get-existing))))))
|
||||
(batch-transact (transactions->txs transactions transaction->bank-account (rm/rule-applying-fn all-rules) (get-existing))))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user