Adds validation for external ledger import

This commit is contained in:
Bryce Covert
2020-09-29 21:19:07 -07:00
parent b4755840ae
commit a38894a10a
14 changed files with 1371 additions and 31 deletions

View File

@@ -793,6 +793,7 @@
(let [sort-bys (conj (:sort args)
{:sort-key "default" :asc true})
_ (log/info sort-bys)
length (count sort-bys)
comparator (fn [xs ys]
(reduce

View File

@@ -9,7 +9,7 @@
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find ['?sort-default]
(let [query (cond-> {:query {:find []
:in ['$ ]
:where ['[?e :journal-entry/date ?sort-default]]}
:args [db]}
@@ -101,12 +101,11 @@
:args [(:location args)]})
true
(merge-query {:query {:find ['?sort-default '?e] :where ['[?e :journal-entry/date ?sort-default]
'[?e :journal-entry/amount ?ja2]
'[(not= 0.0 ?ja2)]]}}))]
(->> query
(merge-query {:query {:find ['?sort-default '?e] :where ['[?e :journal-entry/date ?sort-default]]}}))]
(->> (doto query println)
(d/query)
(apply-sort-3 args)
(apply-sort-3 (update args :sort conj {:sort-key "default-2" :asc true}))
(apply-pagination args))))
(defn graphql-results [ids db args]

View File

@@ -25,7 +25,6 @@
[journal-entries journal-entries-count] (l/get-graphql (<-graphql args))
journal-entries (mapv
(fn [je]
(update je :journal-entry/line-items
(fn [jels]
(mapv
@@ -260,13 +259,16 @@
(throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found."))))
(when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry))
(throw (Exception. (str "Date must be MM/dd/yyyy"))))
(when-not (dollars= (doto (reduce + 0.0 (map :debit (:line_items entry))))
(when-not (dollars= (reduce + 0.0 (map :debit (:line_items entry)))
(reduce + 0.0 (map :credit (:line_items entry))))
(throw (Exception. (str "Debits '"
(reduce + 0 (map :debit (:line_items entry)))
"' and credits '"
(reduce + 0 (map :credit (:line_items entry)))
"' do not add up."))))
(when (dollars= (reduce + 0.0 (map :debit (:line_items entry)))
0.0)
(throw (Exception. (str "Cannot have ledger entries that total $0.00"))))
(remove-nils
{:journal-entry/source (:source entry)
:journal-entry/client [:client/code (:client_code entry)]
@@ -284,22 +286,44 @@
(get all-client-locations (:client_code entry))
(:location ea))
(throw (Exception. (str "Location '" (:location ea) "' not found."))))
(when (< (or (:debit ea) (:credit ea)) 0.0)
(throw (Exception. (str (or (:debit ea) (:credit ea)) "must be greater than 0."))))
(when (and (<= (:debit ea 0.0) 0.0)
(<= (:credit ea 0.0) 0.0))
(throw (Exception. (str "Line item amount " (or (:debit ea) (:credit ea)) " must be greater than 0."))))
(when (and (not (all-accounts (:account_identifier ea)))
(not (get
(get all-client-bank-accounts (:client_code entry))
(:account_identifier ea))))
(throw (Exception. (str "Account '" (:account_identifier ea) "' not found."))))
(remove-nils {:journal-entry-line/account
(if (re-matches #"^[0-9]+$" (:account_identifier ea))
(:db/id (a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))
[:bank-account/code (:account_identifier ea)])
:journal-entry-line/location (:location ea)
:journal-entry-line/debit (when (> (:debit ea) 0)
(:debit ea))
:journal-entry-line/credit (when (> (:credit ea) 0)
(:credit ea))}))
(let [matching-account (when (re-matches #"^[0-9]+$" (:account_identifier ea))
(a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))]
(when (and matching-account
(:account/location matching-account)
(not= (:account/location matching-account)
(:location ea)))
(throw (Exception. (str "Account '"
(:account/numeric-code matching-account)
"' requires location '"
(:account/location matching-account)
"' but got '"
(:location ea)
"'"
))))
(when (and matching-account
(not (:account/location matching-account))
(= "A" (:location ea)))
(throw (Exception. (str "Account '"
(:account/numeric-code matching-account)
"' cannot use location '"
(:location ea)
"'"))))
(remove-nils (cond-> {:journal-entry-line/location (:location ea)
:journal-entry-line/debit (when (> (:debit ea) 0)
(:debit ea))
:journal-entry-line/credit (when (> (:credit ea) 0)
(:credit ea))}
matching-account (assoc :journal-entry-line/account (:db/id matching-account))
(not matching-account) (assoc :journal-entry-line/account [:bank-account/code (:account_identifier ea)])))))
(:line_items entry))
:journal-entry/cleared true})))))
@@ -323,7 +347,7 @@
([lookup-account all-ledger-entries]
(->> all-ledger-entries
(reduce
(fn [[rollup cache] [_ jel account location debit credit]]
(fn [[rollup cache] [_ _ jel account location debit credit]]
(let [rollup (-> rollup
(update-in [[location account] :debit] (fnil + 0.0) debit)
(update-in [[location account] :credit] (fnil + 0.0) credit)
@@ -349,7 +373,7 @@
(defn running-balance-for [client-id]
(let [lookup-account (build-account-lookup client-id)]
(->> (d/query
{:query {:find ['?d '?jel '?account '?location '?debit '?credit]
{:query {:find ['?d '?e '?jel '?account '?location '?debit '?credit]
:in ['$ '?client-id]
:where '[[?e :journal-entry/client ?client-id]
[?e :journal-entry/date ?d]
@@ -369,7 +393,7 @@
[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(d/db conn) client-id]})
(sort-by first)
(sort-by (juxt first second))
(build-running-balance lookup-account))))
(defn build-running-balance-cache []

View File

@@ -22,8 +22,7 @@
[grid/cell {} (date->str date) ]
[grid/cell {} ]
[grid/cell {:class "has-text-right"} (nf amount )]
[grid/cell {:class "has-text-right"} (nf amount )]
#_[grid/cell {}]]
[grid/cell {:class "has-text-right"} (nf amount )]]
[:<>
(for [{:keys [debit credit location account id running-balance]} line-items
:let [account (or (accounts-by-id (:id account))
@@ -35,7 +34,8 @@
[grid/cell {} ]
[grid/cell {} ]
[grid/cell {} (if (:name account)
(str location ": " (:name account))
[:span.has-tooltip-arrow.has-tooltip-right {:data-tooltip (str "Balance as of this entry: " (nf running-balance ))}
(str location ": " (:name account)) ]
[:i "unknown"])]
[grid/cell {:class "has-text-right"} (when debit (nf debit ))]
[grid/cell {:class "has-text-right"} (when credit (nf credit ))]