From 2a3c538849dbae1ea803fc7180e883cefb0e4ca5 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 3 Apr 2023 12:32:04 -0700 Subject: [PATCH] (cloud) deleting old approach --- iol_ion/src/iol_ion/tx.clj | 22 ++++ src/clj/auto_ap/graphql/checks.clj | 2 +- src/clj/auto_ap/ledger.clj | 190 ++--------------------------- things-to-search-for.txt | 2 - 4 files changed, 36 insertions(+), 180 deletions(-) diff --git a/iol_ion/src/iol_ion/tx.clj b/iol_ion/src/iol_ion/tx.clj index cd7ad944..5b66a376 100644 --- a/iol_ion/src/iol_ion/tx.clj +++ b/iol_ion/src/iol_ion/tx.clj @@ -401,3 +401,25 @@ :invoice/status (if (> new-outstanding-balance 0) :invoice-status/unpaid :invoice-status/paid)}))) + + +;; TODO expected-deposit ledger entry +#_(defmethod entity-change->ledger :expected-deposit + [db [type id]] + (let [{:expected-deposit/keys [total client date]} (d/pull db '[:expected-deposit/total :expected-deposit/client :expected-deposit/date] id)] + #:journal-entry + {:source "expected-deposit" + :original-entity id + + :client client + :date date + :amount total + :vendor :vendor/ccp-square + :line-items [#:journal-entry-line + {:credit total + :location "A" + :account :account/receipts-split} + #:journal-entry-line + {:debit total + :location "A" + :account :account/ccp}]})) diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index 3f35ce55..8b377075 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -500,7 +500,7 @@ (let [invoice (:invoice-payment/invoice x) new-balance (+ (:invoice/outstanding-balance invoice) (:invoice-payment/amount x))] - [[:db.fn/retractEntity (:db/id x)] + [[:db/retractEntity (:db/id x)] `(upsert-invoice ~{:db/id (:db/id invoice) :invoice/outstanding-balance new-balance :invoice/status (if (dollars-0? new-balance) diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index bef28bb6..b55e426c 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -1,162 +1,18 @@ (ns auto-ap.ledger (:require - [auto-ap.datomic - :refer [audit-transact - conn - pull-id - pull-ref - remove-nils - transact-with-backoff]] - [auto-ap.utils :refer [by dollars-0? dollars= heartbeat]] + [auto-ap.datomic :refer [conn pull-id pull-ref transact-with-backoff]] + [auto-ap.utils :refer [by dollars= heartbeat]] [clj-time.coerce :as c] [clj-time.core :as t] - [clojure.tools.logging :as log] [com.brunobonacci.mulog :as mu] [com.unbounce.dogstatsd.core :as statsd] [datomic.client.api :as dc] - [iol-ion.tx :refer [upsert-ledger]] + [iol-ion.tx :refer [upsert-invoice upsert-ledger upsert-transaction]] [manifold.deferred :as de] [manifold.stream :as s] [mount.core :as mount] [yang.scheduler :as scheduler])) -(defn datums->impacted-entity [db [e changes]] - (let [entity (dc/pull db '[{:invoice/_expense-accounts [:db/id] :transaction/_accounts [:db/id]}] e) - namespaces (->> changes - (map #(:db/ident (dc/pull db '[:db/ident] (:a %)))) - (map namespace) - set)] - (cond (namespaces "invoice" ) [[:invoice e]] - (namespaces "invoice-expense-account" ) [[:invoice (:db/id (:invoice/_expense-accounts entity))]] - (namespaces "transaction-account" ) [[:transaction (:db/id (:transaction/_accounts entity))]] - (namespaces "transaction" ) [[:transaction e]] - #_#_(namespaces "expected-deposit" ) [[:expected-deposit e]] - :else nil))) - -(defmulti entity-change->ledger (fn [_ [type]] - type)) - -(defmethod entity-change->ledger :invoice - [db [_ id]] - (when id - (let [entity (dc/pull db ['* {:invoice/vendor '[*] - :invoice/payment '[*] - :invoice/status '[:db/ident] - :invoice/import-status '[:db/ident]}] 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))) - (dollars-0? (:invoice/total 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 (:db/id entity) - :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 (:db/id entity) "-" 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 (:db/id entity) "-" (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)) - )}))))) - -(defmethod entity-change->ledger :transaction - [db [_ id]] - (when id - (let [entity (dc/pull db ['* {:transaction/vendor '[*] - :transaction/client '[*] - :transaction/approval-status '[*] - :transaction/bank-account '[* {:bank-account/type [:db/ident]}] - :transaction/accounts '[* - {:transaction-account/account [*]}] }] id) - decreasing? (< (or (:transaction/amount entity) 0.0) 0.0) - credit-from-bank? decreasing? - debit-from-bank? (not decreasing?)] - (when (and (not (= :transaction-approval-status/excluded (:db/ident (:transaction/approval-status entity)))) - (not (= :transaction-approval-status/suppressed (:db/ident (:transaction/approval-status entity)))) - (:transaction/amount entity) - (not (dollars-0? (:transaction/amount entity)))) - (remove-nils - {:journal-entry/source "transaction" - :journal-entry/client (:db/id (:transaction/client entity)) - :journal-entry/date (:transaction/date entity) - :journal-entry/original-entity (:db/id entity) - :journal-entry/alternate-description (:transaction/description-original entity) - :journal-entry/vendor (:db/id (:transaction/vendor entity)) - :journal-entry/amount (Math/abs (:transaction/amount entity)) - :journal-entry/cleared-against (:transaction/cleared-against entity) - - :journal-entry/line-items (into [(remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity)) - :db/id (str (:db/id entity) "-" 0) - :journal-entry-line/location "A" - :journal-entry-line/credit (when credit-from-bank? - (Math/abs (:transaction/amount entity))) - :journal-entry-line/debit (when debit-from-bank? - (Math/abs (:transaction/amount entity)))}) - ] - (map-indexed - (fn [i a] - (remove-nils{ - :db/id (str (:db/id entity) "-" (inc i)) - :journal-entry-line/account (:db/id (:transaction-account/account a)) - :journal-entry-line/location (:transaction-account/location a) - :journal-entry-line/debit (when credit-from-bank? - (Math/abs (:transaction-account/amount a))) - :journal-entry-line/credit (when debit-from-bank? - (Math/abs (:transaction-account/amount a)))})) - (if (seq (:transaction/accounts entity)) - (:transaction/accounts entity) - [{:transaction-account/amount (:transaction/amount entity)}]))) - - :journal-entry/cleared true}))))) - -#_(defmethod entity-change->ledger :expected-deposit - [db [type id]] - (let [{:expected-deposit/keys [total client date]} (d/pull db '[:expected-deposit/total :expected-deposit/client :expected-deposit/date] id)] - #:journal-entry - {:source "expected-deposit" - :original-entity id - - :client client - :date date - :amount total - :vendor :vendor/ccp-square - :line-items [#:journal-entry-line - {:credit total - :location "A" - :account :account/receipts-split} - #:journal-entry-line - {:debit total - :location "A" - :account :account/ccp}]})) - -(defmethod entity-change->ledger :invoice-expense-account - [_ _] - nil - ) - -(defmethod entity-change->ledger nil - [_ _] - nil) - (defn reconcile-ledger ([] (reconcile-ledger (-> (t/now) (t/plus (t/months -6)) @@ -174,7 +30,8 @@ ]} :args [(dc/db conn) start-date]}) (map first) - (mapv #(entity-change->ledger (dc/db conn) [:transaction %]))) + (mapv (fn [t] + `(upsert-transaction ~{:db/id t})))) invoices-missing-ledger-entries (->> (dc/q {:query {:find ['?t ] @@ -190,7 +47,8 @@ ]} :args [(dc/db conn) start-date]}) (map first) - (mapv #(entity-change->ledger (dc/db conn) [:invoice %]))) + (mapv (fn [i] + `(upsert-invoice ~{:db/id i})))) repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))] (when (seq repairs) (mu/log ::ledger-repairs-needed @@ -203,36 +61,14 @@ (defn touch-transaction [e] - (when-let [change (entity-change->ledger (dc/db conn) - [:transaction e])] - (dc/transact conn {:tx-data [{:db/id "datomic.tx" - :db/doc "touching transaction to update ledger"} - `(upsert-ledger ~change)]}))) + (dc/transact conn {:tx-data [{:db/id "datomic.tx" + :db/doc "touching transaction to update ledger"} + `(upsert-transaction ~{:db/id e})]})) (defn touch-invoice [e] - (when-let [change (entity-change->ledger (dc/db conn) - [:invoice e])] - (dc/transact conn {:tx-data [{:db/id "datomic.tx" - :db/doc "touching invoice to update ledger"} - `(upsert-ledger ~change)]}))) - -(defn lazy-tx-range - ([start end xf] (lazy-tx-range start end xf 0)) - ([start end xf o] - (let [next-results (dc/tx-range conn {:start start - :end end - :offset o - :limit 200})] - (lazy-seq - (if (seq next-results) - (concat (sequence (comp (mapcat :data) - xf) next-results) (lazy-tx-range start - end - xf - (+ (count next-results) - o))) - next-results))))) - + (dc/transact conn {:tx-data [{:db/id "datomic.tx" + :db/doc "touching invoice to update ledger"} + `(upsert-invoice ~{:db/id e})]})) diff --git a/things-to-search-for.txt b/things-to-search-for.txt index 3f145ea1..75044f15 100644 --- a/things-to-search-for.txt +++ b/things-to-search-for.txt @@ -3,14 +3,12 @@ upsertledger - matching transaction rule might not assign an account. Other thin Double check each job still functions in the new system Make reports just be based on running-balances Test exports -Move pay into iol-ion.tx, make sure to use upsert-invoice Some jobs just aren't so big they need to be jobs anymore: Refreshing running balance for journal entry lines Refreshing current balance for bank accounts Closing auto invoices - Running Balance Cache * Add tests for upsert-ledger * Make a new way to reset the entire cache for a client