major switch to support import from excel.
This commit is contained in:
@@ -18,21 +18,42 @@
|
||||
(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 parse-company [{:keys [company-code company]} companies]
|
||||
(if-let [id (:id (or (companies company-code)
|
||||
(companies company)))]
|
||||
id
|
||||
(throw (Exception. (str "Company code '" company-code "' and company named '" company "' not found.")))))
|
||||
|
||||
(defn assoc-vendor [i vendors]
|
||||
(assoc i :vendor-id (-> i
|
||||
:vendor-name
|
||||
vendors
|
||||
:id)))
|
||||
(defn parse-invoice-number [{:keys [invoice-number]}]
|
||||
(or invoice-number ""))
|
||||
|
||||
(defn parse-vendor [{:keys [vendor-name]} vendors]
|
||||
(if-let [id (:id (vendors vendor-name))]
|
||||
id
|
||||
(throw (Exception. (str "Vendor '" vendor-name "' not found.")))))
|
||||
|
||||
(defn parse-amount [i]
|
||||
(try
|
||||
(Double/parseDouble (str/replace (or (second
|
||||
(re-matches #"[^0-9\.,]*([0-9\.,]+)[^0-9\.,]*" (:amount i)))
|
||||
"0")
|
||||
#"," ""))
|
||||
(catch Exception e
|
||||
(throw (Exception. (str "Could not parse total from value '" (:amount i) "'") e)))))
|
||||
|
||||
(defn parse-date [{:keys [raw-date]}]
|
||||
(try
|
||||
(parse/parse-value :clj-time "MM/dd/yyyy" raw-date)
|
||||
(catch Exception e
|
||||
(throw (Exception. (str "Could not parse date from '" raw-date "'") e)))))
|
||||
|
||||
(defn parse-or-error [key f]
|
||||
(fn [x]
|
||||
(try
|
||||
(assoc x key (f x))
|
||||
(catch Exception e
|
||||
(update x :errors conj {:info (.getMessage e)
|
||||
:details (str e)})))))
|
||||
|
||||
(defroutes routes
|
||||
(wrap-routes
|
||||
@@ -78,7 +99,7 @@
|
||||
|
||||
(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]
|
||||
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on]
|
||||
|
||||
all-vendors (reduce
|
||||
(fn [x y]
|
||||
@@ -100,23 +121,27 @@
|
||||
(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)
|
||||
(map (parse-or-error :company-id #(parse-company % all-companies)))
|
||||
(map (parse-or-error :vendor-id #(parse-vendor % all-vendors)))
|
||||
(map (parse-or-error :invoice-number parse-invoice-number))
|
||||
(map (parse-or-error :total parse-amount))
|
||||
(map (parse-or-error :date parse-date)))
|
||||
error-rows (filter :errors rows)
|
||||
insert-rows (vec (->> (filter #(not (seq (:errors %))) rows)
|
||||
(map (fn [{:keys [vendor-id total company-id amount date invoice-number]}]
|
||||
{:vendor-id vendor-id
|
||||
:company-id company-id
|
||||
:total total
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:invoice-number invoice-number
|
||||
:date date}))))]
|
||||
|
||||
(invoices/upsert-multi! insert-rows)
|
||||
|
||||
{:status 200
|
||||
:body (pr-str rows)
|
||||
:body (pr-str {:imported (count insert-rows)
|
||||
:errors (map #(dissoc % :date) error-rows)})
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
;; Removing the export view for now...
|
||||
|
||||
Reference in New Issue
Block a user