can import from excel now.
This commit is contained in:
@@ -6,7 +6,33 @@
|
||||
[auto-ap.parse :as parse]
|
||||
[auto-ap.routes.utils :refer [wrap-secure]]
|
||||
[compojure.core :refer [GET POST context defroutes
|
||||
wrap-routes]]))
|
||||
wrap-routes]]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(defn reset-id [i]
|
||||
(update i :invoice-number
|
||||
(fn [n] (if (re-matches #"#+" n)
|
||||
nil
|
||||
n))))
|
||||
|
||||
(defn assoc-company-code [i]
|
||||
(assoc i :company-code (first (str/split (:location i) #"-" ))))
|
||||
|
||||
(defn assoc-company [i companies]
|
||||
(assoc i :company-id (or (-> i
|
||||
:company-code
|
||||
companies
|
||||
:id)
|
||||
(-> i
|
||||
:company
|
||||
companies
|
||||
:id))))
|
||||
|
||||
(defn assoc-vendor [i vendors]
|
||||
(assoc i :vendor-id (-> i
|
||||
:vendor-name
|
||||
vendors
|
||||
:id)))
|
||||
|
||||
(defroutes routes
|
||||
(wrap-routes
|
||||
@@ -48,7 +74,50 @@
|
||||
(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"}}))
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
(POST "/upload-integreat"
|
||||
{{:keys [excel-rows]} :edn-params}
|
||||
(let [columns [:date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on]
|
||||
|
||||
all-vendors (reduce
|
||||
(fn [x y]
|
||||
(assoc x (:name y) y))
|
||||
{}
|
||||
(vendors/get-all))
|
||||
|
||||
all-companies (reduce
|
||||
(fn [x y]
|
||||
(-> x
|
||||
(assoc (:code y) y)
|
||||
(assoc (:name y) y)))
|
||||
{}
|
||||
(companies/get-all))
|
||||
|
||||
|
||||
rows (->> (str/split excel-rows #"\n" )
|
||||
(map #(str/split % #"\t"))
|
||||
(map #(into {} (map (fn [c k] [k c] ) % columns)))
|
||||
(map reset-id)
|
||||
(map assoc-company-code)
|
||||
(map #(assoc-company % all-companies))
|
||||
(map #(assoc-vendor % all-vendors))
|
||||
(map (fn [{:keys [vendor-id company-id amount date invoice-number]}]
|
||||
{:vendor-id vendor-id
|
||||
:company-id company-id
|
||||
:total (Double/parseDouble (second
|
||||
(re-matches #"[^0-9\.]*([0-9\.]+)[^0-9\.]*" amount)))
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:invoice-number invoice-number
|
||||
:date (parse/parse-value :clj-time "MM/dd/yyyy" date )
|
||||
}))
|
||||
)]
|
||||
(invoices/insert-multi! rows)
|
||||
|
||||
{:status 200
|
||||
:body (pr-str rows)
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
;; Removing the export view for now...
|
||||
#_(wrap-json-response (GET "/export" {:keys [query-params]}
|
||||
|
||||
Reference in New Issue
Block a user