revamped logging!
This commit is contained in:
@@ -19,7 +19,10 @@
|
||||
wrap-routes]]
|
||||
[clojure.string :as str]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.data.csv :as csv]))
|
||||
[clojure.data.csv :as csv]
|
||||
[unilog.context :as lc]
|
||||
[clojure.tools.logging :as log]
|
||||
[auto-ap.logging :refer [info-event]]))
|
||||
|
||||
(defn reset-id [i]
|
||||
(update i :invoice-number
|
||||
@@ -203,88 +206,91 @@
|
||||
{:vendor-code vendor-code})))))
|
||||
|
||||
(defn import-uploaded-invoice [client forced-location forced-vendor imports]
|
||||
(let [clients (d-clients/get-all)
|
||||
transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text] :as info}]
|
||||
(println "searching for" vendor-code)
|
||||
(let [ _ (println "matching" customer-identifier)
|
||||
matching-client (or (and account-number
|
||||
(parse/best-match clients account-number 0.0))
|
||||
(and customer-identifier
|
||||
(parse/best-match clients customer-identifier))
|
||||
(if client
|
||||
(first (filter (fn [c]
|
||||
(= (:db/id c) (Long/parseLong client)))
|
||||
clients))))
|
||||
_ (when-not matching-client
|
||||
(throw (ex-info (str "Searched clients for '" customer-identifier "'. No client found in file. Select a client first.")
|
||||
{:invoice-number invoice-number
|
||||
:customer-identifier customer-identifier
|
||||
:vendor-code vendor-code})))
|
||||
|
||||
matching-vendor (match-vendor vendor-code forced-vendor)
|
||||
|
||||
|
||||
_ (println "invoice \"" invoice-number "\" matches client " (:client/name matching-client) " (" (:db/id matching-client) ")")
|
||||
matching-location (or (when-not (str/blank? forced-location)
|
||||
forced-location)
|
||||
(parse/best-location-match matching-client text full-text))
|
||||
[existing-id existing-outstanding-balance existing-status import-status] (when (and matching-client matching-location)
|
||||
(try
|
||||
(->> (d/query
|
||||
(cond-> {:query {:find ['?e '?outstanding-balance '?status '?import-status2]
|
||||
:in ['$ '?invoice-number '?vendor '?client]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
[?e :invoice/outstanding-balance ?outstanding-balance]
|
||||
[?e :invoice/status ?status]
|
||||
[?e :invoice/import-status ?import-status]
|
||||
[?import-status :db/ident ?import-status2]]}
|
||||
:args [(d/db (d/connect uri)) invoice-number (:db/id matching-vendor) (:db/id matching-client)]}))
|
||||
first)
|
||||
(catch Exception e
|
||||
(throw (ex-info (str "Failed to find potential matching invoice with"
|
||||
" invoice " invoice-number
|
||||
" vendor " matching-vendor
|
||||
" client " (:client/name matching-client)
|
||||
". "
|
||||
(.toString e))
|
||||
{:args [ invoice-number matching-vendor (:db/id matching-client)]})))
|
||||
))
|
||||
automatically-paid-for (set (map :db/id (:vendor/automatically-paid-when-due matching-vendor)))]
|
||||
(lc/with-context {:area "upload-invoice"}
|
||||
(let [clients (d-clients/get-all)
|
||||
transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text] :as info}]
|
||||
|
||||
(cond
|
||||
(not (and matching-location matching-client))
|
||||
result
|
||||
(let [
|
||||
matching-client (or (and account-number
|
||||
(parse/best-match clients account-number 0.0))
|
||||
(and customer-identifier
|
||||
(parse/best-match clients customer-identifier))
|
||||
(if client
|
||||
(first (filter (fn [c]
|
||||
(= (:db/id c) (Long/parseLong client)))
|
||||
clients))))
|
||||
_ (when-not matching-client
|
||||
(throw (ex-info (str "Searched clients for '" customer-identifier "'. No client found in file. Select a client first.")
|
||||
{:invoice-number invoice-number
|
||||
:customer-identifier customer-identifier
|
||||
:vendor-code vendor-code})))
|
||||
|
||||
(= :import-status/imported import-status)
|
||||
result
|
||||
matching-vendor (match-vendor vendor-code forced-vendor)
|
||||
|
||||
:else
|
||||
(conj result (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
|
||||
:invoice/client-identifier customer-identifier
|
||||
:invoice/vendor (:db/id matching-vendor)
|
||||
:invoice/invoice-number invoice-number
|
||||
:invoice/automatically-paid-when-due (boolean (automatically-paid-for (:db/id matching-client)))
|
||||
:invoice/total (Double/parseDouble total)
|
||||
:invoice/date (to-date date)
|
||||
:invoice/import-status :import-status/pending
|
||||
:invoice/outstanding-balance (or existing-outstanding-balance (Double/parseDouble total))
|
||||
:invoice/status (or existing-status :invoice-status/unpaid)
|
||||
:invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (d-vendors/account-for-client-id matching-vendor (:db/id matching-client))
|
||||
:location matching-location
|
||||
:amount (Double/parseDouble total)}])
|
||||
:db/id existing-id
|
||||
})
|
||||
(:vendor/terms matching-vendor) (assoc :invoice/due (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client)))))))))
|
||||
))
|
||||
[]
|
||||
imports)]
|
||||
(when-not (seq transactions)
|
||||
(throw (ex-info "No invoices found."
|
||||
{:imports (str imports)})))
|
||||
@(d/transact (d/connect uri) (vec (set transactions)))))
|
||||
|
||||
_ (info-event "Found match for invoice" {:invoice-number invoice-number
|
||||
:vendor (select-keys matching-vendor [:vendor/name :db/id])
|
||||
:client (select-keys matching-client [:client/name :db/id])})
|
||||
matching-location (or (when-not (str/blank? forced-location)
|
||||
forced-location)
|
||||
(parse/best-location-match matching-client text full-text))
|
||||
[existing-id existing-outstanding-balance existing-status import-status] (when (and matching-client matching-location)
|
||||
(try
|
||||
(->> (d/query
|
||||
(cond-> {:query {:find ['?e '?outstanding-balance '?status '?import-status2]
|
||||
:in ['$ '?invoice-number '?vendor '?client]
|
||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||
[?e :invoice/vendor ?vendor]
|
||||
[?e :invoice/client ?client]
|
||||
[?e :invoice/outstanding-balance ?outstanding-balance]
|
||||
[?e :invoice/status ?status]
|
||||
[?e :invoice/import-status ?import-status]
|
||||
[?import-status :db/ident ?import-status2]]}
|
||||
:args [(d/db (d/connect uri)) invoice-number (:db/id matching-vendor) (:db/id matching-client)]}))
|
||||
first)
|
||||
(catch Exception e
|
||||
(throw (ex-info (str "Failed to find potential matching invoice with"
|
||||
" invoice " invoice-number
|
||||
" vendor " matching-vendor
|
||||
" client " (:client/name matching-client)
|
||||
". "
|
||||
(.toString e))
|
||||
{:args [ invoice-number matching-vendor (:db/id matching-client)]})))
|
||||
))
|
||||
automatically-paid-for (set (map :db/id (:vendor/automatically-paid-when-due matching-vendor)))]
|
||||
|
||||
(cond
|
||||
(not (and matching-location matching-client))
|
||||
result
|
||||
|
||||
(= :import-status/imported import-status)
|
||||
result
|
||||
|
||||
:else
|
||||
(conj result (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
|
||||
:invoice/client-identifier customer-identifier
|
||||
:invoice/vendor (:db/id matching-vendor)
|
||||
:invoice/invoice-number invoice-number
|
||||
:invoice/automatically-paid-when-due (boolean (automatically-paid-for (:db/id matching-client)))
|
||||
:invoice/total (Double/parseDouble total)
|
||||
:invoice/date (to-date date)
|
||||
:invoice/import-status :import-status/pending
|
||||
:invoice/outstanding-balance (or existing-outstanding-balance (Double/parseDouble total))
|
||||
:invoice/status (or existing-status :invoice-status/unpaid)
|
||||
:invoice/expense-accounts (when-not existing-id [#:invoice-expense-account {:account (d-vendors/account-for-client-id matching-vendor (:db/id matching-client))
|
||||
:location matching-location
|
||||
:amount (Double/parseDouble total)}])
|
||||
:db/id existing-id
|
||||
})
|
||||
(:vendor/terms matching-vendor) (assoc :invoice/due (coerce/to-date
|
||||
(time/plus date (time/days (d-vendors/terms-for-client-id matching-vendor (:db/id matching-client)))))))))
|
||||
))
|
||||
[]
|
||||
imports)]
|
||||
(when-not (seq transactions)
|
||||
(throw (ex-info "No invoices found."
|
||||
{:imports (str imports)})))
|
||||
@(d/transact (d/connect uri) (vec (set transactions))))))
|
||||
|
||||
|
||||
(defn validate-account-rows [rows code->existing-account]
|
||||
@@ -318,7 +324,6 @@
|
||||
:in ['$ '?z]
|
||||
:where [['?e :client/code '?z]]}
|
||||
:args [(d/db (d/connect uri)) customer]})))
|
||||
_ (println client-id)
|
||||
headers (map read-string header)
|
||||
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
|
||||
{:account/applicability [:db/ident]}
|
||||
@@ -427,7 +432,7 @@
|
||||
(map (parse-or-error :amount parse-amount))
|
||||
(map (parse-or-error :date parse-date)))
|
||||
error-rows (filter :errors rows)
|
||||
_ (println "importing raw transactions" rows)
|
||||
_ (log/info "Importing " (count rows) "raw transactions")
|
||||
raw-transactions (vec (->> rows
|
||||
(filter #(not (seq (:errors %))) )
|
||||
(map (fn [{:keys [description-original client-code status high-level-category amount bank-account-code date]}]
|
||||
@@ -445,7 +450,7 @@
|
||||
:errors (map #(dissoc % :date) error-rows)})
|
||||
:headers {"Content-Type" "application/edn"}})
|
||||
(catch Exception e
|
||||
(println e)
|
||||
(log/error e)
|
||||
{:status 500
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)
|
||||
@@ -469,18 +474,18 @@
|
||||
vendor (some-> (or vendor vendor-2)
|
||||
(Long/parseLong))
|
||||
{:keys [filename tempfile]} files]
|
||||
#_(println params (.getPath tempfile) filename)
|
||||
(try
|
||||
(import-uploaded-invoice client location vendor (parse/parse-file (.getPath tempfile) filename))
|
||||
{:status 200
|
||||
:body (pr-str {})
|
||||
:headers {"Content-Type" "application/edn"}}
|
||||
(catch Exception e
|
||||
{:status 500
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)
|
||||
:data (ex-data e)})
|
||||
:headers {"Content-Type" "application/edn"}}))))
|
||||
(lc/with-context {:parsing-file filename}
|
||||
(try
|
||||
(import-uploaded-invoice client location vendor (parse/parse-file (.getPath tempfile) filename))
|
||||
{:status 200
|
||||
:body (pr-str {})
|
||||
:headers {"Content-Type" "application/edn"}}
|
||||
(catch Exception e
|
||||
{:status 500
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)
|
||||
:data (ex-data e)})
|
||||
:headers {"Content-Type" "application/edn"}})))))
|
||||
|
||||
(POST "/upload-integreat"
|
||||
{{:keys [excel-rows]} :edn-params user :identity}
|
||||
@@ -515,7 +520,6 @@
|
||||
files-2 "file"} :params :as params
|
||||
user :identity}
|
||||
(let [files (or files files-2)
|
||||
_ (println files)
|
||||
{:keys [filename tempfile]} files]
|
||||
(assert-admin user)
|
||||
(try
|
||||
@@ -524,7 +528,7 @@
|
||||
:body (pr-str {})
|
||||
:headers {"Content-Type" "application/edn"}}
|
||||
(catch Exception e
|
||||
(println e)
|
||||
(log/error e)
|
||||
{:status 500
|
||||
:body (pr-str {:message (.getMessage e)
|
||||
:error (.toString e)
|
||||
@@ -545,7 +549,7 @@
|
||||
:body (import-account-overrides client (.getPath tempfile))
|
||||
:headers {"Content-Type" "application/json"}}
|
||||
(catch Exception e
|
||||
(println e)
|
||||
(log/error e)
|
||||
{:status 500
|
||||
:body {:message (.getMessage e)
|
||||
:data (ex-data e)}
|
||||
|
||||
Reference in New Issue
Block a user