progress on making it possible to import from excel.
This commit is contained in:
@@ -21,21 +21,25 @@
|
||||
(defn upsert-multi! [rows]
|
||||
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported]))
|
||||
column-names (str/join "," (map name k))]
|
||||
(doseq [rows (partition-all 2000 rows)]
|
||||
(j/db-do-prepared (get-conn)
|
||||
(sql/format {:with [[[:v (str "(" column-names ")")]
|
||||
(helpers/values (->> rows
|
||||
(map clj->db )
|
||||
(map (apply juxt k))))]]
|
||||
:insert-into [[:invoices k]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported ]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
[:= :exist.invoice-number :v.invoice-number]
|
||||
[:= :exist.company-id :v.company-id]
|
||||
[:= :exist.vendor-id :v.vendor-id]]]
|
||||
:where [:= :exist.id nil] }] })))))
|
||||
(reduce
|
||||
(fn [affected rows]
|
||||
(+ affected
|
||||
(first (j/db-do-prepared (get-conn)
|
||||
(sql/format {:with [[[:v (str "(" column-names ")")]
|
||||
(helpers/values (->> rows
|
||||
(map clj->db )
|
||||
(map (apply juxt k))))]]
|
||||
:insert-into [[:invoices k]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported ]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
[:= :exist.invoice-number :v.invoice-number]
|
||||
[:= :exist.company-id :v.company-id]
|
||||
[:= :exist.vendor-id :v.vendor-id]]]
|
||||
:where [:= :exist.id nil] }] })))))
|
||||
0
|
||||
(partition-all 2000 rows))))
|
||||
|
||||
(def base-query (sql/build :select :invoices.*
|
||||
:from :invoices))
|
||||
|
||||
@@ -135,12 +135,16 @@
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:invoice-number invoice-number
|
||||
:date date}))))]
|
||||
:date date}))))
|
||||
|
||||
(invoices/upsert-multi! insert-rows)
|
||||
inserted-row-count (invoices/upsert-multi! insert-rows)
|
||||
already-imported-count (- (count insert-rows) inserted-row-count)]
|
||||
|
||||
|
||||
|
||||
{:status 200
|
||||
:body (pr-str {:imported (count insert-rows)
|
||||
:body (pr-str {:imported inserted-row-count
|
||||
:already-imported already-imported-count
|
||||
:errors (map #(dissoc % :date) error-rows)})
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
|
||||
@@ -38,18 +38,26 @@
|
||||
:uri (str "/api/invoices/upload-integreat")
|
||||
:on-success [::save-complete]
|
||||
:on-error [::save-error]}
|
||||
:db (assoc-in db [::excel-import :rows] nil)})))
|
||||
:db (-> db
|
||||
(assoc-in [::excel-import :rows] nil)
|
||||
(assoc-in [::excel-import :saving?] true))})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save-complete
|
||||
(fn [{:keys [db]} [_ rows]]
|
||||
{:dispatch [::edit nil]
|
||||
:db (assoc-in db [::excel-import :rows] rows)}))
|
||||
:db
|
||||
(-> db
|
||||
(assoc-in [::excel-import :rows] rows)
|
||||
(assoc-in [::excel-import :saving?] false))}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save-error
|
||||
(fn [{:keys [db]}]
|
||||
{:dispatch [::change [:error] true]}))
|
||||
{:dispatch [::change [:error] true]
|
||||
:db (-> db
|
||||
(assoc-in [::excel-import :rows] nil)
|
||||
(assoc-in [::excel-import :saving?] false))}))
|
||||
|
||||
(defn admin-excel-import-page []
|
||||
[:div
|
||||
@@ -63,12 +71,18 @@
|
||||
:event ::change
|
||||
:subscription excel-import-data}]]
|
||||
|
||||
[:button.button.is-large.is-pulled-right.is-primary {:on-click (dispatch-event [::save])} "Import"]
|
||||
[:button.button.is-large.is-pulled-right.is-primary {:on-click (dispatch-event [::save])
|
||||
:class (when (:saving? excel-import-data)
|
||||
"is-loading")
|
||||
:disabled (when (:saving? excel-import-data) "disabled")} "Import"]
|
||||
|
||||
[:div.is-clearfix
|
||||
(when-let [imported (:imported (:rows excel-import-data))]
|
||||
(str imported " rows imported."))
|
||||
]
|
||||
[:p
|
||||
(when-let [imported (:imported (:rows excel-import-data))]
|
||||
(str imported " rows imported."))]
|
||||
[:p
|
||||
(when-let [already-imported (:already-imported (:rows excel-import-data))]
|
||||
(str already-imported " rows already imported."))]]
|
||||
(when-let [errors (:errors (:rows excel-import-data))]
|
||||
|
||||
[:div
|
||||
Reference in New Issue
Block a user