diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index a20e4aa6..942c5fd1 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -53,44 +53,81 @@ (let [ conn (d/connect uri) - norms-map {:auto-ap/base-schema {:txes auto-ap.datomic/base-schema} - :auto-ap/functions {:txes-fn 'auto-ap.datomic.migrate/functions :requires [:auto-ap/base-schema]} - :auto-ap/fx-pay-function-2 {:txes-fn 'auto-ap.datomic.migrate/fix-pay-function :requires [:auto-ap/functions]} - :auto-ap/migrate-vendors {:txes-fn 'auto-ap.datomic/migrate-vendors :requires [:auto-ap/base-schema]} - :auto-ap/migrate-clients {:txes-fn 'auto-ap.datomic/migrate-clients :requires [:auto-ap/migrate-vendors]} - :auto-ap/migrate-users {:txes-fn 'auto-ap.datomic/migrate-users :requires [:auto-ap/migrate-clients]} - :auto-ap/migrate-invoices {:txes-fn 'auto-ap.datomic/migrate-invoices :requires [:auto-ap/migrate-vendors :auto-ap/migrate-clients]} - :auto-ap/migrate-payments {:txes-fn 'auto-ap.datomic/migrate-payments :requires [:auto-ap/migrate-invoices]} - :auto-ap/migrate-invoices-payments {:txes-fn 'auto-ap.datomic/migrate-invoices-payments :requires [:auto-ap/migrate-payments :auto-ap/migrate-invoices]} - :auto-ap/migrate-invoices-expense-accounts {:txes-fn 'auto-ap.datomic/migrate-invoices-expense-accounts :requires [:auto-ap/migrate-invoices-payments]} - :auto-ap/migrate-transactions {:txes-fn 'auto-ap.datomic/migrate-transactions :requires [:auto-ap/migrate-invoices-expense-accounts]} - :auto-ap/add-client-codes {:txes-fn 'auto-ap.datomic.migrate.add-client-codes/add-client-codes :requires [:auto-ap/migrate-transactions]} - :auto-ap/add-bank-account-codes-schema {:txes-fn 'auto-ap.datomic.migrate.add-bank-account-codes/add-bank-account-codes-schema :requires [:auto-ap/add-client-codes]} - :auto-ap/add-bank-account-codes {:txes-fn 'auto-ap.datomic.migrate.add-bank-account-codes/add-bank-account-codes :requires [:auto-ap/add-bank-account-codes-schema]} - :auto-ap/add-nick-the-greek {:txes [[{:client/name "Nick the Greek" :client/code "NGAK" :client/locations ["MH"] :client/bank-accounts [{:bank-account/code "NGAK-0" :bank-account/type :bank-account-type/cash :bank-account/name "Cash"}]}]] :requires [:auto-ap/add-bank-account-codes]} - :auto-ap/rename-codes-1 {:txes-fn 'auto-ap.datomic.migrate.rename-codes/rename-codes-1 :requires [:auto-ap/add-nick-the-greek]} - :auto-ap/invoice-converter {:txes auto-ap.datomic.migrate.invoice-converter/add-matches :requires [:auto-ap/rename-codes-1]} - :auto-ap/starter {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-starter :requires [:auto-ap/invoice-converter]} - :auto-ap/add-default-location {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location :requires [:auto-ap/invoice-converter]} - :auto-ap/add-default-location-2 {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location-2 :requires [:auto-ap/add-default-location]} - :auto-ap/add-import-status {:txes auto-ap.datomic.migrate.invoice-converter/add-import-status :requires [:auto-ap/add-default-location-2]} - :auto-ap/add-import-status-existing-invoices {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-import-status-existing-invoices :requires [:auto-ap/add-import-status]} - :auto-ap/fix-check-numbers {:txes-fn 'auto-ap.datomic.migrate.check-numbers/fix-check-numbers :requires [:auto-ap/add-import-status-existing-invoices]} - :auto-ap/add-new-vendors {:txes-fn 'auto-ap.datomic.migrate.add-new-vendors/add-new-vendors :requires [:auto-ap/fix-check-numbers]} - :auto-ap/add-account-visibility-fields {:txes-fn 'auto-ap.datomic.migrate.account-sorting/add-account-visibility-fields :requires [:auto-ap/add-new-vendors]} - :auto-ap/make-every-account-visible {:txes-fn 'auto-ap.datomic.migrate.account-sorting/make-every-account-visible :requires [:auto-ap/add-account-visibility-fields]} - :auto-ap/add-general-ledger6 {:txes add-general-ledger/add-general-ledger :requires [:auto-ap/make-every-account-visible]} - :auto-ap/add-general-ledger-fns2 {:txes-fn 'auto-ap.datomic.migrate.add-general-ledger/add-general-ledger-fns :requires [:auto-ap/add-general-ledger6]} - :auto-ap/add-accounts {:txes auto-ap.datomic.migrate.add-general-ledger/add-accounts :requires [:auto-ap/add-general-ledger-fns2]} - :auto-ap/add-transaction-account {:txes auto-ap.datomic.migrate.add-general-ledger/add-transaction-account :requires [:auto-ap/add-accounts]} - :auto-ap/change-expense-account-to-entity {:txes auto-ap.datomic.migrate.add-general-ledger/change-expense-account-to-entity :requires [:auto-ap/add-transaction-account]} - :auto-ap/add-account-to-vendor {:txes auto-ap.datomic.migrate.add-general-ledger/add-account-to-vendor :requires [:auto-ap/change-expense-account-to-entity]} - :auto-ap/convert-vendors {:txes-fn `add-general-ledger/convert-vendors :requires [:auto-ap/add-account-to-vendor]} - :auto-ap/convert-invoices {:txes-fn `add-general-ledger/convert-invoices :requires [:auto-ap/add-account-to-vendor]} - :auto-ap/convert-transactions {:txes-fn `add-general-ledger/convert-transactions :requires [:auto-ap/convert-invoices]} - #_#_:auto-ap/bulk-load-invoice-ledger2 {:txes-fn 'auto-ap.datomic.migrate.add-general-ledger/bulk-load-invoice-ledger :requires [:auto-ap/make-entity-not-unique]} + norms-map {:auto-ap/base-schema {:txes auto-ap.datomic/base-schema} + :auto-ap/functions {:txes-fn 'auto-ap.datomic.migrate/functions + :requires [:auto-ap/base-schema]} + :auto-ap/fx-pay-function-2 {:txes-fn 'auto-ap.datomic.migrate/fix-pay-function + :requires [:auto-ap/functions]} + :auto-ap/migrate-vendors {:txes-fn 'auto-ap.datomic/migrate-vendors + :requires [:auto-ap/base-schema]} + :auto-ap/migrate-clients {:txes-fn 'auto-ap.datomic/migrate-clients + :requires [:auto-ap/migrate-vendors]} + :auto-ap/migrate-users {:txes-fn 'auto-ap.datomic/migrate-users + :requires [:auto-ap/migrate-clients]} + :auto-ap/migrate-invoices {:txes-fn 'auto-ap.datomic/migrate-invoices + :requires [:auto-ap/migrate-vendors :auto-ap/migrate-clients]} + :auto-ap/migrate-payments {:txes-fn 'auto-ap.datomic/migrate-payments + :requires [:auto-ap/migrate-invoices]} + :auto-ap/migrate-invoices-payments {:txes-fn 'auto-ap.datomic/migrate-invoices-payments + :requires [:auto-ap/migrate-payments :auto-ap/migrate-invoices]} + :auto-ap/migrate-invoices-expense-accounts {:txes-fn 'auto-ap.datomic/migrate-invoices-expense-accounts + :requires [:auto-ap/migrate-invoices-payments]} + :auto-ap/migrate-transactions {:txes-fn 'auto-ap.datomic/migrate-transactions + :requires [:auto-ap/migrate-invoices-expense-accounts]} + :auto-ap/add-client-codes {:txes-fn 'auto-ap.datomic.migrate.add-client-codes/add-client-codes + :requires [:auto-ap/migrate-transactions]} + :auto-ap/add-bank-account-codes-schema {:txes-fn 'auto-ap.datomic.migrate.add-bank-account-codes/add-bank-account-codes-schema + :requires [:auto-ap/add-client-codes]} + :auto-ap/add-bank-account-codes {:txes-fn 'auto-ap.datomic.migrate.add-bank-account-codes/add-bank-account-codes + :requires [:auto-ap/add-bank-account-codes-schema]} + :auto-ap/add-nick-the-greek {:txes [[{:client/name "Nick the Greek" :client/code "NGAK" :client/locations ["MH"] :client/bank-accounts [{:bank-account/code "NGAK-0" :bank-account/type :bank-account-type/cash :bank-account/name "Cash"}]}]] + :requires [:auto-ap/add-bank-account-codes]} + :auto-ap/rename-codes-1 {:txes-fn 'auto-ap.datomic.migrate.rename-codes/rename-codes-1 + :requires [:auto-ap/add-nick-the-greek]} + :auto-ap/invoice-converter {:txes auto-ap.datomic.migrate.invoice-converter/add-matches + :requires [:auto-ap/rename-codes-1]} + :auto-ap/starter {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-starter + :requires [:auto-ap/invoice-converter]} + :auto-ap/add-default-location {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location + :requires [:auto-ap/invoice-converter]} + :auto-ap/add-default-location-2 {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-default-location-2 + :requires [:auto-ap/add-default-location]} + :auto-ap/add-import-status {:txes auto-ap.datomic.migrate.invoice-converter/add-import-status + :requires [:auto-ap/add-default-location-2]} + :auto-ap/add-import-status-existing-invoices {:txes-fn 'auto-ap.datomic.migrate.invoice-converter/add-import-status-existing-invoices + :requires [:auto-ap/add-import-status]} + :auto-ap/fix-check-numbers {:txes-fn 'auto-ap.datomic.migrate.check-numbers/fix-check-numbers + :requires [:auto-ap/add-import-status-existing-invoices]} + :auto-ap/add-new-vendors {:txes-fn 'auto-ap.datomic.migrate.add-new-vendors/add-new-vendors + :requires [:auto-ap/fix-check-numbers]} + :auto-ap/add-account-visibility-fields {:txes-fn 'auto-ap.datomic.migrate.account-sorting/add-account-visibility-fields + :requires [:auto-ap/add-new-vendors]} + :auto-ap/make-every-account-visible {:txes-fn 'auto-ap.datomic.migrate.account-sorting/make-every-account-visible + :requires [:auto-ap/add-account-visibility-fields]} + :auto-ap/add-general-ledger6 {:txes add-general-ledger/add-general-ledger + :requires [:auto-ap/make-every-account-visible]} + :auto-ap/add-general-ledger-fns2 {:txes-fn 'auto-ap.datomic.migrate.add-general-ledger/add-general-ledger-fns + :requires [:auto-ap/add-general-ledger6]} + :auto-ap/add-accounts {:txes auto-ap.datomic.migrate.add-general-ledger/add-accounts + :requires [:auto-ap/add-general-ledger-fns2]} + :auto-ap/add-transaction-account {:txes auto-ap.datomic.migrate.add-general-ledger/add-transaction-account + :requires [:auto-ap/add-accounts]} + :auto-ap/change-expense-account-to-entity {:txes auto-ap.datomic.migrate.add-general-ledger/change-expense-account-to-entity + :requires [:auto-ap/add-transaction-account]} + :auto-ap/add-account-to-vendor {:txes auto-ap.datomic.migrate.add-general-ledger/add-account-to-vendor + :requires [:auto-ap/change-expense-account-to-entity]} + :auto-ap/add-location-to-transaction {:txes add-general-ledger/add-location-to-transaction + :requires [:auto-ap/add-account-to-vendor]} + :auto-ap/convert-vendors {:txes-fn `add-general-ledger/convert-vendors + :requires [:auto-ap/add-location-to-transaction]} + :auto-ap/convert-invoices {:txes-fn `add-general-ledger/convert-invoices + :requires [:auto-ap/convert-vendors]} + :auto-ap/convert-transactions {:txes-fn `add-general-ledger/convert-transactions + :requires [:auto-ap/convert-invoices]} + + #_#_:auto-ap/bulk-load-invoice-ledger2 {:txes-fn 'auto-ap.datomic.migrate.add-general-ledger/bulk-load-invoice-ledger :requires [:auto-ap/convert-transactions]} - }] + }] (println "Conforming database...") (println (c/ensure-conforms conn norms-map)) (d/release conn) diff --git a/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj b/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj index e22c16b4..4ac9935a 100644 --- a/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj +++ b/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj @@ -225,10 +225,17 @@ (fn [[transaction-id vendor-id]] (remove-nils {:db/id transaction-id :transaction/vendor vendor-id + :transaction/location "A" :transaction/account (:db/id (accounts/get-account-by-numeric-code-and-sets 2110 ["default"])) })) matched-transactions)])) +(def add-location-to-transaction + [[{:db/ident :transaction/location + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Location of the transaction's target"}]]) + #_(do (doseq [tran (convert-transactions (d/connect auto-ap.datomic/uri))] @(d/transact (d/connect auto-ap.datomic/uri) tran)) diff --git a/src/clj/auto_ap/datomic/transactions.clj b/src/clj/auto_ap/datomic/transactions.clj index 34f0a3dc..b9ba7b17 100644 --- a/src/clj/auto_ap/datomic/transactions.clj +++ b/src/clj/auto_ap/datomic/transactions.clj @@ -92,7 +92,7 @@ (defn get-by-id [id] (-> (d/pull (d/db (d/connect uri)) - '[* {:transaction/client [:client/name :db/id :client/code] + '[* {:transaction/client [:client/name :db/id :client/code :client/locations] :transaction/bank-account [:bank-account/name :bank-account/code :bank-account/yodlee-account-id :db/id] :transaction/vendor [:db/id :vendor/name] :transaction/account [:db/id :account/name :account/numeric-code]}] diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index e876c4e7..10e4380b 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -155,6 +155,7 @@ :amount {:type 'String} :description_original {:type 'String} :description_simple {:type 'String} + :location {:type 'String} :status {:type 'String} :merchant_name {:type 'String} :client {:type :client} @@ -417,6 +418,7 @@ :edit_transaction {:fields {:id {:type :id} :vendor_id {:type :id} + :location {:type 'String} :account_id {:type :id}}} :edit_account diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index 65c948a3..aed4da08 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -8,7 +8,8 @@ [com.walmartlabs.lacinia.executor :as executor] [com.walmartlabs.lacinia.resolve :as resolve] [auto-ap.utils :refer [by]] - [auto-ap.time :refer [parse normal-date]])) + [auto-ap.time :refer [parse normal-date]] + [auto-ap.datomic.clients :as d-clients])) (defn get-transaction-page [context args value] (let [args (assoc args :id (:id context)) @@ -24,13 +25,23 @@ -(defn edit-transaction [context {{:keys [id account_id vendor_id] :as transaction} :transaction} value] - (assert-can-see-client (:id context) (:db/id (d-transactions/get-by-id id))) - (println transaction) - @(d/transact (d/connect uri) - [{:db/id id - :transaction/vendor vendor_id - :transaction/account account_id}]) - (->graphql (d-transactions/get-by-id id)) +(defn edit-transaction [context {{:keys [id location account_id vendor_id] :as transaction} :transaction} value] + (let [transaction (d-transactions/get-by-id id)] + (assert-can-see-client (:id context) (:transaction/client transaction) ) + (when-not (-> (:transaction/client transaction) + :client/locations + set + (conj "A") + (conj "HQ") + (get location)) + (throw (ex-info (str "Location '" location "' not found on client.") {})) + ) + + @(d/transact (d/connect uri) + [{:db/id id + :transaction/vendor vendor_id + :transaction/location location + :transaction/account account_id}]) + (->graphql (d-transactions/get-by-id id))) #_(->graphql {:id id :vendor (d-vendors/get-by-id vendor_id) })) diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index ad4791c2..1d94fe51 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -43,6 +43,7 @@ :journal-entry/amount (:invoice/total entity) :journal-entry/line-items (into [{:journal-entry-line/account (a/get-account-by-numeric-code-and-sets 2110 ["default"]) + :journal-entry-line/location "A" :journal-entry-line/credit (:invoice/total entity)}] (map (fn [ea] (remove-nils {:journal-entry-line/account (:db/id (:invoice-expense-account/account ea)) @@ -67,14 +68,14 @@ :journal-entry/amount (Math/abs (:transaction/amount entity)) :journal-entry/line-items [(remove-nils{:journal-entry-line/account (:db/id (:transaction/account entity)) - :journal-entry-line/location "HQ" + :journal-entry-line/location (:transaction/location entity) :journal-entry-line/debit (when (< (:transaction/amount entity) 0.0) (Math/abs (:transaction/amount entity))) :journal-entry-line/credit (when (>= (:transaction/amount entity) 0.0) (Math/abs (:transaction/amount entity)))}) (remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity)) - :journal-entry-line/location "HQ" + :journal-entry-line/location "A" :journal-entry-line/credit (when (< (:transaction/amount entity) 0.0) (Math/abs (:transaction/amount entity))) :journal-entry-line/debit (when (>= (:transaction/amount entity) 0.0) diff --git a/src/clj/auto_ap/yodlee/import.clj b/src/clj/auto_ap/yodlee/import.clj index 159e573e..b350e26e 100644 --- a/src/clj/auto_ap/yodlee/import.clj +++ b/src/clj/auto_ap/yodlee/import.clj @@ -88,9 +88,10 @@ :vendor (when check (:db/id (:payment/vendor check))) + :location (when check + "A") :account (when check - (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"])) - #_(:db/id (:payment/vendor check))) + (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))) })] diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index e4b5cfdb..8c114fe3 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -50,12 +50,18 @@ [] clients)))) +;; Bank accounts only, not including cash (re-frame/reg-sub - ::real-bank-accounts + ::real-bank-accounts :<- [::client] (fn [client] (->> client :bank-accounts (filter #(= (:type %) :check)) (sort-by :sort-order)))) +(re-frame/reg-sub + ::locations-for-client + + (fn [db [_ client]] + (:locations (get (:clients db) (or client (:client db)))))) (re-frame/reg-sub ::clients-by-id diff --git a/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs b/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs index ec8205fc..3d7e59ca 100644 --- a/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs +++ b/src/cljs/auto_ap/views/pages/admin/accounts/form.cljs @@ -14,7 +14,7 @@ true)) (re-frame/reg-sub - ::account-request + ::request :<- [::forms/form ::form] (fn [{{:keys [id location type numeric-code name account-set]} :data}] {:id id @@ -49,7 +49,7 @@ :query-obj {:venia/operation {:operation/type :mutation :operation/name "UpsertAccount"} :venia/queries [{:query/data [:upsert-account - {:account @(re-frame/subscribe [::account-request])} + {:account @(re-frame/subscribe [::request])} [:id :type :name :account-set :numeric-code :location]]}]} :on-success [::edited edit-completed] :on-error [::forms/save-error ::form]}})))) @@ -130,7 +130,7 @@ [:div.notification.is-warning.animated.fadeInUp error]) - [:button.button.is-large.is-primary {:disabled (if (s/valid? ::entity/account @(re-frame/subscribe [::account-request])) + [:button.button.is-large.is-primary {:disabled (if (s/valid? ::entity/account @(re-frame/subscribe [::request])) "" "disabled") :on-click (dispatch-event [::saving edit-completed]) diff --git a/src/cljs/auto_ap/views/pages/ledger/table.cljs b/src/cljs/auto_ap/views/pages/ledger/table.cljs index f09156b1..03abaccc 100644 --- a/src/cljs/auto_ap/views/pages/ledger/table.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/table.cljs @@ -99,14 +99,14 @@ [:td.has-text-right (nf amount )] [:td status]]] - (for [{:keys [debit credit account]} line-items] + (for [{:keys [debit credit location account]} line-items] [:tr {:class (:class i)} (when-not selected-client [:td ]) [:td ] #_[:td description-original] [:td ] - [:td (:name account)] + [:td location ": " (:name account)] [:td.has-text-right (when debit (nf debit ))] [:td.has-text-right (when credit (nf credit ))] diff --git a/src/cljs/auto_ap/views/pages/transactions/common.cljs b/src/cljs/auto_ap/views/pages/transactions/common.cljs index 928d9cb2..4dd591e3 100644 --- a/src/cljs/auto_ap/views/pages/transactions/common.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/common.cljs @@ -3,6 +3,7 @@ (def transaction-read [:id :amount + :location [:vendor [:name :id]] [:account [:id :name]] :date diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index 27123bd1..f68de1a8 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -11,10 +11,20 @@ (fn [db [_ which]] (-> db (forms/start-form ::edit-transaction {:id (:id which) + :location (:location which) + :client-id (:id (:client which)) :account-id (:id (:account which)) :account-name (:name (:account which)) :vendor-id (:id (:vendor which)) :vendor-name (:name (:vendor which))})))) +(re-frame/reg-sub + ::request + :<- [::forms/form ::edit-transaction] + (fn [{{:keys [id vendor-id account-id location]} :data}] + {:transaction {:id id + :location location + :vendor-id vendor-id + :account-id account-id}})) (re-frame/reg-sub ::can-submit @@ -34,26 +44,33 @@ ::saving (fn [{:keys [db]} [_ edit-completed]] (when @(re-frame/subscribe [::can-submit]) - (let [{{:keys [id vendor-id account-id]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])] + (let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])] {:db (forms/loading db ::edit-transaction ) :graphql {:token (-> db :user) :query-obj {:venia/operation {:operation/type :mutation :operation/name "EditTransaction"} :venia/queries [{:query/data [:edit-transaction - {:transaction {:id id - :vendor-id vendor-id - :account-id account-id}} + @(re-frame/subscribe [::request]) transaction-read]}]} :on-success [::edited edit-completed] :on-error [::forms/save-error ::edit-transaction]}})))) +(re-frame/reg-event-fx + ::change-account + (fn [{:keys [db]} [_ f a]] + (if-let [forced-location (and (= f [:account-id]) + (:location @(re-frame/subscribe [::subs/account a])))] + {:dispatch-n [[::forms/change ::edit-transaction f a] + [::forms/change ::edit-transaction [:location] forced-location]]} + {:dispatch [::forms/change ::edit-transaction f a]}))) + (defn form [{:keys [edit-completed]}] [forms/side-bar-form {:form ::edit-transaction } (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::edit-transaction]) data (assoc data :merchant-name "Hello") ;; TODO - just until merchant is added - current-client @(re-frame/subscribe [::subs/client]) + locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)]) change-event [::forms/change ::edit-transaction]] ^{:key id} [:form { :on-submit (fn [e] @@ -98,7 +115,7 @@ :field [:always-map] :subscription data}] " Always match Merchant '" (:merchant-name data) "' to '" (:vendor-name data) "'" ]]]) - + [:div.field [:p.help "Credit Account"] [:div.control @@ -107,9 +124,26 @@ :type "typeahead" :field [:account-id] :text-field [:account-name] - :event change-event + :event [::change-account] :subscription data}]]]] + [:div.field + [:p.help "Location"] + [:div.control + (if-let [forced-location (:location @(re-frame/subscribe [::subs/account (-> data :account-id )]))] + [:div.select + [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] + [:div.select + [bind-field + [:select {:type "select" + :allow-nil? true + :field [:location] + :spec (set locations) + :event change-event + :subscription data} + (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]] + + (when error ^{:key error} [:div.notification.is-warning.animated.fadeInUp error]) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 18610e07..a806dd1b 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -81,7 +81,7 @@ (defn with-keys [children] (map-indexed (fn [i c] ^{:key i} c) children)) -(defmethod do-bind "select" [dom {:keys [field subscription event class value spec] :as keys} & rest] +(defmethod do-bind "select" [dom {:keys [field allow-nil? subscription event class value spec] :as keys} & rest] (let [field (if (keyword? field) [field] field) event (if (keyword? event) [event] event) keys (assoc keys @@ -91,8 +91,11 @@ :class (str class (when (and spec (not (s/valid? spec (get-in subscription field)))) " is-danger"))) - keys (dissoc keys :field :subscription :event :spec)] - (into [dom keys] (with-keys rest)))) + keys (dissoc keys :field :subscription :event :spec) + options (if allow-nil? + (with-keys (conj rest [:option {:value nil}])) + (with-keys rest))] + (into [dom keys] options))) (defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest]