From 0fe2d2a84b6879dd5bc2f736edb5f746c8b8fffc Mon Sep 17 00:00:00 2001 From: Bryce Date: Tue, 8 Oct 2024 23:33:23 -0700 Subject: [PATCH] Transactions get unlinked when voided --- src/clj/auto_ap/ssr/company/plaid.clj | 1 + src/clj/auto_ap/ssr/payments.clj | 47 +++++++++++++++++---------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/clj/auto_ap/ssr/company/plaid.clj b/src/clj/auto_ap/ssr/company/plaid.clj index 13a86e66..e168ec4f 100644 --- a/src/clj/auto_ap/ssr/company/plaid.clj +++ b/src/clj/auto_ap/ssr/company/plaid.clj @@ -232,3 +232,4 @@ (def page (helper/page-route grid-page)) (def table (helper/table-route grid-page)) + diff --git a/src/clj/auto_ap/ssr/payments.clj b/src/clj/auto_ap/ssr/payments.clj index f6471bf1..a95b3754 100644 --- a/src/clj/auto_ap/ssr/payments.clj +++ b/src/clj/auto_ap/ssr/payments.clj @@ -462,7 +462,7 @@ #(assert-can-see-client identity (:db/id (:payment/client check)))) (notify-if-locked (:db/id (:payment/client check)) (:payment/date check)) - (let [removing-payments (mapcat (fn [x] + (let [ removing-payments (mapcat (fn [x] (let [invoice (:invoice-payment/invoice x) new-balance (+ (:invoice/outstanding-balance invoice) (:invoice-payment/amount x))] @@ -476,7 +476,11 @@ updated-payment {:db/id (:db/id check) :payment/amount 0.0 :payment/status :payment-status/voided}] - (audit-transact (conj removing-payments updated-payment) + (audit-transact (cond-> removing-payments + true (conj updated-payment) + (:transaction/_payment check) (conj [:db/retract (:db/id (first (:transaction/_payment check))) + :transaction/payment + (:db/id check)])) identity) (html-response (row* (:identity request) updated-payment {:delete-after-settle? true :class "live-removed" @@ -536,24 +540,33 @@ [?p :payment/date ?d] [(>= ?d ?lu)]] (dc/db conn)) - (map first))] + (map first)) + transactions-to-unlink (->> payments-to-update + (dc/q '[:find ?p ?t + :in $ [?p ...] + :where [?t :transaction/payment ?p]] + (dc/db conn)))] (audit-transact (->> payments-to-update (mapcat (fn [{:keys [:db/id] invoices :invoice-payment/_payment}] - (into - [{:db/id id - :payment/amount 0.0 - :payment/status :payment-status/voided}] - (->> invoices - (mapcat (fn [{:keys [:invoice-payment/invoice :db/id :invoice-payment/amount]}] - (let [new-balance (+ (:invoice/outstanding-balance invoice) - amount)] - [[:db.fn/retractEntity id] - [:upsert-invoice {:db/id (:db/id invoice) - :invoice/outstanding-balance new-balance - :invoice/status (if (dollars-0? new-balance) - (:invoice/status invoice) - :invoice-status/unpaid)}]])))))))) + (-> [{:db/id id + :payment/amount 0.0 + :payment/status :payment-status/voided}] + ;; TODO this doesn't do anything because payments with invoices are filtered out + (into (->> transactions-to-unlink + (map (fn [[p t]] + [:db/retract t :transaction/payment p])))) + (into + (->> invoices + (mapcat (fn [{:keys [:invoice-payment/invoice :db/id :invoice-payment/amount]}] + (let [new-balance (+ (:invoice/outstanding-balance invoice) + amount)] + [[:db.fn/retractEntity id] + [:upsert-invoice {:db/id (:db/id invoice) + :invoice/outstanding-balance new-balance + :invoice/status (if (dollars-0? new-balance) + (:invoice/status invoice) + :invoice-status/unpaid)}]]))))))))) id) (count payments-to-update)))