Manual import of ledger entries.
This commit is contained in:
@@ -12,14 +12,89 @@
|
||||
[clojure.string :as str]))
|
||||
|
||||
|
||||
|
||||
(re-frame/reg-sub
|
||||
::loading
|
||||
(fn [db]
|
||||
(-> db ::loading)))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit
|
||||
(fn [db]
|
||||
true))
|
||||
|
||||
(defn line->id [{:keys [source id client-code date vendor-name] :as line}]
|
||||
(str client-code "-" source "-" id))
|
||||
|
||||
(defn textarea->table [{:keys [headings value on-change]} & children]
|
||||
(re-frame/reg-sub
|
||||
::request
|
||||
:<- [::forms/form ::form]
|
||||
(fn [{{lines :line-items :as d} :data :as g}]
|
||||
(into []
|
||||
(for [[external-id lines] (group-by line->id lines)
|
||||
:let [{:keys [source id client-code date vendor-name] :as line} (first lines)]]
|
||||
{:source source
|
||||
:external-id (line->id line)
|
||||
:client-code client-code
|
||||
:date date
|
||||
:vendor-name vendor-name
|
||||
:amount (reduce + 0
|
||||
(->> lines
|
||||
(map :debit)
|
||||
(map js/parseFloat)))
|
||||
:line-items (map (fn [{:keys [debit credit account-identifier location]}]
|
||||
{:account-identifier account-identifier
|
||||
:location location
|
||||
:debit debit
|
||||
:credit credit})
|
||||
lines)}))))
|
||||
|
||||
;; EVENTS
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::imported
|
||||
(fn [{:keys [db]} [_ result]]
|
||||
(let [successful-set (set (map :external-id (:successful (:import-ledger result))))
|
||||
error-set (into {} (map (juxt :external-id :error) (:errors (:import-ledger result))))
|
||||
existing-set (set (map :external-id (:existing (:import-ledger result))))]
|
||||
|
||||
{:db (-> (forms/save-succeeded db ::form )
|
||||
(update-in [::forms/forms ::form :data :line-items]
|
||||
(fn [lis]
|
||||
(mapv
|
||||
#(assoc % :status
|
||||
(cond (successful-set (line->id %))
|
||||
[:span.icon [:i.fa.fa-check]]
|
||||
|
||||
(existing-set (line->id %))
|
||||
""
|
||||
|
||||
(error-set (line->id %))
|
||||
[:span.has-text-warning
|
||||
[:span.is-warning.icon {:title (error-set (line->id %))} [:i.fa.fa-exclamation-triangle]]
|
||||
]))
|
||||
lis))))})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::importing
|
||||
(fn [{:keys [db]} _]
|
||||
(when @(re-frame/subscribe [::can-submit])
|
||||
{:db (forms/loading db ::form )
|
||||
:graphql
|
||||
{:token (-> db :user)
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "ImportLedger"}
|
||||
:venia/queries [{:query/data [:import-ledger
|
||||
{:entries @(re-frame/subscribe [::request])}
|
||||
[[:successful [:external-id]]
|
||||
[:existing [:external-id]]
|
||||
[:errors [:external-id :error]]]]}]}
|
||||
:on-success [::imported]
|
||||
:on-error [::forms/save-error ::form]}})))
|
||||
|
||||
;; COMPONENTS
|
||||
|
||||
(defn textarea->table [{:keys [headings value on-change read-only-headings]} & children]
|
||||
(let [text-form (r/atom "")
|
||||
table-form (r/atom nil)]
|
||||
(fn [{:keys [headings value on-change]}]
|
||||
@@ -31,13 +106,27 @@
|
||||
[:tr
|
||||
(list
|
||||
(for [[heading-name _] headings]
|
||||
[:th heading-name]))
|
||||
(list
|
||||
(for [[heading-name _] read-only-headings]
|
||||
[:th heading-name]))]]
|
||||
(list
|
||||
(for [row value]
|
||||
(for [[index row] (map vector (range )value)]
|
||||
[:tr
|
||||
(list
|
||||
(for [cell row]
|
||||
[:td [:input.input {:value cell}]]))]))]
|
||||
(for [[_ k] headings]
|
||||
[:td
|
||||
[:input.input {:on-change (fn [x]
|
||||
(println "TEST")
|
||||
(.preventDefault x)
|
||||
(println value)
|
||||
(on-change
|
||||
(assoc-in value [index k] (.. x -target -value))))
|
||||
:value (get row k)}]]))
|
||||
(list
|
||||
(for [[_ k] read-only-headings]
|
||||
[:td
|
||||
(get row k)]))]))]
|
||||
children]
|
||||
[:div
|
||||
[:h1.title.is-2 "External Import"]
|
||||
@@ -47,10 +136,11 @@
|
||||
[:button.button.is-primary.is-pulled-right.is-large {:on-click (fn [e]
|
||||
(.preventDefault e)
|
||||
(on-change
|
||||
(drop 1
|
||||
(mapv
|
||||
#(str/split % "\t")
|
||||
(str/split @text-form #"\n")))))}
|
||||
(->> (str/split @text-form #"\n")
|
||||
(drop 1)
|
||||
(mapv #(->> (str/split % "\t")
|
||||
(map (fn [[_ k] v] [k v]) headings)
|
||||
(into {}))))))}
|
||||
"Evaluate"]])])))
|
||||
|
||||
(def balance-sheet-content
|
||||
@@ -59,26 +149,28 @@
|
||||
(let [current-client @(re-frame/subscribe [::subs/client])
|
||||
user @(re-frame/subscribe [::subs/user])
|
||||
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form]) ]
|
||||
(if @(re-frame/subscribe [::loading])
|
||||
(if @(re-frame/subscribe [::forms/is-loading? ::form])
|
||||
[:div [:i.icon.fa.fa-spin.fa-spinner]]
|
||||
[:div
|
||||
|
||||
[bind-field
|
||||
[textarea->table {:type "textarea->table"
|
||||
:field [::line-items]
|
||||
:field [:line-items]
|
||||
:headings [["Id" :id]
|
||||
["Client" :client-code]
|
||||
["Source" :source]
|
||||
["Total" :amount]
|
||||
["Vendor" :vendor-id]
|
||||
["Vendor" :vendor-name]
|
||||
["Date" :date]
|
||||
["Account" :account]
|
||||
["Account" :account-identifier]
|
||||
["Location" :location]
|
||||
["Debit" :debit]
|
||||
["Credit" :credit]]
|
||||
:read-only-headings
|
||||
[["status" :status]]
|
||||
|
||||
:event [::forms/change ::form]
|
||||
:subscription data}
|
||||
[:button.button.is-primary.is-pulled-right.is-large "Import"]]]])))
|
||||
[:button.button.is-primary.is-pulled-right.is-large {:on-click (dispatch-event [::importing])} "Import"]]]])))
|
||||
{}))
|
||||
|
||||
(defn external-import-page []
|
||||
|
||||
Reference in New Issue
Block a user