From 6ca4a1b0458e31bf83b6ff1826665536aafd5b8e Mon Sep 17 00:00:00 2001 From: Bryce Date: Thu, 2 May 2024 20:05:19 -0700 Subject: [PATCH] Search by financial account --- src/clj/auto_ap/ssr/account.clj | 107 ++++++++++++++++--------------- src/clj/auto_ap/ssr/invoices.clj | 15 ++++- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/clj/auto_ap/ssr/account.clj b/src/clj/auto_ap/ssr/account.clj index 211fe7d1..4a63a128 100644 --- a/src/clj/auto_ap/ssr/account.clj +++ b/src/clj/auto_ap/ssr/account.clj @@ -35,61 +35,62 @@ :name (first name)}))) -(defn account-search [{{:keys [q client-id purpose vendor-id] :as qp} :query-params id :identity}] - (when client-id - (assert-can-see-client id client-id)) - (let [num (some-> (re-find #"([0-9]+)" q) - second - (not-empty) - Integer/parseInt) +(defn account-search [{{:keys [q client-id purpose vendor-id] :as qp} :query-params id :identity :as request}] + (let [client-id (or client-id (:db/id (:client request)))] + (when client-id + (assert-can-see-client id client-id)) + (let [num (some-> (re-find #"([0-9]+)" q) + second + (not-empty) + Integer/parseInt) - valid-allowances (cond-> #{:allowance/allowed - :allowance/warn} - (is-admin? id) (conj :allowance/admin-only)) - allowance (cond (= purpose "vendor") - :account/vendor-allowance - (= purpose "invoice") - :account/invoice-allowance - :else - :account/default-allowance) + valid-allowances (cond-> #{:allowance/allowed + :allowance/warn} + (is-admin? id) (conj :allowance/admin-only)) + allowance (cond (= purpose "vendor") + :account/vendor-allowance + (= purpose "invoice") + :account/invoice-allowance + :else + :account/default-allowance) - vendor-account (when vendor-id - (-> (dc/q '[:find ?da - :in $ ?v - :where [?v :vendor/default-account ?da]] - (dc/db conn) - vendor-id) - ffirst)) - xform (comp - (filter (fn [[_ a]] - (or - (valid-allowances (-> a allowance :db/ident)) - (= (:db/id a) vendor-account)))) - (map (fn [[n a]] - {:label (str (:account/numeric-code a) " - " n) - :value (:db/id a) - :location (:account/location a) - :warning (when (= :allowance/warn (-> a allowance :db/ident)) - "This account is not typically used for this purpose.")})))] - {:body (take 10 (if q - (if num - (->> (dc/q '[:find ?n (pull ?i pattern) - :in $ ?numeric-code ?allowance pattern - :where [?i :account/numeric-code ?numeric-code] - [?i :account/name ?n] - (or [?i :account/applicability :account-applicability/global] - [?i :account/applicability :account-applicability/optional] - [?i :account/applicability :account-applicability/customized])] - (dc/db conn) - num - allowance - search-pattern) - (sequence xform)) - (->> (search- id q client-id) - (sequence - (comp (map (fn [i] [(:name i) (dc/pull (dc/db conn) search-pattern (:account_id i))])) - xform)))) - []))})) + vendor-account (when vendor-id + (-> (dc/q '[:find ?da + :in $ ?v + :where [?v :vendor/default-account ?da]] + (dc/db conn) + vendor-id) + ffirst)) + xform (comp + (filter (fn [[_ a]] + (or + (valid-allowances (-> a allowance :db/ident)) + (= (:db/id a) vendor-account)))) + (map (fn [[n a]] + {:label (str (:account/numeric-code a) " - " n) + :value (:db/id a) + :location (:account/location a) + :warning (when (= :allowance/warn (-> a allowance :db/ident)) + "This account is not typically used for this purpose.")})))] + {:body (take 10 (if q + (if num + (->> (dc/q '[:find ?n (pull ?i pattern) + :in $ ?numeric-code ?allowance pattern + :where [?i :account/numeric-code ?numeric-code] + [?i :account/name ?n] + (or [?i :account/applicability :account-applicability/global] + [?i :account/applicability :account-applicability/optional] + [?i :account/applicability :account-applicability/customized])] + (dc/db conn) + num + allowance + search-pattern) + (sequence xform)) + (->> (search- id q client-id) + (sequence + (comp (map (fn [i] [(:name i) (dc/pull (dc/db conn) search-pattern (:account_id i))])) + xform)))) + []))}))) (def account-search (wrap-json-response (wrap-schema-enforce account-search :query-schema [:map diff --git a/src/clj/auto_ap/ssr/invoices.clj b/src/clj/auto_ap/ssr/invoices.clj index 3be1fbd8..7f21f3ce 100644 --- a/src/clj/auto_ap/ssr/invoices.clj +++ b/src/clj/auto_ap/ssr/invoices.clj @@ -84,6 +84,14 @@ :value (:vendor (:query-params request)) :value-fn :db/id :content-fn :vendor/name})) + (com/field {:label "Account"} + (com/typeahead {:name "account" + :id "account" + :url (bidi/path-for ssr-routes/only-routes :account-search) + :value (:account (:query-params request)) + :value-fn :db/id + :content-fn #(:account/name (d-accounts/clientize (dc/pull (dc/db conn) d-accounts/default-read (:db/id %)) + (:db/id (:client request))))})) (date-range-field* request) (com/field {:label "Check #"} (com/text-input {:name "check-number" @@ -99,7 +107,7 @@ :value (:invoice-number (:query-params request)) :placeholder "e.g., ABC-456" :size :small})) - + (com/field {:label "Amount"} [:div.flex.space-x-4.items-baseline (com/money-input {:name "amount-gte" @@ -179,11 +187,11 @@ :where ['[?e :invoice/vendor ?vendor-id]]} :args [(:db/id (:vendor query-params))]}) - (:account-id query-params) + (:account query-params) (merge-query {:query {:in ['?account-id] :where ['[?e :invoice/expense-accounts ?iea ?] '[?iea :invoice-expense-account/account ?account-id]]} - :args [(:account-id query-params)]}) + :args [(:db/id (:account query-params))]}) (:amount-gte query-params) (merge-query {:query {:in ['?amount-gte] @@ -296,6 +304,7 @@ [:amount-gte {:optional true} [:maybe :double]] [:amount-lte {:optional true} [:maybe :double]] [:vendor {:optional true :default nil} [:maybe [:entity-map {:pull [:db/id :vendor/name]}]]] + [:account {:optional true :default nil} [:maybe [:entity-map {:pull [:db/id :account/name]}]]] [:check-number {:optional true} [:maybe [:string {:decode/string strip}]]] [:invoice-number {:optional true} [:maybe [:string {:decode/string strip}]]] [:status {:optional true} [:maybe (ref->enum-schema "invoice-status")]]