cleaning up interface for account.
This commit is contained in:
@@ -50,7 +50,7 @@
|
|||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::change
|
::change
|
||||||
(fn [db [_ form & path-pairs]]
|
(fn [db [_ form & path-pairs]]
|
||||||
(println "CHANGING" form path-pairs)
|
(println "CHANGING" path-pairs)
|
||||||
(reduce
|
(reduce
|
||||||
(fn [db [path value]]
|
(fn [db [path value]]
|
||||||
(assoc-in db (into [::forms form :data] path) value))
|
(assoc-in db (into [::forms form :data] path) value))
|
||||||
|
|||||||
@@ -7,6 +7,41 @@
|
|||||||
[re-frame.core :as re-frame]
|
[re-frame.core :as re-frame]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
(defn can-replace-with-default? [accounts]
|
||||||
|
(and (or (not (seq accounts))
|
||||||
|
(<= 1 (count accounts)))
|
||||||
|
(not (get-in accounts [0 :account :id]))))
|
||||||
|
|
||||||
|
(defn default-account [accounts default-account amount]
|
||||||
|
[{:id (str "new-" (random-uuid))
|
||||||
|
:amount (Math/abs amount)
|
||||||
|
:amount-percentage 100
|
||||||
|
:amount-mode "%"
|
||||||
|
:location (or
|
||||||
|
(:location default-account)
|
||||||
|
(get-in accounts [0 :account :location]))
|
||||||
|
:account default-account}])
|
||||||
|
|
||||||
|
|
||||||
|
(defn from-graphql [accounts total locations]
|
||||||
|
(println accounts total locations)
|
||||||
|
(if (seq accounts)
|
||||||
|
(vec (map
|
||||||
|
(fn [a]
|
||||||
|
(-> a
|
||||||
|
(update :amount js/parseFloat)
|
||||||
|
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
|
||||||
|
(Math/abs (js/parseFloat total)))))
|
||||||
|
(assoc :amount-mode "$")))
|
||||||
|
accounts))
|
||||||
|
[{:id (str "new-" (random-uuid))
|
||||||
|
:amount-mode "$"
|
||||||
|
:amount (Math/abs total)
|
||||||
|
:amount-percentage 100
|
||||||
|
:location (if (= 1 (count locations))
|
||||||
|
(first locations)
|
||||||
|
nil)}]))
|
||||||
|
|
||||||
|
|
||||||
;; EVENTS
|
;; EVENTS
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
[auto-ap.subs :as subs]
|
[auto-ap.subs :as subs]
|
||||||
[auto-ap.views.components.dropdown :refer [drop-down]]
|
[auto-ap.views.components.dropdown :refer [drop-down]]
|
||||||
[auto-ap.views.components.typeahead :refer [typeahead]]
|
[auto-ap.views.components.typeahead :refer [typeahead]]
|
||||||
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field recalculate-amounts]]
|
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field recalculate-amounts] :as expense-accounts-field]
|
||||||
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
|
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
|
||||||
[auto-ap.views.utils
|
[auto-ap.views.utils
|
||||||
:refer
|
:refer
|
||||||
@@ -101,19 +101,12 @@
|
|||||||
submit-query)
|
submit-query)
|
||||||
|
|
||||||
;; EVENTS
|
;; EVENTS
|
||||||
(re-frame/reg-event-db
|
|
||||||
::adding
|
|
||||||
(fn [db [_ new]]
|
|
||||||
(-> db (forms/start-form ::form (assoc new :expense-accounts [{:amount 0
|
|
||||||
:id (str "new-" (random-uuid))
|
|
||||||
:amount-percentage 100
|
|
||||||
:amount-mode "%"}])))))
|
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::editing
|
::editing
|
||||||
(fn [db [_ which]]
|
(fn [db [_ which]]
|
||||||
(let [edit-invoice (update which :date #(date->str % standard))
|
(let [edit-invoice (update which :date #(date->str % standard))
|
||||||
edit-invoice (assoc edit-invoice :original edit-invoice)]
|
edit-invoice (assoc edit-invoice :original edit-invoice)
|
||||||
|
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])]
|
||||||
(-> db
|
(-> db
|
||||||
(forms/start-form ::form {:id (:id edit-invoice)
|
(forms/start-form ::form {:id (:id edit-invoice)
|
||||||
:status (:status edit-invoice)
|
:status (:status edit-invoice)
|
||||||
@@ -124,19 +117,9 @@
|
|||||||
:vendor-id (:id (:vendor edit-invoice))
|
:vendor-id (:id (:vendor edit-invoice))
|
||||||
:vendor-name (:name (:vendor edit-invoice))
|
:vendor-name (:name (:vendor edit-invoice))
|
||||||
:client-id (:id (:client edit-invoice))
|
:client-id (:id (:client edit-invoice))
|
||||||
:expense-accounts (if (seq (:expense-accounts which))
|
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
|
||||||
(vec (map
|
(:amount which)
|
||||||
(fn [a]
|
locations)
|
||||||
(-> a
|
|
||||||
(update :amount #(js/parseFloat %))
|
|
||||||
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
|
|
||||||
(Math/abs (js/parseFloat (:total which))))))
|
|
||||||
(assoc :amount-mode "%")))
|
|
||||||
(:expense-accounts edit-invoice)))
|
|
||||||
[{:id (str "new-" (random-uuid))
|
|
||||||
:amount-mode "$"
|
|
||||||
:amount (Math/abs (:total edit-invoice))
|
|
||||||
:amount-percentage 100}])
|
|
||||||
:client-name (:name (:client edit-invoice))})))))
|
:client-name (:name (:client edit-invoice))})))))
|
||||||
|
|
||||||
|
|
||||||
@@ -159,25 +142,18 @@
|
|||||||
field value
|
field value
|
||||||
[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)]}))
|
[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)]}))
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::change-vendor
|
::change-vendor
|
||||||
[(forms/in-form ::form)]
|
[(forms/in-form ::form)]
|
||||||
(fn [{{:keys [data]} :db} [_ location field value]]
|
(fn [{{:keys [data]} :db} [_ field value]]
|
||||||
(let [has-only-one-expense-account? (and value
|
(if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data)))
|
||||||
(or (not (seq (:expense-accounts data)))
|
{:dispatch [::forms/change ::form
|
||||||
(<= 1 (count (:expense-accounts data))))
|
field value
|
||||||
(not (get-in data [:expense-accounts 0 :account :id])))]
|
[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data)
|
||||||
(if has-only-one-expense-account?
|
@(re-frame/subscribe [::subs/vendor-default-account value])
|
||||||
{:dispatch [::forms/change ::form
|
(:amount data))]}
|
||||||
field value
|
{:dispatch [::forms/change ::form field value]})))
|
||||||
[:expense-accounts] [{:id (str "new-" (random-uuid))
|
|
||||||
:amount (:total data)
|
|
||||||
:amount-percentage 100
|
|
||||||
:amount-mode "%"
|
|
||||||
:account @(re-frame/subscribe [::subs/vendor-default-account value])}]]}
|
|
||||||
{:dispatch [::forms/change ::form
|
|
||||||
field value]}))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
@@ -267,9 +243,8 @@
|
|||||||
:disabled exists?
|
:disabled exists?
|
||||||
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
|
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
|
||||||
:field [:vendor-id]
|
:field [:vendor-id]
|
||||||
:text-field [:vendor-name]
|
|
||||||
:text-event change-event
|
:text-event change-event
|
||||||
:event [::change-vendor [::form]]
|
:event [::change-vendor]
|
||||||
:spec (s/nilable ::invoice/vendor-id)
|
:spec (s/nilable ::invoice/vendor-id)
|
||||||
:subscription data}]]]]
|
:subscription data}]]]]
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
(:require [auto-ap.forms :as forms]
|
(:require [auto-ap.forms :as forms]
|
||||||
[auto-ap.subs :as subs]
|
[auto-ap.subs :as subs]
|
||||||
[auto-ap.views.components.typeahead :refer [typeahead]]
|
[auto-ap.views.components.typeahead :refer [typeahead]]
|
||||||
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field]]
|
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field] :as expense-accounts-field]
|
||||||
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
|
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
|
||||||
[auto-ap.views.utils :refer [bind-field dispatch-event]]
|
[auto-ap.views.utils :refer [bind-field dispatch-event]]
|
||||||
[re-frame.core :as re-frame]
|
[re-frame.core :as re-frame]
|
||||||
@@ -57,22 +57,9 @@
|
|||||||
:client-id (:id (:client which))
|
:client-id (:id (:client which))
|
||||||
:vendor-id (:id (:vendor which))
|
:vendor-id (:id (:vendor which))
|
||||||
:vendor-name (:name (:vendor which))
|
:vendor-name (:name (:vendor which))
|
||||||
:accounts (if (seq (:accounts which))
|
:accounts (expense-accounts-field/from-graphql (:accounts which)
|
||||||
(vec (map
|
(:amount which)
|
||||||
(fn [a]
|
locations)})))))
|
||||||
(-> a
|
|
||||||
(update :amount js/parseFloat)
|
|
||||||
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
|
|
||||||
(Math/abs (js/parseFloat (:amount which))))))
|
|
||||||
(assoc :amount-mode "$")))
|
|
||||||
(:accounts which)))
|
|
||||||
[{:id (str "new-" (random-uuid))
|
|
||||||
:amount-mode "$"
|
|
||||||
:amount (Math/abs (:amount which))
|
|
||||||
:amount-percentage 100
|
|
||||||
:location (if (= 1 (count locations))
|
|
||||||
(first locations)
|
|
||||||
nil)}])})))))
|
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
@@ -111,22 +98,14 @@
|
|||||||
::change-vendor
|
::change-vendor
|
||||||
[(forms/in-form ::edit-transaction)]
|
[(forms/in-form ::edit-transaction)]
|
||||||
(fn [{{:keys [data]} :db} [_ field value]]
|
(fn [{{:keys [data]} :db} [_ field value]]
|
||||||
(let [has-only-one-expense-account? (and value
|
(if (and value (expense-accounts-field/can-replace-with-default? (:accounts data)))
|
||||||
(or (not (seq (:accounts data)))
|
{:dispatch [::forms/change ::edit-transaction
|
||||||
(<= 1 (count (:accounts data))))
|
field value
|
||||||
(not (get-in data [:accounts 0 :account :id])))]
|
[:accounts] (expense-accounts-field/default-account (:accounts data)
|
||||||
(if has-only-one-expense-account?
|
@(re-frame/subscribe [::subs/vendor-default-account value])
|
||||||
{:dispatch [::forms/change ::edit-transaction
|
(:amount data))]}
|
||||||
field value
|
{:dispatch [::forms/change ::edit-transaction field value]})))
|
||||||
[:accounts] [{:id (str "new-" (random-uuid))
|
|
||||||
:amount (Math/abs (:amount data))
|
|
||||||
:amount-percentage 100
|
|
||||||
:amount-mode "%"
|
|
||||||
:location (or
|
|
||||||
(:location @(re-frame/subscribe [::subs/vendor-default-account value]))
|
|
||||||
(get-in data [:accounts 0 :account :location]))
|
|
||||||
:account @(re-frame/subscribe [::subs/vendor-default-account value])}]]}
|
|
||||||
{:dispatch [::forms/change ::edit-transaction field value]}))))
|
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::manual-match
|
::manual-match
|
||||||
@@ -149,11 +128,6 @@
|
|||||||
(re-frame/dispatch-sync [::saving edit-completed]))}
|
(re-frame/dispatch-sync [::saving edit-completed]))}
|
||||||
[:h1.title.is-2 "Edit Transaction"]
|
[:h1.title.is-2 "Edit Transaction"]
|
||||||
|
|
||||||
(comment
|
|
||||||
[:div.notification
|
|
||||||
[:p "This transaction matches Invoice 'ABC' for 'DBI Beverages'. " [:a "Create payment and match"] "."]])
|
|
||||||
|
|
||||||
|
|
||||||
[:div.field
|
[:div.field
|
||||||
[:p.help "Merchant"]
|
[:p.help "Merchant"]
|
||||||
[:div.control
|
[:div.control
|
||||||
|
|||||||
Reference in New Issue
Block a user