(init-repl) (comment (defn setup-randy-queries [] (import '[java.util UUID]) (def my-id "a4345849-00a7-424e-b393-06257d3c1820") (auto-ap.routes.queries/put-query my-id (str '[:find ?an ?cn ?a2 ?fmt-date ?action ?payee :where [(ground ["SCSJ-USB9606" "SCCB-USB9598" "SCMH-USB4250" "SCM8-UB3082" "PWL-PWL2370"]) [?bac ...]] [(iol-ion.query/recent-date 5) ?sd] [(ground (clj-time.format/with-zone (clj-time.format/formatter "MMddyyyy") (clj-time.core/time-zone-for-id "America/Los_Angeles"))) ?fmt] [?ba :bank-account/code ?bac] [?ba :bank-account/number ?an] [?p :payment/bank-account ?ba] [?p :payment/date ?pd] [(>= ?pd ?sd)] [?p :payment/type :payment-type/check] [?p :payment/amount ?a] [(format "%.2f" ?a) ?a2] [?p :payment/check-number ?cn] [(clj-time.coerce/from-date ?pd) ?pd2] [(clj-time.format/unparse ?fmt ?pd2) ?fmt-date] [(ground "IS") ?action] [?p :payment/vendor ?v] [(get-some $ ?v :vendor/print-as :vendor/name) [_ ?payee]]]) "One-off for randy")) (setup-randy-queries) (dc/q '[:find ?cd ?bal (count ?l) :where [?b :bank-account/locations ?bal] [?c :client/bank-accounts ?b] [?c :client/code ?cd] [?c :client/locations ?l]] (dc/db conn)) (init-repl) (seq (dc/q '[:find ?a ?n ?n2 :where [?a :account/name ?n] [?a :account/numeric-code ?n2] (not [?a :account/code])] (dc/db conn))) (dc/q '[:find (pull ?a [* {:account/applicability [:db/ident] :account/default-allowance [*]}]) :where [?a :account/numeric-code 34090]] (dc/db conn)) (dc/q '[:find ?a :where [?a :account/numeric-code ?nc] (not [?a :account/default-allowance])] (dc/since (dc/db conn) #inst "2023-02-01")) @(dc/transact conn (->> (dc/q '[:find ?a :where [?a :account/numeric-code ?nc] (not [?a :account/default-allowance])] (dc/since (dc/db conn) #inst "2023-02-01")) (map (fn [[a]] {:db/id a :account/default-allowance :allowance/allowed})))) (dc/q '[:find (pull ?l [*]) :in $ ?a :where [?a :invoice/client] [?l :journal-entry/original-entity ?a]] (dc/db conn) 17592316421929) (dc/pull (dc/db conn) '[*] 17592316421929) (entity-history 17592316421929) (dc/q '[:find (pull ?l [*]) :in $ ?a :where [?a :invoice/client] [?l :journal-entry/original-entity ?a]] (dc/db conn) 17592316421929) ;; Find journal entries that have been divorced from the original entity @(dc/transact auto-ap.datomic/conn (->> (dc/q '[:find ?l :in $ $$ $$$ :where [$$ ?l :journal-entry/amount] (not [$ ?l :journal-entry/external-id]) [$ ?l :journal-entry/source "invoice"] (not [$ ?l :journal-entry/original-entity]) [$ ?l :journal-entry/client ?c] [$ ?c :client/code ?cd] [$$$ ?l :journal-entry/original-entity _ ?tx false]] (dc/db conn) (dc/since (dc/db conn) #inst "2024-02-04") (dc/history (dc/db conn))) (map (fn [[jl]] [:db/retractEntity jl])) seq)) (entity-history 13194269907490) (user/tx-detail 13194269907766) (dc/tx-range (dc/log conn) 13194269907490 13194269907490) (dc/q '[:find ?l :in $ $$ $$$ :where [$$ ?l :journal-entry/amount] (not [$ ?l :journal-entry/external-id]) [$ ?l :journal-entry/source "invoice"] (not [$ ?l :journal-entry/original-entity]) [$ ?l :journal-entry/client ?c] [$ ?c :client/code ?cd] [$$$ ?l :journal-entry/original-entity _ ?tx false]] (dc/db conn) (dc/since (dc/db conn) #inst "2024-02-04") (dc/history (dc/db conn))) (dc/transact conn (->> (dc/q '[:find ?v :in $ :where [?v :vendor/plaid-merchant] (not [?v :vendor/name]) [?t :transaction/vendor ?v] [?t :transaction/date ?d]] (dc/db conn)) (mapv (fn [[i]] [:db/retractEntity i])))) @(dc/transact conn (->> (dc/q '[:find ?c :where [?c :client/name ?n] [(clojure.string/upper-case ?n) ?n2] [(clojure.string/includes? ?n2 "NICK")]] (dc/db conn)) (map (fn [[c]] {:db/id c :client/groups ["NTG"]})))) (dc/q '[:find (count ?je) :in $$ :where [$$ ?je :journal-entry/client 17592238607837]] (dc/before (dc/since (dc/db conn) #inst "2024-02-13T15:20:00-08:00") #inst "2024-02-13T17:00:00-08:00")) (user/init-repl) (let [start #inst "2024-02-13T11:20:00-08:00" end #inst "2024-02-13T18:00:00-08:00"] (->> (dc/tx-range (dc/log conn) start end) seq (mapcat :data) (filter (fn [{:keys [a] :as z}] (= "journal-entry" (str (namespace (auto-ap.datomic/pull-attr (dc/db conn) :db/ident a)))))) (map :e) (into #{}) (map (fn [m] [(try (dc/pull (dc/as-of (dc/db conn) start) '[{:journal-entry/client [:client/code]} :db/id *] m) (catch Exception e nil)) (try (dc/pull (dc/as-of (dc/db conn) end) '[{:journal-entry/client [:client/code]} :db/id *] m) (catch Exception e nil))])) (filter (fn [[l r]] (not= l r))) (filter (fn [[l r]] (or (= "NGRI" (-> l :journal-entry/client :client/code)) (= "NGRI" (-> r :journal-entry/client :client/code))))) count)) (auto-ap.routes.queries/get-query {:identity {:user/role "admin"} :params {:query-id "a4345849-00a7-424e-b393-06257d3c1820"}}) (auto-ap.datomic/pull-id (dc/db conn) [:client/code "NGRV"]) (->> (dc/q '[:find (pull ?ba [:bank-account/name :bank-account/code {:client/_bank-accounts [:client/code]}]) :in $ $$ :where [$$ ?ba :bank-account/include-in-reports false ?tx true] [(get-else $ ?ba :bank-account/include-in-reports false) ?include] [(= ?include false)] [?tx :db/txInstant ?d] [(>= ?d #inst "2024-02-01")]] (dc/db conn) (dc/history (dc/db conn))) (map (fn [[ba]] (println ba) [(-> ba :client/_bank-accounts :client/code) (:bank-account/code ba) (:bank-account/name ba)])) (#(clojure.data.csv/write-csv *out* % :separator \tab)))) (comment (init-repl) (entity-history 17592317777728) (entity-history-with-revert 17592310252806) (sort (dc/q '[:find ?d ?tx :in $ $$ :where [?v :vendor/plaid-merchant _] (not [?v :vendor/name]) [$$ ?v :vendor/plaid-merchant _ ?tx true] [?tx :db/txInstant ?d]] (dc/db conn) (dc/history (dc/db conn)))) (map (juxt :e (fn [d] (auto-ap.datomic/pull-attr (dc/db conn) :db/ident (:a d))) :v) (:data (first (dc/tx-range (dc/log conn) 13194263741698 13194263741699)))) @(dc/transact conn (->> (dc/q '[:find ?v :where [?v :vendor/plaid-merchant _] (not [?v :vendor/name])] (dc/db conn)) (map (fn [[v]] [:db/retractEntity v]))))) (comment (->> (dc/q {:find ['?vendor-id '?e '?outstanding-balance '?d] :in ['$ '?client-id] :where ['[?e :invoice/client ?client-id] '[?e :invoice/status :invoice-status/unpaid] '(not [_ :invoice-payment/invoice ?e]) '[?e :invoice/vendor ?vendor-id] '[?e :invoice/outstanding-balance ?outstanding-balance] '[?e :invoice/date ?d]]} (dc/db conn) 17592232545948) (sort-by last) ;; sort by scheduled payment date (group-by first) ;; group by vendors vals count)) (comment (init-repl) (defn delete-transactions [all-ids] (let [db (dc/db conn)] (auto-ap.logging/info ::bulk-delete-transactions :count (count all-ids) :sample (take 3 all-ids)) (auto-ap.datomic/audit-transact-batch (mapcat (fn [i] (let [transaction (dc/pull db [:transaction/payment :transaction/expected-deposit :db/id] i) payment-id (-> transaction :transaction/payment :db/id) expected-deposit-id (-> transaction :transaction/expected-deposit :db/id)] (cond->> [[:db/retractEntity [:journal-entry/original-entity i]]] payment-id (into [{:db/id payment-id :payment/status :payment-status/pending} [:db/retract (:db/id transaction) :transaction/payment payment-id]]) expected-deposit-id (into [{:db/id expected-deposit-id :expected-deposit/status :expected-deposit-status/pending} [:db/retract (:db/id transaction) :transaction/expected-deposit expected-deposit-id]])))) all-ids) {:user/name "Cleaning up bad transactions that are duplicates"}) (auto-ap.datomic/audit-transact-batch (mapcat (fn [i] (let [transaction-tx [:db/retractEntity i]] [transaction-tx [:db/retractEntity [:journal-entry/original-entity i]]])) all-ids) {:user/name "Cleaning up bad transactions that are duplicates"}) {:message (str "Succesfully deleted " (count all-ids) " transactions.")})) (def bank-accounts (dc/q '[:find ?ba ?ya :where [?ba :bank-account/yodlee-account ?ya] [?ba :bank-account/plaid-account]] (dc/db conn))) (def bank-accounts (dc/q '[:find ?ba :in $ [?client-code ...] :where [?c :client/code ?client-code] [?c :client/bank-accounts ?ba]] (dc/db conn) [ "SKPL" "NGFT" "NGFN" "NGLM"])) (let [yodlee-accounts (->> bank-accounts (map (fn [[ba ya]] [:db/retract ba :bank-account/yodlee-account ya]))) bad-transactions (->> (dc/q '[:find ?t :in $ [?ba ...] :where [(ground #inst "2024-01-25") ?start-date] [?ib :import-batch/source :import-source/yodlee2] [?ib :import-batch/date ?d] [(>= ?d ?start-date)] [?ib :import-batch/entry ?t] [?t :transaction/bank-account ?ba]] (dc/db conn) (map first bank-accounts) #_[[:bank-account/code "NGFN-CB6375"]]) (map first))] (delete-transactions bad-transactions) #_(count bad-transactions)) #_(seq (dc/tx-range (dc/log conn) #inst "2024-02-28T19:25:00-08:00" #inst "2024-02-29T19:35:00-08:00")) #_(dc/pull (dc/db conn) [:transaction/payment :transaction/expected-deposit :db/id] 17592317964222) #_(dc/pull (dc/db conn) '[*] 17592302069146) @(dc/transact conn [{:bank-account/code "NGKG-AMEX81007" :bank-account/visible true}]) ) (clojure.data.csv/write-csv *out* (let [db (dc/db conn)] (dc/q '[:find ?d4 ?s (sum ?total) :in $ [?clients ?start-date ?end-date] :where [(iol-ion.query/scan-sales-orders $ ?clients ?start-date ?end-date) [[?e _ ?sort-default] ...]] [?e :sales-order/source ?s] [?e :sales-order/total ?total] [?e :sales-order/date ?d] [(iol-ion.query/excel-date ?d) ?d4]] db [ [ (pull-attr db :db/id [:client/code "NGA1"])] #inst "2023-01-01" #inst "2024-01-01"])) :separator \tab) (pull-attr (dc/db conn) :db/id [:client/code "NGRV"]) (clojure.data.csv/write-csv *out* (let [db (dc/db conn)] (dc/q '[:find ?d4 (sum ?a) :in $ [?clients ?start-date ?end-date] ?v :where [(iol-ion.query/scan-transactions $ ?clients ?start-date ?end-date) [[?e _ ?sort-default] ...]] [?e :transaction/date ?d] [(iol-ion.query/excel-date ?d) ?d4] [?e :transaction/amount ?a] [?e :transaction/vendor ?v]] db [ [ (pull-attr db :db/id [:client/code "NGA1"])] #inst "2023-01-01" #inst "2024-01-01"] (ffirst (dc/q '[:find ?v :where [?v :vendor/name "CCP Doordash"]] db)))) :separator \tab) (def taptelis-clients (->> (dc/q '[:find ?c :where [?u :user/name ?n] [?u :user/clients ?c] [?c :client/code ?cc] [(clojure.string/includes? ?n "Nick Taptelis")]] (dc/db conn)) (map first) set)) (clojure.data.csv/write-csv *out* (dc/q '[:find ?v ?cnt :in $ [?c ...] :where [?vu :vendor-usage/vendor ?v] [?vu :vendor-usage/client ?c] [?vu :vendor-usage/count ?cnt] [(> ?cnt 0)]] (dc/db conn) taptelis-clients) :separator \tab) (comment (defn cleanup-duplicate-vendors-get-merge [] (with-open [i (io/reader (io/resource "duplicate_vendors.csv"))] (let [[header & rest] (clojure.data.csv/read-csv i)] (->> rest (map (fn [h r] (into {} (map vector h r))) (repeat header)) (filter (fn [row] (not= (get row "Ben says don't merge?") "TRUE"))) (map (fn [row] {:from (Long/parseLong (get row "vendor_id")) :to (Long/parseLong (get row "master_vendor_id"))})) (filter (fn [row] (not= (:from row) (:to row)))) (into [])))) ) (cleanup-duplicate-vendors-get-merge) (defn merge-vendor [{:keys [from to]}] (let [valid-keys #{:transaction/recommended-vendor :cash-drawer-shift/vendor :payment/vendor :journal-entry/vendor :sales-refund/vendor :transaction/vendor :sales-order/vendor :vendor-usage/vendor :transaction-rule/vendor :invoice/vendor :expected-deposit/vendor} transaction (->> (dc/q {:find '[?x ?a2] :in '[$ [ ?a2 ...] ?vendor-from] :where ['[?x ?a2 ?vendor-from] ]} (dc/db conn) valid-keys from) (mapcat (fn [[src attr]] [[:db/retract src attr from] [:db/add src attr to]])) (into []))] (auto-ap.datomic/audit-transact-batch transaction {:user/role "VENDOR-DEDUPE-CLEANUP"}) (auto-ap.datomic/audit-transact [[:db/retractEntity from]] {:user/role "VENDOR-DEDUPE-CLEANUP"}))) (doseq [v (cleanup-duplicate-vendors-get-merge)] (println v) (merge-vendor v)) (dc/q {:find '[?a2] :in '[$] :where ['[?v :vendor/name] '[_ ?a ?v] '[?a :db/ident ?a2]]} (dc/db conn)) ) (dc/q '[:find ?ba (pull ?pa [* {:bank-account/_plaid-account [:db/id { :bank-account/integration-status [*]}] }]) :in $ ?ba :where [?ba :bank-account/plaid-account ?pa]] (dc/db conn) [:bank-account/code "VS-BA6149"]) (init-repl) (filter (fn [[_ x]] (> x 1)) (dc/q '[:find ?pa (count ?ba) :where [?ba :bank-account/plaid-account ?pa]] (dc/db conn))) (dc/pull (dc/db conn) '[* {:bank-account/_plaid-account [:bank-account/code]}] 17592310327452) (seq (dc/q '[:find ?ns :in $ $$ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] [?a :db/ident ?a2] [(namespace ?a2) ?ns] ] (dc/db conn) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/log conn))) (dc/q '[:find ?d2 ?ns ?code ?a2 ?added (count ?x) :in $ $$ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] [(get-some $$ ?e :transaction/client :invoice/client :payment/client :journal-entry/client) [?attr ?c]] [?c :client/code ?code] [?a :db/ident ?a2] [(namespace ?a2) ?ns] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2]] (dc/db conn) (dc/history (dc/since (dc/db conn) #inst "2024-07-01T00:00:00-08:00")) (dc/log conn)) (defn join-attrs [xs] (clojure.string/join ", " (sort xs))) (comment "Behaviors in time region" (clojure.data.csv/write-csv *out* (concat (->> (dc/q '[:find ?d2 ?code ?entity ?e ?desc (user/join-attrs ?attr) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] ($$$ or-join [?e ?c] [?c :client/bank-accounts ?e] [?e :account-client-override/client ?c] [?e :journal-entry/client ?c] (and [?t :transaction/accounts ?e] [?t :transaction/client ?c]) (and [?e :client/code] [?e ?c]) (and [?je :journal-entry/line-items ?e] [?je :journal-entry/client ?c]) [?e :payment/client ?c] [?e :invoice/client ?c] (and [?i :invoice/expense-accounts ?e] [?i :invoice/client ?c]) [?e :transaction/client ?c] (and [?e :invoice-payment/invoice ?i] [?i :invoice/client ?c]) #_[(ground 0) ?c]) [(get-else $ ?c :client/code "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq) (->> (dc/q '[:find ?d2 ?code ?entity ?e ?desc (user/join-attrs ?attr) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] [(ground "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] (not [(get #{"invoice" "transaction" "account-client-override" "journal-entry" "journal-entry-line" "bank-account" "payment" "invoice-payment" "client" "invoice-expense-account" "transaction-account"} ?entity)]) [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq)) :separator \tab) ) (clojure.data.csv/write-csv *out* (concat (->> (dc/q '[:find ?d2 ?code ?entity (count ?e) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] ($$$ or-join [?e ?c] [?c :client/bank-accounts ?e] [?e :account-client-override/client ?c] [?e :journal-entry/client ?c] (and [?t :transaction/accounts ?e] [?t :transaction/client ?c]) (and [?e :client/code] [?e ?c]) (and [?je :journal-entry/line-items ?e] [?je :journal-entry/client ?c]) [?e :payment/client ?c] [?e :invoice/client ?c] (and [?i :invoice/expense-accounts ?e] [?i :invoice/client ?c]) [?e :transaction/client ?c] (and [?e :invoice-payment/invoice ?i] [?i :invoice/client ?c]) #_[(ground 0) ?c]) [(get-else $ ?c :client/code "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq) (->> (dc/q '[:find ?d2 ?code ?entity (count ?e) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] [(ground "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] (not [(get #{"invoice" "transaction" "account-client-override" "journal-entry" "journal-entry-line" "bank-account" "payment" "invoice-payment" "client" "invoice-expense-account" "transaction-account"} ?entity)]) [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq)) :separator \tab) (clojure.data.csv/write-csv *out* (concat (->> (dc/q '[:find ?code ?entity (count ?e) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] ($$$ or-join [?e ?c] [?c :client/bank-accounts ?e] [?e :account-client-override/client ?c] [?e :journal-entry/client ?c] (and [?t :transaction/accounts ?e] [?t :transaction/client ?c]) (and [?e :client/code] [?e ?c]) (and [?je :journal-entry/line-items ?e] [?je :journal-entry/client ?c]) [?e :payment/client ?c] [?e :invoice/client ?c] (and [?i :invoice/expense-accounts ?e] [?i :invoice/client ?c]) [?e :transaction/client ?c] (and [?e :invoice-payment/invoice ?i] [?i :invoice/client ?c]) #_[(ground 0) ?c]) [(get-else $ ?c :client/code "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq) (->> (dc/q '[:find ?code ?entity (count ?e) :in $$$ $$ $ ?log :where [$$ ?x :audit/user "admin-Ashlee Phillips"] [(tx-data ?log ?x) [[?e ?a ?v _ ?added]]] [(ground "") ?code] [?a :db/ident ?a2] [(namespace ?a2) ?entity] (not [(get #{"invoice" "transaction" "account-client-override" "journal-entry" "journal-entry-line" "bank-account" "payment" "invoice-payment" "client" "invoice-expense-account" "transaction-account"} ?entity)]) [(name ?a2) ?attr] #_[(= ?a3 "account-client-override")] [?x :db/txInstant ?d] [(iol-ion.query/excel-date ?d) ?d2] [(if ?added "changed" "replaced/deleted") ?desc]] (dc/history (dc/db conn)) (dc/history (dc/since (dc/db conn) #inst "2024-06-15T00:00:00-08:00")) (dc/db conn) (dc/log conn)) seq)) :separator \tab) (dc/q '[:find ?c :in $ [?e ...] :where (or-join [?e ?c] [?e :invoice/client ?c] [?e :transaction/client ?c])] (dc/db conn) [17592296942460]) 1