From a007b04685a2cff5e25726f675e4970fb53a1899 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 7 Sep 2021 16:39:04 -0700 Subject: [PATCH] scratch sessions --- ...-not-matching-invoices-or-transactions.clj | 86 +++++ scratch-sessions/ngak-to-gl.clj | 38 ++ scratch-sessions/square-corrections.clj | 196 ++++++++++ scratch-sessions/temp.clj | 362 ++++++++++++++++++ 4 files changed, 682 insertions(+) create mode 100644 scratch-sessions/find-ledgers-not-matching-invoices-or-transactions.clj create mode 100644 scratch-sessions/ngak-to-gl.clj create mode 100644 scratch-sessions/square-corrections.clj create mode 100644 scratch-sessions/temp.clj diff --git a/scratch-sessions/find-ledgers-not-matching-invoices-or-transactions.clj b/scratch-sessions/find-ledgers-not-matching-invoices-or-transactions.clj new file mode 100644 index 00000000..190c547b --- /dev/null +++ b/scratch-sessions/find-ledgers-not-matching-invoices-or-transactions.clj @@ -0,0 +1,86 @@ +;; 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. +(require '[auto-ap.utils :refer [dollars=]]) + +(defn get-bad-ones [] + (->> (d/query {:query {:find ['?je '?a '(sum ?debit) '(sum ?credit)] + :with ['?jel] + :in '[$] + :where ['[?je :journal-entry/amount ?a] + '[?je :journal-entry/line-items ?jel] + '[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit] + '[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]] + } + :args [(d/db auto-ap.datomic/conn)]}) + (filter (fn [[_ a d c]] + (or (not (dollars= a d)) + (not (dollars= a c))))) + #_(count) + #_(take 3))) + +(def bad-ones (get-bad-ones)) + +(defn bad-invoices [bad-ones] + (->> bad-ones + + (map first) + (map (fn [je] + (:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn) + je)))) + (filter (fn [invoice?] + (:invoice/total invoice?))) + (map (fn [invoice] + {:total (:invoice/total invoice) + :client-code (:client/code (:invoice/client invoice)) + :invoice-number (:invoice/invoice-number invoice) + :vendor (:vendor/name (:invoice/vendor invoice)) + :accounts (map + (fn [ea] + (str (:account/numeric-code (:invoice-expense-account/account ea)) ": " (:invoice-expense-account/amount ea))) + (:invoice/expense-accounts invoice))})) + )) + +(defn bad-transactions [bad-ones] + (->> bad-ones + + (map first) + (map (fn [je] + (:journal-entry/original-entity (d/entity (d/db auto-ap.datomic/conn) + je)))) + (filter (fn [invoice?] + (:transaction/amount invoice?))) + + )) + +(doseq [i (bad-invoices bad-ones)] + (println "touching invoice " i) + (auto-ap.ledger/touch-invoice (:db/id i))) + +(doseq [i (bad-transactions bad-ones)] + (println "touching tx " i) + (auto-ap.ledger/touch-transaction (:db/id i))) + +(clojure.pprint/pprint (bad-invoices (get-bad-ones))) +(count (bad-transactions (get-bad-ones))) + + +(defn get-bad-transactions2 [] + (->> (d/query {:query {:find ['?tx '?a '(sum ?a2)] + :with ['?acc] + :in '[$] + :where ['[?tx :transaction/amount ?a] + '[?tx :transaction/accounts ?acc] + '[(get-else $ ?acc :transaction-account/amount 0.0) ?a2]] + } + :args [(d/db auto-ap.datomic/conn)]}) + (filter (fn [[_ a d ]] + (not (dollars= (Math/abs a) (Math/abs d))))) + #_(count) + #_(take 3))) + +(clojure.pprint/pprint (count (get-bad-transactions2))) + +() diff --git a/scratch-sessions/ngak-to-gl.clj b/scratch-sessions/ngak-to-gl.clj new file mode 100644 index 00000000..83f4ddef --- /dev/null +++ b/scratch-sessions/ngak-to-gl.clj @@ -0,0 +1,38 @@ +;; 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. + +(require '[auto-ap.datomic :refer [conn]]) + + +(clojure.pprint/pprint (for [[date-str location numeric-code total invoice-id expense-account-id] + [["2/24/2019" "MH" 50000 18452.93 17592189466511 17592189466512] + ["3/6/2019" "HQ" 85100 1502.52 17592189253677 17592189253678] + ["3/12/2019" "MH" 82100 4559.00 17592189226804 17592189226805] + ["3/19/2019" "MH" 74800 1500.00 17592207148562 17592207148563] + ["4/1/2019" "MH" 82100 4559.00 17592190913262 17592190913263] + ["4/29/2019" "MH" 74100 0.00 17592194022072 17592194022073] + ["5/1/2019" "MH" 82100 4599.00 17592201540199 17592201540200] + ["6/11/2019" "MH" 82100 4599.00 17592201540193 17592201540194] + ["11/20/2019" "MH" 82100 5014.89 17592215052851 17592215052852] + ["11/22/2019" "MH" 50000 0.00 17592215522630 17592215522631] + ["11/22/2019" "HQ" 75990 0.00 17592215653503 17592215653504] + ["11/22/2019" "HQ" 75990 0.00 17592215653493 17592215653494] + ["11/25/2019" "HQ" 75990 0.00 17592215653495 17592215653496] + ["11/25/2019" "HQ" 75990 0.00 17592215653519 17592215653520] + ["12/3/2019" "MH" 50000 2194.45 17592217089192 17592217089193] + ["12/7/2019" "MH" 50000 42.20 17592217089189 17592217089190] + ["1/12/2020" "MH" 50000 -727.25 17592220829257 17592220829258] + ["3/19/2020" "MH" 50000 3689.78 17592228959014 17592228959015]]] + [{:db/id invoice-id + :invoice/total total + :invoice/date (clj-time.coerce/to-date (auto-ap.time/parse date-str auto-ap.time/normal-date)) + :invoice/location location + :payment (d/query {:query {:find ['?p '?a] + :in '[$ ?invoice-id] + :where ['[?pi :invoice-payment/invoice ?invoice-id] + '[?pi :invoice-payment/payment ?p] + '[?pi :invoice-payment/amount ?a]]} + :args [(d/db conn) invoice-id]} )}])) diff --git a/scratch-sessions/square-corrections.clj b/scratch-sessions/square-corrections.clj new file mode 100644 index 00000000..8d449758 --- /dev/null +++ b/scratch-sessions/square-corrections.clj @@ -0,0 +1,196 @@ +;; 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. + +(-> (order "WrNb9vXPkflwbVz7sCHGkpr8k3VZY" ) + :order + ((fn [order] + (let [[client loc] (location_id->client-location (:location_id order))] + (remove-nils + #:sales-order + {:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles"))) + :client [:client/code client] + :location loc + :external-id (str "square/order/" client "-" loc "-" (:id order)) + :total (-> order :net_amounts :total_money amount->money) + :tax (-> order :net_amounts :tax_money amount->money) + :tip (-> order :net_amounts :tip_money amount->money) + :discount (-> order :net_amounts :discount_money amount->money) + :service-charge (-> order :net_amounts :service_charge_money amount->money) + :returns (+ (- (-> order :return_amounts :total_money amount->money) + (-> order :return_amounts :tax_money amount->money) + (-> order :return_amounts :tip_money amount->money) + (-> order :return_amounts :service_charge_money amount->money) + + ;; not returning discount money since that was already marked off the price + ;;(-> order :return_amounts :discount_money amount->money) + )) + :charges (->> (:tenders order) + (map (fn [t] + (remove-nils + #:charge + {:type-name (:type t) + :processor (condp = (some-> (:note t) str/lower-case) + "doordash" :ccp-processor/doordash + "dd" :ccp-processor/doordash + "ubereats" :ccp-processor/uber-eats + "ue" :ccp-processor/uber-eats + "grubhub" :ccp-processor/grubhub + "grub" :ccp-processor/grubhub + "gh" :ccp-processor/grubhub + (condp = (:name (:source order)) + "GRUBHUB" :ccp-processor/grubhub + "UBEREATS" :ccp-processor/uber-eats + "DOORDASH" :ccp-processor/doordash + :ccp-processor/na)) + :total (amount->money (:amount_money t)) + :tip (amount->money (:tip_money t))})))) + :line-items (->> (:line_items order) + (map (fn [li] + (remove-nils + #:order-line-item + {:item-name (:name li) + :category (if (= "GIFT_CARD" (:item_type li)) + "Gift Card" + (item-id->category-name (:catalog_object_id li))) + :total (amount->money (:total_money li)) + :tax (amount->money (:total_tax_money li)) + :discount (amount->money (:total_discount_money li))}))))})))) + (clojure.pprint/pprint)) + + + + +(d/query {:query {:find '[?d4 ?type (sum ?total) (sum ?tip)] + :with '[?s ?charge] + :in '[$] + :where + '[[?c :client/code "NGVZ"] + [?s :sales-order/client ?c] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name ?type] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + [?s :sales-order/date ?date] + [(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] + [(= ?d4 "06/25/2021")]]} + :args [(d/db conn)]}) + +(count (settlements "ACNTYY8WVZ6DV")) + +@(d/transact conn (->> (d/query {:query {:find '[?s] + :in '[$] + :where + '[[?c :client/code "NGVZ"] + [?s :sales-order/client ?c] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name ?type] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + [?s :sales-order/date ?date] + [(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] + [(= ?d4 "06/25/2021")]]} + :args [(d/db conn)]}) + (mapv (fn [[s]] + [:db/retractEntity s])) + )) + +(->> (search "ACNTYY8WVZ6DV" #inst "2021-06-25T00:00:00-08:00") + (map :id) + #_(mapcat :tenders) + #_(filter #(= "CARD" (:type %))) + #_(map :amount_money) + #_(map :amount) + count + #_(reduce + 0 )) + + +(d/query {:query {:find '[(pull ?s [*])] + :in '[$] + :where + '[[?c :client/code "NGVZ"] + [?s :sales-order/client ?c] + (not [?s :sales-order/charges]) + #_[?charge :charge/type-name "CARD"] + #_[?charge :charge/total ?total] + #_[?charge :charge/tip ?tip] + [?s :sales-order/date ?date] + [(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] + [(= ?d4 "06/25/2021")]]} + :args [(d/db conn)]}) + + +(order "WrNb9vXPkflwbVz7sCHGkpr8k3VZY" ) +k + + + + + +(defn settlements-2 [l d] + (log/info "Searching for" l) + (let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements" "?begin_time=" "2021-06-15" "&end_time=" "2021-06-18" "&limit=" "10") + {:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7" + "Content-Type" "application/json"} + :as :json}) + :body + (map :id))] + (loop [[s & xs] (take 15 settlements) + result []] + (log/info "Looking up settlement " s " for location " l) + (let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s) + {:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7" + "Content-Type" "application/json"} + :as :json + :retry-handler (fn [ex try-count http-context] + (log/warn "Retrying after failure " ex) + (if (> try-count 4) false true))})))] + (if (seq xs) + (recur xs (conj result n)) + (conj result n)))))) + +(defn daily-settlements-2 [] + (for [settlement (settlements-2 "ACNTYY8WVZ6DV" nil) + :let [[client loc] (location_id->client-location "ACNTYY8WVZ6DV")]] + #:expected-deposit {:external-id (str "square/settlement/" (:id settlement)) + :total (amount->money (:total_money settlement)) + :client [:client/code client] + :location loc + :fee (- (reduce + 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))})) + + +(clojure.pprint/pprint (daily-settlements-2)) + + + +(for [g ["3ZT28C9V6EA08YQ6DDBY12E74TPD" "3ZAM1XMMYDGW8YQ6DDBY12E74TPD" "3Z3B2JHJECQ48YQ6DDBY12E74TPD" "3Z3B2JFKECQ48YQ6DDBY12E74TPD" "3Z3B2JD2YCQ48YQ6DDBY12E74TPD" "3Z6FY3M3PACC8YQ6DDBY12E74TPD" "3ZXAN5JAP9K48YQ6DDBY12E74TPD" "3ZPPMSRM68T08YQ6DDBY12E74TPD" "3Z9YW2MEP80W8YQ26RBY12E74TP9"]] + (:expected-deposit/fee (d/entity (d/db conn) [:expected-deposit/external-id (str "square/settlement/" g)]))) + + +(d/entity (d/db conn) [:expected-deposit/external-id (str "square/settlement/3Z3KFBM8PF388YQ6DDBY12E74TPD")]) + + + + +@(d/transact conn [#:expected-deposit{:external-id + "square/settlement/3Z3KFBM8PF388YQ6DDBY12E74TPD", + :total 2282.05, + :client [:client/code "NGVZ"], + :location "NP", + :fee 65.35, + :date #inst "2021-06-17T03:14:22.000-00:00"}]) + diff --git a/scratch-sessions/temp.clj b/scratch-sessions/temp.clj new file mode 100644 index 00000000..0964f362 --- /dev/null +++ b/scratch-sessions/temp.clj @@ -0,0 +1,362 @@ +;; 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. + +(defn upsert-settlements-2 + ([] (upsert-settlements nil)) + ([location-ids] + (lc/with-context {:source "Square settlements loading "} + (try + (let [existing (->> (d/query {:query {:find ['?external-id] + :in ['$] + :where ['[_ :expected-deposit/external-id ?external-id]]} + :args [(d/db conn)]}) + (map first) + set) + _ (log/info (count existing) "settlements already exist") + to-create (if location-ids + (daily-settlements location-ids) + (daily-settlements))] + (doseq [x (partition-all 20 to-create)] + (log/info "Loading expected deposit" (count x)) + @(d/transact conn x))) + (catch Exception e + (log/error e))) + (log/info "Done loading settlements")))) + +;; ?charge ?d4 ?p2 (sum ?total) (sum ?tip) +(->> + (map vector + (->> (d/q '[:find ?total ?i + :with ?charge + :in $ + :where + [?c :client/code "NGEZ"] + [?s :sales-order/client ?c] + [?s :sales-order/charges ?charge] + [?s :sales-order/external-id ?i] + [?charge :charge/type-name "CARD"] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + [(get-else $ ?charge :charge/processor :na) ?ccp] + [(get-else $ ?ccp :db/ident :na) ?p] + [(name ?p) ?p2] + [?s :sales-order/date ?date] + [(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] + [(= ?d4 "08/30/2021")]] + (d/db conn)) + (sort-by first)) + [2.28 4.26 5.99 7.0 7.28 7.41 7.49 7.99 8.0 10.61 11.53 11.67 11.67 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 12.33 12.34 12.39 12.39 12.39 12.39 12.39 12.39 12.8 13.39 13.57 13.63 13.63 13.63 13.63 13.63 13.63 13.99 14.16 14.16 14.25 14.25 14.25 14.25 14.49 14.75 14.99 15.06 15.52 15.57 15.62 15.89 15.94 15.94 16.05 16.08 16.08 16.11 16.11 16.65 16.73 17.11 17.11 17.13 17.28 17.34 17.36 17.61 17.62 17.65 17.72 17.72 17.72 17.72 17.78 17.78 18.21 18.31 18.53 18.53 18.91 19.15 19.87 19.88 19.98 20.03 20.09 20.26 20.8 20.96 21.15 22.09 22.24 22.4 22.56 23.6 23.6 24.02 24.1 24.19 24.83 25.12 25.75 25.96 25.96 26.88 26.88 27.26 27.91 28.5 28.72 28.98 28.99 30.11 30.6 30.7 31.35 31.73 34.32 34.96 35.19 35.44 35.44 36.59 38.53 42.45 44.33 44.42 44.55 44.86 48.14 49.93 50.23 51.92 70.94 80.48]) + + (map (fn [[[x s] y]] + [x y (Math/round (* 100 (Math/abs (- x y)))) s] + )) + #_(filter (fn [[_ _ x]] + (> x 0))) + clojure.pprint/pprint) + + +(2.28 4.26 5.99 7.0 7.28 7.41 7.49 7.99 8.0 10.61 11.53 11.67 11.67 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 11.8 12.33 12.34 12.39 12.39 12.39 12.39 12.39 12.39 12.8 13.39 13.57 13.63 13.63 13.63 13.63 13.63 13.63 13.99 14.16 14.16 14.25 14.25 14.25 14.25 14.49 14.75 14.99 15.06 15.52 15.57 15.62 15.89 15.94 15.94 16.05 16.08 16.08 16.11 16.11 16.65 16.73 17.11 17.11 17.13 17.28 17.34 17.36 17.61 17.62 17.65 17.72 17.72 17.72 17.72 17.78 17.78 18.21 18.31 18.53 18.53 18.91 19.15 19.87 19.88 19.98 20.03 20.09 20.26 20.8 20.96 21.15 22.09 22.24 22.4 22.56 23.6 23.6 24.02 24.1 24.19 24.83 25.12 25.75 25.96 25.96 26.88 26.88 27.26 27.91 28.5 28.72 28.98 28.99 30.11 30.6 30.7 31.35 31.73 34.32 34.96 35.19 35.44 35.44 36.59 38.53 42.45 44.33 44.42 44.55 44.86 48.14 49.93 50.23 51.92 70.94 80.48) + +-5.99 + +(clojure.pprint/pprint (d/pull (d/db conn) '[*] [:sales-order/external-id "square/order/NGEZ-JS-tFxan8lDoKZTGHuRTjGXNBpeV"])) + +(clojure.pprint/pprint (d/pull (d/db conn) '[*] [:sales-order/external-id "square/order/NGEZ-JS-RfZgpKBlubxyviP5S6a4hPAfV"])) + +@(d/transact conn [[:db/retractEntity 17592242758496]]) + +() + + +(->> (d/q '[:find (sum ?total) + :with ?charge + :in $ + :where + [?c :client/code "NGEZ"] + [?s :sales-order/client ?c] + [?s :sales-order/charges ?charge] + [?s :sales-order/external-id ?i] + [?charge :charge/type-name "CARD"] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + [(get-else $ ?charge :charge/processor :na) ?ccp] + [(get-else $ ?ccp :db/ident :na) ?p] + [(name ?p) ?p2] + [?s :sales-order/date ?date] + [(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] + [(= ?d4 "08/30/2021")]] + (d/db conn)) + (sort-by first)) + + +@(d/transact conn (->> (d/q '[:find ?charge-2 + :in $ + :where + [?s2 :sales-order/client [:client/code "NGEZ"]] + [?s2 :sales-order/charges ?charge-1 ?tx-1 ] + [?s2 :sales-order/charges ?charge-2 ?tx-2 ] + [(> ?tx-2 ?tx-1 )] + [?charge-1 :charge/total ?t-1] + [?charge-2 :charge/total ?t-2] + [(= ?t-1 ?t-2)]] + (d/history (d/db conn))) + (mapv (fn [[x]] + [:db/retractEntity x])))) + +@(d/transact conn (->> (d/q '[:find ?li-2 + :in $ + :where + [?s2 :sales-order/client [:client/code "NGEZ"]] + [?s2 :sales-order/line-items ?li-1 ?tx-1 ] + [?s2 :sales-order/line-items ?li-2 ?tx-2 ] + [(> ?tx-2 ?tx-1 )] + [?li-1 :order-line-item/total ?t-1] + [?li-2 :order-line-item/total ?t-2] + [(= ?t-1 ?t-2)]] + (d/history (d/db conn))) + (mapv (fn [[x]] + [:db/retractEntity x])))) + +(clojure.pprint/pprint (d/pull (d/db conn) '[*] 17592242758498)) + +{[:db/retractEntity 17592242758498] + [:db/retractEntity 17592242757940] + [:db/retract17592242758483] [17592242758486] [17592242758490] [17592242757945] [17592242757948] [17592242757933] [17592242757936] [17592242758495]} + + +(doseq [[_ [n]] {"2RVBYER6QSV7W" ["NGAK" "MH"] + "8JT71V8XGYAT3" ["NGKG" "NB"] + "SCX0Y8CTGM1S0" ["NGE1" "UC"] + "FNH5VRT890WK8" ["NGMJ" "SC"] + "AMQ0NPA8FGDEF" ["NGPG" "SZ"] + "ACNTYY8WVZ6DV" ["NGVZ" "NP"] + "KMVFQ9CRCXJ10" ["NGZO" "VT"] + "L0J45VZKHWXVR" ["NGRV" "RV"] + "L3GMNBFARX9GG" ["NGOP" "OP"] + "LXJCAHYGZVNEJ" ["NGHG" "DC"] + "LSWNP14T0YKD9" ["NGWL" "WL"] + "LT322EK0S2TRD" ["NGGH" "FM"] + "L0Z167T2T7W7F" ["NGEZ" "JS"] + "LEV4GM1JPJS6R" ["NGEB" "CV"] + "LQTHXQY69MYB6" ["NGDG" "DB"] + "L7S9MXZBJ00HY" ["NGGG" "LM"] + "LRC7WVD77ZM81" ["NGLK" "SM"] + "FZ3ZYC77T3W1T" ["NGA1" "KA"] + "LG5X0MHA4NZDM" ["NGSM" "SM"]}] + (println @(d/transact conn (->> (d/q '[:find ?charge-2 + :in $ ?x + :where + [?s2 :sales-order/client ?x] + [?s2 :sales-order/date ?d] + [(>= ?d #inst "2021-08-01")] + + [?s2 :sales-order/charges ?charge-1 ?tx-1 ] + [?s2 :sales-order/charges ?charge-2 ?tx-2 ] + [(> ?tx-2 ?tx-1 )] + [?charge-1 :charge/total ?t-1] + [?charge-2 :charge/total ?t-2] + [(= ?t-1 ?t-2)] + + + ] + (d/history (d/db conn)) + [:client/code n]) + (mapv (fn [[x]] + [:db/retractEntity x])))))) + +(count (->> (d/q '[:find ?li-2 + :in $ + :where + [?s2 :sales-order/line-items ?li-1 ?tx-1 ] + [?s2 :sales-order/line-items ?li-2 ?tx-2 ] + [(> ?tx-2 ?tx-1 )] + [?li-1 :order-line-item/total ?t-1] + [?li-2 :order-line-item/total ?t-2] + [(= ?t-1 ?t-2)]] + (d/history (d/db conn))) + (mapv (fn [[x]] + [:db/retractEntity x])))) + +(doseq [x (->> (d/q '[:find ?s2 + :in $ ?x + :where + [?s2 :sales-order/client ?x] + [?s2 :sales-order/date ?d] + [(>= ?d #inst "2021-07-01")] + + ] + (d/db conn) + [:client/code "NGEZ"]) + (mapv (fn [[x]] + [:db/retractEntity x])) + (partition-all 20))] + @(d/transact conn x) + ) + + + +(count (->> (d/q '[:find ?s2 + :in $ ?x + :where + [?s2 :sales-order/client ?x] + [?s2 :sales-order/date ?d] + [(>= ?d #inst "2021-07-01")] + + ] + (d/db conn) + [:client/code "NGEZ"]) + (mapv (fn [[x]] + [:db/retractEntity x])) + #_(partition-all 20))) + + +(with-redefs [location_id->client-location (fn location_id->client-location [location] + ({"L0Z167T2T7W7F" ["NGEZ" "JS"]} location))] + + (doseq [d (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -70)) + (time/now) + (time/days 1))] + (upsert d))) + + +(d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) + :with ?charge + :in $ + :where + [?c :client/code "NGEZ"] + [?s :sales-order/client ?c] + [?s :sales-order/charges ?charge] + [?charge :charge/type-name ?type] + [?charge :charge/total ?total] + [?charge :charge/tip ?tip] + [(get-else $ ?charge :charge/processor :na) ?ccp] + [(get-else $ ?ccp :db/ident :na) ?p] + [(name ?p) ?p2] + [?s :sales-order/date ?date] + [(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] + [(= "08/14/2021" ?d4)]] + + (d/db conn)) + + +(d/q '[:find ?d4 ?t (sum ?total) (sum ?fee) + :with ?r + :in $ + :where + [?r :sales-refund/client [:client/code "NGEZ"]] + [?r :sales-refund/total ?total] + [?r :sales-refund/fee ?fee] + [?r :sales-refund/date ?date] + [?r :sales-refund/type ?t] + [(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] + [(= "08/14/2021" ?d4)]] + + (d/db conn)) + + +(d/q '[:find ?d4 (sum ?total) (sum ?tax) (sum ?tip) (sum ?service-charge) (sum ?discount) (sum ?returns) + :with ?s + :in $ + :where + [?s :sales-order/client [:client/code "NGEZ"]] + [?s :sales-order/date ?d] + [?s :sales-order/total ?total] + [?s :sales-order/tax ?tax] + [?s :sales-order/tip ?tip] + [?s :sales-order/service-charge ?service-charge] + [?s :sales-order/returns ?returns] + [?s :sales-order/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] + [(= "08/14/2021" ?d4)] +] + + (d/db conn)) + + +(d/q '[:find ?r ?d4 ?t (sum ?total) (sum ?fee) + :with ?r + :in $ + :where + [?r :sales-refund/client [:client/code "NGEZ"]] + [?r :sales-refund/total ?total] + [?r :sales-refund/fee ?fee] + [?r :sales-refund/date ?date] + [?r :sales-refund/type ?t] + [(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] + [(= "08/09/2021" ?d4)]] + + (d/db conn)) + +(clojure.pprint/pprint + (get-settlement-details [["L0Z167T2T7W7F" "3Z7MGVQN7TA88YQD8ZBY12E3434N"]])) + + + + +(def deposit-dates (->> (daily-settlements) + (map #(select-keys % #{:expected-deposit/date :expected-deposit/sales-date :expected-deposit/external-id})) + vec)) + + +(doseq [d deposit-dates] + + (println (:expected-deposit/external-id d)) + (println (auto-ap.time/unparse-local (auto-ap.time/localize (coerce/to-date-time (:expected-deposit/sales-date d))) auto-ap.time/normal-date)) + (println (auto-ap.time/unparse-local (auto-ap.time/localize (coerce/to-date-time (:expected-deposit/date d))) auto-ap.time/normal-date)) + (println)) + +(clojure.pprint/pprint deposit-dates + ) + + +(doseq [n (partition-all 20 deposit-dates)] + (println "upserting " (count n)) + @(d/transact conn n)) + + +(count (filter :expected-deposit/sales-date deposit-dates)) + +(clojure.pprint/pprint (d/pull (d/db conn) '[*] [:expected-deposit/external-id "square/settlement/3ZJMR8VY7V3C8YQ42SBY12E701TN"])) + + +(def sx (get-settlement-details [["LSWNP14T0YKD9" "3ZQWTTJ7Z2Q48YQ42SBY12E701TN"]])) + +sx + +(->> + (:entries (first sx)) + (filter #(= "OTHER" (:type %)) ) + (map (fn [x] + (:amount (:amount_money x)))) + (reduce + 0)) + + +(def sx (settlements "LSWNP14T0YKD9")) + +(filter identity (map (fn [x] + (if (seq (->> (filter #(= "OTHER" (:type %)) (:entries x)))) + (->> (:entries x) + (filter #(= "OTHER" (:type %)) ) + (map (fn [x] + (:amount (:amount_money x)))) + (reduce + 0)) + nil) ) + sx)) + + +(get-settlement-details )