adding transaction rules.

This commit is contained in:
Bryce Covert
2019-05-07 07:10:21 -07:00
parent 918f2e5be1
commit a1abb4b05e
15 changed files with 266 additions and 15 deletions

View File

@@ -131,9 +131,9 @@
:auto-ap/add-exclude-to-transaction {:txes add-general-ledger/add-exclude-to-transaction :requires [:auto-ap/add-external-id-to-ledger]}
:auto-ap/convert-transactions {:txes-fn `add-general-ledger/convert-transactions :requires [:auto-ap/add-external-id-to-ledger]}
:auto-ap/add-transaction-rules {:txes add-general-ledger/add-transaction-rules :requires [:auto-ap/convert-transactions]}
#_#_:auto-ap/bulk-load-invoice-ledger3 {:txes-fn `add-general-ledger/bulk-load-invoice-ledger :requires [:auto-ap/convert-transactions]}
#_#_:auto-ap/bulk-load-transaction-ledger3 {:txes-fn `add-general-ledger/bulk-load-transaction-ledger :requires [:auto-ap/convert-transactions]}
}]
(println "Conforming database...")

View File

@@ -305,6 +305,44 @@
:db/cardinality :db.cardinality/one
:db/doc "Whether to exclude from the ledger"}]])
(def add-transaction-rules
[[{:db/ident :transaction-rule/client
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The specific client this rule is for"}
{:db/ident :transaction-rule/bank-account
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The specific bank account this rule is for"}
{:db/ident :transaction-rule/yodlee-merchant
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Apply this rule if the yodlee merchant matches"}
{:db/ident :transaction-rule/description
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A description to match this rule against"}
{:db/ident :transaction-rule/note
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A friendly description for this rule (internal)"}
{:db/ident :transaction-rule/amount-lte
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one
:db/doc "Amount has to be less than or equal to this"}
{:db/ident :transaction-rule/amount-gte
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one
:db/doc "Amount has to be greater than or equal to this"}
]])
(def add-credit-bank-account
[[{:db/ident :bank-account-type/credit}]])

View File

@@ -0,0 +1,71 @@
(ns auto-ap.datomic.transaction-rules
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination add-sorter-field]]
[auto-ap.graphql.utils :refer [limited-clients]]
[clojure.set :refer [rename-keys]]
[clj-time.coerce :as c]))
(defn <-datomic [result]
result)
(def default-read '[*
{:transaction-rule/client [:client/name :db/id :client/code]}
{:transaction-rule/bank-account [*]}
{:transaction-rule/yodlee-merchant [*]}])
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find []
:in ['$]
:where []}
:args [db]}
(:sort-by args) (add-sorter-field {"client" ['[?e :transaction-rule/client ?c]
'[?c :client/name ?sorter]]
"yodlee-merchant" ['[?e :transaction-rule/yodlee-merchant ?c]
'[?c :yodlee-merchant/name ?sorter]]
"bank-account" ['[?e :transaction-rule/bank-account ?c]
'[?c :bank-account/name ?sorter]]
"amount_lte" ['[?e :transaction-rule/amount-lte ?sorter]]
"amount_gte" ['[?e :transaction-rule/amount-gte ?sorter]]
}
args)
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :transaction-rule/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :transaction-rule/client ?client-id]]}
:args [(:client-id args)]})
true
(merge-query {:query {:find ['?e]
:where ['[?e :transaction-rule/description]]}}))]
(cond->> query
true (d/query)
true (apply-sort-2 args [:asc])
true (apply-pagination args))))
(defn graphql-results [ids db args]
(let [results (->> (d/pull-many db default-read ids)
(group-by :db/id))
transaction-rules (->> ids
(map results)
(map first)
(mapv <-datomic))]
transaction-rules))
(defn get-graphql [args]
(let [db (d/db (d/connect uri))
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))
(defn get-by-id [id]
(->>
(d/pull (d/db (d/connect uri)) default-read id)
(<-datomic)))

View File

@@ -24,6 +24,7 @@
[auto-ap.graphql.checks :as gq-checks]
[auto-ap.graphql.invoices :as gq-invoices]
[auto-ap.graphql.transactions :as gq-transactions]
[auto-ap.graphql.transaction-rules :as gq-transaction-rules]
[auto-ap.time :as time]
[clojure.walk :as walk]
[clojure.string :as str])
@@ -173,6 +174,17 @@
:bank_account {:type :bank_account}
:date {:type 'String}
:post_date {:type 'String}}}
:transaction_rule {:fields {:id {:type :id}
:note {:type 'String}
:client {:type :client}
:bank_account {:type :bank_account}
:yodlee_merchant {:type :yodlee_merchant}
:description {:type 'String}
:amount_lte {:type 'String}
:amount_gte {:type 'String}
:vendor {:type :vendor}}}
:invoice_payment
{:fields {:id {:type :id}
:amount {:type 'String}
@@ -237,6 +249,12 @@
:start {:type 'Int}
:end {:type 'Int}}}
:transaction_rule_page {:fields {:transaction_rules {:type '(list :transaction_rule)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}
:ledger_page {:fields {:journal_entries {:type '(list :journal_entry)}
:count {:type 'Int}
:total {:type 'Int}
@@ -334,6 +352,13 @@
:resolve :get-transaction-page}
:transaction_rule_page {:type :transaction_rule_page
:args {:client_id {:type :id}
:start {:type 'Int}
:sort_by {:type 'String}
:asc {:type 'Boolean}}
:resolve :get-transaction-rule-page}
:ledger_page {:type :ledger_page
:args {:client_id {:type :id}
:bank_account_id {:type :id}
@@ -699,6 +724,7 @@
:get-ledger-page gq-ledger/get-ledger-page
: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-expense-account-stats get-expense-account-stats
:get-invoice-stats get-invoice-stats

View File

@@ -5,7 +5,7 @@
[auto-ap.datomic.accounts :as a]
[auto-ap.utils :refer [by dollars=]]
[auto-ap.time :refer [parse iso-date]]
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin]]
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page]]
[clj-time.coerce :as coerce]
[clojure.string :as str]
[clj-time.core :as time]
@@ -16,13 +16,8 @@
(defn get-ledger-page [context args value]
(let [args (assoc args :id (:id context))
[journal-entries journal-entries-count] (l/get-graphql (<-graphql args))
journal-entries (map ->graphql journal-entries)]
{:journal_entries journal-entries
:total journal-entries-count
:count (count journal-entries)
:start (:start args 0)
:end (+ (:start args 0) (count journal-entries))}))
[journal-entries journal-entries-count] (l/get-graphql (<-graphql args))]
(result->page journal-entries journal-entries-count :journal_entries args)))
;; TODO a better way to do this might be to accumulate ALL credits and ALL debits, and then just do for credits: balance = credits - debits. and for debits balance = debits - credits
(defn credit-account? [account]

View File

@@ -0,0 +1,8 @@
(ns auto-ap.graphql.transaction-rules
(:require [auto-ap.datomic.transaction-rules :as tr]
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page]]))
(defn get-transaction-rule-page [context args value]
(let [args (assoc args :id (:id context))
[journal-entries journal-entries-count] (tr/get-graphql (<-graphql args))]
(result->page journal-entries journal-entries-count :transaction_rules args)))

View File

@@ -70,3 +70,10 @@
(= (:user/role id) "user")
(:user/clients id [])))
(defn result->page [results result-count key args]
{key (map ->graphql results)
:total result-count
:count (count results)
:start (:start args 0)
:end (+ (:start args 0) (count results))})