From 19caf80bd8668c1a1366e17dbb79cd6abdabeaad Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 11 Apr 2020 09:26:59 -0700 Subject: [PATCH] making sysco and mama lus work. --- resources/public/css/main.css | 2 +- src/clj/auto_ap/parse.clj | 2 +- src/clj/auto_ap/parse/csv.clj | 30 ++++++++++-- src/clj/auto_ap/parse/templates.clj | 48 +++++++++---------- src/clj/auto_ap/routes/invoices.clj | 29 ++++++++--- .../auto_ap/views/pages/import_invoices.cljs | 17 +++++-- 6 files changed, 85 insertions(+), 43 deletions(-) diff --git a/resources/public/css/main.css b/resources/public/css/main.css index 8d5b1c6a..b351b5cf 100644 --- a/resources/public/css/main.css +++ b/resources/public/css/main.css @@ -1,4 +1,4 @@ - form.dz { border: 2px dashed lightgray;} + form.dz .notification { border: 2px dashed lightgray;} html,body { font-family: 'Open Sans', serif; diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index c68b3c8b..4a2ca084 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -91,7 +91,7 @@ (sort-by second) ffirst) - word-set (set (str/split (.toLowerCase invoice-client-name) #"\s" )) + word-set (set (str/split (.toLowerCase invoice-client-name) #"[\s:]" )) client-word-match (->> clients (map (fn [{:keys [:db/id :client/matches :client/name] :as client :or {matches []}}] diff --git a/src/clj/auto_ap/parse/csv.clj b/src/clj/auto_ap/parse/csv.clj index 82247043..cc830b9e 100644 --- a/src/clj/auto_ap/parse/csv.clj +++ b/src/clj/auto_ap/parse/csv.clj @@ -7,11 +7,15 @@ (defn determine [[header :as z]] (prn header) - (cond (str/includes? (second header) "Customer's PO No.") - :mama-lus + (doto (cond (str/includes? (second header) "Customer's PO No.") + :mama-lus - :else - nil)) + (str/includes? (str header) "Closed Date") + :sysco + + :else + nil) + println)) (defmulti parse-csv determine @@ -29,6 +33,23 @@ (first))] (u/parse-value :clj-time valid-fmt d))) +(defmethod parse-csv :sysco + [rows] + (println "Importing Sysco") + (transduce + (comp (drop 1) + (map (fn [[invoice-number invoice-date _ amount :as row]] + {:vendor-code "Sysco" + :customer-identifier nil + :invoice-number invoice-number + :date (parse-date-fallover invoice-date ["M/d/yyyy"]) + :total (str/replace amount #"[,\$]" "") + :text (str/join " " row) + :full-text (str/join " " row)}))) + conj + [] + rows)) + (defmethod parse-csv :mama-lus [rows] (println "MAMA LU4") @@ -51,7 +72,6 @@ nil) (defn parse-file [file filename] - (println "HEREERE") (with-open [reader (io/reader file)] (let [rows (csv/read-csv reader :separator \,)] (parse-csv rows)))) diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index b3e7f1af..2fa94e59 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -324,6 +324,30 @@ :total [#"Total Invoice" 0 5] :date [#"Date" 0 0 #"Date: (.*)"] :invoice-number [#"Invoice #" 0 0 #"Invoice #: (.*)"]}} + {:vendor "Mama Lu's Foods" + :keywords [#"Mama Lu's Foods"] + :extract (fn [wb vendor] + (let [[sheet] (d/sheet-seq wb)] + (transduce (comp + (drop 5) + (filter + (fn [r] + (and + r + (->> r d/cell-seq second d/read-cell)))) + (map + (fn [r] + (let [[_ customer-order-number num date name amount] (map d/read-cell (d/cell-seq r))] + {:customer-identifier (second (re-find #"([^:]*):" name)) + :text name + :full-text name + :date date + :invoice-number (str customer-order-number "-" (int num)) + :total (str amount) + :vendor-code vendor})))) + conj + [] + (d/row-seq sheet))))} {:vendor "DVW Commercial" :keywords [#"Total for" #"Num"] :extract (fn [wb vendor] @@ -351,30 +375,6 @@ :keywords [#"Alt_invoice_number"] :extract (fn [wb vendor] (let [[sheet] (d/sheet-seq wb)] - (println "COUNT" (count (transduce (comp - (drop-while (fn [c] - (not (re-find #"Customer_id" (str (d/read-cell c)))))) - (drop 9) - (filter (fn [c] - (= 0 (.getColumnIndex c)))) - (filter (fn [c] - (not (str/blank? (str/trim (or (d/read-cell (d/select-cell (offset c 1 0) sheet)) "")))))) - (map (fn [c] - {:customer-identifier (str/trim (d/read-cell (d/select-cell (offset c 1 0) sheet))) - :text (d/read-cell (d/select-cell (offset c 1 0) sheet)) - :full-text (d/read-cell (d/select-cell (offset c 1 0) sheet)) - :date (u/parse-value :clj-time "MM/dd/yyyy" (str/trim (d/read-cell (d/select-cell (offset c 5 0) sheet)))) - :invoice-number (->> - (re-find #"^(?:0+([A-Z0-9]+))|([A-Z]+[A-Z0-9]+)" (str/trim (d/read-cell (d/select-cell (offset c 2 0) sheet)))) - (drop 1 ) - (filter identity) - first) - :total (str (d/read-cell (d/select-cell (offset c 7 0) sheet))) - :vendor-code vendor})) - (filter :customer-identifier)) - conj - [] - (d/cell-seq sheet)))) (transduce (comp (drop-while (fn [c] (not (re-find #"Customer_id" (str (d/read-cell c)))))) diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 55b48950..9c484af6 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -159,9 +159,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-expense-account] (->> (d/query @@ -177,9 +176,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 @@ -277,16 +288,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"}})) )) diff --git a/src/cljs/auto_ap/views/pages/import_invoices.cljs b/src/cljs/auto_ap/views/pages/import_invoices.cljs index 974677e8..b228fde3 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -11,14 +11,21 @@ [auto-ap.entities.vendors :as vendor] [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [cljsjs.dropzone :as dropzone] - [cljs.reader :as edn])) + [cljs.reader :as edn] + [clojure.string :as str])) (def dropzone (let [client (re-frame/subscribe [::subs/client]) token (re-frame/subscribe [::subs/token])] (with-meta (fn [] [:form.dz {:action "/api/invoices/upload"} + [:div.field.has-addons + [:p.control + [:a.button.is-static "Force Location"]] + [:p.control + [:input.input {:name "location" :placeholder "SG" :size "4" :maxlength "2" :style {:display "inline"}}]]] [:div.tile.notification + [:div.has-text-centered {:style {:padding "80px 0px" :width "100%"}} [:span [:span {:class "icon"} @@ -30,7 +37,7 @@ (.on (js-this) "success" (fn [_ files] (re-frame/dispatch [::invalidated]))) (.on (js-this) "error" (fn [_ error] - (re-frame/dispatch [::errored error])))) + (re-frame/dispatch [::errored error])))) :paramName "file" :headers {"Authorization" (str "Token " @token)} :url (str "/api/invoices/upload" @@ -39,6 +46,7 @@ :previewsContainer "#dz-hidden" :previewTemplate "
"})))}))) + (re-frame/reg-sub ::invoice-page (fn [db] @@ -171,6 +179,7 @@ error (re-frame/subscribe [::error])] [:div [:h1.title "Upload invoices"] + [dropzone] @@ -191,9 +200,7 @@ :invoice-page invoice-page :overrides {:client (fn [row] [:p (:name (:client row)) - [:p [:i.is-size-7 (:client-identifier row)]]] - - )} + [:p [:i.is-size-7 (:client-identifier row)]]])} :check-boxes true :checked (:checked @invoice-page) :on-check-changed (fn [which invoice]