transaction links is visible.

This commit is contained in:
2025-03-02 20:44:08 -08:00
parent 7c7fc10a8d
commit bd501b512a
2 changed files with 117 additions and 69 deletions

File diff suppressed because one or more lines are too long

View File

@@ -251,41 +251,76 @@
(render-step
[this {:keys [multi-form-state] :as request}]
(let [extant? (mm/get-mfs-field multi-form-state :db/id)
tx (d-transactions/get-by-id extant?)]
(let [tx-id (mm/get-mfs-field multi-form-state :db/id)
tx (d-transactions/get-by-id tx-id)]
(alog/info ::TRANSACTION :i multi-form-state)
(mm/default-render-step
linear-wizard this
:head [:div.p-2 (if extant?
"Edit transaction"
"New transaction")]
:head [:div.p-2 "Edit transaction"]
:body (mm/default-step-body
{}
[:div {:x-data (hx/json {:clientId (or (fc/field-value (:transaction/client fc/*current*))
(:db/id (:client request)))
:vendorId (fc/field-value (:transaction/vendor fc/*current*))})}
(fc/with-field :db/id
(when extant?
(com/hidden {:name (fc/field-name)
:value (fc/field-value)})))
;; Read-only transaction details
[:div.mb-6.border.rounded-lg.p-4.bg-gray-50
[:h3.text-lg.font-semibold.mb-2 "Transaction Details"]
[:div.grid.grid-cols-2.gap-4
[:div
[:div.text-sm.font-medium.text-gray-500 "Amount"]
[:div.text-base (format "$%,.2f" (Math/abs (:transaction/amount tx)))]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Date"]
[:div.text-base (some-> tx :transaction/date coerce/to-date-time (atime/unparse-local atime/normal-date))]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Bank Account"]
[:div.text-base (or (-> tx :transaction/bank-account :bank-account/name) "-")]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Post Date"]
[:div.text-base (some-> tx :transaction/post-date coerce/to-date-time (atime/unparse-local atime/normal-date))]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Original Description"]
[:div.text-base (or (:transaction/description-original tx) "-")]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Simplified Description"]
[:div.text-base (or (:transaction/description-simple tx) "-")]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Check Number"]
[:div.text-base (or (:transaction/check-number tx) "-")]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Status"]
[:div.text-base (or (some-> tx :transaction/status) "-")]
]
[:div
[:div.text-sm.font-medium.text-gray-500 "Transaction Type"]
[:div.text-base (or (some-> tx :transaction/type) "-")]
]
]]
;; Hidden ID field
(fc/with-field :db/id
(com/hidden {:name (fc/field-name)
:value (fc/field-value)}))
;; Hidden client field
(fc/with-field :transaction/client
(if (or (:client request) extant?)
(com/hidden {:name (fc/field-name)
:value (or (mm/get-mfs-field multi-form-state :transaction/client)
(:db/id (:client request)))})
(com/validated-field
{:label "Client"
:errors (fc/field-errors)}
[:div.w-96
(com/typeahead {:name (fc/field-name)
:error? (fc/error?)
:class "w-96"
:placeholder "Search..."
:url (bidi/path-for ssr-routes/only-routes :company-search)
:value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :client/name c))
:x-model "clientId"})])))
(:db/id (:client request)))}))
;; Editable fields section
[:div.mt-6
[:h3.text-lg.font-semibold.mb-4 "Editable Fields"]]
;; Vendor field
(fc/with-field :transaction/vendor
(com/validated-field
{:label "Vendor"
@@ -308,6 +343,7 @@
"Add new vendor")
" in a new window, then return here."]]
;; Memo field
(fc/with-field :transaction/memo
(com/validated-field
{:label "Memo"
@@ -318,6 +354,7 @@
:error? (fc/field-errors)
:placeholder "Optional note"})]))
;; Approval status field
(fc/with-field :transaction/approval-status
(com/validated-field
{:label "Status"
@@ -327,10 +364,7 @@
:value (name (or (fc/field-value) :transaction-approval-status/unapproved))
:name (fc/field-name)})))])
:footer
(mm/default-step-footer linear-wizard this :validation-route ::route/edit-wizard-navigate
#_#_:next-button (com/button {:color :primary :x-ref "next" :class "w-32"
:hx-put (bidi.bidi/path-for ssr-routes/only-routes
::route/edit-wizard-navigate)} "Next"))
(mm/default-step-footer linear-wizard this :validation-route ::route/edit-wizard-navigate)
:validation-route ::route/edit-wizard-navigate)))
mm/Initializable
@@ -576,39 +610,53 @@
(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))
patterns (dc/q '[:find ?r ?name ?pattern
patterns (dc/q '[:find (pull ?r
[:db/id
:transaction-rule/description
:transaction-rule/note
:transaction-rule/client-group
:transaction-rule/dom-gte :transaction-rule/dom-lte
:transaction-rule/amount-gte :transaction-rule/amount-lte
:transaction-rule/client :transaction-rule/bank-account
:transaction-rule/yodlee-merchant])
:where
[?r :transaction-rule/description ?pattern]
[?r :transaction-rule/name ?name]]
[?r :transaction-rule/description]
]
(dc/db conn))]
(when tx
patterns
#_(filter (fn [[rule-id rule-name pattern]]
(rm/pattern-matches? pattern (:transaction/description-original tx)))
patterns))))
(->> patterns
(map first)
(filter (fn [rule]
(rm/rule-applies? (-> tx
(update :transaction/date coerce/to-date)
) (-> rule
(update :transaction-rule/description #(some-> % iol-ion.query/->pattern))))))
))))
(defn transaction-rules-view [request]
(let [matching-rules (get-available-rules request)]
(alog/peek ::MATCHING matching-rules)
[:div
(if (seq matching-rules)
[:div
[:h3.text-lg.font-bold.mb-4 "Matching Transaction Rules"]
[:div.space-y-2
(for [[rule-id rule-name pattern] matching-rules]
[:form.py-2.border-b.border-gray-200 {:hx-post (bidi/path-for ssr-routes/only-routes
(for [{:keys [:db/id :transaction-rule/note :transaction-rule/description]} matching-rules]
[:div.py-2.border-b.border-gray-200 {:hx-post (bidi/path-for ssr-routes/only-routes
::route/apply-rule)
:hx-trigger "applyRule"
:hx-target "#modal-holder"
:hx-swap "outerHTML"}
(com/hidden {:name "transaction-id"
:value (get-in request [:multi-form-state :snapshot :db/id])})
(com/hidden {:name "rule-id"
:value rule-id})
:value id})
[:div.flex.justify-between.items-center
[:div.space-y-1
[:div.font-medium rule-name]
[:div.font-medium note]
[:div.text-sm.text-gray-600
(str "Pattern: " pattern)]]
(com/button {:color :primary :size :small} "Apply")]])]]
(str "Pattern: " description)]]
(com/a-button {:color :primary :size :small "@click" "$dispatch('applyRule')"} "Apply")]])]]
[:div.text-center.py-4.text-gray-500 "No matching rules found for this transaction."])]))
(defn payment-info-view [request]
@@ -716,10 +764,8 @@
(let [tx-data (-> multi-form-state :snapshot)
tx-id (:db/id tx-data)
client-id (->db-id (:transaction/client tx-data))
existing-tx (when tx-id (d-transactions/get-by-id tx-id))
transaction [:upsert-transaction (cond-> tx-data
true (dissoc :db/id)
tx-id (assoc :db/id tx-id))]]
existing-tx (d-transactions/get-by-id tx-id)
transaction [:upsert-transaction (assoc tx-data :db/id tx-id)]]
(alog/info ::transaction transaction :entity transaction)
(exception->4xx #(assert-can-see-client (:identity request) client-id))
@@ -739,7 +785,7 @@
(let [transaction-result (audit-transact [transaction] (:identity request))]
(try
(solr/touch-with-ledger (or tx-id (get-in transaction-result [:tempids "invoice"])))
(solr/touch-with-ledger tx-id)
(catch Exception e
(alog/error ::cant-save-solr :error e)))
@@ -751,21 +797,21 @@
(defn initial-edit-wizard-state [request]
(let [tx-id (-> request :route-params :db/id)
entity (when tx-id
(dc/pull (dc/db conn)
entity (dc/pull (dc/db conn)
'[:db/id
:transaction/vendor
:transaction/client
:transaction/description-original
:transaction/status
:transaction/type
:transaction/memo
{ [ :transaction/approval-status :xform iol-ion.query/ident] [:db/ident]}
:transaction/amount
:transaction/accounts]
tx-id))
entity (if entity
(-> entity
tx-id)
entity (-> entity
(update :transaction/vendor :db/id)
(update :transaction/client :db/id))
{})]
(update :transaction/client :db/id))]
(mm/->MultiStepFormState entity
[]
entity)))
@@ -916,7 +962,8 @@
(defn apply-rule [{{:strs [transaction-id rule-id]} :form-params :as request}]
(let [transaction-id (Long/parseLong transaction-id)
rule-id (Long/parseLong rule-id)
transaction (d-transactions/get-by-id transaction-id)
transaction (-> (d-transactions/get-by-id transaction-id)
(update :transaction/date coerce/to-date)) ;; TODO make rule application flexible for date-time and dates
transaction-rule (dc/pull (dc/db conn)
[:transaction-rule/description
:transaction-rule/vendor
@@ -941,6 +988,7 @@
:transaction/amount (:transaction/amount transaction)}
transaction-rule
locations)]
(alog/peek ::APPLY-RULE updated-tx)
(audit-transact [[:upsert-transaction updated-tx]] (:identity request)))
(solr/touch-with-ledger transaction-id)