makes all sales orders use new grid helper

This commit is contained in:
2023-09-27 23:52:57 -07:00
parent da6e492b4d
commit fddd3d9007
14 changed files with 926 additions and 122 deletions

View File

@@ -0,0 +1,185 @@
(ns auto-ap.ssr.pos.refunds
(:require
[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.routes.utils
:refer [wrap-client-redirect-unauthenticated wrap-secure]]
[auto-ap.ssr.pos.common :refer [date-range-field* processor-field* total-field*]]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.components :as com]
[auto-ap.ssr.grid-page-helper :as helper]
[auto-ap.ssr.svg :as svg]
[auto-ap.time :as atime]
[bidi.bidi :as bidi]
[clj-time.coerce :as c]
[datomic.api :as dc]
[clojure.set :as set]))
;; TODO refunds
;; always should be fast
;; make params parsing composable
(defn filters [params]
[:form {"hx-trigger" "change delay:500ms, keyup changed from:.hot-filter delay:1000ms"
"hx-get" (bidi/path-for ssr-routes/only-routes
:pos-refund-table)
"hx-target" "#refund-table"
"hx-indicator" "#refund-table"
#_#_:hx-disabled-elt "find fieldset"}
[:fieldset.space-y-6
(date-range-field* params)
(total-field* params)]])
(defn <-datomic [result]
(cond-> result
true (update :sales-refund/date c/from-date)))
(def default-read '[*
{:sales-refund/client [:client/name :db/id :client/code]}])
(defn fetch-ids [db args]
(let [query-params (:parsed-query-params args)
valid-clients (extract-client-ids (:clients args)
(:client-id query-params)
(when (:client-code query-params)
[:client/code (:client-code query-params)]))
valid-clients (->> valid-clients
(take 10)
set)
start-date (some-> args
:parsed-query-params
:start-date
(atime/parse atime/normal-date))
end-date (some-> args
:parsed-query-params
:end-date
(atime/parse atime/normal-date))
total-gte (some-> args :parsed-query-params :total-gte not-empty (#(if (string? %) (Double/parseDouble %) (double %))))
total-lte (some-> args :parsed-query-params :total-lte not-empty (#(if (string? %) (Double/parseDouble %) (double %))))
query (cond-> {:query {:find []
:in ['$ '[?clients ?start-date ?end-date]]
:where '[[(iol-ion.query/scan-sales-refunds $ ?clients ?start-date ?end-date) [[?e _ ?sort-default] ...]]]}
:args [db [valid-clients
(some-> start-date c/to-date)
(some-> end-date c/to-date )]]}
(:sort query-params) (add-sorter-fields {"client" ['[?e :sales-refund/client ?c]
'[?c :client/name ?sort-client]]
"date" ['[?e :sales-refund/date ?sort-date]]
"total" ['[?e :sales-refund/total ?sort-total]]
"fee" ['[?e :sales-refund/fee ?sort-tip]]
"type" ['[?e :sales-refund/type ?type]]}
query-params)
(:exact-match-id query-params)
(merge-query {:query {:in ['?e]
:where []}
:args [(:exact-match-id query-params)]})
total-gte
(merge-query {:query {:in ['?total-gte]
:where ['[?e :sales-refund/total ?a]
'[(>= ?a ?total-gte)]]}
:args [total-gte]})
total-lte
(merge-query {:query {:in ['?total-lte]
:where ['[?e :sales-refund/total ?a]
'[(<= ?a ?total-lte)]]}
:args [total-lte]})
start-date
(merge-query {:query {:in '[?start-date]
:where ['[?e :sales-refund/date ?date]
'[(>= ?date ?start-date)]]}
:args [(c/to-date start-date)]})
end-date
(merge-query {:query {:in '[?end-date]
:where ['[?e :sales-refund/date ?date]
'[(<= ?date ?end-date)]]}
:args [(c/to-date end-date)]})
true
(merge-query {:query {:find ['?sort-default '?e]
:where ['[?e :sales-refund/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))
refunds (->> ids
(map results)
(map first)
(mapv <-datomic)
)]
refunds))
(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 "refund-table"
:nav (com/main-aside-nav)
:page-specific-nav filters
:id-fn :db/id
:fetch-page fetch-page
:oob-render
(fn [_ params]
[(assoc-in (date-range-field* params) [1 :hx-swap-oob] true)])
:breadcrumbs [[:a {:href (bidi/path-for ssr-routes/only-routes
:company)}
"POS"]
[:a {:href (bidi/path-for ssr-routes/only-routes
:pos-refunds)}
"Refunds"]]
:title "Refunds"
:entity-name "Refund"
:route :pos-refund-table
:action-buttons (fn [_ args])
:row-buttons (fn [_ e])
:headers [{:key "client"
:name "Client"
:sort-key "client"
:hide? (fn [args]
(= (count (:clients args)) 1))
:render #(-> % :sales-refund/client :client/code)}
{:key "date"
:name "Date"
:sort-key "date"
:render #(atime/unparse-local (:sales-refund/date %) atime/standard-time)}
{:key "total"
:name "Total"
:sort-key "total"
:render #(some->> % :sales-refund/total (format "$%.2f"))}
{:key "type"
:name "Type"
:sort-key "type"
:render :sales-refund/type}
{:key "fee"
:name "Fee"
:sort-key "fee"
:render #(some->> % :sales-refund/fee (format "$%.2f"))}]})
(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 key->handler
{:pos-refunds (wrap-client-redirect-unauthenticated (wrap-secure page))
:pos-refund-table (wrap-client-redirect-unauthenticated (wrap-secure table))})