Supports forcing vendor

This commit is contained in:
Bryce Covert
2020-05-11 19:21:48 -07:00
parent 8f66aac170
commit 55960888cf
5 changed files with 118 additions and 25 deletions

View File

@@ -167,7 +167,33 @@
))
(defn import-uploaded-invoice [client forced-location imports]
(defn match-vendor [vendor-code forced-vendor]
(when (and (not forced-vendor) (str/blank? vendor-code))
(throw (ex-info (str "No vendor found. Please supply an forced vendor.")
{:vendor-code vendor-code})))
(let [vendor-id (or forced-vendor
(->> (d/query
{:query {:find ['?vendor]
:in ['$ '?vendor-name]
:where ['[?vendor :vendor/name ?vendor-name]]}
:args [(d/db (d/connect uri)) vendor-code]})
first
first))]
(when-not vendor-id
(throw (ex-info (str "Vendor matching name \"" vendor-code "\" not found.")
{:vendor-code vendor-code})))
(if-let [matching-vendor (->> (d/query
{:query {:find [(list 'pull '?vendor-id d-vendors/default-read)]
:in ['$ '?vendor-id]}
:args [(d/db (d/connect uri)) vendor-id]})
first
first)]
matching-vendor
(throw (ex-info (str "No vendor with the name " vendor-code " was found.")
{: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 total date vendor-code text full-text] :as info}]
(println "searching for" vendor-code)
@@ -184,18 +210,10 @@
:customer-identifier customer-identifier
:vendor-code vendor-code})))
[matching-vendor] (->> (d/query
{:query {:find [(list 'pull '?vendor d-vendors/default-read)]
:in ['$ '?vendor-name]
:where ['[?vendor :vendor/name ?vendor-name]]}
:args [(d/db (d/connect uri)) vendor-code]})
first)
_ (when-not matching-vendor
(throw (ex-info (str "No vendor with the name " vendor-code " was found.")
{:invoice-number invoice-number
:customer-identifier customer-identifier
:vendor-code vendor-code})))
_ (println "invoice \"" invoice-number "\"matches client " (:client/name matching-client) " (" (:db/id matching-client) ")")
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))
@@ -231,7 +249,7 @@
:else
(conj result (cond-> (remove-nils #:invoice {:invoice/client (:db/id matching-client)
:invoice/client-identifier customer-identifier
:invoice/vendor matching-vendor
:invoice/vendor (:db/id matching-vendor)
:invoice/invoice-number invoice-number
:invoice/total (Double/parseDouble total)
:invoice/date (to-date date)
@@ -248,7 +266,7 @@
))
[]
imports)]
(when-not (seq transactions)
(when-not (seq transactions)
(throw (ex-info "No invoices found."
{:imports imports})))
@(d/transact (d/connect uri) (vec (set transactions)))))
@@ -308,14 +326,18 @@
client :client
client-2 "client"
location :location
location-2 "location"} :params :as params}
location-2 "location"
vendor :vendor
vendor-2 "vendor"} :params :as params}
(let [files (or files files-2)
client (or client client-2)
location (or location location-2)
vendor (some-> (or vendor vendor-2)
(Long/parseLong))
{:keys [filename tempfile]} files]
#_(println params (.getPath tempfile) filename)
(try
(import-uploaded-invoice client location (parse/parse-file (.getPath tempfile) filename))
(import-uploaded-invoice client location vendor (parse/parse-file (.getPath tempfile) filename))
{:status 200
:body (pr-str {})
:headers {"Content-Type" "application/edn"}}