improved import
This commit is contained in:
@@ -43,16 +43,7 @@
|
|||||||
(filter #(re-find #"application/pdf" (:content-type %)) ))]
|
(filter #(re-find #"application/pdf" (:content-type %)) ))]
|
||||||
(let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]
|
(let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")]
|
||||||
(io/copy (:body pdf-stream) (io/file filename))
|
(io/copy (:body pdf-stream) (io/file filename))
|
||||||
(invoices/insert-multi!
|
(invoices/import (parse/parse-file filename filename) companies)
|
||||||
(for [{:keys [total date invoice-number customer-identifier vendor] :as row}
|
|
||||||
(parse/parse-file filename filename)]
|
|
||||||
(do
|
|
||||||
(println row)
|
|
||||||
(assoc row
|
|
||||||
:company (:name (parse/best-match companies customer-identifier))
|
|
||||||
:imported false
|
|
||||||
:potential-duplicate false))))
|
|
||||||
|
|
||||||
(io/delete-file filename))))))
|
(io/delete-file filename))))))
|
||||||
(sqs/delete-message (assoc message :queue-url queue-url )))))
|
(sqs/delete-message (assoc message :queue-url queue-url )))))
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
(ns auto-ap.db.invoices
|
(ns auto-ap.db.invoices
|
||||||
(:require [clojure.java.jdbc :as j]
|
(:require [clojure.java.jdbc :as j]
|
||||||
|
[auto-ap.parse :as parse]
|
||||||
[auto-ap.db.utils :refer [clj->db db->clj get-conn]]))
|
[auto-ap.db.utils :refer [clj->db db->clj get-conn]]))
|
||||||
|
|
||||||
(defn insert-multi! [rows]
|
(defn insert-multi! [rows]
|
||||||
@@ -26,3 +27,13 @@
|
|||||||
(if company
|
(if company
|
||||||
(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) AND company_id = ?" (Integer/parseInt company)]))
|
||||||
(map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null"))))
|
(map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null"))))
|
||||||
|
|
||||||
|
(defn import [parsed-invoices companies]
|
||||||
|
(insert-multi!
|
||||||
|
(for [{:keys [total date invoice-number customer-identifier vendor] :as row} parsed-invoices]
|
||||||
|
(do
|
||||||
|
(println row)
|
||||||
|
(assoc row
|
||||||
|
:company (:name (parse/best-match companies customer-identifier))
|
||||||
|
:imported false
|
||||||
|
:potential-duplicate false)))))
|
||||||
|
|||||||
@@ -40,19 +40,8 @@
|
|||||||
(POST "/upload"
|
(POST "/upload"
|
||||||
{{ files "file"} :params :as params}
|
{{ files "file"} :params :as params}
|
||||||
(let [{:keys [filename tempfile]} files
|
(let [{:keys [filename tempfile]} files
|
||||||
existing-invoices (invoices/get-all)
|
|
||||||
companies (companies/get-all)]
|
companies (companies/get-all)]
|
||||||
(invoices/insert-multi!
|
(invoices/import (parse/parse-file (.getPath tempfile) filename) companies)
|
||||||
(for [{:keys [total date invoice-number customer-identifier vendor] :as row}
|
|
||||||
(parse/parse-file (.getPath tempfile) filename)]
|
|
||||||
(assoc row
|
|
||||||
:company-id (:id (parse/best-match companies customer-identifier))
|
|
||||||
|
|
||||||
:imported false
|
|
||||||
:potential-duplicate (boolean (seq (filter #(and (= vendor (:vendor %))
|
|
||||||
(= invoice-number (:invoice-number %)))
|
|
||||||
existing-invoices)))
|
|
||||||
)))
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (pr-str (invoices/get-pending ((:query-params params ) "company")))
|
:body (pr-str (invoices/get-pending ((:query-params params ) "company")))
|
||||||
:headers {"Content-Type" "application/edn"}}))
|
:headers {"Content-Type" "application/edn"}}))
|
||||||
|
|||||||
Reference in New Issue
Block a user