Add vendor pre-population for bulk code and individual edit forms

- Add vendor-changed HTMX handlers for both bulk code and individual edit
- Pre-populate default account at 100% when vendor is selected and no accounts exist
- Fix render-accounts-section to render from step-params correctly
- Change bulk code vendor-changed from hx-get to hx-post to include form data
- Add routes for vendor-changed endpoints
- Update e2e tests to cover vendor pre-population
- Run lein cljfmt fix across codebase
This commit is contained in:
2026-05-21 14:45:19 -07:00
parent 8bd0cee1b1
commit ba87805d4c
210 changed files with 8694 additions and 9627 deletions

View File

@@ -14,7 +14,7 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn dollars= [amt1 amt2]
(dollars-0? (- amt1 amt2) ))
(dollars-0? (- amt1 amt2)))
(defn localize [d]
(time/to-time-zone d (time/time-zone-for-id "America/Los_Angeles")))
@@ -22,7 +22,6 @@
(defn local-now []
(localize (time/now)))
(defn recent-date
([]
(recent-date 90))
@@ -32,16 +31,16 @@
(def excel-formatter (f/with-zone (f/formatter "MM/dd/yyyy") (time/time-zone-for-id "America/Los_Angeles")))
(defn excel-date [d]
(->> d
(coerce/to-date-time)
localize
(f/unparse excel-formatter )))
(coerce/to-date-time)
localize
(f/unparse excel-formatter)))
(def iso-formatter (f/with-zone (f/formatter "yyyy-MM-dd") (time/time-zone-for-id "America/Los_Angeles")))
(defn iso-date [d]
(->> d
(coerce/to-date-time)
localize
(f/unparse iso-formatter )))
(coerce/to-date-time)
localize
(f/unparse iso-formatter)))
(defn sales-orders-in-range [db client start end]
(let [end (or end #inst "2050-01-01")]
@@ -53,9 +52,6 @@
[client start]
[client end]))))
(defn can-see-client? [identity client]
(when (not client)
(println "WARNING - permission checking for null client"))
@@ -63,11 +59,9 @@
((set (map :db/id (:user/clients identity))) (:db/id client))
((set (map :db/id (:user/clients identity))) client)))
(defn ->pattern [x]
(. java.util.regex.Pattern (compile x java.util.regex.Pattern/CASE_INSENSITIVE)))
(defn dom [^java.util.Date x]
(-> x
(.toInstant)
@@ -85,8 +79,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:sales-order/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-charges [db clients start end]
@@ -94,8 +88,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:charge/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-sales-refunds [db clients start end]
@@ -103,8 +97,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:sales-refund/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-expected-deposits [db clients start end]
@@ -112,8 +106,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:expected-deposit/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-cash-drawer-shifts [db clients start end]
@@ -121,8 +115,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:cash-drawer-shift/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-invoices [db clients start end]
@@ -130,17 +124,17 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:invoice/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-transactions [db clients start end]
(for [c clients
:let [c (entid db c)]
r (seq (dc/index-range db
:transaction/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
:transaction/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-ledger [db clients start end]
@@ -148,8 +142,8 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:journal-entry/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn scan-payments [db clients start end]
@@ -157,15 +151,14 @@
:let [c (entid db c)]
r (seq (dc/index-range db
:payment/client+date
[c (or start #inst "2001-01-01T08:00:00.000-00:00") ]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00") ]))]
[c (or start #inst "2001-01-01T08:00:00.000-00:00")]
[c (or (next-day end) #inst "2030-03-05T08:00:00.000-00:00")]))]
[(:e r) (first (:v r)) (second (:v r))]))
(defn ident [x]
(:db/ident x))
(deftype Line [^Long id ^Long client-id ^Long account-id ^String location ^java.util.Date date ^Double debit ^Double credit ^Double running-balance]
)
(deftype Line [^Long id ^Long client-id ^Long account-id ^String location ^java.util.Date date ^Double debit ^Double credit ^Double running-balance])
(defmethod print-method Line [entity writer]
(.write writer (format "Line %d: client:%d account:%d location:%s date:%s"
@@ -175,18 +168,16 @@
(.-location entity)
(iso-date (.-date entity)))))
(defn ->line [{[current-client current-account current-location current-date debit credit running-balance]
:v
id :e}]
(Line. id current-client current-account current-location current-date debit credit running-balance)
)
(Line. id current-client current-account current-location current-date debit credit running-balance))
(defn compare-account [^Line l1 ^Line l2]
(defn compare-account [^Line l1 ^Line l2]
(let [a (compare (.-date l1) (.-date l2))]
(if (not= 0 a)
a
a
(compare (.-id l1) (.-id l2)))))
(defn account-sets [db client-id]
@@ -194,7 +185,7 @@
(seq)
(map ->line)
(partition-by (fn set-partition [^Line l]
[(.-account-id l) (.-location l)]))) ]
[(.-account-id l) (.-location l)])))]
(->> running-balance-set
(sort compare-account))))
@@ -205,35 +196,35 @@
(take-while (fn until-date [^Line l]
(let [^java.util.Date d (.-date l)]
(<= (.compareTo ^java.util.Date d end) 0))))
last) ]
last)]
:when (and z (.-id z))]
[(.-client-id z) (.-account-id z) (.-location z) (.-date z) (.-running-balance z)]))
#_(doseq [[ n] (dc/q '[:find ?cd :where [?c :client/code ?cd] [?c :client/groups "NTG"]] (dc/db auto-ap.datomic/conn))]
(println n)
(dc/q '[:find ?code ?name ?afc ?an ?l ?d2 ?balance
:in $ ?end ?group
:where
[(clj-time.coerce/to-date-time ?end) ?end2]
[(iol-ion.query/localize ?end2) ?end3]
[(clj-time.coerce/to-date ?end3) ?end4]
(or
[?c :client/groups ?group]
[?c :client/code ?group])
[?c :client/name ?name]
[?c :client/code ?code]
[?c :client/bank-accounts ?b]
[(iol-ion.query/account-snapshot $ ?c ?end4) [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]
[(not= nil ?a)]
[(iol-ion.query/excel-date ?date) ?d2]
(or-join [?a ?afc ?an]
(and [?a :account/name ?an]
[?a :account/numeric-code ?afc])
(and [?a :bank-account/name ?an]
[?a :bank-account/numeric-code ?afc]))]
(dc/db auto-ap.datomic/conn)
#inst "2024-10-10" n))
#_(doseq [[n] (dc/q '[:find ?cd :where [?c :client/code ?cd] [?c :client/groups "NTG"]] (dc/db auto-ap.datomic/conn))]
(println n)
(dc/q '[:find ?code ?name ?afc ?an ?l ?d2 ?balance
:in $ ?end ?group
:where
[(clj-time.coerce/to-date-time ?end) ?end2]
[(iol-ion.query/localize ?end2) ?end3]
[(clj-time.coerce/to-date ?end3) ?end4]
(or
[?c :client/groups ?group]
[?c :client/code ?group])
[?c :client/name ?name]
[?c :client/code ?code]
[?c :client/bank-accounts ?b]
[(iol-ion.query/account-snapshot $ ?c ?end4) [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]
[(not= nil ?a)]
[(iol-ion.query/excel-date ?date) ?d2]
(or-join [?a ?afc ?an]
(and [?a :account/name ?an]
[?a :account/numeric-code ?afc])
(and [?a :bank-account/name ?an]
[?a :bank-account/numeric-code ?afc]))]
(dc/db auto-ap.datomic/conn)
#inst "2024-10-10" n))
(defn detailed-account-snapshot
([db client-id ^java.util.Date end]
@@ -266,12 +257,11 @@
:credits 0.0
:current-balance 0.0})))]
:when client-id]
(do
(do
[client-id account-id location debits credits current-balance count sample]))))
(comment
(->>
(comment
(->>
(detailed-account-snapshot (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"])
@@ -280,65 +270,65 @@
(into #{})
seq)
(account-snapshot (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"])
#inst "2022-01-01")
(account-snapshot (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"])
#inst "2022-01-01")
(def orig (->> [:client/code "NGOP"]
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn))
(account-sets (dc/db auto-ap.datomic/conn))
(mapcat (fn [ls]
ls))
(filter (fn [l] (nil? (.-location l))))
(into #{})))
(def orig (->> [:client/code "NGOP"]
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn))
(account-sets (dc/db auto-ap.datomic/conn))
(mapcat (fn [ls]
ls))
(filter (fn [l] (nil? (.-location l))))
(into #{})))
(.-location orig)
(.-location orig)
(def orig (into [] (take 5000 (mapcat (fn [ls]
(map #(.-id %) ls)) (account-sets (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"]))))))
(def orig (into [] (take 5000 (mapcat (fn [ls]
(map #(.-id %) ls)) (account-sets (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"]))))))
(def n (into [] (take 5000 (mapcat (fn [ls]
(map #(.-id %) ls)) (account-sets (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"]))))))
(def n (into [] (take 5000 (mapcat (fn [ls]
(map #(.-id %) ls)) (account-sets (dc/db auto-ap.datomic/conn)
(auto-ap.datomic/pull-id (dc/db auto-ap.datomic/conn)
[:client/code "NGOP"]))))))
(= orig n)
#_(seq (dc/q '[:find ?c ?a ?l ?date ?balance
:in $
:where [?c :client/code "NGOP"]
[(iol-ion.query/account-snapshot $ ?c #inst "2023-01-01") [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]]
(dc/db auto-ap.datomic/conn)))
#_(seq (dc/q '[:find ?c ?a ?l ?date ?balance
:in $
:where [?c :client/code "NGOP"]
[(iol-ion.query/account-snapshot $ ?c #inst "2023-01-01") [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]]
(dc/db auto-ap.datomic/conn)))
#_(->> (seq (dc/q '[:find ?code ?name ?afc ?an ?l ?d2 ?balance ?end4
:in $ ?end ?group
:where
[(clj-time.coerce/to-date-time ?end) ?end2]
[(iol-ion.query/localize ?end2) ?end3]
[(clj-time.coerce/to-date ?end3) ?end4]
(or
[?c :client/groups ?group]
[?c :client/code ?group])
[?c :client/name ?name]
[?c :client/code ?code]
[?c :client/bank-accounts ?b]
[(iol-ion.query/account-snapshot $ ?c ?end4) [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]
[(iol-ion.query/excel-date ?date) ?d2]
[(not= nil ?a)]
(or-join [?a ?afc ?an]
(and [?a :account/name ?an]
[?a :account/numeric-code ?afc])
(and [?a :bank-account/name ?an]
[?a :bank-account/numeric-code ?afc]))]
(dc/db auto-ap.datomic/conn)
#inst "2024-09-23"
"NGKG"))
(filter (fn [[_ _ afc]]
(= 12990 afc)))
(map (fn [[_ _ _ _ _ _ a]]
(Math/round a)))))
#_(->> (seq (dc/q '[:find ?code ?name ?afc ?an ?l ?d2 ?balance ?end4
:in $ ?end ?group
:where
[(clj-time.coerce/to-date-time ?end) ?end2]
[(iol-ion.query/localize ?end2) ?end3]
[(clj-time.coerce/to-date ?end3) ?end4]
(or
[?c :client/groups ?group]
[?c :client/code ?group])
[?c :client/name ?name]
[?c :client/code ?code]
[?c :client/bank-accounts ?b]
[(iol-ion.query/account-snapshot $ ?c ?end4) [?x ...]]
[(untuple ?x) [_ ?a ?l ?date ?balance]]
[(iol-ion.query/excel-date ?date) ?d2]
[(not= nil ?a)]
(or-join [?a ?afc ?an]
(and [?a :account/name ?an]
[?a :account/numeric-code ?afc])
(and [?a :bank-account/name ?an]
[?a :bank-account/numeric-code ?afc]))]
(dc/db auto-ap.datomic/conn)
#inst "2024-09-23"
"NGKG"))
(filter (fn [[_ _ afc]]
(= 12990 afc)))
(map (fn [[_ _ _ _ _ _ a]]
(Math/round a)))))

View File

@@ -11,7 +11,6 @@
(def pull-many iol-ion.utils/pull-many)
(def remove-nils iol-ion.utils/remove-nils)
;; TODO expected-deposit ledger entry
#_(defmethod entity-change->ledger :expected-deposit
[db [type id]]
@@ -33,9 +32,6 @@
:location "A"
:account :account/ccp}]}))
(defn regenerate-literals []
(require 'com.github.ivarref.gen-fn)
(spit

View File

@@ -13,11 +13,11 @@
(:invoice/invoice-number invoice)
(:invoice/client invoice)
(:invoice/vendor invoice))))
[ locked-until] (first (dc/q '[:find ?locked-until
:in $ ?c
:where [?c :client/locked-until ?locked-until]]
db
(:invoice/client invoice)))
[locked-until] (first (dc/q '[:find ?locked-until
:in $ ?c
:where [?c :client/locked-until ?locked-until]]
db
(:invoice/client invoice)))
is-locked? (cond
(not locked-until) false
(not (:invoice/date invoice)) true

View File

@@ -4,11 +4,11 @@
(defn reset-rels [db e a vs]
(assert (every? :db/id vs) (format "In order to reset attribute %s, every value must have :db/id" a))
(let [ids (when-not (string? e)
(->> (dc/q '[:find ?z
:in $ ?e ?a
:where [?e ?a ?z]]
db e a)
(map first)))
(->> (dc/q '[:find ?z
:in $ ?e ?a
:where [?e ?a ?z]]
db e a)
(map first)))
new-id-set (set (map :db/id vs))
retract-ids (filter (complement new-id-set) ids)
{is-component? :db/isComponent} (dc/pull db [:db/isComponent] a)
@@ -16,6 +16,6 @@
(-> []
(into (map (fn [i] (if is-component?
[:db/retractEntity i]
[:db/retract e a i ])) retract-ids))
[:db/retract e a i])) retract-ids))
(into (map (fn [i] [:db/add e a i]) new-rels))
(into (map (fn [i] [:upsert-entity i]) vs)))))

View File

@@ -2,7 +2,7 @@
(:require [datomic.api :as dc]))
(defn reset-scalars [db e a vs]
(let [extant (when-not (string? e)
(->> (dc/q '[:find ?z
:in $ ?e ?a
@@ -12,5 +12,5 @@
retracts (filter (complement (set vs)) extant)
new (filter (complement (set extant)) vs)]
(-> []
(into (map (fn [i] [:db/retract e a i ]) retracts))
(into (map (fn [i] [:db/retract e a i]) retracts))
(into (map (fn [i] [:db/add e a i]) new)))))

View File

@@ -5,7 +5,6 @@
)
(:import [java.util UUID]))
(defn -random-tempid []
(str (UUID/randomUUID)))
@@ -36,7 +35,6 @@
;; :else
;; v))
(defn upsert-entity [db entity]
(when-not (or (:db/id entity)
(:db/ident entity))
@@ -90,7 +88,7 @@
ops
;; reset relationships if it's refs, and not a lookup (i.e., seq of maps, or empty seq)
(and (sequential? v) (= :db.type/tuple (ident->value-type a)) (not (= :db.cardinality/many (ident->cardinality a))))
(conj ops [:db/add e a v])

View File

@@ -4,13 +4,12 @@
(defn -remove-nils [m]
(let [result (reduce-kv
(fn [m k v]
(if (not (nil? v))
(assoc m k v)
m
))
{}
m)]
(fn [m k v]
(if (not (nil? v))
(assoc m k v)
m))
{}
m)]
(if (seq result)
result
nil)))
@@ -33,41 +32,38 @@
invoice-id)
credit-invoice? (< (:invoice/total entity 0.0) 0.0)]
(when-not (or
(not (:invoice/total entity))
(= true (:invoice/exclude-from-ledger entity))
(= :import-status/pending (:db/ident (:invoice/import-status entity)))
(= :invoice-status/voided (:db/ident (:invoice/status entity)))
(< -0.001 (:invoice/total entity) 0.001))
(-remove-nils
{:journal-entry/source "invoice"
:journal-entry/client (:db/id (:invoice/client entity))
:journal-entry/date (:invoice/date entity)
:journal-entry/original-entity raw-invoice-id
:journal-entry/vendor (:db/id (:invoice/vendor entity))
:journal-entry/amount (Math/abs (:invoice/total entity))
(not (:invoice/total entity))
(= true (:invoice/exclude-from-ledger entity))
(= :import-status/pending (:db/ident (:invoice/import-status entity)))
(= :invoice-status/voided (:db/ident (:invoice/status entity)))
(< -0.001 (:invoice/total entity) 0.001))
:journal-entry/line-items (into [(cond-> {:db/id (str raw-invoice-id "-" 0)
:journal-entry-line/account :account/accounts-payable
:journal-entry-line/location "A"
}
credit-invoice? (assoc :journal-entry-line/debit (Math/abs (:invoice/total entity)))
(not credit-invoice?) (assoc :journal-entry-line/credit (Math/abs (:invoice/total entity))))]
(map-indexed (fn [i ea]
(cond->
{:db/id (str raw-invoice-id "-" (inc i))
:journal-entry-line/account (:db/id (:invoice-expense-account/account ea))
:journal-entry-line/location (or (:invoice-expense-account/location ea) "HQ")
}
credit-invoice? (assoc :journal-entry-line/credit (Math/abs (:invoice-expense-account/amount ea)))
(not credit-invoice?) (assoc :journal-entry-line/debit (Math/abs (:invoice-expense-account/amount ea)))))
(:invoice/expense-accounts entity)))
:journal-entry/cleared (and (< (:invoice/outstanding-balance entity) 0.01)
(every? #(= :payment-status/cleared (:payment/status %)) (:invoice/payments entity))
)}))))
(-remove-nils
{:journal-entry/source "invoice"
:journal-entry/client (:db/id (:invoice/client entity))
:journal-entry/date (:invoice/date entity)
:journal-entry/original-entity raw-invoice-id
:journal-entry/vendor (:db/id (:invoice/vendor entity))
:journal-entry/amount (Math/abs (:invoice/total entity))
:journal-entry/line-items (into [(cond-> {:db/id (str raw-invoice-id "-" 0)
:journal-entry-line/account :account/accounts-payable
:journal-entry-line/location "A"}
credit-invoice? (assoc :journal-entry-line/debit (Math/abs (:invoice/total entity)))
(not credit-invoice?) (assoc :journal-entry-line/credit (Math/abs (:invoice/total entity))))]
(map-indexed (fn [i ea]
(cond->
{:db/id (str raw-invoice-id "-" (inc i))
:journal-entry-line/account (:db/id (:invoice-expense-account/account ea))
:journal-entry-line/location (or (:invoice-expense-account/location ea) "HQ")}
credit-invoice? (assoc :journal-entry-line/credit (Math/abs (:invoice-expense-account/amount ea)))
(not credit-invoice?) (assoc :journal-entry-line/debit (Math/abs (:invoice-expense-account/amount ea)))))
(:invoice/expense-accounts entity)))
:journal-entry/cleared (and (< (:invoice/outstanding-balance entity) 0.01)
(every? #(= :payment-status/cleared (:payment/status %)) (:invoice/payments entity)))}))))
(defn current-date [db]
(let [ last-tx (dc/t->tx (dc/basis-t db))
(let [last-tx (dc/t->tx (dc/basis-t db))
[[date]] (seq (dc/q '[:find ?ti :in $ ?tx
:where [?tx :db/txInstant ?ti]]
db
@@ -80,15 +76,15 @@
invoice-id (or (-> with-invoice :tempids (get (:db/id invoice)))
(:db/id invoice))
journal-entry (invoice->journal-entry (:db-after with-invoice)
invoice-id
(:db/id invoice))
client-id (-> (dc/pull (:db-after with-invoice)
[{:invoice/client [:db/id]}]
invoice-id
(:db/id invoice))
client-id (-> (dc/pull (:db-after with-invoice)
[{:invoice/client [:db/id]}]
invoice-id)
:invoice/client
:invoice/client
:db/id)]
(into upserted-entity
(if journal-entry
(if journal-entry
[[:upsert-ledger journal-entry]]
[[:db/retractEntity [:journal-entry/original-entity (:db/id invoice)]]
{:db/id client-id

View File

@@ -10,7 +10,7 @@
next-jel (->> (dc/index-pull db {:index :avet
:selector [:db/id :journal-entry-line/client+account+location+date]
:start [:journal-entry-line/client+account+location+date
(:journal-entry-line/client+account+location+date jel)
(:journal-entry-line/client+account+location+date jel)
(:db/id jel)]})
(take-while (fn line-must-match-client-account-location [result]
(and
@@ -24,9 +24,8 @@
(def extant-read '[:db/id :journal-entry/date :journal-entry/client {:journal-entry/line-items [:journal-entry-line/account :journal-entry-line/location :db/id :journal-entry-line/client+account+location+date]}])
(defn current-date [db]
(let [ last-tx (dc/t->tx (dc/basis-t db))
(let [last-tx (dc/t->tx (dc/basis-t db))
[[date]] (seq (dc/q '[:find ?ti :in $ ?tx
:where [?tx :db/txInstant ?ti]]
db
@@ -51,7 +50,7 @@
(let [extant-entry (or (when-let [original-entity (:journal-entry/original-entity ledger-entry)]
(dc/pull db extant-read [:journal-entry/original-entity original-entity]))
(when-let [external-id (:journal-entry/external-id ledger-entry)]
(dc/pull db extant-read [:journal-entry/external-id external-id]))) ]
(dc/pull db extant-read [:journal-entry/external-id external-id])))]
(cond->
[[:upsert-entity (into (-> ledger-entry
@@ -59,11 +58,11 @@
(:db/id ledger-entry)
(:db/id extant-entry)
(-random-tempid)))
(update :journal-entry/line-items
(update :journal-entry/line-items
(fn [lis]
(mapv #(-> %
(assoc :journal-entry-line/date (:journal-entry/date ledger-entry))
(assoc :journal-entry-line/client (:journal-entry/client ledger-entry)))
(assoc :journal-entry-line/client (:journal-entry/client ledger-entry)))
lis)))))]
{:db/id (:journal-entry/client ledger-entry)
:client/ledger-last-change (current-date db)}])))

View File

@@ -27,11 +27,10 @@
(get m :ledger-side/debit) (assoc :journal-entry-line/debit (get m :ledger-side/debit))
(get m :ledger-side/credit) (assoc :journal-entry-line/credit (get m :ledger-side/credit))))
aggregated)
total-debits (reduce + 0.0 (map #(get % :ledger-side/debit 0.0) aggregated))
total-credits (reduce + 0.0 (map #(get % :ledger-side/credit 0.0) aggregated))
_ (clojure.pprint/pprint [total-debits total-credits])
]
_ (clojure.pprint/pprint [total-debits total-credits])]
(when (and (seq line-items)
(= (Math/round (* 1000 total-debits))
(Math/round (* 1000 total-credits))))
@@ -60,11 +59,10 @@ _ (clojure.pprint/pprint [total-debits total-credits])
journal-entry (summary->journal-entry db-after summary-id)]
upserted-summary
#_(into upserted-summary
(if journal-entry
[[:upsert-ledger journal-entry]]
(concat
[[:db/retractEntity [:journal-entry/original-entity (:db/id summary)]]]
(if journal-entry
[[:upsert-ledger journal-entry]]
(concat
[[:db/retractEntity [:journal-entry/original-entity (:db/id summary)]]]
(when client-id [{:db/id client-id
:client/ledger-last-change (current-date db)}]))))))
(when client-id [{:db/id client-id
:client/ledger-last-change (current-date db)}]))))))

View File

@@ -81,73 +81,70 @@
[[:upsert-ledger journal-entry]]
[[:db/retractEntity [:journal-entry/original-entity (:db/id transaction)]]]))))
#_(comment
;; If transactions are failing, it is likely that there are multiple bank accounts linked
;; to yodlee or plaid. here is how i debugged
(upsert-transaction (dc/db auto-ap.datomic/conn) {:transaction/matched-rule 17592233159891,
:db/id "34411061-4656-4e77-8cc0-2f2769b4324c",
:transaction/status "POSTED",
:transaction/description-original "Rotten Robbie #03",
:transaction/approval-status {:db/id 17592231963877,
:db/ident :transaction-approval-status/approved},
:transaction/plaid-merchant {:db/id "223ceae4-d9e7-4e7f-92be-4fb00676088b",
:plaid-merchant/name "Rotten Robbie"},
:transaction/bank-account 17592232681223,
:transaction/vendor 17592232627053,
:transaction/date #inst "2024-02-24T08:00:00Z",
:transaction/client 17592232577980,
:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996",
:transaction/amount -84.43,
:transaction/accounts [{:db/id "cad8463f-2dfe-47dc-ab17-831e87a633d5",
:transaction-account/account 17592231963549,
:transaction-account/location "CB",
:transaction-account/amount 84.43}],
:transaction/raw-id "gQypbv5946F08op74wZmidDg8qD8Q1fM6gEBP"})
(upsert-transaction (dc/db auto-ap.datomic/conn) {:transaction/matched-rule 17592233159891,
:db/id "34411061-4656-4e77-8cc0-2f2769b4324c",
:transaction/status "POSTED",
:transaction/description-original "Rotten Robbie #03",
:transaction/approval-status {:db/id 17592231963877,
:db/ident :transaction-approval-status/approved},
:transaction/plaid-merchant {:db/id "223ceae4-d9e7-4e7f-92be-4fb00676088b",
:plaid-merchant/name "Rotten Robbie"},
:transaction/bank-account 17592232681223,
:transaction/vendor 17592232627053,
:transaction/date #inst "2024-02-24T08:00:00Z",
:transaction/client 17592232577980,
:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996",
:transaction/amount -84.43,
:transaction/accounts [{:db/id "cad8463f-2dfe-47dc-ab17-831e87a633d5",
:transaction-account/account 17592231963549,
:transaction-account/location "CB",
:transaction-account/amount 84.43}],
:transaction/raw-id "gQypbv5946F08op74wZmidDg8qD8Q1fM6gEBP"})
["upsert-transaction"]
(user/init-repl)
["upsert-transaction"]
(user/init-repl)
(def my-transaction {:transaction/bank-account 17592232681223,
:transaction/date #inst "2024-02-24T08:00:00.000-00:00",
:transaction/matched-rule 17592233159891,
:transaction/client 17592232577980,
:transaction/status "POSTED",
:transaction/plaid-merchant
{:plaid-merchant/name "Rotten Robbie", :db/id "b2776792-9e2b-46e8-a9c8-bf80abea359e"},
:db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc",
:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996",
:transaction/description-original "Rotten Robbie #03",
:transaction/approval-status {:db/id 17592231963877, :db/ident :transaction-approval-status/approved}, :transaction/amount -84.43,
:transaction/accounts [{:db/id "c402c7b3-c11b-484b-b670-bd48f79a3e5f", :transaction-account/account 17592231963549, :transaction-account/amount 84.43, :transaction-account/location "CB"}],
:transaction/raw-id "gQypbv5946F08op74wZmidDg8qD8Q1fM6gEBP",
:transaction/vendor 17592232627053})
(def my-transaction {:transaction/bank-account 17592232681223,
:transaction/date #inst "2024-02-24T08:00:00.000-00:00",
:transaction/matched-rule 17592233159891,
:transaction/client 17592232577980,
:transaction/status "POSTED",
:transaction/plaid-merchant
{:plaid-merchant/name "Rotten Robbie", :db/id "b2776792-9e2b-46e8-a9c8-bf80abea359e"},
:db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc",
:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996",
:transaction/description-original "Rotten Robbie #03",
:transaction/approval-status {:db/id 17592231963877, :db/ident :transaction-approval-status/approved}, :transaction/amount -84.43,
:transaction/accounts [{:db/id "c402c7b3-c11b-484b-b670-bd48f79a3e5f", :transaction-account/account 17592231963549, :transaction-account/amount 84.43, :transaction-account/location "CB"}],
:transaction/raw-id "gQypbv5946F08op74wZmidDg8qD8Q1fM6gEBP",
:transaction/vendor 17592232627053})
(def my-journal {:journal-entry/alternate-description "Rotten Robbie #03",
:journal-entry/date #inst "2024-02-24T08:00:00.000-00:00",
:journal-entry/original-entity "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc",
:journal-entry/client 17592232577980,
:journal-entry/line-items [{:journal-entry-line/credit 84.43, :journal-entry-line/account 17592232681223, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-0", :journal-entry-line/location "A"}
{:journal-entry-line/account 17592231963549, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-1", :journal-entry-line/debit 84.43, :journal-entry-line/location "CB"}
{:journal-entry-line/account 17592231963549, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-2", :journal-entry-line/debit 84.43, :journal-entry-line/location "CB"}],
:journal-entry/source "transaction",
:journal-entry/cleared true,
:journal-entry/amount 84.43,
:journal-entry/vendor 17592232627053})
(dc/pull (dc/db auto-ap.datomic/conn) '[*] [:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996"])
wl
(user/init-repl)
(def my-journal {:journal-entry/alternate-description "Rotten Robbie #03",
:journal-entry/date #inst "2024-02-24T08:00:00.000-00:00",
:journal-entry/original-entity "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc",
:journal-entry/client 17592232577980,
:journal-entry/line-items [{:journal-entry-line/credit 84.43, :journal-entry-line/account 17592232681223, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-0", :journal-entry-line/location "A"}
{:journal-entry-line/account 17592231963549, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-1", :journal-entry-line/debit 84.43, :journal-entry-line/location "CB"}
{:journal-entry-line/account 17592231963549, :db/id "ac2efd80-bb03-48b2-b0d0-6b47a5c119dc-2", :journal-entry-line/debit 84.43, :journal-entry-line/location "CB"}],
:journal-entry/source "transaction",
:journal-entry/cleared true,
:journal-entry/amount 84.43,
:journal-entry/vendor 17592232627053})
(dc/pull (dc/db auto-ap.datomic/conn) '[*] [:transaction/id "11a4a13e713d63f476009027e9a53e217e13d0192a37df8ab96c0eed4bdbe996"])
wl
(user/init-repl)
(or (when-let [original-entity (:journal-entry/original-entity my-journal)]
(dc/pull (dc/db auto-ap.datomic/conn) iol-ion.tx.upsert-ledger/extant-read [:journal-entry/original-entity original-entity]))
(when-let [external-id (:journal-entry/external-id my-journal)]
(dc/pull (dc/db auto-ap.datomic/conn) iol-ion.tx.upsert-ledger/extant-read [:journal-entry/external-id external-id])))
(or (when-let [original-entity (:journal-entry/original-entity my-journal)]
(dc/pull (dc/db auto-ap.datomic/conn) iol-ion.tx.upsert-ledger/extant-read [:journal-entry/original-entity original-entity]))
(when-let [external-id (:journal-entry/external-id my-journal)]
(dc/pull (dc/db auto-ap.datomic/conn) iol-ion.tx.upsert-ledger/extant-read [:journal-entry/external-id external-id])))
@(dc/transact auto-ap.datomic/conn [[:upsert-entity my-transaction]
[:upsert-ledger my-journal]])
@(dc/transact auto-ap.datomic/conn [[:upsert-entity my-transaction]
[:upsert-ledger my-journal]])
(auto-ap.datomic/pull-attr (dc/db auto-ap.datomic/conn) :bank-account/code 17592232681223)
(auto-ap.datomic/pull-attr (dc/db auto-ap.datomic/conn) :bank-account/code 17592232681228)
)
(auto-ap.datomic/pull-attr (dc/db auto-ap.datomic/conn) :bank-account/code 17592232681223)
(auto-ap.datomic/pull-attr (dc/db auto-ap.datomic/conn) :bank-account/code 17592232681228))

View File

@@ -10,11 +10,11 @@
(by f identity xs))
([f fv xs]
(reduce
#(assoc %1 (f %2) (fv %2))
{}
xs)))
#(assoc %1 (f %2) (fv %2))
{}
xs)))
(defn pull-many [db read ids ]
(defn pull-many [db read ids]
(->> (dc/q '[:find (pull ?e r)
:in $ [?e ...] r]
db
@@ -24,13 +24,12 @@
(defn remove-nils [m]
(let [result (reduce-kv
(fn [m k v]
(if (not (nil? v))
(assoc m k v)
m
))
{}
m)]
(fn [m k v]
(if (not (nil? v))
(assoc m k v)
m))
{}
m)]
(if (seq result)
result
nil)))