Files
integreat/src/clj/user.fiddle
Bryce d73a3153bb payments ssr
voiding

supports bulk void.

exact match id linking

voidnig payments works.

minor tweak.
2024-03-09 11:59:17 -08:00

327 lines
13 KiB
Plaintext

(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}])
)