Files
integreat/scratch-sessions/temp.clj

470 lines
20 KiB
Clojure

;; 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 )
;; working on determing if there are still uncompleted orders
(with-redefs [location_id->client-location (fn location_id->client-location [location]
({"L7S9MXZBJ00HY" ["NGGG" "LM"]} location))
pc (fn [d] {"query" {"filter" {"date_time_filter"
{
"created_at" {
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
"end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (time/plus (coerce/to-date-time d) (time/days 1)) (time/time-zone-for-id "America/Los_Angeles")))
}
}
"state_filter" {"states" ["COMPLETED"]}}
"sort" {
"sort_field" "CREATED_AT"
"sort_order" "DESC"
}}})
daily-results (fn daily-results [d]
(->> (locations)
(map :id)
(filter location_id->client-location)
(mapcat #(search % d))
(filter (fn [order]
(and (not= #{"FAILED"}
(set (map #(:status (:card_details %)) (:tenders order)))))))
(map order->sales-order)))]
(->> (-> []
(into (daily-results #inst "2021-09-05T00:00:00-08:00"))
(into (daily-results #inst "2021-09-06T00:00:00-08:00"))
(into (daily-results #inst "2021-09-07T00:00:00-08:00")))
(filter #(= "09/06/2021" (auto-ap.time/unparse-local (auto-ap.time/localize (coerce/to-date-time (:sales-order/date %))) auto-ap.time/normal-date)))
(mapcat :sales-order/charges)
(group-by :charge/type-name)
(map (fn [[x y]]
[x (count y)]))))
(with-redefs [location_id->client-location (fn location_id->client-location [location]
({"L7S9MXZBJ00HY" ["NGGG" "LM"]} location))
pc (fn [d] {"query" {"filter" {"date_time_filter"
{
"created_at" {
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
"end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (time/plus (coerce/to-date-time d) (time/days 1)) (time/time-zone-for-id "America/Los_Angeles")))
}
}
}
"sort" {
"sort_field" "CREATED_AT"
"sort_order" "DESC"
}}})]
(->> (-> []
(into (search "L7S9MXZBJ00HY" #inst "2021-09-05T00:00:00-08:00"))
(into (search "L7S9MXZBJ00HY" #inst "2021-09-06T00:00:00-08:00"))
(into (search "L7S9MXZBJ00HY" #inst "2021-09-07T00:00:00-08:00")))
(filter #(= "09/06/2021" (auto-ap.time/unparse-local (auto-ap.time/localize (coerce/to-date-time (:created_at %))) auto-ap.time/normal-date)))
(filter (fn [order]
(and (not= #{"FAILED"}
(set (map #(:status (:card_details %)) (:tenders order))))
(> (count (:tenders order)) 0))
))
count))
;; 145 if you only do completed, 117 if you don't
(->> (d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip)
:with ?charge
:in $
:where
[?c :client/code "NGGG"]
[?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]
[(= "09/06/2021" ?d4)]]
(d/db conn))
(sort-by first))
(doseq [d (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -15))
(time/now)
(time/days 1))]
(upsert d))
(d/pull (d/db conn) '[* {:invoice/status [:db/ident]} ] 17592240679462)