;; 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.

{:now 
 (seq (d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) (count ?charge)
             :in $ ?d4
             :where
             [?charge :charge/date ?date]
             [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 90)))) ?min-d]
             [(>= ?date ?min-d)]
             [?charge :charge/client ?c]
             [?c :client/code "NGVG"]
             [?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/iso-date) ?d4]]
           (d/db auto-ap.datomic/conn)
           "2023-01-12"))

 :two-weeks-ago
 (seq (d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) (count ?charge)
             :in $ ?d4
             :where
             [?charge :charge/date ?date]
             [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 90)))) ?min-d]
             [(>= ?date ?min-d)]
             [?charge :charge/client ?c]
             [?c :client/code "NGVG"]
             [?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/iso-date) ?d4]]
           (d/as-of (d/db auto-ap.datomic/conn)
                    #inst "2023-01-26")
           "2023-01-12"))}



{:now 
 (seq (d/q '[:find ?d4 ?type ?p2 ?charge
             :in $ ?d4
             :where
             [?charge :charge/date ?date]
             [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 90)))) ?min-d]
             [(>= ?date ?min-d)]
             [?charge :charge/client ?c]
             [?c :client/code "NGVG"]
             [?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/iso-date) ?d4]]
           (d/db auto-ap.datomic/conn)
           "2023-01-12"))

 :two-weeks-ago
 (seq (d/q '[:find ?d4 ?type ?p2 ?charge
             :in $ ?d4
             :where
             [?charge :charge/date ?date]
             [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 90)))) ?min-d]
             [(>= ?date ?min-d)]
             [?charge :charge/client ?c]
             [?c :client/code "NGVG"]
             [?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/iso-date) ?d4]]
           (d/as-of (d/db auto-ap.datomic/conn)
                    #inst "2023-01-26")
           "2023-01-12"))}


[
 (d/pull (d/db auto-ap.datomic/conn) '[* {:expected-deposit/_charges [:db/id  :expected-deposit/date] :charge/processor [:db/ident] :charge/client [:client/code]}] 17592284780148)

 (d/pull (d/as-of (d/db auto-ap.datomic/conn)
                  #inst "2023-01-25") '[* {:expected-deposit/_charges [:db/id  :expected-deposit/date] :charge/processor [:db/ident] :charge/client [:client/code]}] 17592284780148)]

(user/init-repl)

(entity-history [:client/code "NGVG"])


(d/pull (d/as-of (d/db auto-ap.datomic/conn)
                 #inst "2023-01-27") '[:client/code {:client/square-locations [*]}] [:client/code "NGVV"])
