This commit is contained in:
Bryce Covert
2018-11-10 12:34:38 -08:00
parent 878056e7af
commit 2c14d86e1d

View File

@@ -93,6 +93,60 @@
(update x :errors conj {:info (.getMessage e) (update x :errors conj {:info (.getMessage e)
:details (str e)}))))) :details (str e)})))))
(defn parse-invoice-rows [excel-rows]
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on :default-expense-account]
all-vendors (by :vendor/name (d-vendors/get-graphql {}))
all-clients (d-clients/get-all)
all-clients (merge (by :client/code all-clients) (by :client/name all-clients))
rows (->> (str/split excel-rows #"\n" )
(map #(str/split % #"\t"))
(map #(into {} (map (fn [c k] [k c] ) % columns)))
(map reset-id)
(map assoc-client-code)
(map (parse-or-error :client-id #(parse-client % all-clients)))
(map (parse-or-error :vendor #(parse-vendor % all-vendors)))
(map (parse-or-error :vendor-id #(parse-vendor-id %)))
(map (parse-or-error :default-expense-account parse-default-expense-account))
(map (parse-or-error :invoice-number parse-invoice-number))
(map (parse-or-error :total parse-amount))
(map (parse-or-error :date parse-date)))]
rows))
(defn invoice-rows->transaction [rows]
(->> rows
(mapcat (fn [{:keys [vendor-id total client-id amount date invoice-number default-location default-expense-account check vendor]}]
(let [invoice #:invoice {:db/id (.toString (java.util.UUID/randomUUID))
:vendor vendor-id
:client client-id
:default-location default-location
:default-expense-account default-expense-account
:total total
:outstanding-balance (if (= "Cash" check)
0.0
total)
:status (if (= "Cash" check)
:invoice-status/paid
:invoice-status/unpaid)
:invoice-number invoice-number
:date (to-date date)
:expense-accounts [#:invoice-expense-account {:expense-account-id (or default-expense-account (:vendor/default-expense-account vendor))
:location default-location
:amount total}]}
payment (if (= :invoice-status/paid (:invoice/status invoice))
#:invoice-payment {:invoice (:db/id invoice)
:amount (:invoice/total invoice)
:payment (remove-nils #:payment {:db/id (.toString (java.util.UUID/randomUUID))
:vendor (:invoice/vendor invoice)
:client (:invoice/client invoice)
:type :payment-type/cash
:amount (:invoice/total invoice)
:status :payment-status/cleared
:date (:invoice/date invoice)})}
)]
[invoice payment])))
(filter identity)
(map remove-nils)))
(defroutes routes (defroutes routes
(wrap-routes (wrap-routes
(context "/" [] (context "/" []
@@ -134,78 +188,36 @@
(POST "/upload-integreat" (POST "/upload-integreat"
{{:keys [excel-rows]} :edn-params user :identity} {{:keys [excel-rows]} :edn-params user :identity}
(assert-admin user) (assert-admin user)
(let [columns [:raw-date :vendor-name :check :location :invoice-number :amount :company :bill-entered :bill-rejected :added-on :exported-on :default-expense-account] (let [parsed-invoice-rows (parse-invoice-rows excel-rows)
all-vendors (by :vendor/name (d-vendors/get-graphql {})) existing-rows (->> (d-invoices/raw-graphql {})
all-clients (d-clients/get-all) (filter (fn [i] (not= :invoice-status/voided (:invoice/status i))))
all-clients (merge (by :client/code all-clients) (by :client/name all-clients)) (map (fn [{:keys [:invoice/vendor :invoice/client :invoice/invoice-number]}]
rows (->> (str/split excel-rows #"\n" ) [(:db/id vendor) (:db/id client) invoice-number]))
(map #(str/split % #"\t")) set)
(map #(into {} (map (fn [c k] [k c] ) % columns))) grouped-rows (group-by
(map reset-id) (fn [i]
(map assoc-client-code) (cond (seq (:errors i))
(map (parse-or-error :client-id #(parse-client % all-clients))) :error
(map (parse-or-error :vendor #(parse-vendor % all-vendors)))
(map (parse-or-error :vendor-id #(parse-vendor-id %))) (existing-rows [(:vendor-id i) (:client-id i) (:invoice-number i)])
(map (parse-or-error :default-expense-account parse-default-expense-account)) :exists
(map (parse-or-error :invoice-number parse-invoice-number))
(map (parse-or-error :total parse-amount)) :else
(map (parse-or-error :date parse-date))) :new))
error-rows (filter :errors rows) parsed-invoice-rows)
vendors-not-found (->> rows
vendors-not-found (->> parsed-invoice-rows
(filter #(and (nil? (:vendor-id %)) (filter #(and (nil? (:vendor-id %))
(not= "Cash" (:check %)))) (not= "Cash" (:check %))))
(map :vendor-name) (map :vendor-name)
set) set)
get-dupe-keys (fn [i]
(select-keys i [:invoice/vendor :invoice/client :invoice/invoice-number]))
existing-rows (->> (d-invoices/raw-graphql {})
(filter (fn [i] (not= :invoice-status/voided (:invoice/status i))))
(map (fn [i] (-> i
(update :invoice/client :db/id)
(update :invoice/vendor :db/id))))
(map get-dupe-keys)
set)
total-rows (->> (filter #(not (seq (:errors %))) rows)
(map (fn [{:keys [vendor-id total client-id amount date invoice-number default-location default-expense-account check vendor]}]
#:invoice {:db/id (.toString (java.util.UUID/randomUUID))
:vendor vendor-id
:client client-id
:default-location default-location
:default-expense-account default-expense-account
:total total
:outstanding-balance (if (= "Cash" check)
0.0
total)
:status (if (= "Cash" check)
:invoice-status/paid
:invoice-status/unpaid)
:invoice-number invoice-number
:date (to-date date)
:expense-accounts [#:invoice-expense-account {:expense-account-id (or default-expense-account (:vendor/default-expense-account vendor))
:location default-location
:amount total}]})))
insert-rows (vec (->> total-rows
(filter #(not (existing-rows (get-dupe-keys %) )))
(mapcat (fn [i]
(if (= :invoice-status/paid (:invoice/status i))
[i #:invoice-payment {:invoice (:db/id i)
:amount (:invoice/total i)
:payment (remove-nils #:payment {:db/id (.toString (java.util.UUID/randomUUID))
:vendor (:invoice/vendor i)
:client (:invoice/client i)
:type :payment-type/cash
:amount (:invoice/total i)
:status :payment-status/cleared
:date (:invoice/date i)})}]
[i])))
(map remove-nils)))
inserted-rows @(d/transact (d/connect uri) insert-rows) inserted-rows @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows)))]
already-imported-count (- (count total-rows) (count insert-rows))]
{:status 200 {:status 200
:body (pr-str {:imported (count total-rows) :body (pr-str {:imported (count (:new grouped-rows))
:already-imported already-imported-count :already-imported (count (:exists grouped-rows))
:vendors-not-found vendors-not-found :vendors-not-found vendors-not-found
:errors (map #(dissoc % :date) error-rows)}) :errors (:error grouped-rows)})
:headers {"Content-Type" "application/edn"}})))) :headers {"Content-Type" "application/edn"}}))))
wrap-secure)) wrap-secure))