From be58bcd4a329814823605f7a0c2b6df0194390fa Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 4 Jul 2022 07:55:10 -0700 Subject: [PATCH] shows expected deposit totals. --- .../alternate-linking-settlements-square.clj | 175 ++++++++++++++++++ .../how-to-make-sales-queries-faster.clj | 103 +++++++++++ src/clj/auto_ap/datomic/expected_deposit.clj | 22 ++- src/clj/auto_ap/graphql/expected_deposit.clj | 22 ++- .../views/pages/pos/expected_deposits.cljs | 3 +- .../pages/pos/expected_deposits/table.cljs | 70 ++++--- 6 files changed, 353 insertions(+), 42 deletions(-) create mode 100644 scratch-sessions/alternate-linking-settlements-square.clj create mode 100644 scratch-sessions/how-to-make-sales-queries-faster.clj diff --git a/scratch-sessions/alternate-linking-settlements-square.clj b/scratch-sessions/alternate-linking-settlements-square.clj new file mode 100644 index 00000000..606f8d90 --- /dev/null +++ b/scratch-sessions/alternate-linking-settlements-square.clj @@ -0,0 +1,175 @@ +;; This buffer is for Clojure experiments and evaluation. + +;; Press C-j to evaluate the last expression. + +;; You can also press C-u C-j to evaluate the expression and pretty-print its result. + +(doseq [z (->> (d/q '[:find [?e ...] + :in $ + :where (or-join [?e] + (and + [?e :sales-order/client [:client/code "NGOP"]] + [?e :sales-order/date ?d] + [(>= ?d #inst "2022-06-19")]) + (and + [?e :expected-deposit/client [:client/code "NGOP"]] + [?e :expected-deposit/date ?d] + [(>= ?d #inst "2022-06-19")]))] + (d/db auto-ap.datomic/conn)) + (map (fn [x] [:db/retractEntity x])) + (partition-all 20))] + @(d/transact auto-ap.datomic/conn z) + ) + +(doseq [client (get-square-clients "NGOP") + square-location (:client/square-locations client) + :when (:square-location/client-location square-location)] + (upsert client square-location (time/plus (time/now) (time/days -13)) (time/now))) + + + + +(require '[datomic.api :as d]) + +(user/init-repl) + +(d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) + :with ?charge + :in $ + :where + [?c :client/code "NGOP"] + [?s :sales-order/client ?c] + [?s :sales-order/date ?date] + [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 10)))) ?min-d] + [(>= ?date ?min-d)] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name ?type] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + (or + + (and [_ :expected-deposit/charges ?charge ] + [(ground :settlement) ?ccp] + [(ground :settlement) ?p]) + (and + (not [_ :expected-deposit/charges ?charge]) + [(get-else $ ?charge :charge/processor :na) ?ccp] + [(get-else $ ?ccp :db/ident :na) ?p] + )) + [(name ?p) ?p2] + [(clj-time.coerce/to-date-time ?date) ?d2] + [(auto-ap.time/localize ?d2) ?d3] + [(auto-ap.time/unparse-local ?d3 auto-ap.time/normal-date) ?d4]] + (d/db auto-ap.datomic/conn)) + +(user/init-repl) + + +(->> (d/q '[:find ?date (pull ?s [:sales-order/external-id ]) (pull ?charge [*]) + :in $ + :where + [?c :client/code "NGOP"] + [?s :sales-order/client ?c] + [?s :sales-order/date ?date] + [(<= #inst "2022-06-22T00:00:00-07:00" ?date)] + [(< ?date #inst "2022-06-23T00:00:00-07:00" )] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name "CARD"] + (not [_ :expected-deposit/charges ?charge])] + (d/db auto-ap.datomic/conn)) + #_(map second) + #_(map :sales-order/external-id) + #_(map #(last (str/split % #"-"))) + #_(take 5) + #_(map (fn [x] + (auto-ap.square.core/get-order (first (auto-ap.square.core/get-square-clients "NGOP")) + (first (filter :square-location/client-location (:client/square-locations (first (auto-ap.square.core/get-square-clients "NGOP"))))) + + x) + )) + + + ) + +(user/init-repl) + +(auto-ap.square.core/get-payment (first (auto-ap.square.core/get-square-clients "NGOP")) "VMTHRlak2el5DRd9EDzdBc9p6NRZY") + +(auto-ap.square.core/get-order (first (auto-ap.square.core/get-square-clients "NGOP")) + (first (filter :square-location/client-location (:client/square-locations (first (auto-ap.square.core/get-square-clients "NGOP"))))) + + "oiUPZkjiUHZotszGBL5ErZlJd4JZY") + +fXG1gjzTIVoAyBybf4D4r2IOvaB +D7piioWfWorhmO7a8ecER3N5taB +50QOwfhkUaiPZuvs0oCeoXSEuaB + +(auto-ap.square.core/settlements (first (auto-ap.square.core/get-square-clients "NGOP")) + (first (filter :square-location/client-location (:client/square-locations (first (auto-ap.square.core/get-square-clients "NGOP")))))) + +(auto-ap.square.core/get-settlement-details (first (auto-ap.square.core/get-square-clients "NGOP")) + (first (filter :square-location/client-location (:client/square-locations (first (auto-ap.square.core/get-square-clients "NGOP"))))) + ["3Z701Y9EZKDC8YS863BY12E701TS"]) + +(doseq [client (get-square-clients "NGOP") + square-location (:client/square-locations client) + :when (:square-location/client-location square-location) + settlement (settlements client square-location [["2022-06-25" "2022-06-30"]] )] + + (println (str "square/settlement/" (:id settlement))) + @(d/transact + auto-ap.datomic/conn + + [#:expected-deposit {:external-id (str "square/settlement/" (:id settlement)) + :vendor :vendor/ccp-square + :status :expected-deposit-status/pending + :total (amount->money (:total_money settlement)) + :client (:db/id client) + :location (:square-location/client-location square-location) + :fee (- (reduce + 0.0 (map (fn [entry] + (if (= (:type entry) "REFUND") + (- (amount->money (:fee_money entry))) + (amount->money (:fee_money entry)))) + (:entries settlement)))) + :date (-> (:initiated_at settlement) + (coerce/to-date)) + :sales-date (or (:sales-date settlement) + (-> (:initiated_at settlement) + (coerce/to-date))) + :charges (->> (:entries settlement) + (filter :payment_id) + (map (fn [p] {:charge/external-id (str "square/charge/" (:payment_id p))})))}]) + ) + + + + +(user/init-repl) + +(let [client-id (d-clients/code->id "NGGG") + query [[:all-accounts + [:id :numeric_code :type :applicability :location :name [:client_overrides [:name [:client [:id :code :name]]]]]]] + all-accounts (graphql/query {:user/role "admin"} (venia/graphql-query {:venia/queries (->graphql query)}))] + + all-accounts + #_(list (transduce + (comp + (filter (fn [a] + (let [overriden-clients (set (map (comp :id :client) (:client-overrides a)))] + (or (nil? (:applicability a)) + (= :global (:applicability a)) + (overriden-clients (str client-id)))))) + (map (fn [a] + (let [client->name (reduce + (fn [override co] + (assoc override (str (:id (:client co))) (:name co))) + {} + (:client-overrides a))] + (-> a + (assoc :global-name (:name a)) + (assoc :client-name (client->name (str client-id) (:name a))) + (dissoc :client-overrides)))))) + conj + (list) + (:all-accounts (:data all-accounts))))) + diff --git a/scratch-sessions/how-to-make-sales-queries-faster.clj b/scratch-sessions/how-to-make-sales-queries-faster.clj new file mode 100644 index 00000000..ae2a5229 --- /dev/null +++ b/scratch-sessions/how-to-make-sales-queries-faster.clj @@ -0,0 +1,103 @@ +;; This buffer is for Clojure experiments and evaluation. + +;; Press C-j to evaluate the last expression. + +;; You can also press C-u C-j to evaluate the expression and pretty-print its result. + +(user/init-repl) + +(d/q '[:find ?c + :in $ + :where + [_ :expected-deposit/charges ?c] + [_ :sales-order/charges ?c] + ] + (d/db auto-ap.datomic/conn)) + +(take 4 (d/q '[:find (pull ?ed [* {:expected-deposit/charges [*] :expected-deposit/client [:client/code]}]) + :in $ + :where + [?ed :expected-deposit/sales-date ?d] + [(>= ?d #inst "2022-06-24")] + [?ed :expected-deposit/charges ?c] + ] + (d/db auto-ap.datomic/conn))) + +(println "test") + +(let [query-id "54b9bf6b-99d3-408f-b696-96f255f0f073" + obj (s3/get-object :bucket-name (:data-bucket env) + :key (str "queries/" query-id)) + query-string (str (slurp (:object-content obj)))] + (log/info obj) + (println query-string)) + +(do (time (execute-query {} {:query-id "54b9bf6b-99d3-408f-b696-96f255f0f073"})) + (println "done")) + +(execute-query {} {:query-id "54b9bf6b-99d3-408f-b696-96f255f0f073"}) + + +(d/q '[:find ?d4 ?n ?n2 (sum ?total) (sum ?tax) (sum ?discount) + :with ?s ?li + :in $ + :where + [?s :sales-order/client [:client/code "NGVZ"]] + [?s :sales-order/date ?d] + [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 30)))) ?min-d] + [(>= ?d ?min-d)] + [?s :sales-order/line-items ?li] + [?li :order-line-item/category ?n] + [(get-else $ ?li :order-line-item/item-name "") ?n2] + [?li :order-line-item/total ?total] + [?li :order-line-item/tax ?tax] + [?li :order-line-item/discount ?discount] + [(clj-time.coerce/to-date-time ?d) ?d2] + [(auto-ap.time/localize ?d2) ?d3] + [(auto-ap.time/unparse-local ?d3 auto-ap.time/normal-date) ?d4] + ] + (d/db auto-ap.datomic/conn)) + +(d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) + :with ?charge + :in $ + :where + [?c :client/code "NGOP"] + [?s :sales-order/client ?c] + [?s :sales-order/date ?date] + [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 3)))) ?min-d] + [(>= ?date ?min-d)] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name ?type] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + (or-join [?charge ?p2] + + (and [_ :expected-deposit/charges ?charge ] + [(ground "settlement") ?p2]) + (and + [(get-else $ ?charge :charge/processor :na) ?ccp] + [(get-else $ ?ccp :db/ident :na) ?p] + [(name ?p) ?p2])) + + [(clj-time.coerce/to-date-time ?date) ?d2] + [(auto-ap.time/localize ?d2) ?d3] + [(auto-ap.time/unparse-local ?d3 auto-ap.time/normal-date) ?d4]] + (d/db auto-ap.datomic/conn)) + + +(d/q '[:find (pull ?c [*]) + :in $ ?ed + :where [?ed :expected-deposit/charges ?c] + #_[_ :sales-order/charges ?c]] + (d/db auto-ap.datomic/conn) + 17592266347939) + +(auto-ap.square.core/get-payment (first (auto-ap.square.core/get-square-clients "NGOP")) + "lWSahgO1aZMK6tb5Jv3t3SgzvaB") + +(auto-ap.square.core/get-order + (first (auto-ap.square.core/get-square-clients "NGOP")) + (first (filter :square-location/client-location (:client/square-locations (auto-ap.square.core/get-square-clients "NGOP")))) + "h0tVO9MK5Eh7Wpj3TEHVjX6eV") + diff --git a/src/clj/auto_ap/datomic/expected_deposit.clj b/src/clj/auto_ap/datomic/expected_deposit.clj index 02a4c678..6f894ee5 100644 --- a/src/clj/auto_ap/datomic/expected_deposit.clj +++ b/src/clj/auto_ap/datomic/expected_deposit.clj @@ -2,7 +2,8 @@ (:require [auto-ap.datomic :refer [add-sorter-fields apply-pagination apply-sort-3 merge-query conn]] [auto-ap.graphql.utils :refer [limited-clients]] [clj-time.coerce :as c] - [datomic.api :as d])) + [datomic.api :as d] + [clj-time.core :as t])) (defn <-datomic [result] @@ -97,7 +98,24 @@ payments (->> ids (map results) (map first) - (mapv <-datomic))] + (mapv <-datomic) + (map (fn get-totals [ed] + (assoc ed :totals + (->> (d/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]] + (d/db conn) + (:db/id ed)) + (map (fn [[date count amount]] + {:date (c/to-date-time date) + :count count + :amount amount})))))))] payments)) (defn get-graphql [args] diff --git a/src/clj/auto_ap/graphql/expected_deposit.clj b/src/clj/auto_ap/graphql/expected_deposit.clj index 7b8889ed..73d76eb5 100644 --- a/src/clj/auto_ap/graphql/expected_deposit.clj +++ b/src/clj/auto_ap/graphql/expected_deposit.clj @@ -27,15 +27,19 @@ (result->page expected-deposits expected-deposit-count :expected_deposits args))) (def objects - {:expected_deposit {:fields {:id {:type :id} - :location {:type 'String} - :external_id {:type 'String} - :total {:type :money} - :transaction {:type :transaction} - :status {:type :expected_deposit_status} - :fee {:type :money} - :client {:type :client} - :date {:type 'String}}} + {:expected_deposit_total {:fields {:date {:type :iso_date} + :count {:type 'Int} + :amount {:type :money}}} + :expected_deposit {:fields {:id {:type :id} + :location {:type 'String} + :external_id {:type 'String} + :total {:type :money} + :transaction {:type :transaction} + :status {:type :expected_deposit_status} + :fee {:type :money} + :client {:type :client} + :date {:type 'String} + :totals {:type '(list :expected_deposit_total)}}} :expected_deposit_page {:fields {:expected_deposits {:type '(list :expected_deposit)} :count {:type 'Int} diff --git a/src/cljs/auto_ap/views/pages/pos/expected_deposits.cljs b/src/cljs/auto_ap/views/pages/pos/expected_deposits.cljs index 92994a87..9d04cd7a 100644 --- a/src/cljs/auto_ap/views/pages/pos/expected_deposits.cljs +++ b/src/cljs/auto_ap/views/pages/pos/expected_deposits.cljs @@ -28,8 +28,9 @@ :date-range (:date-range params) :client-id (:id @(re-frame/subscribe [::subs/client]))} [[:expected-deposits [:id :total :fee :location :date :status + [:totals [:date :count :amount]] [:transaction [:id :date]] - [:client [:name :id]]]] + [:client [:name :id]]]] :total :start :end]]]} diff --git a/src/cljs/auto_ap/views/pages/pos/expected_deposits/table.cljs b/src/cljs/auto_ap/views/pages/pos/expected_deposits/table.cljs index 36625b3a..5c7f60b8 100644 --- a/src/cljs/auto_ap/views/pages/pos/expected_deposits/table.cljs +++ b/src/cljs/auto_ap/views/pages/pos/expected_deposits/table.cljs @@ -2,24 +2,23 @@ (ns auto-ap.views.pages.pos.expected-deposits.table (:require [auto-ap.events :as events] + [auto-ap.routes :as routes] [auto-ap.subs :as subs] [auto-ap.views.components.buttons :as buttons] + [auto-ap.views.components.dropdown + :refer [drop-down drop-down-contents]] [auto-ap.views.components.grid :as grid] [auto-ap.views.pages.data-page :as data-page] [auto-ap.views.pages.pos.form :as form] - [auto-ap.views.utils :refer [date->str nf dispatch-event-with-propagation pretty str->date standard]] - [auto-ap.views.components.dropdown - :refer - [drop-down drop-down-contents]] - + [auto-ap.views.utils + :refer [date->str dispatch-event-with-propagation nf pretty ->$]] [bidi.bidi :as bidi] [cemerick.url :as url] - [re-frame.core :as re-frame] - [auto-ap.routes :as routes])) + [re-frame.core :as re-frame])) (defn row [{sales-order :sales-order selected-client :selected-client}] - (let [{:keys [client transaction status location date total fee id]} sales-order] + (let [{:keys [client transaction status location date total fee id totals]} sales-order] [grid/row {:class (:class sales-order) :id id} (when-not selected-client [grid/cell {} (:name client)]) @@ -30,28 +29,39 @@ [grid/cell {:class "has-text-right"} (nf fee )] [grid/button-cell {} [:div.buttons - [buttons/fa-icon {:event [::form/editing sales-order] :icon "fa-pencil"}]] - (when transaction - [drop-down {:id [::links id] - :is-right? true - :header [buttons/fa-icon {:class "badge" - :on-click (dispatch-event-with-propagation [::events/toggle-menu [::links id]]) - :data-badge (str 1) - :icon "fa-paperclip"}]} - [drop-down-contents - [:div.dropdown-item - [:table.table.grid.compact - [:tbody - (when transaction - [:tr - [:td - "Transaction"] - [:td (some-> transaction :date (date->str pretty))] - [:td - [buttons/fa-icon {:icon "fa-external-link" - :href (str (bidi/path-for routes/routes :transactions ) - "?" - (url/map->query {:exact-match-id (:id transaction)}))}]]])]]]]])]])) + [buttons/fa-icon {:event [::form/editing sales-order] :icon "fa-pencil"}] + (when (or transaction + (seq totals)) + [drop-down {:id [::links id] + :is-right? true + :header [buttons/fa-icon {:class "badge" + :on-click (dispatch-event-with-propagation [::events/toggle-menu [::links id]]) + :data-badge (str 1) + :icon "fa-paperclip"}]} + [drop-down-contents + [:div.dropdown-item + [:table.table.grid.compact + [:tbody + (when transaction + [:tr + [:td + "Transaction"] + [:td (some-> transaction :date (date->str pretty))] + [:td + [buttons/fa-icon {:icon "fa-external-link" + :href (str (bidi/path-for routes/routes :transactions ) + "?" + (url/map->query {:exact-match-id (:id transaction)}))}]]]) + (when (seq totals) + (into + [:<>] + (for [t totals] + [:tr + [:td + "Sales"] + [:td (some-> t :date (date->str pretty))] + [:td (str (some-> t :count ) " (" (some-> t :amount ->$) ")") + ]])))]]]]])]]])) (defn table [{:keys [data-page]}] (let [selected-client @(re-frame/subscribe [::subs/client])