now invoices are by vendor and company

This commit is contained in:
Bryce Covert
2018-04-10 07:25:35 -07:00
parent 39fa8a8032
commit 03f3df8643
10 changed files with 36 additions and 16 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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

View File

@@ -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)))))

View File

@@ -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

View File

@@ -28,7 +28,7 @@
cell-value))))
first)))
{:vendor vendor}
{:vendor-code vendor}
extract))
(defn parse-file

View File

@@ -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]

View File

@@ -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"}}))

View File

@@ -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"}]

View File

@@ -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]