new invoice progress

This commit is contained in:
Bryce
2024-03-22 20:56:22 -07:00
parent c22cd9914c
commit 9d2b4ba646
3 changed files with 175 additions and 119 deletions

View File

@@ -14,6 +14,7 @@ document.addEventListener('alpine:init', () => {
effect(() => { effect(() => {
dependent_properties(props => { dependent_properties(props => {
console.log("CHANGED PROPS", expression, props)
el.dispatchEvent( el.dispatchEvent(
new CustomEvent(value, { new CustomEvent(value, {
props, props,

View File

@@ -12,10 +12,12 @@
[auto-ap.ssr.components.multi-modal :as mm] [auto-ap.ssr.components.multi-modal :as mm]
[auto-ap.ssr.form-cursor :as fc] [auto-ap.ssr.form-cursor :as fc]
[auto-ap.ssr.hx :as hx] [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.svg :as svg]
[auto-ap.ssr.utils [auto-ap.ssr.utils
:refer [apply-middleware-to-all-handlers clj-date-schema :refer [apply-middleware-to-all-handlers clj-date-schema
entity-id html-response money wrap-schema-enforce]] entity-id html-response money wrap-schema-decode
wrap-schema-enforce]]
[auto-ap.time :as atime] [auto-ap.time :as atime]
[bidi.bidi :as bidi] [bidi.bidi :as bidi]
[clj-time.core :as time] [clj-time.core :as time]
@@ -27,6 +29,7 @@
[:map [:map
[:invoice/client entity-id] [:invoice/client entity-id]
[:invoice/date clj-date-schema] [:invoice/date clj-date-schema]
[:invoice/due {:optional true} [:maybe clj-date-schema]]
[:invoice/vendor entity-id] [:invoice/vendor entity-id]
[:invoice/expense-accounts [:invoice/expense-accounts
[:vector {:coerce? true} [:vector {:coerce? true}
@@ -46,7 +49,7 @@
[]) [])
(step-schema [_] (step-schema [_]
(mut/select-keys (mm/form-schema linear-wizard) #{:invoice/client :invoice/vendor :invoice/date})) (mut/select-keys (mm/form-schema linear-wizard) #{:invoice/client :invoice/vendor :invoice/date :invoice/due}))
(render-step [this request] (render-step [this request]
(alog/peek ::check (:multi-form-state request)) (alog/peek ::check (:multi-form-state request))
@@ -55,7 +58,11 @@
:head [:div.p-2 "New invoice"] :head [:div.p-2 "New invoice"]
:body (mm/default-step-body :body (mm/default-step-body
{} {}
[:div {} [:div {:x-data (hx/json {:clientId (or (:db/id (:client request))
(fc/field-value (:invoice/client fc/*current*)))
:vendorId (fc/field-value (:invoice/vendor fc/*current*))
:date (-> (fc/field-value (:invoice/date fc/*current*))
(atime/unparse-local atime/normal-date))})}
(fc/with-field :invoice/client (fc/with-field :invoice/client
(if (:client request) (if (:client request)
(com/hidden {:name (fc/field-name) (com/hidden {:name (fc/field-name)
@@ -70,7 +77,8 @@
:placeholder "Search..." :placeholder "Search..."
:url (bidi/path-for ssr-routes/only-routes :company-search) :url (bidi/path-for ssr-routes/only-routes :company-search)
:value (fc/field-value) :value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :client/name c))})]))) :content-fn (fn [c] (pull-attr (dc/db conn) :client/name c))
:x-model "clientId"})])))
(fc/with-field :invoice/vendor (fc/with-field :invoice/vendor
(com/validated-field (com/validated-field
{:label "Vendor" {:label "Vendor"
@@ -82,7 +90,8 @@
:placeholder "Search..." :placeholder "Search..."
:url (bidi/path-for ssr-routes/only-routes :vendor-search) :url (bidi/path-for ssr-routes/only-routes :vendor-search)
:value (fc/field-value) :value (fc/field-value)
:content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))})])) :content-fn (fn [c] (pull-attr (dc/db conn) :vendor/name c))
:x-model "vendorId"})]))
[:div.flex.space-x-8 [:div.flex.space-x-8
(fc/with-field :invoice/date (fc/with-field :invoice/date
@@ -94,12 +103,22 @@
(atime/unparse-local atime/normal-date)) (atime/unparse-local atime/normal-date))
:name (fc/field-name) :name (fc/field-name)
:error? (fc/field-errors) :error? (fc/field-errors)
"@change" "date=$event.target.value" ;; TODO x-model does not work
:placeholder "1/1/2024"})])) :placeholder "1/1/2024"})]))
(fc/with-field :invoice/due (fc/with-field :invoice/due
(com/validated-field (com/validated-field
{:label "Due (optional)" {:label "Due (optional)"
:errors (fc/field-errors)} :errors (fc/field-errors)}
[:div {:class "w-24"} [:div {:class "w-24"
:hx-put (bidi.bidi/path-for ssr-routes/only-routes ::route/due-date)
:x-hx-val:client-id "clientId" ;; TODO maybe just use the whole form
:x-hx-val:vendor-id "vendorId"
:x-hx-val:date "date"
:x-dispatch:changed "[clientId, vendorId, date]"
:x-effect "console.log([clientId, vendorId, date])"
:hx-trigger "changed"
:hx-target "this"
:hx-swap "innerHTML"}
(com/date-input {:value (some-> (fc/field-value) (com/date-input {:value (some-> (fc/field-value)
(atime/unparse-local atime/normal-date)) (atime/unparse-local atime/normal-date))
:name (fc/field-name) :name (fc/field-name)
@@ -173,7 +192,7 @@
[{:keys [value client-id]}] [{:keys [value client-id]}]
(com/data-grid-row (com/data-grid-row
(-> {:x-data (hx/json {:show (boolean (not (fc/field-value (:new? value)))) (-> {:x-data (hx/json {:show (boolean (not (fc/field-value (:new? value))))
:accountId (:db/id (fc/field-value (:invoice-expense-account/account value)))}) :accountId (fc/field-value (:invoice-expense-account/account value))})
:data-key "show" :data-key "show"
:x-ref "p"} :x-ref "p"}
hx/alpine-mount-then-appear) hx/alpine-mount-then-appear)
@@ -233,6 +252,7 @@
(mut/select-keys (mm/form-schema linear-wizard) #{:invoice/expense-accounts})) (mut/select-keys (mm/form-schema linear-wizard) #{:invoice/expense-accounts}))
(render-step [this {{:keys [snapshot]} :multi-form-state :as request}] (render-step [this {{:keys [snapshot]} :multi-form-state :as request}]
(alog/peek ::mfs (:step-params (:multi-form-state request)))
(mm/default-render-step (mm/default-render-step
linear-wizard this linear-wizard this
:head [:div.p-2 "Invoice accounts "] :head [:div.p-2 "Invoice accounts "]
@@ -310,12 +330,46 @@
(pull-attr (dc/db conn) :account/location)) (pull-attr (dc/db conn) :account/location))
:client-locations (some->> client-id :client-locations (some->> client-id
(pull-attr (dc/db conn) :client/locations))}))) (pull-attr (dc/db conn) :client/locations))})))
(defn due-date [{:keys [multi-form-state]}]
(alog/peek ::date multi-form-state)
(let [vendor (dc/pull (dc/db conn)
[:vendor/terms {:vendor/terms-overrides
[:vendor-terms-override/client :vendor-terms-override/terms]}]
(:invoice/vendor (:step-params multi-form-state)))
good-date
(or (when (and (:invoice/date (:step-params multi-form-state))
(-> vendor :vendor/terms)) ;; TODO get specific terms
(time/plus (:invoice/date (:step-params multi-form-state))
(time/days (-> vendor :vendor/terms))))
(:invoice/due (:step-params multi-form-state)))]
(alog/peek ::good-date multi-form-state)
(html-response
(com/date-input {:value (some-> good-date
(atime/unparse-local atime/normal-date))
:name "step-params[invoice/due]"
:error? false
:placeholder "1/1/2024"}))))
(def key->handler (def key->handler
(apply-middleware-to-all-handlers (apply-middleware-to-all-handlers
{::route/new-wizard (-> mm/open-wizard-handler {::route/new-wizard (-> mm/open-wizard-handler
(mm/wrap-wizard new-wizard) (mm/wrap-wizard new-wizard)
(mm/wrap-init-multi-form-state initial-new-wizard-state)) (mm/wrap-init-multi-form-state initial-new-wizard-state))
::route/due-date (-> due-date
#_(wrap-schema-decode :form-schema [:map
[:step-params {:optional true}
[:maybe
[:map
[:invoice/due {:optional true} [:maybe clj-date-schema]]]]]
[:date {:optional true} [:maybe clj-date-schema]]
[:due-date {:optional true} [:maybe clj-date-schema]]
[:client-id {:optional true} [:maybe entity-id]]
[:vendor-id {:optional true} [:maybe entity-id]]])
(mm/wrap-wizard new-wizard)
(mm/wrap-decode-multi-form-state)
(wrap-nested-form-params))
::route/location-select (-> location-select ::route/location-select (-> location-select
(wrap-schema-enforce :query-schema [:map (wrap-schema-enforce :query-schema [:map
[:name :string] [:name :string]

View File

@@ -5,6 +5,7 @@
"/voided" ::voided-page} "/voided" ::voided-page}
"/new" {:get ::new-wizard "/new" {:get ::new-wizard
:post ::new-invoice-submit :post ::new-invoice-submit
"/due-date" ::due-date
"/navigate" ::new-wizard-navigate "/navigate" ::new-wizard-navigate
"/account/new" ::new-wizard-new-account "/account/new" ::new-wizard-new-account
"/account/location-select" ::location-select} "/account/location-select" ::location-select}