This commit is contained in:
Bryce Covert
2020-04-17 21:53:09 -07:00
10 changed files with 159 additions and 59 deletions

View File

@@ -164,9 +164,8 @@
))
(defn import-uploaded-invoice [imports]
(defn import-uploaded-invoice [client forced-location 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)
(let [[matching-vendor default-account] (->> (d/query
@@ -182,9 +181,21 @@
:customer-identifier customer-identifier
:vendor-code vendor-code})))
_ (println "matching" customer-identifier "-" matching-vendor)
matching-client (parse/best-match clients customer-identifier)
_ (println "invoice \"" invoice-number "\"matches client" (:client/name matching-client))
matching-location (parse/best-location-match matching-client text full-text)
matching-client (or (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 "No client found in file. Select a client first.")
{: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-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
@@ -278,16 +289,20 @@
(context "/invoices" []
(POST "/upload"
{{ files "file"} :params :as params}
{{ files "file"
client "client"
location "location"} :params :as params}
(let [{:keys [filename tempfile]} files]
(println params)
(try
(import-uploaded-invoice (parse/parse-file (.getPath tempfile) filename))
(import-uploaded-invoice client location (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"}}))
))