allows manually matching rules.
This commit is contained in:
@@ -330,6 +330,10 @@
|
||||
:potential_payment_matches {:type '(list :payment)
|
||||
:args {:transaction_id {:type :id}}
|
||||
:resolve :get-potential-payments}
|
||||
|
||||
:potential_transaction_rule_matches {:type '(list :transaction_rule)
|
||||
:args {:transaction_id {:type :id}}
|
||||
:resolve :get-transaction-rule-matches}
|
||||
:balance_sheet {:type :balance_sheet
|
||||
:args {:client_id {:type :id}
|
||||
:date {:type :iso_date}}
|
||||
@@ -635,6 +639,11 @@
|
||||
:args {:transaction_id {:type :id}
|
||||
:payment_id {:type :id}}
|
||||
:resolve :mutation/match-transaction}
|
||||
|
||||
:match_transaction_rule {:type :transaction
|
||||
:args {:transaction_id {:type :id}
|
||||
:transaction_rule_id {:type :id}}
|
||||
:resolve :mutation/match-transaction-rule}
|
||||
:void_invoice {:type :invoice
|
||||
:args {:invoice_id {:type :id}}
|
||||
:resolve :mutation/void-invoice}
|
||||
@@ -787,10 +796,10 @@
|
||||
:get-balance-sheet gq-ledger/get-balance-sheet
|
||||
:get-profit-and-loss gq-ledger/get-profit-and-loss
|
||||
:get-transaction-rule-page gq-transaction-rules/get-transaction-rule-page
|
||||
:get-transaction-rule-matches gq-transaction-rules/get-transaction-rule-matches
|
||||
:get-expense-account-stats get-expense-account-stats
|
||||
:get-invoice-stats get-invoice-stats
|
||||
:get-yodlee-merchants ym/get-yodlee-merchants
|
||||
|
||||
:get-client gq-clients/get-client
|
||||
:get-user get-user
|
||||
:mutation/add-handwritten-check gq-checks/add-handwritten-check
|
||||
@@ -805,6 +814,7 @@
|
||||
:mutation/upsert-transaction-rule gq-transaction-rules/upsert-transaction-rule
|
||||
:test-transaction-rule gq-transaction-rules/test-transaction-rule
|
||||
:mutation/match-transaction gq-transactions/match-transaction
|
||||
:mutation/match-transaction-rule gq-transactions/match-transaction-rule
|
||||
:mutation/edit-client gq-clients/edit-client
|
||||
:mutation/upsert-vendor gq-vendors/upsert-vendor
|
||||
:mutation/upsert-account gq-accounts/upsert-account
|
||||
|
||||
@@ -7,7 +7,10 @@
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page snake->kebab]]
|
||||
[clj-time.coerce :as c]
|
||||
[clojure.set :as set]
|
||||
[clojure.string :as str])
|
||||
[clojure.string :as str]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
[auto-ap.rule-matching :as rm]
|
||||
[clj-time.coerce :as coerce])
|
||||
(:import [java.time.temporal ChronoField]))
|
||||
|
||||
(defn ident->enum-f [k]
|
||||
@@ -21,6 +24,13 @@
|
||||
(map (ident->enum-f :transaction-rule/transaction-approval-status)))
|
||||
journal-entries-count :transaction_rules args)))
|
||||
|
||||
(defn get-transaction-rule-matches [context args value]
|
||||
(if (= "admin" (:user/role (:id context)))
|
||||
(let [all-rules (tr/get-all)
|
||||
transaction (update (d-transactions/get-by-id (:transaction_id args)) :transaction/date coerce/to-date)]
|
||||
(map ->graphql (rm/get-matching-rules transaction all-rules)))
|
||||
nil))
|
||||
|
||||
(defn deleted-accounts [transaction accounts]
|
||||
(let [current-accounts (:transaction-rule/accounts transaction)
|
||||
specified-ids (->> accounts
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(ns auto-ap.graphql.transactions
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client]]
|
||||
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-client assert-admin]]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
@@ -13,7 +13,10 @@
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[clojure.set :as set]
|
||||
[clojure.string :as str]
|
||||
[auto-ap.datomic.accounts :as a]))
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[auto-ap.datomic.transaction-rules :as tr]
|
||||
[auto-ap.rule-matching :as rm]
|
||||
[clj-time.coerce :as coerce]))
|
||||
|
||||
(defn get-transaction-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
@@ -86,7 +89,7 @@
|
||||
(when-not (dollars= (- (:transaction/amount transaction))
|
||||
(:payment/amount payment))
|
||||
(throw (ex-info "Amounts don't match" {:validation-error "Amounts don't match"})))
|
||||
(d/transact (d/connect uri)
|
||||
@(d/transact (d/connect uri)
|
||||
(into
|
||||
[{:db/id (:db/id payment)
|
||||
:payment/status :payment-status/cleared}
|
||||
@@ -101,3 +104,22 @@
|
||||
(map (fn [x] [:db/retractEntity (:db/id x)] )
|
||||
(:transaction/accounts transaction)))))
|
||||
(->graphql (d-transactions/get-by-id transaction_id)))
|
||||
|
||||
(defn match-transaction-rule [context {:keys [transaction_id transaction_rule_id]} value]
|
||||
(let [_ (assert-admin (:id context))
|
||||
transaction (update (d-transactions/get-by-id transaction_id) :transaction/date coerce/to-date)
|
||||
transaction-rule (update (tr/get-by-id transaction_rule_id) :transaction-rule/description #(some-> % re-pattern))]
|
||||
(when (not (rm/rule-applies? transaction transaction-rule))
|
||||
(throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"})))
|
||||
|
||||
(when (:transaction/payment transaction)
|
||||
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"})))
|
||||
|
||||
@(d/transact (d/connect uri)
|
||||
[(rm/apply-rule {:db/id (:db/id transaction)
|
||||
:transaction/amount (:transaction/amount transaction)}
|
||||
transaction-rule
|
||||
|
||||
;; TODO use bank account locations as well
|
||||
(-> transaction :transaction/client :client/locations))]))
|
||||
(->graphql (d-transactions/get-by-id transaction_id)))
|
||||
|
||||
@@ -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