create manual ledger experience
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn pull-attr]]
|
||||
[auto-ap.datomic.accounts :as d-accounts]
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.permissions :refer [wrap-must]]
|
||||
[auto-ap.routes.ledger :as route]
|
||||
[auto-ap.routes.utils :refer [wrap-client-redirect-unauthenticated]]
|
||||
@@ -12,11 +14,57 @@
|
||||
[auto-ap.ssr.hx :as hx]
|
||||
[auto-ap.ssr.nested-form-params :refer [wrap-nested-form-params]]
|
||||
[auto-ap.ssr.svg :as svg]
|
||||
[auto-ap.ssr.utils :refer [apply-middleware-to-all-handlers entity-id
|
||||
modal-response wrap-schema-enforce]]
|
||||
[auto-ap.ssr.utils :refer [apply-middleware-to-all-handlers check-allowance
|
||||
check-location-belongs clj-date-schema entity-id
|
||||
html-response modal-response money
|
||||
wrap-form-4xx-2 wrap-schema-enforce]]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]
|
||||
[datomic.api :as dc]))
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as dc]
|
||||
[iol-ion.query :refer [dollars=]])
|
||||
(:import [java.util UUID]))
|
||||
|
||||
(def new-ledger-schema
|
||||
[:and
|
||||
[:map
|
||||
[:db/id {:optional true} [:maybe entity-id]]
|
||||
[:journal-entry/client [:entity-map {:pull [:db/id :client/name :client/locations]}]]
|
||||
[:journal-entry/date clj-date-schema]
|
||||
[:journal-entry/vendor {:optional true :default nil}
|
||||
[:entity-map {:pull [:db/id :vendor/name]}]]
|
||||
[:journal-entry/amount {:min 0.01}
|
||||
money]
|
||||
[:journal-entry/line-items
|
||||
[:vector {:coerce? true}
|
||||
[:and
|
||||
[:map
|
||||
[:journal-entry-line/account [:and [:entity-map {:pull a/default-read}]
|
||||
[:fn {:error/message "Not an allowed account."}
|
||||
(fn check-allow [x]
|
||||
(check-allowance x :account/default-allowance))]]]
|
||||
[:journal-entry-line/debit {:optional true :default nil} [:maybe money]]
|
||||
[:journal-entry-line/credit {:optional true :default nil} [:maybe money]]
|
||||
[:journal-entry-line/location :string]]
|
||||
[:fn {:error/fn (fn [r x] (:type r))
|
||||
:error/path [:invoice-expense-account/location]}
|
||||
(fn [iea]
|
||||
(check-location-belongs (:journal-entry-line/location iea)
|
||||
(:journal-entry-line/account iea)))]]]]]
|
||||
|
||||
[:fn {:error/message "Debits and Credits must add up to amount"}
|
||||
(fn [je]
|
||||
(and
|
||||
(dollars= (:journal-entry/amount je) (->> je
|
||||
:journal-entry/line-items
|
||||
(map :journal-entry-line/debit)
|
||||
(filter identity)
|
||||
(reduce + 0.0)))
|
||||
(dollars= (:journal-entry/amount je) (->> je
|
||||
:journal-entry/line-items
|
||||
(map :journal-entry-line/credit)
|
||||
(filter identity)
|
||||
(reduce + 0.0)))))]])
|
||||
|
||||
(defn- account-typeahead*
|
||||
[{:keys [name value client-id x-model]}]
|
||||
@@ -28,47 +76,50 @@
|
||||
:x-model x-model
|
||||
:value value
|
||||
:content-fn (fn [value]
|
||||
(let [a (dc/pull (dc/db conn) d-accounts/default-read value)]
|
||||
(when value
|
||||
(when value
|
||||
(str
|
||||
(:account/numeric-code a)
|
||||
(:account/numeric-code value)
|
||||
" - "
|
||||
(:account/name (d-accounts/clientize a
|
||||
client-id))))))})])
|
||||
(:account/name (d-accounts/clientize value
|
||||
client-id)))))})])
|
||||
|
||||
(defn- location-select*
|
||||
[{:keys [name account-location client-locations value]}]
|
||||
(com/select {:options (into [["" ""]]
|
||||
(cond account-location
|
||||
[[account-location account-location]]
|
||||
|
||||
(seq client-locations)
|
||||
(into [["Shared" "Shared"]]
|
||||
(for [cl client-locations]
|
||||
[cl cl]))
|
||||
|
||||
:else
|
||||
[["Shared" "Shared"]]))
|
||||
(for [c (seq client-locations)]
|
||||
[c c])))
|
||||
:name name
|
||||
:value value
|
||||
:class "w-full"}))
|
||||
|
||||
(defn location-select [{{:keys [name account-id client-id value] :as qp} :query-params}]
|
||||
(html-response (location-select* {:name name
|
||||
:value value
|
||||
:account-location (some->> account-id
|
||||
(pull-attr (dc/db conn) :account/location))
|
||||
:client-locations (some->> client-id
|
||||
(pull-attr (dc/db conn) :client/locations))})))
|
||||
|
||||
(defn- line-item-row*
|
||||
[account client-id client-locations]
|
||||
(com/data-grid-row
|
||||
(-> {:x-data (hx/json {:accountId (or (:db/id (fc/field-value (:transaction-rule-account/account account)))
|
||||
(fc/field-value (:transaction-rule-account/account account)))
|
||||
:location (fc/field-value (:transaction-rule-account/location account))
|
||||
(-> {:x-data (hx/json {:accountId (or (:db/id (fc/field-value (:journal-entry-line/account account)))
|
||||
(fc/field-value (:journal-entry-line/account account)))
|
||||
:location (fc/field-value (:journal-entry-line/location account))
|
||||
:show (boolean (not (fc/field-value (:new? account))))})
|
||||
:data-key "show"
|
||||
:x-ref "p"}
|
||||
hx/alpine-mount-then-appear)
|
||||
(let [account-name (fc/field-name (:transaction-rule-account/account account))]
|
||||
(let [account-name (fc/field-name (:journal-entry-line/account account))]
|
||||
(list
|
||||
|
||||
(fc/with-field :db/id
|
||||
(com/hidden {:name (fc/field-name)
|
||||
:value (fc/field-value)}))
|
||||
(fc/with-field :transaction-rule-account/account
|
||||
(fc/with-field :journal-entry-line/account
|
||||
(com/data-grid-cell
|
||||
{}
|
||||
(com/validated-field
|
||||
@@ -82,7 +133,7 @@
|
||||
:client-id client-id
|
||||
:name (fc/field-name)
|
||||
:x-model "accountId"}))))
|
||||
(fc/with-field :transaction-rule-account/location
|
||||
(fc/with-field :journal-entry-line/location
|
||||
(com/data-grid-cell
|
||||
{}
|
||||
(com/validated-field
|
||||
@@ -96,119 +147,177 @@
|
||||
:hx-get (bidi/path-for ssr-routes/only-routes ::route/location-select)
|
||||
:x-init "$watch('clientId', cid => $dispatch('changed', $data)); $watch('accountId', cid => $dispatch('changed', $data) )"}]
|
||||
(location-select* {:name (fc/field-name)
|
||||
:account-location (:account/location (cond->> (:transaction-rule-account/account @account)
|
||||
(nat-int? (:transaction-rule-account/account @account)) (dc/pull (dc/db conn)
|
||||
'[:account/location])))
|
||||
:account-location (:account/location (:account/location (:journal-entry-line/account @account)))
|
||||
:client-locations client-locations
|
||||
:x-model "location"
|
||||
:value (fc/field-value)}))))
|
||||
(fc/with-field :transaction-rule-account/percentage
|
||||
(fc/with-field :journal-entry-line/debit
|
||||
(com/data-grid-cell
|
||||
{}
|
||||
(com/validated-field
|
||||
{:errors (fc/field-errors)}
|
||||
(com/money-input {:name (fc/field-name)
|
||||
:class "w-16"
|
||||
:value (some-> (fc/field-value)
|
||||
(* 100)
|
||||
(long))}))))))
|
||||
:value (fc/field-value)}))))
|
||||
(fc/with-field :journal-entry-line/credit
|
||||
(com/data-grid-cell
|
||||
{}
|
||||
(com/validated-field
|
||||
{:errors (fc/field-errors)}
|
||||
(com/money-input {:name (fc/field-name)
|
||||
:class "w-16"
|
||||
:value (fc/field-value)}))))))
|
||||
(com/data-grid-cell {:class "align-top"}
|
||||
(com/a-icon-button {"@click.prevent.stop" "show=false; setTimeout(() => $refs.p.remove(), 500)"} svg/x))))
|
||||
|
||||
(defn account-typeahead [{{:keys [name value client-id] :as qp} :query-params}]
|
||||
(html-response (account-typeahead* {:name name
|
||||
:value value
|
||||
:client-id client-id
|
||||
:x-model "accountId"})))
|
||||
|
||||
(defn form* [request]
|
||||
(fc/start-form (:form-params request)
|
||||
(:form-errors request)
|
||||
[:div.flex.gap-4.flex-col
|
||||
(fc/with-field :journal-entry/client
|
||||
(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))})]))
|
||||
(fc/with-field :invoice/date
|
||||
(com/validated-field
|
||||
{:label "Date"
|
||||
:errors (fc/field-errors)}
|
||||
[:div {:class "w-24"}
|
||||
(com/date-input {:value (some-> (fc/field-value)
|
||||
(atime/unparse-local atime/normal-date))
|
||||
:name (fc/field-name)
|
||||
:error? (fc/field-errors)
|
||||
:placeholder "1/1/2024"})]))
|
||||
(fc/with-field :journal-entry/vendor
|
||||
(com/validated-field
|
||||
{:label "Vendor"
|
||||
:errors (fc/field-errors)}
|
||||
[:div.w-96
|
||||
(com/typeahead {:name (fc/field-name)
|
||||
:error? (fc/error?)
|
||||
:disabled (boolean (-> request :multi-form-state :snapshot :db/id))
|
||||
:class "w-96"
|
||||
:placeholder "Search..."
|
||||
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
||||
:value (fc/field-value)
|
||||
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})]))
|
||||
(fc/with-field :invoice/total
|
||||
(com/validated-field
|
||||
{:label "Total"
|
||||
:errors (fc/field-errors)}
|
||||
[:div {:class "w-16"}
|
||||
(com/money-input {:value (-> (fc/field-value))
|
||||
:name (fc/field-name)
|
||||
:class "w-24"
|
||||
:error? (fc/field-errors)
|
||||
:placeholder "212.44"})]))
|
||||
(fc/with-field :journal-entry/line-items
|
||||
(com/validated-field
|
||||
{:errors (fc/field-errors)}
|
||||
(let [client-locations (some->> (fc/field-value) :transaction-rule/client (pull-attr (dc/db conn) :client/locations))]
|
||||
(alog/peek :FP (:form-errors request))
|
||||
(let [client (some-> request :form-params :journal-entry/client)
|
||||
client-locations (some-> client :client/locations)]
|
||||
(fc/start-form (:form-params request)
|
||||
(:form-errors request)
|
||||
[:div.flex.gap-4.flex-col {:x-data (hx/json {:clientId (or (:db/id (fc/field-value (:journal-entry/client fc/*current*)))
|
||||
(:db/id (:client request)))
|
||||
:vendorId (:db/id (fc/field-value (:journal-entry/vendor fc/*current*))) })}
|
||||
(fc/with-field :journal-entry/client
|
||||
(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)
|
||||
:value-fn :db/id
|
||||
:content-fn :client/name
|
||||
:x-model "clientId"})]))
|
||||
(fc/with-field :journal-entry/date
|
||||
(com/validated-field
|
||||
{:label "Date"
|
||||
:errors (fc/field-errors)}
|
||||
[:div {:class "w-24"}
|
||||
(com/date-input {:value (some-> (fc/field-value)
|
||||
(atime/unparse-local atime/normal-date))
|
||||
:name (fc/field-name)
|
||||
:error? (fc/field-errors)
|
||||
:placeholder "1/1/2024"})]))
|
||||
(fc/with-field :journal-entry/vendor
|
||||
(com/validated-field
|
||||
{:label "Vendor"
|
||||
:errors (fc/field-errors)}
|
||||
[:div.w-96
|
||||
(com/typeahead {:name (fc/field-name)
|
||||
:error? (fc/error?)
|
||||
:disabled (boolean (-> request :multi-form-state :snapshot :db/id))
|
||||
:class "w-96"
|
||||
:placeholder "Search..."
|
||||
:url (bidi/path-for ssr-routes/only-routes :vendor-search)
|
||||
:value (fc/field-value)
|
||||
:value-fn :db/id
|
||||
:content-fn :vendor/name})]))
|
||||
(fc/with-field :journal-entry/amount
|
||||
(com/validated-field
|
||||
{:label "Total"
|
||||
:errors (fc/field-errors)}
|
||||
[:div {:class "w-16"}
|
||||
(com/money-input {:value (-> (fc/field-value))
|
||||
:name (fc/field-name)
|
||||
:class "w-24"
|
||||
:error? (fc/field-errors)
|
||||
:placeholder "212.44"})]))
|
||||
(fc/with-field :journal-entry/line-items
|
||||
(com/validated-field
|
||||
{:errors (fc/field-errors)}
|
||||
(com/data-grid {:headers [(com/data-grid-header {} "Account")
|
||||
(com/data-grid-header {:class "w-32"} "Location")
|
||||
(com/data-grid-header {:class "w-16"} "%")
|
||||
(com/data-grid-header {:class "w-16"} "Debit")
|
||||
(com/data-grid-header {:class "w-16"} "Credit")
|
||||
(com/data-grid-header {:class "w-16"})]}
|
||||
(fc/cursor-map #(line-item-row* % (:transaction-rule/client (fc/field-value)) client-locations))
|
||||
(com/data-grid-new-row {:colspan 4
|
||||
(fc/cursor-map #(line-item-row* % client client-locations))
|
||||
(com/data-grid-new-row {:colspan 5
|
||||
:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
::route/new-line-item)
|
||||
:index (count (fc/field-value))
|
||||
:tr-params (hx/bind-alpine-vals {} {"client-id" "clientId"})}
|
||||
"New account")))))]))
|
||||
"New account"))))])))
|
||||
|
||||
|
||||
(defn new [request]
|
||||
(alog/peek ::FP (:form-params request))
|
||||
(modal-response
|
||||
(com/modal {}
|
||||
(com/modal-card {:class ""}
|
||||
[:div "New ledger entry"]
|
||||
[:div (form* request)]
|
||||
[:div (com/button {:color :primary} "Save")]))
|
||||
#_[:div]))
|
||||
(com/modal {:hx-target "this"
|
||||
:hx-indicator "this"}
|
||||
[:form {:hx-post (bidi/path-for ssr-routes/only-routes
|
||||
::route/new-submit)}
|
||||
(com/modal-card {:class "md:h-[800px] md:w-[750px] flex-col relative"
|
||||
:error (when (vector? (:form-errors request))
|
||||
(str/join ", "(:form-errors request) ))}
|
||||
[:div "New ledger entry"]
|
||||
[:div.overflow-y-scroll.relative (form* request)]
|
||||
[:div (com/button {:color :primary} "Save")])])))
|
||||
|
||||
(defn new-submit [request]
|
||||
@(dc/transact conn
|
||||
[(-> (:form-params request)
|
||||
(update :journal-entry/client :db/id)
|
||||
(update :journal-entry/vendor :db/id)
|
||||
(update :journal-entry/line-items
|
||||
(fn [li]
|
||||
(mapv
|
||||
#(update % :journal-entry-line/account :db/id)
|
||||
li)))
|
||||
(assoc :journal-entry/external-id (str "manual-" (UUID/randomUUID))))])
|
||||
|
||||
|
||||
(html-response
|
||||
[:div "GOOD"]
|
||||
:headers (cond-> {"hx-trigger" "modalclose"
|
||||
#_#_"hx-retarget" (format "#entity-table tr[data-id=\"%d\"]" (:db/id invoice))
|
||||
#_#_"hx-reswap" "outerHTML"})))
|
||||
|
||||
|
||||
(def key->handler
|
||||
(apply-middleware-to-all-handlers
|
||||
(->
|
||||
{::route/new (-> new
|
||||
#_(wrap-schema-enforce :query-schema query-schema)
|
||||
#_(wrap-form-4xx-2 profit-and-loss))
|
||||
::route/new-line-item
|
||||
#_(wrap-schema-enforce :query-schema query-schema)
|
||||
#_(wrap-form-4xx-2 profit-and-loss))
|
||||
::route/account-typeahead (-> account-typeahead
|
||||
(wrap-schema-enforce :query-schema [:map
|
||||
[:name :string]
|
||||
[:client-id {:optional true}
|
||||
[:maybe entity-id]]
|
||||
[:value {:optional true}
|
||||
[:maybe entity-id]]]))
|
||||
::route/new-submit (-> new-submit
|
||||
(wrap-schema-enforce :form-schema new-ledger-schema)
|
||||
(wrap-form-4xx-2 new))
|
||||
::route/location-select (-> location-select
|
||||
(wrap-schema-enforce :query-schema [:map
|
||||
[:name :string]
|
||||
[:client-id {:optional true}
|
||||
[:maybe entity-id]]
|
||||
[:account-id {:optional true}
|
||||
[:maybe entity-id]]]))
|
||||
::route/new-line-item
|
||||
(-> (add-new-entity-handler [:journal-entry/line-items]
|
||||
(fn render [cursor request]
|
||||
(line-item-row*
|
||||
cursor
|
||||
(:client-id (:query-params request))
|
||||
(some->> (:client-id (:query-params request)) (pull-attr (dc/db conn) :client/locations))))
|
||||
(fn build-new-row [base _]
|
||||
(assoc base :transaction-rule-account/location "Shared")))
|
||||
(wrap-schema-enforce :query-schema [:map
|
||||
[:client-id {:optional true}
|
||||
[:maybe entity-id]]]))})
|
||||
|
||||
(fn render [cursor request]
|
||||
(line-item-row*
|
||||
cursor
|
||||
(:client-id (:query-params request))
|
||||
(some->> (:client-id (:query-params request)) (pull-attr (dc/db conn) :client/locations)))))
|
||||
(wrap-schema-enforce :query-schema [:map
|
||||
[:client-id {:optional true}
|
||||
[:maybe entity-id]]]))})
|
||||
|
||||
(fn [h]
|
||||
(-> h
|
||||
#_(wrap-merge-prior-hx)
|
||||
|
||||
Reference in New Issue
Block a user