Greatly simplifies keeping bank accounts in check

This commit is contained in:
2024-09-27 15:59:21 -07:00
parent b7d976cf19
commit 4c14e43734
7 changed files with 157 additions and 288 deletions

View File

@@ -1,5 +1,5 @@
(ns iol-ion.tx.upsert-ledger
(:import [java.util UUID])
(:import [java.util UUID Date])
(:require [datomic.api :as dc]))
(defn -random-tempid []
@@ -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
@@ -25,6 +25,9 @@
(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 now []
(Date.))
(defn calc-client+account+location+date [je jel]
[(or
(:db/id (:journal-entry/client je))
@@ -43,8 +46,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])))
extant-entry-exists? (:db/id extant-entry)]
(dc/pull db extant-read [:journal-entry/external-id external-id]))) ]
(cond->
[[:upsert-entity (into (-> ledger-entry
@@ -55,12 +57,9 @@
(update :journal-entry/line-items
(fn [lis]
(mapv #(-> %
(assoc :journal-entry-line/dirty true)
(assoc :journal-entry-line/client+account+location+date
(calc-client+account+location+date ledger-entry %)))
lis)))))]]
extant-entry-exists? (into (map (fn [li]
{:journal-entry-line/dirty true
:db/id li})
(get-line-items-after db extant-entry))))))
(assoc :journal-entry-line/date (:journal-entry/date ledger-entry))
(assoc :journal-entry-line/client (:journal-entry/client ledger-entry)))
lis)))))]
{:db/id (:journal-entry/client ledger-entry)
:client/ledger-last-change (now)}])))