Supports ledger ignore
This commit is contained in:
@@ -420,6 +420,7 @@
|
||||
|
||||
:import_ledger_result {:fields {:successful {:type '(list :import_ledger_entry_result)}
|
||||
:existing {:type '(list :import_ledger_entry_result)}
|
||||
:ignored {:type '(list :import_ledger_entry_result)}
|
||||
:errors {:type '(list :import_ledger_entry_result)}
|
||||
}}
|
||||
|
||||
|
||||
@@ -201,7 +201,9 @@
|
||||
(try
|
||||
(f entry)
|
||||
(catch Exception e
|
||||
(assoc entry :error (.getMessage e))))))
|
||||
(assoc entry :error (.getMessage e)
|
||||
:status (or (:status (ex-data e))
|
||||
:error))))))
|
||||
|
||||
(defn delete-external-ledger [context args value]
|
||||
(let [_ (assert-admin (:id context))
|
||||
@@ -274,91 +276,104 @@
|
||||
lis))))]
|
||||
(let [vendor (all-vendors (:vendor_name entry))]
|
||||
(when-not (all-clients (:client_code entry))
|
||||
(throw (Exception. (str "Client '" (:client_code entry )"' not found.")) ))
|
||||
(throw (ex-info (str "Client '" (:client_code entry )"' not found.") {:status :error}) ))
|
||||
(when-not vendor
|
||||
(throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found."))))
|
||||
(throw (ex-info (str "Vendor '" (:vendor_name entry) "' not found.") {:status :error})))
|
||||
(when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry))
|
||||
(throw (Exception. (str "Date must be MM/dd/yyyy"))))
|
||||
(throw (ex-info (str "Date must be MM/dd/yyyy") {:status :error})))
|
||||
(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."))))
|
||||
(throw (ex-info (str "Debits '"
|
||||
(reduce + 0 (map :debit (:line_items entry)))
|
||||
"' and credits '"
|
||||
(reduce + 0 (map :credit (:line_items entry)))
|
||||
"' do not add up.")
|
||||
{:status :error})))
|
||||
(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)]
|
||||
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
|
||||
:journal-entry/external-id (:external_id entry)
|
||||
:journal-entry/vendor (:db/id (doto (all-vendors (:vendor_name entry))
|
||||
println))
|
||||
:journal-entry/amount (:amount entry)
|
||||
:journal-entry/note (:note entry)
|
||||
:journal-entry/cleared-against (:cleared_against entry)
|
||||
(throw (ex-info (str "Cannot have ledger entries that total $0.00")
|
||||
{:status :ignored})))
|
||||
(assoc entry
|
||||
:status :success
|
||||
:tx
|
||||
(remove-nils
|
||||
{:journal-entry/source (:source entry)
|
||||
:journal-entry/client [:client/code (:client_code entry)]
|
||||
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
|
||||
:journal-entry/external-id (:external_id entry)
|
||||
:journal-entry/vendor (:db/id (all-vendors (:vendor_name entry)))
|
||||
:journal-entry/amount (:amount entry)
|
||||
:journal-entry/note (:note entry)
|
||||
:journal-entry/cleared-against (:cleared_against entry)
|
||||
|
||||
:journal-entry/line-items
|
||||
(mapv (fn [ea]
|
||||
(when-not (get
|
||||
(get all-client-locations (:client_code entry))
|
||||
(:location ea))
|
||||
(throw (Exception. (str "Location '" (:location ea) "' not found."))))
|
||||
(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."))))
|
||||
(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)
|
||||
"'"
|
||||
))))
|
||||
:journal-entry/line-items
|
||||
(mapv (fn [ea]
|
||||
(when-not (get
|
||||
(get all-client-locations (:client_code entry))
|
||||
(:location ea))
|
||||
(throw (ex-info (str "Location '" (:location ea) "' not found.")
|
||||
{:status :error})))
|
||||
(when (and (<= (:debit ea 0.0) 0.0)
|
||||
(<= (:credit ea 0.0) 0.0))
|
||||
(throw (ex-info (str "Line item amount " (or (:debit ea) (:credit ea)) " must be greater than 0.")
|
||||
{:status :error})))
|
||||
(when (and (not (all-accounts (:account_identifier ea)))
|
||||
(not (get
|
||||
(get all-client-bank-accounts (:client_code entry))
|
||||
(:account_identifier ea))))
|
||||
(throw (ex-info (str "Account '" (:account_identifier ea) "' not found.")
|
||||
{:status :error})))
|
||||
(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 (ex-info (str "Account '"
|
||||
(:account/numeric-code matching-account)
|
||||
"' requires location '"
|
||||
(:account/location matching-account)
|
||||
"' but got '"
|
||||
(:location ea)
|
||||
"'")
|
||||
{:status :error})))
|
||||
|
||||
(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})))))
|
||||
(when (and matching-account
|
||||
(not (:account/location matching-account))
|
||||
(= "A" (:location ea)))
|
||||
(throw (ex-info (str "Account '"
|
||||
(:account/numeric-code matching-account)
|
||||
"' cannot use location '"
|
||||
(:location ea)
|
||||
"'")
|
||||
{:status :error})))
|
||||
(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}))))))
|
||||
(:entries args)))
|
||||
errors (filter :error transaction)
|
||||
success (filter (comp not :error) transaction)
|
||||
retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:journal-entry/external-id x)]])
|
||||
errors (filter #(= (:status %) :error) transaction)
|
||||
ignored (filter #(= (:status %) :ignored) transaction)
|
||||
success (filter #(= (:status %) :success) transaction)
|
||||
retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:external_id x)]])
|
||||
success)]
|
||||
(log/info "manual ledger import has " (count success) " new rows")
|
||||
|
||||
|
||||
(audit-transact-batch retraction (:id context))
|
||||
(audit-transact-batch success (:id context))
|
||||
(log/info (map :tx success))
|
||||
(audit-transact-batch (map :tx success) (:id context))
|
||||
|
||||
{:successful (map (fn [x] {:external_id (:journal-entry/external-id x)}) success)
|
||||
{:successful (map (fn [x] {:external_id (:external_id x)}) success)
|
||||
:ignored (map (fn [x]
|
||||
{:external_id (:external_id x)})
|
||||
ignored)
|
||||
:existing []
|
||||
:errors (map (fn [x] {:external_id (:external_id x)
|
||||
:error (:error x)}) errors)})))
|
||||
|
||||
@@ -82,9 +82,10 @@
|
||||
(let [err (str "Account " name " uses location " (:location a) ", but is supposed to be " location)]
|
||||
(throw (ex-info err {:validation-error err}) )))
|
||||
|
||||
(when (not (get (into #{"Shared"} (:client/locations client))
|
||||
(:location a)))
|
||||
(let [err (str "Account " name " uses location " (:location a) ", but doesn't belong to the client " location)]
|
||||
(when (and (not location)
|
||||
(not (get (into #{"Shared"} (:client/locations client))
|
||||
(:location a))))
|
||||
(let [err (str "Account " name " uses location " (:location a) ", but doesn't belong to the client.")]
|
||||
(throw (ex-info err {:validation-error err}) ))))
|
||||
rule-id (if id
|
||||
id
|
||||
|
||||
Reference in New Issue
Block a user