diff --git a/migrator/migrations/1523368332-DOWN-link-invoices-to-vendor.sql b/migrator/migrations/1523368332-DOWN-link-invoices-to-vendor.sql new file mode 100644 index 00000000..4c069e0a --- /dev/null +++ b/migrator/migrations/1523368332-DOWN-link-invoices-to-vendor.sql @@ -0,0 +1,5 @@ +-- 1523368332 DOWN link-invoices-to-vendor +delete from invoices; + +alter table invoices drop column vendor_id; +alter table invoices add column vendor varchar(255); diff --git a/migrator/migrations/1523368332-UP-link-invoices-to-vendor.sql b/migrator/migrations/1523368332-UP-link-invoices-to-vendor.sql new file mode 100644 index 00000000..935dc71b --- /dev/null +++ b/migrator/migrations/1523368332-UP-link-invoices-to-vendor.sql @@ -0,0 +1,6 @@ +-- 1523368332 UP link-invoices-to-vendor +delete from invoices; + +alter table invoices drop column vendor; +alter table invoices add column vendor_id integer; +alter table invoices add constraint fk_invoices__vendor_id foreign key (vendor_id) references vendors(id); diff --git a/src/clj/auto_ap/background/mail.clj b/src/clj/auto_ap/background/mail.clj index 4ffaa9a5..bf484c06 100644 --- a/src/clj/auto_ap/background/mail.clj +++ b/src/clj/auto_ap/background/mail.clj @@ -2,6 +2,7 @@ (:require [amazonica.aws.s3 :as s3] [amazonica.aws.sqs :as sqs] [auto-ap.db.companies :as companies] + [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] [auto-ap.parse :as parse] [clojure-mail.message :as message] @@ -16,7 +17,8 @@ (defn process-sqs [] (try (println "Fetching messages from sqs...") - (let [companies (companies/get-all)] + (let [companies (companies/get-all) + vendors (vendors/get-all)] (doseq [message (:messages (sqs/receive-message {:queue-url (:invoice-import-queue-url env) :wait-time-seconds 5 :max-number-of-messages 10 diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index 85497fce..24c47f88 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -2,6 +2,7 @@ (:require [auto-ap.db.utils :refer [clj->db db->clj get-conn]] [auto-ap.parse :as parse] [auto-ap.entities.companies :as company] + [auto-ap.entities.vendors :as vendor] [clojure.java.jdbc :as j])) (defn insert-multi! [rows] @@ -29,12 +30,16 @@ (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company_id = ?" (Integer/parseInt company)])) (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null")))) -(defn import [parsed-invoices companies] +(defn import [parsed-invoices companies vendors] (insert-multi! - (for [{:keys [total date invoice-number customer-identifier vendor] :as row} parsed-invoices] + (for [{:keys [total date invoice-number customer-identifier vendor-code] :as row} parsed-invoices] (do + (println "VENDORS" vendors) + (println "VENDOR" (::vendor-id (first (filter #(= (::vendor/code %) vendor-code) vendors)))) - (assoc row - :company-id (::company/id (parse/best-match companies customer-identifier)) - :imported false - :potential-duplicate false))))) + (dissoc (assoc row + :company-id (::company/id (parse/best-match companies customer-identifier)) + :vendor-id (::vendor/id (first (filter #(= (::vendor/code %) vendor-code) vendors))) + :imported false + :potential-duplicate false) + :vendor-code))))) diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index 2c445190..59e0e679 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -22,7 +22,7 @@ (fn [result k v] (assoc result k (some-> (first (map second (re-seq v text))) str/trim ))) - {:vendor (:vendor template)}))])) + {:vendor-code (:vendor template)}))])) (defn parse [text] (->> t/pdf-templates diff --git a/src/clj/auto_ap/parse/excel.clj b/src/clj/auto_ap/parse/excel.clj index 0a8d1f80..5afcdd6b 100644 --- a/src/clj/auto_ap/parse/excel.clj +++ b/src/clj/auto_ap/parse/excel.clj @@ -28,7 +28,7 @@ cell-value)))) first))) - {:vendor vendor} + {:vendor-code vendor} extract)) (defn parse-file diff --git a/src/clj/auto_ap/parse/templates.clj b/src/clj/auto_ap/parse/templates.clj index 7e1aff6e..ad681b34 100644 --- a/src/clj/auto_ap/parse/templates.clj +++ b/src/clj/auto_ap/parse/templates.clj @@ -30,7 +30,7 @@ :total [#"PAY THIS" -1 0] :date [#"INVOICE DATE" 0 1] :invoice-number [#"INVOICE NUMBER" 0 1]}} - {:vendor "Southern Wine Online" + {:vendor "SWO" :keywords [#"Please note that the total invoice amount may"] :extract {:customer-identifier [#"Customer #" 1 0] :total [#"Total Invoice" 0 5] diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 5827ed56..efcb53b0 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -1,5 +1,6 @@ (ns auto-ap.routes.invoices (:require [auto-ap.db.companies :as companies] + [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] [auto-ap.parse :as parse] [auto-ap.routes.utils :refer [wrap-secure]] @@ -41,8 +42,9 @@ (POST "/upload" {{ files "file"} :params :as params} (let [{:keys [filename tempfile]} files - companies (companies/get-all)] - (invoices/import (parse/parse-file (.getPath tempfile) filename) companies) + companies (companies/get-all) + vendors (vendors/get-all)] + (invoices/import (parse/parse-file (.getPath tempfile) filename) companies vendors) {:status 200 :body (pr-str (invoices/get-pending ((:query-params params ) "company"))) :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 e561d357..6e1f6905 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -57,10 +57,10 @@ [:th "Date"] [:th "Amount"] [:th]]] - [:tbody (for [{:keys [vendor potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices] + [:tbody (for [{:keys [vendor-id potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices] ^{:key (str company-id "-" invoice-number "-" date "-" total "-" id)} [:tr - [:td vendor] + [:td vendor-id] (if company-id [:td company-id] [:td [:i.icon.fa.fa-warning {:title "potential duplicate"}] diff --git a/src/cljs/auto_ap/views/pages/paid_invoices.cljs b/src/cljs/auto_ap/views/pages/paid_invoices.cljs index f62d9e6f..c07711bd 100644 --- a/src/cljs/auto_ap/views/pages/paid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/paid_invoices.cljs @@ -23,10 +23,10 @@ [:th "Invoice #"] [:th "Date"] [:th "Amount"]]] - [:tbody (for [{:keys [company invoice-number date total id vendor] :as i} @invoices] + [:tbody (for [{:keys [company invoice-number date total id vendor-id] :as i} @invoices] ^{:key (str company "-" invoice-number "-" date "-" total "-" id)} [:tr - [:td vendor] + [:td vendor-id] [:td company] [:td invoice-number] [:td date]