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

(println "hi")

(user/init-repl)

(require '[auto-ap.square.core3 :as square3])
(require '[auto-ap.time-reader])

(def result (first (for [c     (square3/get-square-clients "NGPG")
                         l     (:client/square-locations c)
                         :when (:square-location/client-location l)]
                     (->> @(square3/search c l #clj-time/date-time "2022-01-25" #clj-time/date-time "2022-01-27")
                          (mapcat :line_items)
                          
                          (group-by (juxt :catalog_object_id :catalog_version))
                          (map (fn [[[ci cv] v]]
                                 [@(square3/item-id->category-name c ci cv) (count v)]))))))


(take 5 result)

(reduce +  0 (map second result))


(def result (first (for [c     (square3/get-square-clients "NGPG")
                         l     (:client/square-locations c)
                         :when (:square-location/client-location l)]
                     (->> @(square3/daily-results c l #clj-time/date-time "2022-01-25" #clj-time/date-time "2022-01-27")
                          (mapcat :sales-order/line-items)
                          count
                          
                          #_(group-by (juxt :catalog_object_id :catalog_version))
                          #_(map (fn [[[ci cv] v]]
                                 [@(square3/item-id->category-name c ci cv) (count v)]))))))




(def result (d/q '[:find (pull ?i [:db/id :invoice/invoice-number {:invoice/client [:client/code]} [:client/code]   :invoice/date {:invoice/expense-accounts [{:invoice-expense-account/account [:account/numeric-code]}]
                                                                                                                                    :invoice/vendor [:vendor/name {:vendor/default-account [:account/numeric-code]}]}])
                   :in $ ?m
                   :where [?i :invoice/date ?d]
                   [(>= ?d #inst "2022-01-01T00:00:00-08:00")]
                   [?i :invoice/invoice-number ?n]
                   [(re-find ?m ?n)]
                   (not [?i :invoice/status :invoice-status/voided])]
                 (d/db auto-ap.datomic/conn)
                 (re-pattern "(?i)cash")))

(clojure.data.csv/write-csv *out* 
                            (->> result
                                 (map (fn [[i]]
                                        [(-> i :db/id)
                                         (-> i :invoice/client :client/code)
                                         (-> i :invoice/date clj-time.coerce/to-date-time (auto-ap.time/unparse auto-ap.time/iso-date))
                                         (-> i :invoice/invoice-number)
                                         (->> i :invoice/expense-accounts (map (comp :account/numeric-code :invoice-expense-account/account)) (str/join ", "))
                                         (-> i :invoice/vendor :vendor/name)
                                         (-> i :invoice/vendor :vendor/default-account :account/numeric-code)
                                         ]))
                                 (sort-by (fn [[_ x y]]
                                            [x y])))
                            :separator \tab)

(count result)


(def result (first (for [c     (square3/get-square-clients "NGOP")
                         l     (:client/square-locations c)
                         :when (:square-location/client-location l)]
                     #_(square3/get-order c l "jd2T2IPneyaHaBYNbrATg1qeV")
                     (->> @(square3/search c l #clj-time/date-time "2023-02-01" #clj-time/date-time "2023-02-04")
                          (filter (fn [o]
                                    (= (:id o) "jd2T2IPneyaHaBYNbrATg1qeV")
                                    #_(= ["CUSTOM_AMOUNT"]
                                       (mapv :item_type (:line_items o )))))
                          (map #(deref (square3/order->sales-order c l %)))))))

(square3/get-order )
;; jd2T2IPneyaHaBYNbrATg1qeV
result

(require '[datomic.api :as d])

(d/pull (d/db auto-ap.datomic/conn) '[* {:expected-deposit/_charges [*]}] [:charge/external-id "square/charge/dwrB4neXdgMcoiahg6504FOxvaB",])


(->> (d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip) 
            :in $
            :where
            [?charge :charge/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)]
            [?charge :charge/client ?c]
            [?c :client/code "NGOP"]
            [?charge :charge/type-name ?type]
            [?charge :charge/total ?total]
            [?charge :charge/tip ?tip]
            [?charge :charge/external-id ?eid]
            (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))
     seq)


(in-ns 'auto-ap.ledger)

(unbalanced-invoices)


(def unbalance-2 (unbalanced-invoices))

unbalance-2

(for [u unbalance]
  (d/pull (d/db conn) '[* {:journal-entry/_original-entity [*]}] u))


(entity-change->ledger (d/db conn) [:invoice (first unbalance)])

(transact-with-ledger
  [{:db/id                                      17592214378663,
    :invoice-expense-account/amount             1034.52,}
   {:db/id                                      17592214378678,
    :invoice-expense-account/amount             17.9,}
   {:db/id 17592218322713
    :invoice-expense-account/amount 254.28}
   {:db/id                                      17592219719913,
    :invoice-expense-account/amount             -1783.15,}
   {:db/id                                      17592222153002,
    :invoice-expense-account/amount             196.08}]

  {:user/name "Bryce fixing one-off errors from invoice update"})

(user/entity-history-with-revert 17592275172406)

(user/entity-history-with-revert 13194240237619)

(user/entity-history 13194238151156)


(def unbalance-4 (unbalanced-transactions))

(def unbalance-3 [17592275172406 17592261857304 17592275269571])

(for [u unbalance-3]
  (d/pull (d/db conn) '[* {:journal-entry/_original-entity [*]}] u))

(transact-with-ledger
  [[:db/retractEntity 17592286748717]
   [:db/retractEntity 17592277209926]
   [:db/retractEntity 17592270946326]
   ]
  {:user/name "Bryce fixing one-off errors from invoice update"})
