diff --git a/scratch-sessions/fixing-duplicate-vendors.clj b/scratch-sessions/fixing-duplicate-vendors.clj new file mode 100644 index 00000000..38e51b16 --- /dev/null +++ b/scratch-sessions/fixing-duplicate-vendors.clj @@ -0,0 +1,165 @@ +;; 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. + +(init-repl) + +(def best 17592232929447) +best + + +(def others (->> (dc/q '[:find ?v + :in $ [?name ...] ?b + :where [?v :vendor/name ?name] + (not [(= ?v ?b)])] + (dc/db conn) + ["CCP Grubhub" "CCP Grub Hub" "CCP GrubHub"] + best) + (map first) + (into #{}))) + +(count others) + +#_@(dc/transact auto-ap.datomic/conn + (map (fn [o] + [:db/retractEntity o]) + others)) + + +(count + (for [[incoming incoming-attr from-vendor] (dc/q {:find '[?x ?a2 ?vendor-from] + :in '[$ [?a ...] [?vendor-from ...] ] + :where ['[?x ?a ?vendor-from] + '[?a :db/ident ?a2]]} + (dc/db conn) #{:expected-deposit/vendor, :invoice/vendor, :journal-entry/vendor, :payment/vendor, :sales-order/vendor, :sales-refund/vendor, :transaction/vendor, :transaction-rule/vendor, :cash-drawer-shift/vendor} others) + part [[:db/retract incoming incoming-attr from-vendor] + [:db/add incoming incoming-attr best]]] + part + )) + +(auto-ap.datomic/audit-transact-batch + (for [[incoming incoming-attr from-vendor] (dc/q {:find '[?x ?a2 ?vendor-from] + :in '[$ [?a ...] [?vendor-from ...] ] + :where ['[?x ?a ?vendor-from] + '[?a :db/ident ?a2]]} + (dc/db conn) #{:expected-deposit/vendor, :invoice/vendor, :journal-entry/vendor, :payment/vendor, :sales-order/vendor, :sales-refund/vendor, :transaction/vendor, :transaction-rule/vendor, :cash-drawer-shift/vendor} others) + part [[:db/retract incoming incoming-attr from-vendor] + [:db/add incoming incoming-attr best]]] + part + ) + {:user/name "Fixing duplicate vendors"}) + + +(set (map second (dc/q '[:find ?x ?a2 ?vendor-from + :in $ [?a ...] [?vendor-from ...] + :where [?x ?a ?vendor-from] + [?a :db/ident ?a2]] + (dc/db conn) #{ :invoice/vendor, :journal-entry/vendor, :payment/vendor, :sales-order/vendor, :sales-refund/vendor, :transaction/vendor, :transaction-rule/vendor, :cash-drawer-shift/vendor} others))) + + + +(->> (dc/q '[:find ?v ?c (count ?e) + :in $ ?v + :where + [?v :vendor/name] + (or-join [?v ?c ?e] + (and + [?e :invoice/vendor ?v] + [?e :invoice/client ?c]) + (and + [?e :transaction/vendor ?v] + [?e :transaction/client ?c]) + (and + [?e :journal-entry/vendor ?v] + [?e :journal-entry/client ?c]))] + (dc/db conn) + best) + (map (fn [[v c cnt]] + #:vendor-usage {:vendor v + :client c + :key (str v "-" c) + :count cnt}))) + + +(auto-ap.datomic/audit-transact-batch + (for [[e a] (dc/q '[:find ?e ?a + :in $$ [?tx ...] [?other ...] ?false-best + :where + [$$ ?e ?a ?false-best ?tx true] + [$$ ?e ?a ?other ?tx false] + [$$ ?other :vendor/name ?vn] + [(clojure.string/includes? ?vn "rub")] + ] + + (dc/history (dc/db conn)) + (map (comp dc/t->tx :t) (dc/tx-range (dc/log conn) #inst "2023-06-06T21:00:00-08:00" #inst "2023-06-06T22:31:00-08:00")) + #_(dc/tx-range (dc/log conn) #inst "2023-06-06T22:15:00-08:00" #inst "2023-06-06T22:31:00-08:00") + others + 17592232586913) + part [[:db/retract e a 17592232586913] + [:db/add e a 17592270032394]]] + part + ) + {:user/name "re-fixing grub-hub"}) + + +(map :t (dc/tx-range (dc/log conn) #inst "2023-06-06T21:00:00-08:00" #inst "2023-06-06T22:31:00-08:00")) + +(dc/pull (dc/db conn) '[*] 17592232929447) + + + +(let [best 17592232586918 + others (->> (dc/q '[:find ?v + :in $ [?name ...] ?b + :where [?v :vendor/name ?name] + (not [(= ?v ?b)])] + (dc/db conn) + ["CCP Uber Eats" "CCP UBER" "CCP Ubereats" "CCP Uber Eats MP" "CCP Uber Eats SC" "CCP UberEats" "CCP Ubereats" "CCP Uber"] + best) + (map first) + (into #{}))] + + (auto-ap.datomic/audit-transact-batch + (for [[incoming incoming-attr from-vendor] (dc/q {:find '[?x ?a2 ?vendor-from] + :in '[$ [?a ...] [?vendor-from ...] ] + :where ['[?x ?a ?vendor-from] + '[?a :db/ident ?a2]]} + (dc/db conn) #{:expected-deposit/vendor, :invoice/vendor, :journal-entry/vendor, :payment/vendor, :sales-order/vendor, :sales-refund/vendor, :transaction/vendor, :transaction-rule/vendor, :cash-drawer-shift/vendor} others) + part [[:db/retract incoming incoming-attr from-vendor] + [:db/add incoming incoming-attr best]]] + part + ) + {:user/name "Fixing duplicate vendors"}) + + @(dc/transact auto-ap.datomic/conn + (map (fn [o] + [:db/retractEntity o]) + others))) + + +(require '[datomic.api :as dc]) + +(require '[auto-ap.datomic :refer [conn]]) +(user/init-repl) + +(auto-ap.datomic/audit-transact-batch + (for [[bad] (seq (dc/q '[:find ?x ?vn + :in $ + :where + [?x :vendor/name ?vn] + (not [_ :expected-deposit/vendor ?x]) + (not [_ :invoice/vendor ?x]) + (not [_ :journal-entry/vendor ?x]) + (not [_ :payment/vendor ?x]) + (not [_ :sales-order/vendor ?x]) + (not [_ :payment/vendor ?x]) + (not [_ :sales-refund/vendor ?x]) + (not [_ :transaction/vendor ?x]) + (not [_ :transaction-rule/vendor ?x]) + (not [_ :cash-drawer-shift/vendor ?x])] + (dc/db conn)))] + [:db/retractEntity bad]) + {:user/name "fix-unused-vendors"})