slightly improved excel import.

This commit is contained in:
Bryce Covert
2019-03-20 22:35:43 -07:00
parent 2727573a96
commit a55e6d890f
3 changed files with 56 additions and 35 deletions

View File

@@ -22,9 +22,12 @@
n)))) n))))
(defn assoc-client-code [i] (defn assoc-client-code [i]
(-> i (let [[client-code default-location] (str/split (:location i) #"-" )]
(assoc :client-code (first (str/split (:location i) #"-" ))) (cond-> i
(assoc :default-location (second (str/split (:location i) #"-" ))))) client-code (assoc :client-code client-code)
default-location (assoc :default-location default-location)
(not client-code) (update :errors conj {:info "No client code found"})
(not default-location) (update :errors conj {:info "No default location found"}))))
(defn parse-client [{:keys [client-code client]} clients] (defn parse-client [{:keys [client-code client]} clients]
(if-let [id (:db/id (or (clients client-code) (if-let [id (:db/id (or (clients client-code)
@@ -105,6 +108,8 @@
(map (parse-or-error :invoice-number parse-invoice-number)) (map (parse-or-error :invoice-number parse-invoice-number))
(map (parse-or-error :total parse-amount)) (map (parse-or-error :total parse-amount))
(map (parse-or-error :date parse-date)))] (map (parse-or-error :date parse-date)))]
(println "ROWS" rows)
rows)) rows))
(defn invoice-rows->transaction [rows] (defn invoice-rows->transaction [rows]
@@ -229,6 +234,7 @@
:bank-account-id (:db/id (all-bank-accounts bank-account-code))}))))] :bank-account-id (:db/id (all-bank-accounts bank-account-code))}))))]
(manual-import raw-transactions) (manual-import raw-transactions)
{:status 200 {:status 200
:body (pr-str {:imported (count raw-transactions) :body (pr-str {:imported (count raw-transactions)
@@ -272,10 +278,11 @@
set) set)
inserted-rows @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows)))] inserted-rows @(d/transact (d/connect uri) (invoice-rows->transaction (:new grouped-rows)))]
{:status 200 {:status 200
:body (pr-str {:imported (count (:new grouped-rows)) :body (pr-str {:imported (count (:new grouped-rows))
:already-imported (count (:exists grouped-rows)) :already-imported (count (:exists grouped-rows))
:vendors-not-found vendors-not-found :vendors-not-found vendors-not-found
:errors (:error grouped-rows)}) :errors (map #(dissoc % :date) (:error grouped-rows))})
:headers {"Content-Type" "application/edn"}})))) :headers {"Content-Type" "application/edn"}}))))
wrap-secure)) wrap-secure))

View File

@@ -56,9 +56,11 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::save-error ::save-error
(fn [db [_ form result]] (fn [db [_ form result]]
(-> db (-> db
(assoc-in [::forms form :status] :error) (assoc-in [::forms form :status] :error)
(assoc-in [::forms form :error] (:message (first result)))))) (assoc-in [::forms form :error] (or (:message (first result))
result)))))
(defn side-bar-form [{:keys [form]} children] (defn side-bar-form [{:keys [form]} children]
[:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::form-closing form])}] [:div children]]) [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::form-closing form])}] [:div children]])
@@ -67,3 +69,8 @@
(-> db (-> db
(assoc-in [::forms id :status] :loading) (assoc-in [::forms id :status] :loading)
(assoc-in [::forms id :error] nil))) (assoc-in [::forms id :error] nil)))
(defn save-succeeded [db id]
(-> db
(assoc-in [::forms id :status] nil)
(assoc-in [::forms id :error] nil)))

View File

@@ -6,6 +6,7 @@
[auto-ap.events :as all-events] [auto-ap.events :as all-events]
[auto-ap.events.admin.clients :as events] [auto-ap.events.admin.clients :as events]
[auto-ap.entities.clients :as entity] [auto-ap.entities.clients :as entity]
[auto-ap.forms :as forms]
[auto-ap.views.components.layouts :refer [side-bar-layout]] [auto-ap.views.components.layouts :refer [side-bar-layout]]
[auto-ap.views.components.admin.side-bar :refer [admin-side-bar]] [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
@@ -31,39 +32,36 @@
(fn [db [_ field v]] (fn [db [_ field v]]
(assoc-in db (into [::excel-import] field) v))) (assoc-in db (into [::excel-import] field) v)))
(re-frame/reg-event-db
::edit
(fn [db [_ field v]]
(assoc db ::excel-import nil )
db))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save ::save
(fn [{:keys [db]}] [(forms/in-form ::excel-import)]
(let [excel-import (::excel-import db)] (fn [{{excel-import-data :data :as excel-import-form} :db}]
{:http {:token (:user db) (let [user @(re-frame/subscribe [::subs/token])]
{:db (-> excel-import-form
(assoc :status :loading)
(assoc :error nil))
:http {:token user
:method :post :method :post
:body (pr-str excel-import) :body (pr-str excel-import-data)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/invoices/upload-integreat") :uri (str "/api/invoices/upload-integreat")
:on-success [::save-complete] :on-success [::save-complete]
:on-error [::save-error]} :on-error [::forms/save-error ::excel-import]}})))
:db (-> db
(assoc-in [::excel-import :rows] nil)
(assoc-in [::excel-import :saving?] true))})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save-complete ::save-complete
(fn [{:keys [db]} [_ rows]] (fn [{:keys [db]} [_ rows]]
{:dispatch [::edit nil] {:db
:db
(-> db (-> db
(assoc-in [::excel-import :rows] rows) (forms/save-succeeded ::excel-import)
(assoc-in [::excel-import :saving?] false))})) (assoc-in [::excel-import :rows] rows))}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save-error ::save-error
(fn [{:keys [db]}] (fn [{:keys [db]}]
(println "ERROR")
{:dispatch [::change [:error] true] {:dispatch [::change [:error] true]
:db (-> db :db (-> db
(assoc-in [::excel-import :rows] nil) (assoc-in [::excel-import :rows] nil)
@@ -82,14 +80,20 @@
(fn [{:keys [db]}] (fn [{:keys [db]}]
(let [excel-import (::excel-import db)] (let [excel-import (::excel-import db)]
(println (::expense-accounts db)) (println (::expense-accounts db))
{:https {:requests (map (fn [v] {:graphql {:token (:user db)
{:token (:user db) :query-obj {:venia/operation {:operation/type :mutation
:method :post :operation/name "UpsertVendor"}
:body (pr-str {:name v :default-expense-account (-> db ::expense-accounts (get v) :default-expense-account) })
:headers {"Content-Type" "application/edn"} :venia/queries (map (fn [v ]
:uri (str "/api/vendors/")}) {:query/data [:upsert-vendor
(doto (get-in db [::excel-import :create-vendors]) {:vendor {:name v :default-expense-account (-> db ::expense-accounts (get v) :default-expense-account)}}
println)) [:id :name :default-expense-account
[:primary-contact [:name :phone :email :id]]
[:secondary-contact [:id :name :phone :email]]
:print-as :invoice-reminder-schedule :code
[:address [:street1 :street2 :city :state :zip]]]]})
(get-in db [::excel-import :create-vendors]))}
:on-success [::create-vendor-complete] :on-success [::create-vendor-complete]
:on-error [::create-vendor-error]} :on-error [::create-vendor-error]}
:db (-> db :db (-> db
@@ -112,9 +116,12 @@
:or {create-vendors #{}} :or {create-vendors #{}}
:as excel-import-data} @(re-frame/subscribe [::excel-import]) :as excel-import-data} @(re-frame/subscribe [::excel-import])
data @(re-frame/subscribe [::expense-accounts]) data @(re-frame/subscribe [::expense-accounts])
form @(re-frame/subscribe [::forms/form ::excel-import])
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts]) chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
change-event [::all-events/change-form [::expense-accounts]]] change-event [::all-events/change-form [::expense-accounts]]]
(println form)
[:div [:div
[:h1.title "Import Invoices from Integreat Excel"] [:h1.title "Import Invoices from Integreat Excel"]
(when (seq vendors-not-found) (when (seq vendors-not-found)
@@ -161,13 +168,13 @@
[:textarea.textarea {:rows "20" [:textarea.textarea {:rows "20"
:field :excel-rows :field :excel-rows
:type "text" :type "text"
:event ::change :event [::forms/change ::excel-import]
:subscription excel-import-data}]] :subscription (:data form)}]]
[:button.button.is-large.is-pulled-right.is-primary {:on-click (dispatch-event [::save]) [:button.button.is-large.is-pulled-right.is-primary {:on-click (dispatch-event [::save])
:class (when (:saving? excel-import-data) :class (str @(re-frame/subscribe [::forms/loading-class ::excel-import])
"is-loading") (when (:error form) " animated shake"))
:disabled (when (:saving? excel-import-data) "disabled")} "Import"] :disabled (when (= :saving (:status form)) "disabled")} "Import"]
[:div.is-clearfix] [:div.is-clearfix]
[:div.is-clearfix [:div.is-clearfix