Search by financial account

This commit is contained in:
2024-05-02 20:05:19 -07:00
parent 2a94bcc509
commit 6ca4a1b045
2 changed files with 66 additions and 56 deletions

View File

@@ -35,61 +35,62 @@
:name (first name)}))) :name (first name)})))
(defn account-search [{{:keys [q client-id purpose vendor-id] :as qp} :query-params id :identity}] (defn account-search [{{:keys [q client-id purpose vendor-id] :as qp} :query-params id :identity :as request}]
(when client-id (let [client-id (or client-id (:db/id (:client request)))]
(assert-can-see-client id client-id)) (when client-id
(let [num (some-> (re-find #"([0-9]+)" q) (assert-can-see-client id client-id))
second (let [num (some-> (re-find #"([0-9]+)" q)
(not-empty) second
Integer/parseInt) (not-empty)
Integer/parseInt)
valid-allowances (cond-> #{:allowance/allowed valid-allowances (cond-> #{:allowance/allowed
:allowance/warn} :allowance/warn}
(is-admin? id) (conj :allowance/admin-only)) (is-admin? id) (conj :allowance/admin-only))
allowance (cond (= purpose "vendor") allowance (cond (= purpose "vendor")
:account/vendor-allowance :account/vendor-allowance
(= purpose "invoice") (= purpose "invoice")
:account/invoice-allowance :account/invoice-allowance
:else :else
:account/default-allowance) :account/default-allowance)
vendor-account (when vendor-id vendor-account (when vendor-id
(-> (dc/q '[:find ?da (-> (dc/q '[:find ?da
:in $ ?v :in $ ?v
:where [?v :vendor/default-account ?da]] :where [?v :vendor/default-account ?da]]
(dc/db conn) (dc/db conn)
vendor-id) vendor-id)
ffirst)) ffirst))
xform (comp xform (comp
(filter (fn [[_ a]] (filter (fn [[_ a]]
(or (or
(valid-allowances (-> a allowance :db/ident)) (valid-allowances (-> a allowance :db/ident))
(= (:db/id a) vendor-account)))) (= (:db/id a) vendor-account))))
(map (fn [[n a]] (map (fn [[n a]]
{:label (str (:account/numeric-code a) " - " n) {:label (str (:account/numeric-code a) " - " n)
:value (:db/id a) :value (:db/id a)
:location (:account/location a) :location (:account/location a)
:warning (when (= :allowance/warn (-> a allowance :db/ident)) :warning (when (= :allowance/warn (-> a allowance :db/ident))
"This account is not typically used for this purpose.")})))] "This account is not typically used for this purpose.")})))]
{:body (take 10 (if q {:body (take 10 (if q
(if num (if num
(->> (dc/q '[:find ?n (pull ?i pattern) (->> (dc/q '[:find ?n (pull ?i pattern)
:in $ ?numeric-code ?allowance pattern :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 :account/applicability :account-applicability/global] (or [?i :account/applicability :account-applicability/global]
[?i :account/applicability :account-applicability/optional] [?i :account/applicability :account-applicability/optional]
[?i :account/applicability :account-applicability/customized])] [?i :account/applicability :account-applicability/customized])]
(dc/db conn) (dc/db conn)
num num
allowance allowance
search-pattern) search-pattern)
(sequence xform)) (sequence xform))
(->> (search- id q client-id) (->> (search- id q client-id)
(sequence (sequence
(comp (map (fn [i] [(:name i) (dc/pull (dc/db conn) search-pattern (:account_id i))])) (comp (map (fn [i] [(:name i) (dc/pull (dc/db conn) search-pattern (:account_id i))]))
xform)))) xform))))
[]))})) []))})))
(def account-search (wrap-json-response (wrap-schema-enforce account-search (def account-search (wrap-json-response (wrap-schema-enforce account-search
:query-schema [:map :query-schema [:map

View File

@@ -84,6 +84,14 @@
:value (:vendor (:query-params request)) :value (:vendor (:query-params request))
:value-fn :db/id :value-fn :db/id
:content-fn :vendor/name})) :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) (date-range-field* request)
(com/field {:label "Check #"} (com/field {:label "Check #"}
(com/text-input {:name "check-number" (com/text-input {:name "check-number"
@@ -179,11 +187,11 @@
:where ['[?e :invoice/vendor ?vendor-id]]} :where ['[?e :invoice/vendor ?vendor-id]]}
:args [(:db/id (:vendor query-params))]}) :args [(:db/id (:vendor query-params))]})
(:account-id query-params) (:account query-params)
(merge-query {:query {:in ['?account-id] (merge-query {:query {:in ['?account-id]
:where ['[?e :invoice/expense-accounts ?iea ?] :where ['[?e :invoice/expense-accounts ?iea ?]
'[?iea :invoice-expense-account/account ?account-id]]} '[?iea :invoice-expense-account/account ?account-id]]}
:args [(:account-id query-params)]}) :args [(:db/id (:account query-params))]})
(:amount-gte query-params) (:amount-gte query-params)
(merge-query {:query {:in ['?amount-gte] (merge-query {:query {:in ['?amount-gte]
@@ -296,6 +304,7 @@
[:amount-gte {:optional true} [:maybe :double]] [:amount-gte {:optional true} [:maybe :double]]
[:amount-lte {:optional true} [:maybe :double]] [:amount-lte {:optional true} [:maybe :double]]
[:vendor {:optional true :default nil} [:maybe [:entity-map {:pull [:db/id :vendor/name]}]]] [: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}]]] [:check-number {:optional true} [:maybe [:string {:decode/string strip}]]]
[:invoice-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")]] [:status {:optional true} [:maybe (ref->enum-schema "invoice-status")]]