Makes it so vendors and invoices respect allowances.

This commit is contained in:
2023-02-03 20:59:52 -08:00
parent 1ecd076ba1
commit afa6b79fcc
7 changed files with 82 additions and 33 deletions

View File

@@ -183,7 +183,8 @@
(d/db conn)) (d/db conn))
(map (fn [i] (map (fn [i]
{:db/id i {:db/id i
:account/vendor-allowance :allowance/allowed}))) :account/vendor-allowance :allowance/allowed
:account/default-allowance :allowance/allowed})))
]) ])
@@ -568,15 +569,19 @@
:requires [:auto-ap/add-account-options]} :requires [:auto-ap/add-account-options]}
:auto-ap/add-vendor-account-options {:txes [[{:db/ident :account/vendor-allowance :auto-ap/add-vendor-account-options2 {:txes [[{:db/ident :account/vendor-allowance
:db/valueType :db.type/ref :db/valueType :db.type/ref
:db/cardinality :db.cardinality/one :db/cardinality :db.cardinality/one
:db/doc "Whether this account can be used for vendors"} :db/doc "Whether this account can be used for vendors"}
{:db/ident :account/default-allowance
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Whether this account can be used generally"}
]] ]]
:requires [:auto-ap/backfill-account-options]} :requires [:auto-ap/backfill-account-options]}
:auto-ap/backfill-account-options2 {:txes-fn `backfill-account-options2 :auto-ap/backfill-account-options3 {:txes-fn `backfill-account-options2
:requires [:auto-ap/add-vendor-account-options]}} :requires [:auto-ap/add-vendor-account-options2]}}

View File

@@ -104,7 +104,8 @@
:account_search_result :account_search_result
{:fields {:name {:type 'String} {:fields {:name {:type 'String}
:id {:type :id} :id {:type :id}
:location {:type 'String}}} :location {:type 'String}
:warning {:type 'String}}}
:yodlee_provider_account :yodlee_provider_account
{:fields {:id {:type 'Int} {:fields {:id {:type 'Int}
@@ -361,6 +362,7 @@
:resolve :search-vendor} :resolve :search-vendor}
:search_account {:type '(list :account_search_result) :search_account {:type '(list :account_search_result)
:args {:query {:type 'String} :args {:query {:type 'String}
:allowance {:type :account_allowance}
:client_id {:type :id}} :client_id {:type :id}}
:resolve :search-account} :resolve :search-account}
@@ -518,6 +520,9 @@
{:enum-value :square} {:enum-value :square}
{:enum-value :uber_eats} {:enum-value :uber_eats}
{:enum-value :grubhub}]} {:enum-value :grubhub}]}
:account_allowance {:values [{:enum-value :vendor}
{:enum-value :invoice}]}
:integration_state {:values [{:enum-value :failed} :integration_state {:values [{:enum-value :failed}
{:enum-value :success} {:enum-value :success}
{:enum-value :unauthorized}]} {:enum-value :unauthorized}]}

View File

@@ -74,50 +74,74 @@
(->graphql (->graphql
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"]))))))) (d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
(defn search [context {query :query client :client_id} _] (defn search [context {query :query client :client_id allowance :allowance} _]
(when client (when client
(assert-can-see-client (:id context) client)) (assert-can-see-client (:id context) client))
(let [query (cleanse-query query) (let [query (cleanse-query query)
num (some-> (re-find #"([0-9]+)" query) num (some-> (re-find #"([0-9]+)" query)
second second
(not-empty ) (not-empty )
Integer/parseInt)] Integer/parseInt)
pattern [:db/id :account/numeric-code :account/location {:account/vendor-allowance [:db/ident]} {:account/invoice-allowance [:db/ident]}]
allowance (cond (= allowance :vendor)
:account/vendor-allowance
(= allowance :invoice)
:account/invoice-allowance
:else
:account/default-allowance)]
(if query (if query
(if num (if num
(->> (d/q '[:find ?n (pull ?i [:db/id :account/numeric-code :account/location]) (->> (d/q '[:find ?n (pull ?i pattern)
:in $ ?numeric-code :in $ ?numeric-code ?allowance pattern
:where [?i :account/numeric-code ?numeric-code] :where [?i :account/numeric-code ?numeric-code]
[?i :account/name ?n]] [?i :account/name ?n]
(or [?i ?allowance :allowance/allowed]
[?i ?allowance :allowance/warn])]
(d/db conn) (d/db conn)
num) num
allowance
pattern)
(map (fn [[n a]] (map (fn [[n a]]
{:name (str (:account/numeric-code a) " - " n) {:name (str (:account/numeric-code a) " - " n)
:id (:db/id a) :id (:db/id a)
:location (:account/location a)}))) :location (:account/location a)
:warning (when (= :allowance/warn (-> a allowance :db/ident))
"This account is not typically used for this purpose.")})))
(->> (d/q '[:find ?n (pull ?i [:db/id :account/numeric-code :account/location]) ?s (->> (d/q '[:find ?n (pull ?i pattern) ?s
:in $ ?q :in $ ?q ?allowance pattern
:where [(fulltext $ :account/search-terms ?q) [[?i ?n _ ?s]]] :where [(fulltext $ :account/search-terms ?q) [[?i ?n _ ?s]]]
[?i :account/numeric-code ?numeric-code] [?i :account/numeric-code ?numeric-code]
(or [?i ?allowance :allowance/allowed]
[?i ?allowance :allowance/warn])
(or [?i :account/applicability :account-applicability/global] (or [?i :account/applicability :account-applicability/global]
[?i :account/applicability :account-applicability/optional])] [?i :account/applicability :account-applicability/optional])]
(d/db conn) (d/db conn)
query) query
allowance
pattern)
(concat (when client (concat (when client
(d/q '[:find ?n (pull ?a [:db/id :account/numeric-code :account/location]) ?s (d/q '[:find ?n (pull ?a pattern) ?s
:in $ ?c ?q :in $ ?c ?q ?allowance pattern
:where :where
[?i :account-client-override/client ?c] [?i :account-client-override/client ?c]
[(fulltext $ :account-client-override/search-terms ?q) [[?i ?n _ ?s]]] [(fulltext $ :account-client-override/search-terms ?q) [[?i ?n _ ?s]]]
[?a :account/client-overrides ?i] [?a :account/client-overrides ?i]
[?a :account/numeric-code ?numeric-code]] [?a :account/numeric-code ?numeric-code]
(or [?a ?allowance :allowance/allowed]
[?a ?allowance :allowance/warn])]
(d/db conn) (d/db conn)
client client
query))) query
allowance
pattern)))
(sort-by (comp - last)) (sort-by (comp - last))
(map (fn [[n a]] (map (fn [[n a]]
{:name (str (:account/numeric-code a) " - " n) {:name (str (:account/numeric-code a) " - " n)
:id (:db/id a) :id (:db/id a)
:location (:account/location a)})))) :location (:account/location a)
:warning (when (= :allowance/warn (-> a allowance :db/ident))
"This account is not typically used for this purpose.")}))))
[]))) [])))

View File

@@ -111,16 +111,20 @@
[:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]] [:th {:style {:width "5em"}}]]]
[:tbody [:tbody
(doall (for [[id _] expense-accounts] (doall (for [[id a] expense-accounts]
^{:key id} ^{:key id}
[:tr [:tr {:class (when (:warning (:account a))
"has-background-warning-light")}
[:td.expandable [:div.control [:td.expandable [:div.control
(println a)
[form-builder/raw-field-v2 {:field [:expense-accounts id :account]} [form-builder/raw-field-v2 {:field [:expense-accounts id :account]}
[search-backed-typeahead {:search-query (fn [i] [search-backed-typeahead {:search-query (fn [i]
[:search_account [:search_account
{:query i {:query i
:client-id (:id client)} :client-id (:id client)
[:name :id :location]])}]]]] :allowance :invoice}
[:name :id :location :warning]])
:class "is-warn"}]]]]
(when multi-location? (when multi-location?
[:td [:td

View File

@@ -74,7 +74,7 @@
(defn expense-accounts-field-v2 [{value :value on-change :on-change expense-accounts :value client :client max-value :max locations :locations disabled :disabled percentage-only? :percentage-only? :or {percentage-only? false}}] (defn expense-accounts-field-v2 [{value :value on-change :on-change allowance :allowance expense-accounts :value client :client max-value :max locations :locations disabled :disabled percentage-only? :percentage-only? :or {percentage-only? false}}]
[form-builder/virtual-builder {:value value [form-builder/virtual-builder {:value value
:schema schema :schema schema
:on-change (fn [expense-accounts original-expense-accounts] :on-change (fn [expense-accounts original-expense-accounts]
@@ -129,8 +129,9 @@
[search-backed-typeahead {:search-query (fn [i] [search-backed-typeahead {:search-query (fn [i]
[:search_account [:search_account
{:query i {:query i
:allowance allowance
:client-id (:id client)} :client-id (:id client)}
[:name :id :location]]) [:name :id :location :warning]])
:disabled disabled}]]]] :disabled disabled}]]]]
[:div.column.is-narrow [:div.column.is-narrow
[form-builder/field-v2 {:required? true [form-builder/field-v2 {:required? true
@@ -142,6 +143,9 @@
locations)) locations))
:disabled (boolean (:location account)) :disabled (boolean (:location account))
:allow-nil? true}]]]]] :allow-nil? true}]]]]]
(when-let [warning (:warning account)]
[:div.notification.is-warning.is-small.is-light {:style {:padding "0.5rem 1.25rem"}}
warning])
(if percentage-only? (if percentage-only?
[form-builder/raw-field-v2 {:field [index :amount-percentage]} [form-builder/raw-field-v2 {:field [index :amount-percentage]}
[percentage-field {}]] [percentage-field {}]]

View File

@@ -162,7 +162,8 @@
(defn form-content [] (defn form-content []
(let [is-admin? @(re-frame/subscribe [::subs/is-admin?]) (let [is-admin? @(re-frame/subscribe [::subs/is-admin?])
clients @(re-frame/subscribe [::subs/client-refs])] clients @(re-frame/subscribe [::subs/client-refs])
{vendor :data} @(re-frame/subscribe [::forms/form ::vendor-form])]
[form-builder/builder {:submit-event [::save] [form-builder/builder {:submit-event [::save]
:id ::vendor-form :id ::vendor-form
:schema schema} :schema schema}
@@ -231,10 +232,14 @@
"Default" "Default"
[search-backed-typeahead {:search-query (fn [i] [search-backed-typeahead {:search-query (fn [i]
[:search_account [:search_account
{:query i} {:query i
[:name :id]]) :allowance :vendor}
:style {:width "19em"}}]] [:name :id :warning]])
:style {:width "19em"}}]
]
(when (:warning (:default-account vendor))
[:div.notification.is-warning.is-light
(:warning (:default-account vendor))])
(when is-admin? (when is-admin?
[form-builder/field-v2 {:field [:account-overrides]} [form-builder/field-v2 {:field [:account-overrides]}
"Overrides" "Overrides"
@@ -248,8 +253,9 @@
[search-backed-typeahead {:search-query (fn [i] [search-backed-typeahead {:search-query (fn [i]
[:search_account [:search_account
{:query i {:query i
:client_id (:id (:client entity))} :client_id (:id (:client entity))
[:name :id]]) :allowance :vendor}
[:name :id :warning]])
:style {:width "15em"}}]]]) :style {:width "15em"}}]]])
:schema [:sequential account-override-schema] :schema [:sequential account-override-schema]
:key-fn :id :key-fn :id

View File

@@ -415,6 +415,7 @@
[form-builder/field-v2 {:field :expense-accounts} [form-builder/field-v2 {:field :expense-accounts}
"Expense Accounts" "Expense Accounts"
[expense-accounts-field-v2 {:descriptor "expense account" [expense-accounts-field-v2 {:descriptor "expense account"
:allowance :invoice
:locations (:locations (:client data)) :locations (:locations (:client data))
:max (:total data) :max (:total data)
:client (or (:client data) active-client)}]] :client (or (:client data) active-client)}]]