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

