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

o
(init-repl)

(entity-history 17592274428323)

(entity-history 17592274428330)

(entity-history 17592274428430)

(entity-history [:journal-entry/original-entity 17592274428430])

(entity-history 17592274428432)

(entity-history 17592274428441)

17592274428441
17592274428432
(entity-history-with-revert 17592274428429)

(tx-detail 13194227917325)

(defn recently-changed-entities [start end]
   (set (map (fn [d]
               (:e d))
             (mapcat :data (dc/tx-range conn {:start start
                                              :end   end})))))

(def recent
  (d/q '[:find [?t ...]
         :in $ ?log ?start ?end
         :where
         [(tx-ids ?log ?start ?end) [?tx ...]]
         [(tx-data ?log ?tx) [[?t]]]
         [?t :journal-entry/original-entity]]
       (d/db auto-ap.datomic/conn)
       (d/log auto-ap.datomic/conn)
       #inst "2022-09-27"
       #inst "2022-10-01"))

(clojure.pprint/pprint (d/pull-many (d/db auto-ap.datomic/conn) '[* {:journal-entry/original-entity [*]}] (take 50 recent)))

(doseq [ recent (partition-all 100  recent )]
  (println "batch")
  @(d/transact auto-ap.datomic/conn (mapv (fn [r]
                                           [:db/retractEntity r])
                                         recent)))

(def recent (d/q '[:find [?je ...] #_(pull ?je [* {:journal-entry/original-entity [*]}])
              :in $
              :where
              [?je :journal-entry/date ?d]
              [(>= ?d #inst "2022-01-01")]
              [?je :journal-entry/original-entity ?o]
              (not [?o :invoice/date])
              (not [?o :transaction/date])]
             (d/db conn)))

(def recent-invoices-without-journal-entries
  (d/q '[:find [?t ...]
         :in $ ?log ?start ?end
         :where
         [(tx-ids ?log ?start ?end) [?tx ...]]
         [(tx-data ?log ?tx) [[?t]]]
         [?t :invoice/date]
         (not [?t :invoice/status :invoice-status/voided])
         (not [?t :invoice/import-status :import-status/pending])
         (not [_ :journal-entry/original-entity ?t])]
       (d/db auto-ap.datomic/conn)
       (d/log auto-ap.datomic/conn)
       #inst "2022-09-25"
       #inst "2022-10-01"))

(count recent-invoices-without-journal-entries)
(d/pull-many (d/db conn) '[*] recent-invoices-without-journal-entries)

(doseq [recent recent-invoices-without-journal-entries]
  #_(clojure.pprint/pprint b)
  (println "recent" recent)
  (when-let [result (auto-ap.ledger/entity-change->ledger (d/db auto-ap.datomic/conn) [:invoice recent])]
    @(d/transact auto-ap.datomic/conn [result])))


(def recent-transactions
  (d/q '[:find [?t ...]
         :in $ ?log ?start ?end
         :where
         [(tx-ids ?log ?start ?end) [?tx ...]]
         [(tx-data ?log ?tx) [[?t]]]
         [?t :transaction/date]
         (not [_ :journal-entry/original-entity ?t])
         (not [?t :transaction/approval-status :transaction-approval-status/excluded])
         (not [?t :transaction/approval-status :transaction-approval-status/suppressed])]
       (d/db auto-ap.datomic/conn)
       (d/log auto-ap.datomic/conn)
       #inst "2022-09-25"
       #inst "2022-10-01"))

(count recent-transactions)

(d/pull-many (d/db conn) '[* {:transaction/client [:client/code]}
                           {:transaction/approval-status [:db/ident]}] recent-transactions)

(doseq [recent recent-transactions]
  #_(clojure.pprint/pprint b)
  (println "recent" recent)
  (when-let [result (auto-ap.ledger/entity-change->ledger (d/db auto-ap.datomic/conn) [:transaction recent])]
    @(d/transact auto-ap.datomic/conn [result])))


(def full-l (full-ledger-for-client [:client/code "VS"]))
(take 5 full-l)

(def lookup (build-account-lookup full-l))

(take 3 lookup)

(def rolled-up (roll-up-until lookup full-l #inst "2022-09-30"))
(reverse (sort-by first (filter (comp #{"CASH"} :name) rolled-up )))

(->> full-l
     seq
     (filter (fn [[_ _ a]]
               (= a 17592238743454))))

(:db/id (d/pull (d/db auto-ap.datomic/conn) [:db/id] [:bank-account/code "VS-0"]))
17592238743454

(d/pull (d/db auto-ap.datomic/conn) '[* {:journal-entry/line-items [*]} {:journal-entry/original-entity [{:transaction/bank-account [:bank-account/include-in-reports]}]}]
        17592274484132)

(->> (d/query 
      {:query {:find ['?d '?jel '?account '?location '?debit '?credit]
               :in ['$ '?client-id '?e]
               :where '[[?e :journal-entry/client ?client-id]
                        [?e :journal-entry/date ?d]
                        [?e :journal-entry/line-items ?jel]
                        (or-join [?e]
                                 (and [?e :journal-entry/original-entity ?i]
                                      (or-join [?e ?i]
                                               (and 
                                                [?i :transaction/bank-account ?b]
                                                (or [?b :bank-account/include-in-reports true]
                                                    (not [?b :bank-account/include-in-reports])))
                                               (not [?i :transaction/bank-account])))
                                 (not [?e :journal-entry/original-entity ]))
                        [(get-else $ ?jel :journal-entry-line/account :account/unknown) ?account]
                        [(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit ]
                        [(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
                        [(get-else $ ?jel :journal-entry-line/location "") ?location]]
               }
       :args [(d/db (d/connect uri)) [:client/code "VS"] 17592274484132]})
       )

(user/entity-history 17592271747362)

(user/init-repl)

@(d/transact auto-ap.datomic/conn
             (->> (d/q '[:find ?t ?p ?je (max ?d) 
                        :where
                        [?i :invoice/date ?d]
                        [(>= ?d #inst "2022-01-01T00:00:00-08:00")]
                        [?ip :invoice-payment/invoice ?i]
                        [?ip :invoice-payment/payment ?p]
                        [?p :payment/bank-account ?ba]
                        [?ba :bank-account/type :bank-account-type/cash]
                        [?t :transaction/payment ?p]
                        [?t :transaction/date ?d2]
                        [?je :journal-entry/original-entity ?t]
                        ]
                      (d/db auto-ap.datomic/conn))
                 (mapcat (fn [[t p je d]]
                           [{:transaction/date d
                             :db/id            t}
                            {:payment/date d
                             :db/id        p}
                            {:journal-entry/date d
                             :db/id je}]))))


