166 lines
6.7 KiB
Plaintext
166 lines
6.7 KiB
Plaintext
|
|
(def to-remove (dc/q '[:find (pull ?p [{:payment/client [:client/code]}
|
|
[:payment/date :xform clj-time.coerce/from-date]
|
|
:payment/amount
|
|
:db/id])
|
|
:where [?ip :invoice-payment/payment ?p]
|
|
[?p :payment/status :payment-status/cleared]
|
|
(not [?ip :invoice-payment/invoice]) ]
|
|
(dc/db conn)))
|
|
|
|
#'user/to-remove
|
|
|
|
(def to-remove-ids (into #{} (map (comp :db/id first) to-remove)))
|
|
|
|
|
|
(def new-invoice-fixes
|
|
(into [] (dc/q '[:find ?ip ?pi ?pa ?deleted-invoice-number ?target-i ?target-total
|
|
:in $ $$ [ ?pi ...]
|
|
:where
|
|
[?pi :payment/client ?c]
|
|
[?c :client/code "NGPX"]
|
|
[?pi :payment/amount ?pa]
|
|
[?ip :invoice-payment/payment ?pi]
|
|
(not [?ip :invoice-payment/invoice])
|
|
[$$ ?ip :invoice-payment/invoice ?deleted-i _ true]
|
|
[$$ ?deleted-i :invoice/invoice-number ?deleted-invoice-number _ true]
|
|
[?target-i :invoice/client ?c]
|
|
[?target-i :invoice/status :invoice-status/unpaid]
|
|
[?target-i :invoice/invoice-number ?deleted-invoice-number]
|
|
[?target-i :invoice/total ?target-total]
|
|
#_[(iol-ion.query/dollars= ?target-total ?deleted-i-total)]]
|
|
(dc/db conn)
|
|
(dc/history (dc/db conn))
|
|
to-remove-ids)))
|
|
|
|
|
|
(reduce
|
|
(fn [acc [pi t invoice number a]]
|
|
(-> acc
|
|
(assoc-in [ pi :total] t)
|
|
(update-in [ pi :fixes-total] (fnil + 0.0) a)
|
|
(update-in [ pi :fixes-list] (fnil conj []) [invoice number])))
|
|
{}
|
|
new-invoice-fixes )
|
|
|
|
;;=> {17592295145246
|
|
;; {:total 3026.2300000000005,
|
|
;; :fixes-total 4074.4900000000016,
|
|
;; :fixes-list
|
|
;; [["549126264" 17592339946782]
|
|
;; ["549127019" 17592339946787]
|
|
;; ["549117468" 17592339946772]
|
|
;; ["549130300" 17592339946792]
|
|
;; ["549121810" 17592339946777]
|
|
;; ["549114455" 17592339946767]
|
|
;; ["549137551" 17592339946802]]},
|
|
;; 17592295143072
|
|
;; {:total 7360.6,
|
|
;; :fixes-total 3888.9599999999996,
|
|
;; :fixes-list
|
|
;; [["549136814" 17592339946797]
|
|
;; ["549139494" 17592339946807]
|
|
;; ["549139495" 17592339946812]]}}
|
|
;;
|
|
|
|
(def link-to-new-invoices (for [[ip _ _ _ i] new-invoice-fixes
|
|
n [[:db/add ip :invoice-payment/invoice i]
|
|
[:db/add i :invoice/status :invoice-status/paid]
|
|
[:db/add i :invoice/outstanding-balance 0.0]]]
|
|
n))
|
|
|
|
(def with-new-invoice-fixes (:db-after (dc/with (dc/db conn)
|
|
link-to-new-invoices)))
|
|
|
|
|
|
(def recreate-invoice-fixes
|
|
(into []
|
|
(dc/q '[:find (pull $$$ ?deleted-i [*] ) ?extant-ledger ?ip
|
|
:in $ $$ $$$ [ ?p ...]
|
|
:where
|
|
[?p :payment/client ?c]
|
|
[?p :payment/amount ?a]
|
|
[?c :client/code "NGPX"]
|
|
[?ip :invoice-payment/payment ?p]
|
|
(not [?ip :invoice-payment/invoice])
|
|
[$$ ?ip :invoice-payment/invoice ?deleted-i _ true]
|
|
[$$ ?deleted-i :invoice/invoice-number ?deleted-invoice-number _ true]
|
|
[$$ ?deleted-i :invoice/total ?deleted-invoice-total _ true]
|
|
[$$ ?extant-ledger :journal-entry/original-entity ?deleted-i _ true]
|
|
[?extant-ledger :journal-entry/date ?d]
|
|
(not [?extant-ledger :journal-entry/original-entity ?deleted-i])
|
|
#_[?target-i :invoice/client ?c]
|
|
#_[?target-i :invoice/status :invoice-status/unpaid]
|
|
#_[?target-i :invoice/invoice-number ?deleted-invoice-number]
|
|
#_[?target-i :invoice/total ?target-total]
|
|
#_[(iol-ion.query/dollars= ?target-total ?deleted-i-total)]]
|
|
|
|
with-new-invoice-fixes
|
|
(dc/history (dc/db conn))
|
|
(dc/as-of (dc/db conn) #inst "2023-10-01")
|
|
|
|
to-remove-ids)))
|
|
|
|
;;=> #'user/recreate-invoice-fixes
|
|
;;
|
|
recreate-invoice-fixes
|
|
|
|
(def recreate-invoices
|
|
(for [[{:keys [db/id] :as i} je ip] recreate-invoice-fixes
|
|
n [(assoc i :invoice/status :invoice-status/paid :invoice/outstanding-balance 0.0)
|
|
{:db/id ip :invoice-payment/invoice id}
|
|
{:db/id je :journal-entry/original-entity id}]]
|
|
n))
|
|
|
|
to-remove-ids
|
|
(def with-recreate-invoices
|
|
(:db-after (dc/with
|
|
with-new-invoice-fixes
|
|
recreate-invoices
|
|
)))
|
|
|
|
|
|
|
|
|
|
(into []
|
|
(dc/q '[:find ?a (sum ?ipa) (sum ?it) (pull ?p [:payment/amount :payment/date :payment/memo {:invoice-payment/_payment [:invoice-payment/amount {:invoice-payment/invoice [:invoice/invoice-number :invoice/total]}]}])
|
|
:with ?ip
|
|
:in $ [ ?p ...]
|
|
:where
|
|
[?p :payment/client ?c]
|
|
[?p :payment/amount ?a]
|
|
[?c :client/code "NGPX"]
|
|
[?ip :invoice-payment/payment ?p]
|
|
[?ip :invoice-payment/amount ?ipa ]
|
|
[?ip :invoice-payment/invoice ?i]
|
|
[?i :invoice/total ?it]
|
|
#_(not [?ip :invoice-payment/invoice])
|
|
#_[$$ ?ip :invoice-payment/invoice ?deleted-i _ true]
|
|
#_[$$ ?deleted-i :invoice/invoice-number ?deleted-invoice-number _ true]
|
|
#_[$$ ?deleted-i :invoice/total ?deleted-invoice-total _ true]
|
|
#_[$$ ?extant-ledger :journal-entry/original-entity ?deleted-i _ true]
|
|
#_[?extant-ledger :journal-entry/date ?d]
|
|
#_(not [?extant-ledger :journal-entry/original-entity ?deleted-i])
|
|
#_[?target-i :invoice/client ?c]
|
|
#_[?target-i :invoice/status :invoice-status/unpaid]
|
|
#_[?target-i :invoice/invoice-number ?deleted-invoice-number]
|
|
#_[?target-i :invoice/total ?target-total]
|
|
#_[(iol-ion.query/dollars= ?target-total ?deleted-i-total)]]
|
|
with-recreate-invoices
|
|
|
|
|
|
to-remove-ids))
|
|
|
|
|
|
|
|
(dc/pull (dc/db conn)
|
|
'[*]
|
|
17592295143072)
|
|
|
|
|
|
(reverse (sort (dc/q '[:find ?d
|
|
:where [?je :journal-entry/source "invoice"]
|
|
(not [?je :journal-entry/original-entity])
|
|
[?je :journal-entry/date ?d]]
|
|
(dc/db conn))))
|