further cleans up POS
This commit is contained in:
@@ -1,21 +1,31 @@
|
||||
(ns auto-ap.ssr.pos.expected-deposits
|
||||
(:require
|
||||
[auto-ap.datomic.expected-deposit :as d-expected-deposit]
|
||||
[auto-ap.datomic
|
||||
:refer [add-sorter-fields
|
||||
apply-pagination
|
||||
apply-sort-3
|
||||
conn
|
||||
merge-query
|
||||
pull-many
|
||||
query2]]
|
||||
[auto-ap.graphql.utils :refer [extract-client-ids]]
|
||||
[auto-ap.query-params :as query-params]
|
||||
[auto-ap.routes.utils
|
||||
:refer [wrap-client-redirect-unauthenticated wrap-secure]]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.grid-page-helper :as helper]
|
||||
[auto-ap.ssr.pos.common :refer [date-range-field* total-field*]]
|
||||
[auto-ap.ssr.pos.common :refer [date-range-field* total-field* exact-match-id-field*]]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]))
|
||||
[bidi.bidi :as bidi]
|
||||
[clj-time.coerce :as c]
|
||||
[datomic.api :as dc]
|
||||
[auto-ap.client-routes :as client-routes]))
|
||||
|
||||
;; TODO refunds
|
||||
;; always should be fast
|
||||
;; make params parsing composable
|
||||
|
||||
(defn filters [params]
|
||||
(defn filters [request]
|
||||
[:form {"hx-trigger" "change delay:500ms, keyup changed from:.hot-filter delay:1000ms"
|
||||
"hx-get" (bidi/path-for ssr-routes/only-routes
|
||||
:pos-expected-deposit-table)
|
||||
@@ -24,36 +34,112 @@
|
||||
#_#_:hx-disabled-elt "find fieldset"}
|
||||
|
||||
[:fieldset.space-y-6
|
||||
(date-range-field* params)
|
||||
(total-field* params)]])
|
||||
(date-range-field* request)
|
||||
(exact-match-id-field* request)]])
|
||||
|
||||
(defn args->graphql-params [args]
|
||||
{:clients (:clients args)
|
||||
:start (:start (:parsed-query-params args))
|
||||
:sort (:sort (:parsed-query-params args))
|
||||
:per-page (:per-page (:parsed-query-params args))
|
||||
:exact-match-id (some-> (:parsed-query-params args) :exact-match-id Long/parseLong)
|
||||
:date-range {:start (some-> args
|
||||
:parsed-query-params
|
||||
:start-date
|
||||
(atime/parse atime/normal-date))
|
||||
:end (some-> args
|
||||
:parsed-query-params
|
||||
:end-date
|
||||
(atime/parse atime/normal-date))}
|
||||
:total-gte (some-> args :raw-query-params (get "total-gte") not-empty (#(if (string? %) (Double/parseDouble %) (double %))))
|
||||
:total-lte (some-> args :raw-query-params (get "total-lte") not-empty (#(if (string? %) (Double/parseDouble %) (double %))))})
|
||||
(def default-read '[:db/id
|
||||
:expected-deposit/location
|
||||
:expected-deposit/total
|
||||
:expected-deposit/fee
|
||||
[:expected-deposit/date :xform clj-time.coerce/from-date]
|
||||
{:expected-deposit/client [:client/name :db/id :client/code]
|
||||
[:expected-deposit/status :xform iol-ion.query/ident] [:db/ident]
|
||||
:transaction/_expected-deposit [:transaction/date :db/id]}])
|
||||
|
||||
(defn fetch-ids [db request]
|
||||
|
||||
(let [query-params (:parsed-query-params request)
|
||||
valid-clients (extract-client-ids (:clients request)
|
||||
(:client request)
|
||||
(:client-id query-params)
|
||||
(when (:client-code query-params)
|
||||
[:client/code (:client-code query-params)]))
|
||||
query (cond-> {:query {:find []
|
||||
:in ['$ '[?clients ?start-date ?end-date]]
|
||||
:where '[[(iol-ion.query/scan-expected-deposits $ ?clients ?start-date ?end-date) [[?e _ ?sort-default] ...]]]}
|
||||
:args [db [valid-clients
|
||||
(some-> (:start-date query-params) c/to-date)
|
||||
(some-> (:end-date query-params) c/to-date)]]}
|
||||
(:sort query-params) (add-sorter-fields {"client" ['[?e :expected-deposit/client ?c]
|
||||
'[?c :client/name ?sort-client]]
|
||||
"location" ['[?e :expected-deposit/location ?sort-location]]
|
||||
"date" ['[?e :expected-deposit/date ?sort-date]]
|
||||
"total" ['[?e :expected-deposit/total ?sort-total]]
|
||||
"fee" ['[?e :expected-deposit/fee ?sort-fee]]}
|
||||
query-params)
|
||||
|
||||
(:exact-match-id query-params)
|
||||
(merge-query {:query {:in ['?e]
|
||||
:where []}
|
||||
:args [(:exact-match-id query-params)]})
|
||||
|
||||
|
||||
(:total-gte query-params)
|
||||
(merge-query {:query {:in ['?total-gte]
|
||||
:where ['[?e :expected-deposit/total ?a]
|
||||
'[(>= ?a ?total-gte)]]}
|
||||
:args [(:total-gte query-params)]})
|
||||
|
||||
(:total-lte query-params)
|
||||
(merge-query {:query {:in ['?total-lte]
|
||||
:where ['[?e :expected-deposit/total ?a]
|
||||
'[(<= ?a ?total-lte)]]}
|
||||
:args [(:total-lte query-params)]})
|
||||
|
||||
(:total query-params)
|
||||
(merge-query {:query {:in ['?total]
|
||||
:where ['[?e :expected-deposit/total ?expected-deposit-total]
|
||||
'[(iol-ion.query/dollars= ?expected-deposit-total ?total)]]}
|
||||
:args [(:total query-params)]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?sort-default '?e]
|
||||
:where ['[?e :expected-deposit/date ?sort-default]]}}))]
|
||||
|
||||
(cond->> (query2 query)
|
||||
true (apply-sort-3 query-params)
|
||||
true (apply-pagination query-params))))
|
||||
|
||||
(defn hydrate-results [ids db _]
|
||||
(let [results (->> (pull-many db default-read ids)
|
||||
(group-by :db/id))
|
||||
payments (->> ids
|
||||
(map results)
|
||||
(map first)
|
||||
(map (fn get-totals [ed]
|
||||
(assoc ed :totals
|
||||
(->> (dc/q '[:find ?d4 (count ?c) (sum ?a)
|
||||
:in $ ?ed
|
||||
:where [?ed :expected-deposit/charges ?c]
|
||||
[?c :charge/total ?a]
|
||||
[?o :sales-order/charges ?c]
|
||||
[?o :sales-order/date ?d]
|
||||
[(clj-time.coerce/from-date ?d) ?d2]
|
||||
[(auto-ap.time/localize ?d2) ?d3]
|
||||
[(clj-time.coerce/to-local-date ?d3) ?d4]]
|
||||
(dc/db conn)
|
||||
(:db/id ed))
|
||||
(map (fn [[date count amount]]
|
||||
{:date (c/to-date-time date)
|
||||
:count count
|
||||
:amount amount})))))))]
|
||||
payments))
|
||||
|
||||
(defn fetch-page [args]
|
||||
(let [db (dc/db conn)
|
||||
{ids-to-retrieve :ids matching-count :count} (fetch-ids db args)]
|
||||
|
||||
[(->> (hydrate-results ids-to-retrieve db args))
|
||||
matching-count]))
|
||||
|
||||
(def grid-page {:id "expected-deposit-table"
|
||||
:nav (com/main-aside-nav)
|
||||
:page-specific-nav filters
|
||||
:id-fn :db/id
|
||||
:fetch-page (fn [_ args]
|
||||
(d-expected-deposit/get-graphql
|
||||
(args->graphql-params args)))
|
||||
:fetch-page fetch-page
|
||||
:oob-render
|
||||
(fn [_ params]
|
||||
[(assoc-in (date-range-field* params) [1 :hx-swap-oob] true)])
|
||||
(fn [request]
|
||||
[(assoc-in (date-range-field* request) [1 :hx-swap-oob] true)])
|
||||
:breadcrumbs [[:a {:href (bidi/path-for ssr-routes/only-routes
|
||||
:company)}
|
||||
"POS"]
|
||||
@@ -64,19 +150,18 @@
|
||||
:title "Expected deposits"
|
||||
:entity-name "Expected deposit"
|
||||
:route :pos-expected-deposit-table
|
||||
:action-buttons (fn [_ args]
|
||||
#_(let [{:keys [total tax]} (d-sales/summarize-graphql (args->graphql-params args))]
|
||||
[
|
||||
(com/pill {:color :primary}
|
||||
(format "Total $%.2f" total)
|
||||
)
|
||||
(com/pill {:color :secondary}
|
||||
(format "Tax $%.2f" tax )
|
||||
)]))
|
||||
:action-buttons (fn [request]
|
||||
)
|
||||
:row-buttons (fn [_ e]
|
||||
(when (:expected-deposit/reference-link e)
|
||||
[(com/a-icon-button {:href (:expected-deposit/reference-link e)}
|
||||
svg/external-link)]))
|
||||
[
|
||||
(when (:expected-deposit/reference-link e)
|
||||
(com/a-icon-button {:href (:expected-deposit/reference-link e)}
|
||||
svg/external-link))
|
||||
(when-let [transaction-id (-> e (:transaction/_expected-deposit) first :db/id)]
|
||||
(com/a-button {:href (str (bidi/path-for client-routes/routes
|
||||
:transactions)
|
||||
"?exact-match-id="
|
||||
transaction-id)} "Transaction"))])
|
||||
:headers [{:key "client"
|
||||
:name "Client"
|
||||
:sort-key "client"
|
||||
@@ -102,8 +187,19 @@
|
||||
|
||||
(def row* (partial helper/row* grid-page))
|
||||
(def table* (partial helper/table* grid-page))
|
||||
(def table (partial helper/table grid-page))
|
||||
(def page (partial helper/page grid-page))
|
||||
(def table (-> (partial helper/table grid-page)
|
||||
(query-params/wrap-parse-query-params
|
||||
(comp
|
||||
(query-params/parse-key :total-gte query-params/parse-double)
|
||||
(query-params/parse-key :total-lte query-params/parse-double)
|
||||
(helper/default-parse-query-params grid-page)))))
|
||||
|
||||
(def page (-> (partial helper/page grid-page)
|
||||
(query-params/wrap-parse-query-params
|
||||
(comp
|
||||
(query-params/parse-key :total-gte query-params/parse-double)
|
||||
(query-params/parse-key :total-lte query-params/parse-double)
|
||||
(helper/default-parse-query-params grid-page)))))
|
||||
|
||||
(def key->handler
|
||||
{:pos-expected-deposits (wrap-client-redirect-unauthenticated (wrap-secure page))
|
||||
|
||||
Reference in New Issue
Block a user