Transactions get unlinked when voided
This commit is contained in:
@@ -232,3 +232,4 @@
|
||||
|
||||
(def page (helper/page-route grid-page))
|
||||
(def table (helper/table-route grid-page))
|
||||
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user