supports validation and multiple account entering.

This commit is contained in:
Bryce Covert
2019-04-17 18:35:41 -07:00
parent a4eea929e5
commit 4fe52cad5a
10 changed files with 156 additions and 87 deletions

View File

@@ -76,10 +76,26 @@
]
)
(def add-transaction-account
[[{:db/ident :transaction/account
[[{:db/ident :transaction/accounts
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/isComponent true
:db/doc "The debit(s)/credit(s) for this transaction"}
{:db/ident :transaction-account/account
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The debit/credit for this transaction"}]])
:db/doc "Which account to debit/credit for this transaction"}
{:db/ident :transaction-account/location
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Location for this expense account"}
{:db/ident :transaction-account/amount
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one
:db/doc "How much to debit/credit - must be positive"}]])
(def add-yodlee-merchant
[[{:db/ident :yodlee-merchant/name

View File

@@ -75,7 +75,10 @@
(->> (d/pull-many db '[* {:transaction/client [:client/name :db/id :client/code]
:transaction/bank-account [:bank-account/name :bank-account/code :bank-account/yodlee-account-id :db/id]
:transaction/vendor [:db/id :vendor/name]
:transaction/account [:db/id :account/name :account/numeric-code]
:transaction/accounts [:transaction-account/amount
:db/id
:transaction-account/location
{:transaction-account/account [:db/id :account/name :account/numeric-code]}]
:transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]}]
ids)
(map #(update % :transaction/date c/from-date))
@@ -96,7 +99,10 @@
'[* {:transaction/client [:client/name :db/id :client/code :client/locations]
:transaction/bank-account [:bank-account/name :bank-account/code :bank-account/yodlee-account-id :db/id]
:transaction/vendor [:db/id :vendor/name]
:transaction/account [:db/id :account/name :account/numeric-code]
:transaction/accounts [:transaction-account/amount
:db/id
:transaction-account/location
{ :transaction-account/account [:db/id :account/name :account/numeric-code]}]
:transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]}]
id)
(update :transaction/date c/from-date)

View File

@@ -163,7 +163,7 @@
:status {:type 'String}
:yodlee_merchant {:type :yodlee_merchant}
:client {:type :client}
:account {:type :account}
:accounts {:type '(list :invoices_expense_accounts)}
:payment {:type :payment}
:vendor {:type :vendor}
:bank_account {:type :bank_account}
@@ -423,8 +423,7 @@
:edit_transaction
{:fields {:id {:type :id}
:vendor_id {:type :id}
:location {:type 'String}
:account_id {:type :id}}}
:accounts {:type '(list :edit_expense_account)}}}
:edit_account
{:fields {:id {:type :id}

View File

@@ -3,13 +3,15 @@
[auto-ap.datomic.transactions :as d-transactions]
[auto-ap.datomic.vendors :as d-vendors]
[datomic.api :as d]
[auto-ap.datomic :refer [uri]]
[auto-ap.datomic :refer [uri remove-nils]]
[com.walmartlabs.lacinia :refer [execute]]
[com.walmartlabs.lacinia.executor :as executor]
[com.walmartlabs.lacinia.resolve :as resolve]
[auto-ap.utils :refer [by]]
[auto-ap.utils :refer [by dollars=]]
[auto-ap.time :refer [parse normal-date]]
[auto-ap.datomic.clients :as d-clients]))
[auto-ap.datomic.clients :as d-clients]
[clojure.set :as set]
[clojure.string :as str]))
(defn get-transaction-page [context args value]
(let [args (assoc args :id (:id context))
@@ -21,27 +23,50 @@
:start (:start args 0)
:end (+ (:start args 0) (count transactions))}]))
(defn transaction-account->entity [{:keys [id account_id amount location]}]
(doto (remove-nils #:transaction-account {:amount (Double/parseDouble amount)
:db/id id
:account account_id
:location location})
println))
(defn deleted-accounts [transaction accounts]
(let [current-accounts (:transaction/accounts transaction)
specified-ids (->> accounts
(map :id)
set)
existing-ids (->> current-accounts
(map :db/id)
set)]
(set/difference existing-ids specified-ids)))
(defn edit-transaction [context {{:keys [id location account_id vendor_id] :as transaction} :transaction} value]
(let [transaction (d-transactions/get-by-id id)]
(defn edit-transaction [context {{:keys [id accounts vendor_id] :as transaction} :transaction} value]
(let [transaction (d-transactions/get-by-id id)
deleted (deleted-accounts transaction accounts)
account-total (reduce + 0 (map (fn [x] (Double/parseDouble (:amount x))) accounts))
missing-locations (seq (set/difference
(->> (:transaction/accounts transaction)
(map :transaction-account/location)
set)
(-> (:transaction/client transaction)
:client/locations
set
(conj "A")
(conj "HQ"))))]
(assert-can-see-client (:id context) (:transaction/client transaction) )
(when-not (-> (:transaction/client transaction)
:client/locations
set
(conj "A")
(conj "HQ")
(get location))
(throw (ex-info (str "Location '" location "' not found on client.") {}))
)
(when-not (dollars= (Math/abs (:transaction/amount transaction)) account-total)
(let [error (str "Expense account total (" account-total ") does not equal transaction total (" (Math/abs (:transaction/amount transaction)) ")")]
(throw (ex-info error {:validation-error error}))))
(when missing-locations
(throw (ex-info (str "Location '" (str/join ", " missing-locations) "' not found on client.") {})) )
@(d/transact (d/connect uri)
[{:db/id id
:transaction/vendor vendor_id
:transaction/location location
:transaction/account account_id}])
(->graphql (d-transactions/get-by-id id)))
#_(->graphql {:id id
:vendor (d-vendors/get-by-id vendor_id) }))
(concat [(remove-nils {:db/id id
:transaction/vendor vendor_id
:transaction/accounts (map transaction-account->entity accounts)
})]
(map (fn [d]
[:db/retract id :transaction/accounts d])
deleted)))
(->graphql (d-transactions/get-by-id id))))

View File

@@ -5,7 +5,7 @@
(defn datums->impacted-entity [db [e changes]]
(let [entity (d/pull db '[* {:invoice/_expense-accounts [*]}] e)
(let [entity (d/pull db '[* {:invoice/_expense-accounts [:db/id] :transaction/_accounts [:db/id]}] e)
namespaces (->> changes
(map :a)
(map namespace)
@@ -13,6 +13,7 @@
(cond (namespaces "invoice" ) [[:invoice e]]
(namespaces "invoice-expense-account" ) [[:invoice (:db/id (:invoice/_expense-accounts entity))]]
(namespaces "transaction-account" ) [[:transaction (:db/id (:transaction/_accounts entity))]]
(namespaces "transaction" ) [[:transaction e]]
:else nil)))
@@ -26,6 +27,7 @@
(cond (namespaces "invoice" ) :invoice
(namespaces "invoice-expense-account" ) :invoice-expense-account
(namespaces "transaction-account" ) :transaction-account
:else nil)))
(defmulti entity-change->ledger (fn [_ [type]]
@@ -57,7 +59,8 @@
(defmethod entity-change->ledger :transaction
[db [type id]]
(let [entity (d/pull db ['* {:transaction/vendor '[*] :transaction/client '[*] :transaction/account '[*]}] id)]
(let [entity (d/pull db ['* {:transaction/vendor '[*] :transaction/client '[*] :transaction/accounts '[* {:transaction-account/account [*]}] }] id)]
(println "processing entity" entity)
(when (:transaction/vendor entity)
(remove-nils
{:journal-entry/source "transaction"
@@ -67,20 +70,23 @@
:journal-entry/vendor (:db/id (:transaction/vendor entity))
:journal-entry/amount (Math/abs (:transaction/amount entity))
:journal-entry/line-items [(remove-nils{:journal-entry-line/account (:db/id (:transaction/account entity))
:journal-entry-line/location (:transaction/location entity)
:journal-entry-line/debit (when (< (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))
:journal-entry-line/credit (when (>= (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))})
(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
:journal-entry-line/location "A"
:journal-entry-line/credit (when (< (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))
:journal-entry-line/debit (when (>= (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))})
]
:journal-entry/line-items (into [
(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
:journal-entry-line/location "A"
:journal-entry-line/credit (when (< (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))
:journal-entry-line/debit (when (>= (:transaction/amount entity) 0.0)
(Math/abs (:transaction/amount entity)))})
]
(map
(fn [a]
(remove-nils{:journal-entry-line/account (:db/id (:transaction-account/account a))
:journal-entry-line/location (:transaction-account/location a)
:journal-entry-line/debit (when (< (:transaction/amount entity) 0.0)
(Math/abs (:transaction-account/amount a)))
:journal-entry-line/credit (when (>= (:transaction/amount entity) 0.0)
(Math/abs (:transaction-account/amount a)))}))
(:transaction/accounts entity)))
:journal-entry/cleared true}))))