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


(user/init-repl)

(clojure.data.csv/write-csv *out* 

                            (->> (d/q '[:find
                                        (pull ?c [:client/code])
                                        (pull ?v [:vendor/name
                                                  {:vendor/legal-entity-1099-type [:db/ident]}
                                                  {:vendor/legal-entity-tin-type [:db/ident]}
                                                  {:vendor/address [:address/street1
                                                                    :address/city
                                                                    :address/state
                                                                    :address/zip]}
                                                  :vendor/legal-entity-first-ein
                                                  :vendor/legal-entity-first-name
                                                  :vendor/legal-entity-middle-name
                                                  :vendor/legal-entity-last-name])
                                        (sum ?a)
                                        :in $
                                        :where [?p :payment/date ?d ]
                                        [(>= ?d #inst "2022-01-01T08:00")]
                                        [(< ?d #inst "2023-01-01T08:00")]
                                        [?p :payment/client ?c]
                                        [?p :payment/amount ?a]
                                        [?p :payment/type :payment-type/check]
                                        [?p :payment/vendor ?v]]
                                      (d/db auto-ap.datomic/conn))
                                 (filter (fn [[_ _ a]]
                                           (>= a 600.0)))
                                 (map (fn [[client vendor amount]]
                                        [(:client/code client)
                                         (:vendor/name vendor)
                                         (some-> vendor :vendor/legal-entity-1099-type :db/ident name)
                                         (-> vendor :vendor/legal-entity-first-name)
                                         (-> vendor :vendor/legal-entity-middle-name)
                                         (-> vendor :vendor/legal-entity-last-name)
                                         (some-> vendor :vendor/legal-entity-tin-type :db/ident name)
                                         (-> vendor :vendor/legal-entity-tin)
                                         (-> vendor :vendor/address :address/street1)
                                         (-> vendor :vendor/address :address/street2)
                                         (-> vendor :vendor/address :address/city)
                                         (-> vendor :vendor/address :address/state)
                                         (-> vendor :vendor/address :address/zip)
                                         amount
                                         ]))
                                 (sort )
                                 (into [["Client" "Vendor Name" "1099 Type" "First Name" "Middle Name" "Last Name" "TIN type" "TIN" "Street" "Street 2" "City" "State" "Zip"]]))
                            :quote? (constantly true))

