Makes it so that the edit page works better

This commit is contained in:
2025-03-05 14:25:43 -08:00
parent f881bdcc93
commit 59d48342ea
6 changed files with 118 additions and 76 deletions

View File

@@ -11,6 +11,7 @@
exception->4xx]]
[auto-ap.import.transactions :as i-transactions]
[auto-ap.logging :as alog]
[auto-ap.routes.payments :as payment-route]
[auto-ap.routes.transactions :as route]
[auto-ap.routes.utils
:refer [wrap-client-redirect-unauthenticated]]
@@ -25,7 +26,7 @@
[auto-ap.ssr.svg :as svg]
[auto-ap.ssr.utils
:refer [->db-id apply-middleware-to-all-handlers check-allowance
check-location-belongs entity-id html-response
check-location-belongs entity-id html-response modal-response
ref->enum-schema strip wrap-schema-enforce]]
[auto-ap.time :as atime]
[bidi.bidi :as bidi]
@@ -143,7 +144,7 @@
(:account/name (d-accounts/clientize (dc/pull (dc/db conn) d-accounts/default-read value)
client-id)))})])
(defn- transaction-account-row* [{:keys [value client-id]}]
(defn transaction-account-row* [{:keys [value client-id]}]
(com/data-grid-row
(-> {:x-data (hx/json {:show (boolean (not (fc/field-value (:new? value))))
:accountId (fc/field-value (:transaction-account/account value))})
@@ -601,7 +602,7 @@
[:div.text-sm.text-gray-600 (-> invoice :invoice/vendor :vendor/name)]]
[:div.text-right
[:div.font-medium (format "$%.2f" (:invoice/outstanding-balance invoice))]
(com/hidden {:name "unpaid-invoice-ids[]" :value (:db/id invoice)})]])
(com/hidden {:name "unpaid-invoice-ids" :value (:db/id invoice)})]])
[:div.flex.justify-end.mt-4
(com/button {:color :primary :size :small} "Match")]]])]]
[:div.text-center.py-4.text-gray-500 "No matching unpaid invoices available for this transaction."])]))
@@ -663,7 +664,16 @@
(let [tx-id (or (-> request :multi-form-state :snapshot :db/id)
(get-in request [:route-params :db/id]))
tx (when tx-id (d-transactions/get-by-id tx-id))
payment (:transaction/payment tx)]
payment (dc/pull
(dc/db conn)
'[:payment/amount
[:payment/date :xform clj-time.coerce/from-date]
{ [ :payment/status :xform iol-ion.query/ident] [:db/ident]
:payment/vendor [:vendor/name]}
]
(-> tx :transaction/payment :db/id))]
(println "PAYMENT IS" payment)
(when payment
[:div.my-4.p-4.bg-blue-50.rounded
[:h3.text-lg.font-bold.mb-2 "Linked Payment"]
@@ -676,20 +686,23 @@
[:div (-> payment :payment/vendor :vendor/name)]]
[:div.flex.justify-between
[:div.font-medium "Amount"]
[:div (format "$%.2f" (:payment/amount payment))]]
[:div (some->> (:payment/amount payment) (format "$%.2f" ))]]
[:div.flex.justify-between
[:div.font-medium "Status"]
[:div (-> payment :payment/status :db/ident name)]]
[:div (some-> payment :payment/status name)]]
[:div.flex.justify-between
[:div.font-medium "Date"]
[:div (some-> payment :payment/date coerce/to-date-time (atime/unparse-local atime/normal-date))]]
[:div.mt-4
[:form {:hx-post (bidi/path-for ssr-routes/only-routes ::route/unlink-payment)
:hx-target "#modal-holder"
:hx-swap "outerHTML"
:hx-confirm "Are you sure you want to unlink this payment?"}
(com/hidden {:name "transaction-id" :value tx-id})
(com/button {:color :danger :size :small} "Unlink Payment")]]]])))
[:div (some-> payment :payment/date (atime/unparse-local atime/normal-date))]]
[:div.mt-4 {:hx-post (bidi/path-for ssr-routes/only-routes ::route/unlink-payment)
:hx-params "unlink-transaction-id"
:hx-trigger "unlinkPayment"
:hx-target "#modal-holder"
:hx-swap "outerHTML"
:hx-confirm "Are you sure you want to unlink this payment?"}
(com/hidden {:name "unlink-transaction-id" :value tx-id})
(com/a-button {:color :red :size :small
"@click" "$dispatch('unlinkPayment')"} "Unlink Payment")]]])))
(defn count-payment-matches [request]
(count (get-available-payments request)))
@@ -954,7 +967,7 @@
[:p.text-gray-600.mt-2 "The transaction has been linked to the selected payment."]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose') $dispatch('refreshTable')"} "Close")]]])))
"@click" "$dispatch('modalclose'); $dispatch('refreshTable')"} "Close")]]])))
(defn match-autopay-invoices [{{:strs [transaction-id autopay-invoice-ids]} :form-params :as request}]
(let [transaction-id (Long/parseLong transaction-id)
@@ -1003,13 +1016,10 @@
[:p.text-gray-600.mt-2 "The transaction has been linked to the selected autopay invoices."]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose') $dispatch('refreshTable')"} "Close")]]])))
"@click" "$dispatch('modalclose'); $dispatch('refreshTable')"} "Close")]]])))
(defn match-unpaid-invoices [{{:strs [transaction-id unpaid-invoice-ids]} :form-params :as request}]
(let [transaction-id (Long/parseLong transaction-id)
unpaid-invoice-ids (if (string? unpaid-invoice-ids)
[(Long/parseLong unpaid-invoice-ids)]
(mapv #(Long/parseLong %) unpaid-invoice-ids))
(defn match-unpaid-invoices [{{:keys [transaction-id unpaid-invoice-ids]} :form-params :as request}]
(let [ _ (println "UNPAID INVOOICES " unpaid-invoice-ids)
transaction (d-transactions/get-by-id transaction-id)
db (dc/db conn)
invoice-clients (set (map #(pull-ref db :invoice/client %) unpaid-invoice-ids))
@@ -1021,7 +1031,9 @@
(when (or (> (count invoice-clients) 1)
(not= (-> transaction :transaction/client :db/id)
(first invoice-clients)))
(throw (ex-info "Clients don't match" {:validation-error "Invoice(s) and transaction client do not match."})))
(throw (ex-info "Clients don't match" {:validation-error "Invoice(s) and transaction client do not match."
:transaction-client (-> transaction :transaction/client :db/id)
:invoice-clients invoice-clients})))
(when-not (dollars= (- (:transaction/amount transaction))
invoice-amount)
@@ -1052,7 +1064,7 @@
[:p.text-gray-600.mt-2 "The transaction has been linked to the selected unpaid invoices."]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose') $dispatch('refreshTable')"} "Close")]]])))
"@click" "$dispatch('modalclose'); $dispatch('refreshTable')"} "Close")]]])))
(defn apply-rule [{{:strs [transaction-id rule-id]} :form-params :as request}]
(let [transaction-id (Long/parseLong transaction-id)
@@ -1095,26 +1107,27 @@
[:p.text-gray-600.mt-2 "The selected rule has been applied to this transaction."]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose') $dispatch('refreshTable')"} "Close")]]])))
"@click" "$dispatch('modalclose'); $dispatch('refreshTable')"} "Close")]]])))
(defn unlink-payment [{{:strs [transaction-id]} :form-params :as request}]
(let [transaction-id (Long/parseLong transaction-id)
transaction (dc/pull (dc/db conn)
[:transaction/approval-status
:transaction/status
:transaction/date
:transaction/location
:transaction/vendor
:transaction/accounts
:transaction/client [:db/id]
{:transaction/payment [:payment/date {:payment/status [:db/ident]} :db/id]}]
transaction-id)
(defn unlink-payment [{{:keys [unlink-transaction-id]} :form-params :as request}]
(let [ transaction (dc/pull (dc/db conn)
'[:transaction/approval-status
:transaction/date
:transaction/location
:transaction/vendor
:transaction/accounts
:transaction/status
:transaction/client [:db/id]
{:transaction/payment [:payment/date
{[ :payment/status :xform iol-ion.query/ident] [:db/ident]} :db/id]}]
unlink-transaction-id)
payment (-> transaction :transaction/payment)]
(exception->4xx #(assert-can-see-client (:identity request) (-> transaction :transaction/client :db/id)))
(exception->4xx #(assert-not-locked (-> transaction :transaction/client :db/id) (:transaction/date transaction)))
(when (not= :payment-status/cleared (-> payment :payment/status :db/ident))
(when (not= :payment-status/cleared (-> payment :payment/status))
(throw (ex-info "Payment can't be undone because it isn't cleared."
{:validation-error "Payment can't be undone because it isn't cleared."})))
@@ -1159,16 +1172,31 @@
:transaction/accounts nil}]]
(:identity request))))
(solr/touch-with-ledger transaction-id)
(solr/touch-with-ledger unlink-transaction-id)
(html-response [:div.p-4
[:div.text-2xl.text-center.text-green-600 svg/check]
[:div.text-center.mt-4
[:h3.text-xl.font-bold "Transaction unlinked successfully"]
[:p.text-gray-600.mt-2 "The transaction has been unlinked from its payment."]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose') $dispatch('refreshTable')"} "Close")]]])))
(modal-response
(com/modal {}
(com/modal-card-advanced
{:class "transition duration-300 ease-in-out htmx-swapping:-translate-x-2/3 htmx-swapping:opacity-0 htmx-swapping:scale-0 htmx-added:translate-x-2/3 htmx-added:opacity-0 htmx-added:scale-0 scale-100 translate-x-0 opacity-100"}
(com/modal-body {}
[:div.flex.flex-col.mt-4.space-y-4.items-center
[:div.w-24.h-24.bg-green-50.rounded-full.p-4.text-green-300.animate-gg
svg/thumbs-up]
[:div.text-center.mt-4
[:h3.text-xl.font-bold "Transaction unlinked successfully"]
[:p.text-gray-600.mt-2 "The transaction has been unlinked from its payment."]
[:p "If you'd like to also void the payment, click "
(com/link
{:hx-boost true
:href (hu/url (bidi/path-for ssr-routes/only-routes ::payment-route/all-page)
{:exact-match-id (:db/id payment)})}
"here")
" to view the payment"]
[:div.mt-6
(com/button {:color :primary
"@click" "$dispatch('modalclose'); $dispatch('refreshTable')"} "Close")]]])))
:headers {"hx-trigger" "invalidated"})
))
(defn edit-transaction [{:keys [route-params] :as request}]
(mm/open-wizard-handler (-> request
@@ -1213,9 +1241,14 @@
[:maybe entity-id]]]))
::route/match-payment match-payment
::route/match-autopay-invoices match-autopay-invoices
::route/match-unpaid-invoices match-unpaid-invoices
::route/match-unpaid-invoices (-> match-unpaid-invoices
(wrap-schema-enforce :form-schema
[:map [:transaction-id entity-id]
[:unpaid-invoice-ids [:vector {:coerce? true} entity-id]]]))
::route/apply-rule apply-rule
::route/unlink-payment unlink-payment}
::route/unlink-payment (-> unlink-payment
(wrap-schema-enforce :form-schema
[:map [:unlink-transaction-id entity-id]]))}
(fn [h]
(-> h
(wrap-client-redirect-unauthenticated)))))