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

(def g (auto-ap.ledger/unbalanced-invoices))

(count g)

(take 3 g)

(clojure.data.csv/write-csv *out*
                            (->> g
                                 (map (fn [i]
                                        (let [e (d/pull (d/db auto-ap.datomic/conn)
                                                        '[:invoice/total
                                                          :db/id
                                                          {:invoice/client [:client/code]
                                                           :invoice/expense-accounts [*]}
                                                          {:journal-entry/_original-entity [:journal-entry/amount
                                                                                            {:journal-entry/line-items [*]} ]}]
                                                        i)
                                              je (first (:journal-entry/_original-entity e))]

                                          [(:db/id e)
                                           (:client/code  (:invoice/client e))
                                           (:invoice/total e)
                                           (:journal-entry/amount je)
                                           (clojure.string/join ", " (filter identity (map :journal-entry-line/credit (:journal-entry/line-items je))))
                                           (clojure.string/join ", " (filter identity (map :journal-entry-line/debit (:journal-entry/line-items je))))
                                           (count (:invoice/expense-accounts e))
                                           (count (:journal-entry/line-items je))]
                                          ))))
                            :separator \tab)


(clojure.pprint/pprint (d/pull (d/db auto-ap.datomic/conn) '[*] 17592232790166))

(clojure.pprint/pprint (user/entity-history 17592232790174))



(user/tx-detail 13194187889225)

(defn updated-recently? [i]
  (d/q '[:find ?i
         :in $ ?i
         :where [?i :invoice/date]]
       (d/since (d/db auto-ap.datomic/conn) #inst "2021-11-20")
       i))


(def z (for [[client] (d/q '[:find ?c
                             :in $ [?code ...]
                             :where
                             [?c :client/code ?code]]
                           (d/db auto-ap.datomic/conn)
                           ["MDB" "NGT" "AFH" "WGC" "RCI" "MVSC" "OMG" "BSG" "BCBG" "NGMJ" "BCBC"])
             :let [results  (d/q '[:find ?i ?z ?a ?b
                                   :in $ ?c
                                   :where
                                   [?i :invoice/client ?c]
                                   [?i :invoice/total ?a]
                                   [?i :invoice/expense-accounts ?z]
                                   [?z :invoice-expense-account/amount ?b]]
                                 (d/db auto-ap.datomic/conn) client)
                   results (->> results
                                (group-by first)
                                (map (fn [[i v]]
                                       (let [[[_ _ total]] v]
                                         [i total (reduce + 0
                                                              (map (fn [[_ _ _ a]]
                                                                     a)
                                                                   v))])))
                                (filter (fn  [[i total ea ]]
                                          (not (auto-ap.utils/dollars= total ea)))))]
             [i _ ea] results
             :when (updated-recently? i)]
         {:db/id i
          :invoice/total ea}))

(clojure.pprint/pprint z)

@(d/transact auto-ap.datomic/conn (conj z
                       {:db/id "datomic.tx"
                        :db/doc "Fixing issue where invoice amounts are not adding up"}) )

(count (auto-ap.ledger/unbalanced-invoices))




(clojure.pprint/pprint (d/pull (d/db auto-ap.datomic/conn) '[*] 17592204815836))

(entity-history 17592204815839)

(d/pull (d/db auto-ap.datomic/conn) [:client/locations] [:client/code "WGC"] )
