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


(def my-other-set
  (set
   ["Bsj8zSJLhYfBGYl42u9onKkeV"
    "vBQ3tOGdjj6KIVapfuMMNkleV"
    "CBFtMr7cPqrvq3LFqF1lcbcr0zbZY"
    "BGwIDJQcuIPVp2GLu30leFAfV"
    "DV1nf3Lx78HCiG4fMGzhuBoeV"
    "RAtIC1Kz3MrojHxrUNROkHyeV"
    "1W7Caow6Aynr6FIg6OjhZtzeV"
    "6vMokGcoHp2lKeN2gG0ZfVWHZAUZY"
    "gkVqvQB0idwG9E3e9jCzhzaUxhJZY"
    "4MD2BW5Qoodfy2TEhrhDmxJeWfOZY"
    "yPeoYCJztU3hOj4IIMfgeZqjnIfZY"
    "oUB5nJQnb1RRkYvFy6b5WSHBDQZZY"
    "yfwdGRT9ZKzCBSGRdsBTtqXZ9jWZY"
    "Rcx70uv8SVR1dz5JkI8YZCleV"
    "0oobKSaRtgpoIk6DFUHWnNiE1Y6YY"
    "ydhE5PHyxfHRM9Zig7z1clHC0CKZY"
    "Sp9Jy6iklgEC8wHld7WwdY1A8sLZY"
    "JclkCNxqm0MbngUyvGD4O1keV"
    "WbvGqOoGWUsoy2FA44EDv6YrU0UZY"
    "doQZOXfWgbKomwPSuHSUsUreV"
    "1ASPapG6bV5dbEDjAkCHgi0eV"
    "CtNSISjjj2y9D5h4oa1ClJ8QFhWZY"
    "ytx2QbZmOh1wA56K9RjGCsCOl65YY"
    "E4g7hAZDso6RHQf5rTj7YYAxQkCZY"
    "c8UhaSbWAoCkVI9AMHBvroqIKcUZY"
    "2DNlbJwLVH1Zhtnuex5KYZJgJWIZY"
    "mJUdRyKMU4yanZNnPzI4KbAQ9mcZY"
    "ZEfTvPs0Ojhyl1mVtiD4iLmeV"
    "JodDdbMbZbhH7cjipbshElmeV"
    "RchpFNzbpnmurZNvEMfqqT7eV"
    "HXZHsWbN7oM1Ad2XrYG7TgneV"
    "5GgnDDXVahiPkxlCiRhwhEseV"
    "UOxmIQCicTCzu4iXhipk3ATO4rBZY"
    "A6CfxlWcCKYoqYOoZKXsxIb1LtGZY"
    "qJff1DJyMkdOO3w8CQtyk8JBVkDZY"
    "l2Tuh7tuzwlcXR9RMznlaKneV"
    "rbQu4ahKIPYZEswUaxdTYPmeV"
    "dYWe7mTaTL21OVHqTHlpkoveV"
    "6HQwOES3M3sebg7aHA2kBBKdzOfZY"
    "EGPjtFhk3ZfrXGLlrqcU4ATsdHQZY"
    "jfwoeIa1tSyEJGaKO66nLtleV"
    "2ftQGWeatEKQuq4kYo0s5UOJf8QZY"
    "L32oDHTooH04nVee4KxUOs1eV"
    "2Fs2ILclotx4urEF6nbnZkBvC0AZY"
    "tEW7O0u7WYrpWs8cRJIi7HAfV"
    "Rq83DL5RaUo8F3ssZOEdTRzeV"
    "YEBDzdA9Q7MnoKB2QZUUBgSwL9SZY"
    "AekV8pJUj0IUnyhSLhdoMEV3ZgSZY"
    "C9XLY0V2iQolQCw2ZDPR1NKcd2aZY"
    "OVeOuUUZM74n3FNhLAMtANo7qRMZY"
    "alZzXCYuEQ3CwpIdb3tJWNr6lIZZY"
    "AKkw9cLXB3ElcRJC0uMUqIugbHeZY"
    "rvAZeRm2ndIXGyR25c1FmI3eV"
    "ipvUAHfqno17qMkSbyvPZ9UnIsXZY"
    "Bmgwp2G71UAxoIPVbHyASjqeV"
    "tY8fzdqgUuIrHKKn553Z9w9eV"
    "WZg7Gpqt1BngUoidbj94vM0BjD8YY"
    "1qrvlraW5eULeUAwVlwTXOAfV"
    "Jw1h9kt66Su4SH2unKC13M0eV"
    "sYQmuTeKq2ZmwbyKihLyHaMhbYTZY"
    "MKVWBj3V6wzCMXdlQ6GjWciNppBZY"
    "RgDn8f4BH8P6zOHURueOnZzeV"
    "CHYxKp6RNtLNivoBuw8SAWmVJwYZY"
    "R8hrw6UdXFSAAbzlfVrQWwreV"
    "vRGWNAUf9zwPeiUIGnBXM57eV"
    "wSIuZ01BD8q21ldGRxngbm7IrbXZY"
    "lUe2KBtFGNvmtqRJ5lH3wF2eV"
    "rrGzZBGYeupr1N66SpiKVR6eV"
    "kyZ70CTvLJZYy2JBgQHrrP9TdKCZY"
    "hoPT5Q6FGFyMU0u7uYxdbaseV"
    "kIENLF39suSOks9SGjTJ1xMOWDfZY"
    "bFnrxD7MjkUvJRGYOpE6GNgeV"
    "ntQ5KD5MWfdDe1pnVgVQtM0eV"
    "Pxgdmp58TJGUeE3GGm7KNHveV"
    "vRkdz1GmZt0cscQB9ho4hj9eV"
    "Im0EC0G4no6f0RnkASLcXh8mcR9YY"
    "zPwM9UOeXqLPvU1oa3mRCz3eV"
    "sEaE8dO4HX8ksHs6hdMyPal43D9YY"
    "tAimSXtgI8Eh1rH9PX0eSvjeV"
    "ZQdXrz4cdYFUu8Jz20kAPf9eV"
    "16vV9PDy1Curw4SOid45avmeV"
    "YIvRzxJdPH7zRbMr7J1C4oP5MLRZY"
    "f9iRwhZoM4NbdWWbj18hBj5eV"
    "N4uIjsHvio7zGTdfD57zoTzeV"
    "jLwZEk8DwnzRH3D8moMKpvmeV"
    "xUhz7xcT837WgUSGHAtvM9neV"
    "ztdtsd7I9DtsJXQWTDIGfJ0eV"
    "vNyfGGzDAwtV3n7UyyjC4CneV"
    "Jo5x7G4a4NGQ1loxqA0rGOueV"
    "vjLyqZHtSnEzNa1TsEscc5heV"
    "UufTPUwjmczvLUXdqGJKDMsclfTZY"
    "Vk0L0SzC4VJSIJpmLisLU76eV"
    "k2PX9irWoxgUSRF8xzBzm2iPtODZY"
    "kOZ2pyYxDsER8Wk5uaMaW6tO4cCZY"
    "X5qdSCAlqnISsnib6uGprH3eV"
    "jDANpM0VYDMLnwbgE0RTo90eV"
    "idxqrmyKpLBLmxIyvIc61LuaiNTZY"
    "dCveEvPFYJXTJNkiP0kVmrneV"
    "in0rGzWrEWVefaB6RAqm3Ry8ukdZY"
    "hauaBHwn1lsOaLvGzn63mEueV"
    "9upOpnryJcdzFfzpvMd4m17eV"
    "NeLvGBlVagiitD3f8k1PQA5eV"
    "TxL9j1v1ofhsb94PUDMJyKleV"
    "9ImWb0TEPB1hYwiFHVst4g2eV"
    "BiKMirH38kSQ5o3jPIWOy4weV"
    "fzHAx5m8Ch9weMEMGQbARhheV"
    "JANt7ex4eBmmjZ9Wi1vpILgeV"
    "65DtGODM81tOo2c4S5LgdICHJWdZY"
    "rPILFhv1tT4SfkmO6YKXD80eV"
    "qVB3cqIoM5TL9K1PCfXm6vVXFScZY"
    "DTuiQYzGPVZA0LqacYfnZFveV"
    "XjzvSAoyxwUPAHxyCB56jHueV"
    "d8gaBzmbHWLMynPCvVOSViteV"
    "Yq2KkuRdNHFp4yh4FlS4Zz3m1WJZY"
    "HfRjhczw7EibYA8CnDhTpnueV"
    "nfhpKujldjFhHW0PRRhpKureV"
    "p4tZ6uM7u6vSkQ1mtgrh7I2eV"
    "SHWolfXO0MMF5CPsiK2dD50CDl7YY"
    "7DYuemUQl4zDeQqwIR3i2greV"
    "nHHWqrAXH0z9ICUTuKaHrE0eV"
    "nPvqpIfnLN5Ja6sESGWWzM7eV"
    "bZ5rHVyqEP6IjnmaVaosRMjeV"
    "qJ7Vzqi41jaIgTUOtLXNPHJxOTcZY"
    "0QugWhClLiHkr96m8M06hlS4d4BZY"
    "KlToOBjxOyddwGIRfXgViyfut8WZY"
    "lQmRZdEWg2Ip9gcS5MEgj7eeV"
    "TbgDirp4ugkPUN1c8AZpBx0eV"
    "TBD9pa9KTXDojJeRI5La0bweV"
    "WnRaHxLgvTMijfrgL13XyREYK3eZY"
    "Rcx1TRs6WbA2YBuaJ4agq2peV"
    "6tV52gMQYdo45NoJQXwpkfOV9RCZY"
    "xUHoo5BhX9PoMN2SeDNqbekeV"
    "1UcplezXuW0BixA5JSVOcQoeV"
    "hueFrQFdXvbarFwttJIHnJpeV"
    "L74QHTT903E37Zgu48kI64qeV"
    "McgzDxRo3UF2WjhoMzJwrRM4jkcZY"
    "79PpJ0kqZE7C3Uu1U2EGWT8eV"
    "HRMkJOwawAFNbjfDhUCSgO5eV"
    "7RptH2ZN0rZId7sAfWScQc2eV"
    "Sd3f3tnn1XRM0Z7McXz31nrqYSYZY"
    "rvohcJVk1ddcbAFeGjIRGHoeV"
    "ByuKSnr7zwUtAcxk1Cx4XjheV"
    "M6z1FGrvsmzaM1c2BWWx3nhlO6QZY"
    "FiFu2q2FkoTYDd2O7aBRBYveV"
    "WRI5ixKmHAl14NSqaxvPdK8mUvcZY"
    "HTLlGG1fAuPMAzEg5r3dBKueV"
    "37fb62AyNagTKba75PtZ5uueV"
    "h4pkh8Fkh7MgM9tLh9UP0xheV"
    "CdL3xoGOosCKM29ufZDimEneGIHZY"
    "9qBrllh8izz5pORbeEe508teV"
    "CNLPz0wBVVWUoGxED1LUv3m2irWZY"
    "jfa5AEfdZALtkJ1NgIBPN5neV"
    "nlgNTP8vi86UIwjgYsHkAzgeV"
    "2XTX30n8oIo5CIWDUaybBSIRGoFZY"
    "Y8j0ulnTpZO4nIvSeR0KidXpSzLZY"
    "CpPocrDqcP5Ipke249hlbCC4hmAZY"
    "yfuu1W0X152bc3JRSP5kKOsBkdAZY"
    "40jAlPDdzEvsj78dpGqXkVtG7THZY"
    "LFbbtrPe80pVrg7AnSdj5yjeV"
    "R2EMjYIch4pC3v6rUW55n5yeV"
    "yLSL6l92hHVrtRBth0qoLSyI1veZY"
    "cyrtbxISed5RQccxaxn7ElpeR9OZY"
    "lUYHwJY8RwJ9BcsJIY4O6LreV"
    "OlWkVmNcuhtRSyuiyFa2Zkya23UZY"
    "3dWYpB3cSqO4DKjpQBSS6N9eV"
    "c6duaM91lGGNQ5Mv5Y3z6I9QkeQZY"
    "GV8sWQyJalTFqwSTMunz9ayBE9OZY"
    "vXHz4kDUJ2330VBdtMHa4soeV"
    "TpBUg9ZW11f529Hk3HBtaJseV"
    "8WprF5WnadpQbl1pYREGuEz3LxFZY"
    "PXbj9eoSJk0TxiNlBoAvViveV"
    "Wx4JIAaRG261xio8yNEbPhaMbL6YY"
    "qdngYY7eeqkEn4OsQWZ2YXvboXFZY"
    "d2x2sGHout5s0T7Ovmmfq3teV"
    "vL1dKZtH3i76B6I1xq2Q4CweV"
    "LTWt7GoNyi9GsUKxocssORAfV"
    "088a70M6EISmGmAQWZ70iUKH7pAZY"
    "2H7l6i6j49Wtl0KIdApPNfQm1e8YY"
    ]))

(clojure.set/difference

 my-other-set
 (->> (d/q '[:find ?ext-id
                            :with ?s
                            :in $
                            :where
                            [?s :sales-order/client [:client/code "NGVZ"]]
                            [?s :sales-order/date ?d]
                            [(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]
                            [(= "09/16/2021" ?d4)]
                            [?s :sales-order/external-id ?ext-id]]
                          (d/db conn))
                     (map first)
                     (map (fn [d] (last (str/split d #"-"))))
                     set)
                )

(clojure.pprint/pprint (seq (:returns (:order (order "jfwoeIa1tSyEJGaKO66nLtleV")))))



(lc/with-context {:source "Historical loading data"}
  (doseq [d (clj-time.periodic/periodic-seq (time/plus (time/now) (time/days -20))
                                            (time/now)
                                            (time/days 1))]
    (upsert d))
  (log/info "Done loading historical data"))
