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

(init-repl)

(d/pull (d/db auto-ap.datomic/conn) '[* {:invoice/client [:client/code] :invoice/vendor [:vendor/name]}] 17592253143914)

(entity-history 17592253143914 )
(entity-history 13194206632809)

(d/pull (d/db auto-ap.datomic/conn ) '[*] [:client/code "AFH"])

(auto-ap.parse/best-match (auto-ap.datomic.clients/get-all) "NACHMARKET"  0.5)


(println "hi")

(user/init-repl)

(def z (get-in (get-profit-and-loss {:id {:user/role "admin"}}
                                    {:client_id 17592244691558
                                     :periods   [{:start #inst "2021-01-01"
                                                  :end   #inst "2021-12-31"}]}
                                    nil)
               [:periods 0 :accounts]))


(filter #(not (:numeric_code %)) z)

(d/q '[:find (pull ?je [*])
       :in $
       :where [?je :journal-entry/client [:client/code "NGWH"]]
       [?je :journal-entry/date ?d]
       [?je :journal-entry/line-items ?jel]
       (not [?jel :journal-entry-line/account])]

     (d/db conn))


(d/pull (d/db conn) '[*] 17592253348380)
(user/entity-history 17592253348380)

(d/pull (d/db conn) '[*] 13194206837275)

[(d/pull (d/db conn) '[*] 17592249726033)
 (d/pull (d/db conn) '[*] 17592248612376)]

[(user/entity-history-with-revert 17592249726033)
 (user/entity-history-with-revert 17592248612376)]

(d/pull (d/db conn) '[*] 17592186046415)

(user/entity-history 13194203214928)
;; => [[13194203214928 :db/txInstant #inst "2021-12-01T03:04:17.049-00:00"]
;;     [13194203214928 :audit/user ":admin-Yodlee import"]]


(user/entity-history 13194202101271)
;; => [[13194202101271 :audit/user ":admin-Yodlee import"]
;;     [13194202101271 :db/txInstant #inst "2021-11-25T16:32:46.542-00:00"]]

(user/entity-history 13194203197770)
;; => [[13194203197770 :db/txInstant #inst "2021-12-01T02:07:33.416-00:00"]]



(->> (d/db conn)
     (d/q '[:find ?ba (count ?d)
            :in $
            :where [?ba :bank-account/intuit-bank-account ?d]])
     #_(filter (fn [[_ x]]
               (> x 1))))


(d/pull (d/db auto-ap.datomic/conn) '[* {:transaction/bank-account [*]}] 17592204620282);; => {:transaction/bank-account
;;     {:bank-account/sort-order 5,
;;      :bank-account/include-in-reports true,
;;      :bank-account/numeric-code 11307,
;;      :bank-account/yodlee-account-id 16428403,
;;      :bank-account/number "9392",
;;      :bank-account/code "HIM-5",
;;      :bank-account/current-balance 2063.2899999998335,
;;      :bank-account/external-id 5,
;;      :bank-account/name "HIM Citi Visa Costco 9392",
;;      :db/id 17592186046421,
;;      :bank-account/visible true,
;;      :bank-account/type #:db{:id 17592186045425},
;;      :bank-account/bank-name "Citi Visa CC",
;;      :bank-account/original-id "55-5"},
;;     :transaction/date #inst "2019-08-03T07:00:00.000-00:00",
;;     :transaction/type "PURCHASE",
;;     :transaction/client #:db{:id 17592186046415},
;;     :transaction/status "POSTED",
;;     :transaction/account-id 16428403,
;;     :db/id 17592204620282,
;;     :transaction/id
;;     "192dc451434cb0fc8698fae563618807fddfbc16e2aae8adc488d3dbb1cb5016",
;;     :transaction/description-original "TAO TAO CAFE SUNNYVALE CA",
;;     :transaction/approval-status #:db{:id 17592231963878},
;;     :transaction/amount -40.5,
;;     :transaction/description-simple "TAO TAO CAFE SUNNYVALE CA"}

;; => {:transaction/bank-account #:db{:id 17592186046421},
;;     :transaction/date #inst "2019-08-03T07:00:00.000-00:00",
;;     :transaction/type "PURCHASE",
;;     :transaction/client #:db{:id 17592186046415},
;;     :transaction/status "POSTED",
;;     :transaction/account-id 16428403,
;;     :db/id 17592204620282,
;;     :transaction/id
;;     "192dc451434cb0fc8698fae563618807fddfbc16e2aae8adc488d3dbb1cb5016",
;;     :transaction/description-original "TAO TAO CAFE SUNNYVALE CA",
;;     :transaction/approval-status #:db{:id 17592231963878},
;;     :transaction/amount -40.5,
;;     :transaction/description-simple "TAO TAO CAFE SUNNYVALE CA"}

(d/pull (d/db conn) '[*] 17592233127577)
;; => {:transaction/bank-account #:db{:id 17592186046421},
;;     :transaction/date #inst "2020-07-29T07:00:00.000-00:00",
;;     :transaction/type "PURCHASE",
;;     :transaction/client #:db{:id 17592186046415},
;;     :transaction/status "POSTED",
;;     :transaction/account-id 16428403,
;;     :db/id 17592233127577,
;;     :transaction/id
;;     "217ce419afa1daa217ca118d10ddcfa11e757788b48990bf43d92faf9250e531",
;;     :transaction/description-original "TAO TAO CAFE SUNNYVALE CA",
;;     :transaction/approval-status #:db{:id 17592231963878},
;;     :transaction/amount -49.7,
;;     :transaction/description-simple "tao tao cafe"}


(user/init-repl)

(d/pull (d/db conn) '[* {:transaction/approval-status [:db/ident]}] 17592251272764);; => {:transaction/bank-account #:db{:id 17592234230556},
;;     :transaction/date #inst "2021-12-06T08:00:00.000-00:00",
;;     :transaction/check-number 387,
;;     :transaction/client #:db{:id 17592234230520},
;;     :transaction/status "POSTED",
;;     :db/id 17592251272764,
;;     :transaction/id
;;     "c15d3d1cad77b4a79510877f464b909e9ec9c8c4bfe9885008666526d6ca9885",
;;     :transaction/description-original "Check Paid CHECK 387",
;;     :transaction/approval-status
;;     #:db{:ident :transaction-approval-status/suppressed},
;;     :transaction/amount -12411.0,
;;     :transaction/raw-id
;;     "2021-12-06T00:00:00.000-08:00-17592234230556-Check Paid CHECK 387--12411.0-0-17592234230520"}


(def z (auto-ap.yodlee.core/get-specific-transactions-with-date "16428403" "2021-01-01" "2021-12-31" (auto-ap.yodlee.core/get-auth-header)))

(count z)
(csv/write-csv *out*
               (->> z
                    (sort-by :date)
                    (map (fn [z]
                           (update z :date #(auto-ap.time/unparse (auto-ap.time/parse % auto-ap.time/iso-date) auto-ap.time/normal-date))))
                    (map (fn [z]
                           [(:date z) (:original (:description z)) (:original (:description z)) "" (if (= "DEBIT" (:baseType z))
                                                                                                     (-  (:amount (:amount z)))
                                                                                                     (:amount (:amount z)))]
                           ))
                    
                    )

               :separator \tab
               )

(count z)

(def z-p (d/q '[:find (pull ?t [:transaction/date :transaction/description-original :transaction/amount])
                :in $ ?ba
                :where [?t :transaction/bank-account ?ba]
                [?t :transaction/date ?d]
                [(>= ?d #inst "2021-01-01T08:00:00")]
                [(< ?d #inst "2022-01-01T08:00:00")]
                (not [?t :transaction/approval-status :transaction-approval-status/suppressed])]
              (d/db auto-ap.datomic/conn)
              [:bank-account/code "HIM-5"]))

(csv/write-csv *out*
               (->> z-p
                    (map first)
                    (sort-by :transaction/date)
                    (map (fn [z]
                           (update z :transaction/date #(auto-ap.time/unparse-local (clj-time.coerce/to-date-time %) auto-ap.time/normal-date))))
                    (map (fn [z]
                           [(:transaction/date z) (:transaction/description-original z) (:transaction/description-original z) "" (:transaction/amount z)]
                           ))
                    
                    )

               :separator \tab
               )


(init-repl)

