From afa6b79fcc69dfa5efbd5fdb063774b6000ada7a Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 3 Feb 2023 20:59:52 -0800 Subject: [PATCH] Makes it so vendors and invoices respect allowances. --- src/clj/auto_ap/datomic/migrate.clj | 13 +++-- src/clj/auto_ap/graphql.clj | 7 ++- src/clj/auto_ap/graphql/accounts.clj | 54 +++++++++++++------ .../components/expense_accounts_dialog.cljs | 12 +++-- .../components/expense_accounts_field.cljs | 8 ++- .../views/components/vendor_dialog.cljs | 20 ++++--- .../auto_ap/views/pages/invoices/form.cljs | 1 + 7 files changed, 82 insertions(+), 33 deletions(-) diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index a5de0f36..7f203be3 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -183,7 +183,8 @@ (d/db conn)) (map (fn [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]} - :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/cardinality :db.cardinality/one :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]} - :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]}} diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index e5c9ed83..e430a9d6 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -104,7 +104,8 @@ :account_search_result {:fields {:name {:type 'String} :id {:type :id} - :location {:type 'String}}} + :location {:type 'String} + :warning {:type 'String}}} :yodlee_provider_account {:fields {:id {:type 'Int} @@ -361,6 +362,7 @@ :resolve :search-vendor} :search_account {:type '(list :account_search_result) :args {:query {:type 'String} + :allowance {:type :account_allowance} :client_id {:type :id}} :resolve :search-account} @@ -518,6 +520,9 @@ {:enum-value :square} {:enum-value :uber_eats} {:enum-value :grubhub}]} + + :account_allowance {:values [{:enum-value :vendor} + {:enum-value :invoice}]} :integration_state {:values [{:enum-value :failed} {:enum-value :success} {:enum-value :unauthorized}]} diff --git a/src/clj/auto_ap/graphql/accounts.clj b/src/clj/auto_ap/graphql/accounts.clj index 5c66fe42..7d2598e5 100644 --- a/src/clj/auto_ap/graphql/accounts.clj +++ b/src/clj/auto_ap/graphql/accounts.clj @@ -74,50 +74,74 @@ (->graphql (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 (assert-can-see-client (:id context) client)) (let [query (cleanse-query query) num (some-> (re-find #"([0-9]+)" query) second (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 num - (->> (d/q '[:find ?n (pull ?i [:db/id :account/numeric-code :account/location]) - :in $ ?numeric-code + (->> (d/q '[:find ?n (pull ?i pattern) + :in $ ?numeric-code ?allowance pattern :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) - num) + num + allowance + pattern) (map (fn [[n a]] {:name (str (:account/numeric-code a) " - " n) :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 - :in $ ?q + (->> (d/q '[:find ?n (pull ?i pattern) ?s + :in $ ?q ?allowance pattern :where [(fulltext $ :account/search-terms ?q) [[?i ?n _ ?s]]] [?i :account/numeric-code ?numeric-code] + (or [?i ?allowance :allowance/allowed] + [?i ?allowance :allowance/warn]) (or [?i :account/applicability :account-applicability/global] [?i :account/applicability :account-applicability/optional])] (d/db conn) - query) + query + allowance + pattern) (concat (when client - (d/q '[:find ?n (pull ?a [:db/id :account/numeric-code :account/location]) ?s - :in $ ?c ?q + (d/q '[:find ?n (pull ?a pattern) ?s + :in $ ?c ?q ?allowance pattern :where [?i :account-client-override/client ?c] [(fulltext $ :account-client-override/search-terms ?q) [[?i ?n _ ?s]]] [?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) client - query))) + query + allowance + pattern))) (sort-by (comp - last)) (map (fn [[n a]] {:name (str (:account/numeric-code a) " - " n) :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.")})))) []))) diff --git a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs index 13338f8d..63496c5b 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs @@ -111,16 +111,20 @@ [:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "5em"}}]]] [:tbody - (doall (for [[id _] expense-accounts] + (doall (for [[id a] expense-accounts] ^{:key id} - [:tr + [:tr {:class (when (:warning (:account a)) + "has-background-warning-light")} [:td.expandable [:div.control + (println a) [form-builder/raw-field-v2 {:field [:expense-accounts id :account]} [search-backed-typeahead {:search-query (fn [i] [:search_account {:query i - :client-id (:id client)} - [:name :id :location]])}]]]] + :client-id (:id client) + :allowance :invoice} + [:name :id :location :warning]]) + :class "is-warn"}]]]] (when multi-location? [:td diff --git a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs index 16a4ffa9..4592c24e 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs @@ -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 :schema schema :on-change (fn [expense-accounts original-expense-accounts] @@ -129,8 +129,9 @@ [search-backed-typeahead {:search-query (fn [i] [:search_account {:query i + :allowance allowance :client-id (:id client)} - [:name :id :location]]) + [:name :id :location :warning]]) :disabled disabled}]]]] [:div.column.is-narrow [form-builder/field-v2 {:required? true @@ -142,6 +143,9 @@ locations)) :disabled (boolean (:location account)) :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? [form-builder/raw-field-v2 {:field [index :amount-percentage]} [percentage-field {}]] diff --git a/src/cljs/auto_ap/views/components/vendor_dialog.cljs b/src/cljs/auto_ap/views/components/vendor_dialog.cljs index c2ce51a7..811e8a0d 100644 --- a/src/cljs/auto_ap/views/components/vendor_dialog.cljs +++ b/src/cljs/auto_ap/views/components/vendor_dialog.cljs @@ -162,7 +162,8 @@ (defn form-content [] (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] :id ::vendor-form :schema schema} @@ -231,10 +232,14 @@ "Default" [search-backed-typeahead {:search-query (fn [i] [:search_account - {:query i} - [:name :id]]) - :style {:width "19em"}}]] - + {:query i + :allowance :vendor} + [:name :id :warning]]) + :style {:width "19em"}}] + ] + (when (:warning (:default-account vendor)) + [:div.notification.is-warning.is-light + (:warning (:default-account vendor))]) (when is-admin? [form-builder/field-v2 {:field [:account-overrides]} "Overrides" @@ -248,8 +253,9 @@ [search-backed-typeahead {:search-query (fn [i] [:search_account {:query i - :client_id (:id (:client entity))} - [:name :id]]) + :client_id (:id (:client entity)) + :allowance :vendor} + [:name :id :warning]]) :style {:width "15em"}}]]]) :schema [:sequential account-override-schema] :key-fn :id diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index c094ddc0..ef05a4b6 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -415,6 +415,7 @@ [form-builder/field-v2 {:field :expense-accounts} "Expense Accounts" [expense-accounts-field-v2 {:descriptor "expense account" + :allowance :invoice :locations (:locations (:client data)) :max (:total data) :client (or (:client data) active-client)}]]