new approach to adding invoices.

This commit is contained in:
BC
2019-02-18 16:23:05 -08:00
parent f1a6ada3b8
commit e2b99961e4
3 changed files with 334 additions and 170 deletions

View File

@@ -9,7 +9,8 @@
[auto-ap.entities.invoice :as invoice]
[auto-ap.entities.vendors :as vendor]
[bidi.bidi :as bidi]
[auto-ap.views.components.layouts :refer [side-bar-layout]]
[auto-ap.forms :as forms]
[auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar]]
[auto-ap.routes :as routes]
[auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog]
[auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]
@@ -258,12 +259,12 @@
{:dispatch [::params-change @(re-frame/subscribe [::params])]}))
(re-frame/reg-event-fx
::new-invoice
::new-invoice-clicked
(fn [{:keys [db]} _]
{:dispatch [::events/modal-status ::new-invoice {:visible? true}]
:db (assoc-in db [::new-invoice] {:client-id (:id @(re-frame/subscribe [::subs/client]))
:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))})}))
{:db (forms/start-form db ::new-invoice {:client-id (:id @(re-frame/subscribe [::subs/client]))
:status :unpaid
:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))})}))
(re-frame/reg-event-fx
::edit-invoice
@@ -278,17 +279,23 @@
(re-frame/reg-event-fx
::create-invoice
(fn [{:keys [db]} _]
(let [new-invoice @(re-frame/subscribe [::new-invoice])]
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::new-invoice])]
{:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddInvoice"}
:venia/queries [{:query/data [:add-invoice
{:invoice new-invoice}
{:invoice {:date (:date data)
:vendor-id (:vendor-id data)
:client-id (:client-id data)
:invoice-number (:invoice-number data)
:location (:location data)
:total (:total data)
}}
invoice-read]}]}
:on-success [::invoice-created]
:on-error [::invoice-create-failed]}})))
:on-error [::forms/save-error ::new-invoice]}})))
@@ -373,8 +380,8 @@
(re-frame/reg-event-fx
::invoice-created
(fn [{:keys [db]} [_ {:keys [add-invoice]}]]
{:dispatch [::events/modal-completed ::new-invoice]
:db (-> db
{:db (-> db
(forms/stop-form ::new-invoice)
(update-in [::invoice-page :invoices]
(fn [is]
(into [(assoc add-invoice :class "live-added")]
@@ -431,9 +438,7 @@
{:dispatch [::events/modal-completed ::expense-accounts-dialog/change-expense-accounts]
:db (-> db
(update-in [::invoice-page :invoices]
(fn [is]
(replace-if #(= (:id %1) (:id %2)) updated is)))
(dissoc ::change-expense-accounts))})))
@@ -552,9 +557,9 @@
(let [first-location (-> @(re-frame/subscribe [::subs/clients-by-id])
(get-in [value :locations])
first)]
{:dispatch [::events/change-form location field value]
:db (-> db
(assoc-in [::new-invoice :location] first-location))})))
{:dispatch [::forms/change ::new-invoice
[:client-id] value
[:location] first-location]})))
(defn new-invoice-modal []
(let [data @(re-frame/subscribe [::new-invoice])
@@ -637,6 +642,119 @@
:step "0.01"}]]]]]
]))
(defn edit-invoice-form [{:keys [can-change-amount?]}]
[forms/side-bar-form {:form ::new-invoice }
(let [{:keys [data status active? error]} @(re-frame/subscribe [::forms/form ::new-invoice])
can-change-amount? (= (:status data) :unpaid)
change-event [::forms/change ::new-invoice]
locations (get-in @(re-frame/subscribe [::subs/clients-by-id]) [(:client-id data) :locations])
min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
nil
(- (:total (:original data)) (:outstanding-balance (:original data))))
should-select-location? (and locations
(> (count locations) 1))]
[:form
[:h1.title.is-2 "New Invoice"]
(when-not @(re-frame/subscribe [::subs/client])
[:div.field
[:p.help "Client"]
[:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/clients]))
:type "typeahead"
:field [:client-id]
:event [::change-new-invoice-client [::new-invoice]]
:spec ::invoice/client-id
:subscription data}]]]])
(when should-select-location?
[horizontal-field
[:label.label "Location"]
[:div.select
[bind-field
[:select {:type "select"
:field [:location]
:spec (set locations)
:event change-event
:subscription data}
(map (fn [l] [:option {:value l} l]) locations)]]]])
[:div.field
[:p.help "Vendor"]
[:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors]))
:type "typeahead"
:auto-focus true
:field [:vendor-id]
:text-field [:vendor-name]
:event change-event
:spec (s/nilable ::invoice/vendor-id)
:subscription data}]]]]
[:div.field
[:p.help "Date"]
[:div.control
[bind-field
[:input.input {:type "date"
:field [:date]
:event change-event
:spec ::invoice/date
:subscription data}]]]]
[:div.field
[:p.help "Invoice #"]
[:div.control
[bind-field
[:input.input {:type "text"
:field [:invoice-number]
:event change-event
:spec ::invoice/invoice-number
:subscription data}]]]]
[:div.field
[:p.help "Total"]
[:div.control
[:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
[bind-field
[:input.input {:type "number"
:field [:total]
:disabled (if can-change-amount? "" "disabled")
:event change-event
:min min-total
:subscription data
:spec ::invoice/total
:step "0.01"}]]]]]]
(when error
[:div.notification.is-warning.animated.fadeInUp
error])
(println (s/explain-data ::invoice/invoice data))
[:submit.button.is-large.is-primary {:disabled (if (and (s/valid? ::invoice/invoice data)
(or (not min-total) (>= (:total data) min-total)))
""
"disabled")
:on-click (dispatch-event [::create-invoice])
:class (str @(re-frame/subscribe [::forms/loading-class ::new-invoice]) (when error " animated shake"))} "Save"]
]
#_[action-modal {:id ::edit-invoice
:title "Update Invoice"
:action-text "Save"
:save-event [::edit-invoice-save]
:can-submit? (and #_(s/valid? ::invoice/invoice data)
(or (not min-total) (>= (:total data) min-total)))}
])]
)
(defn edit-invoice-modal [{:keys [can-change-amount?]}]
(let [data @(re-frame/subscribe [::edit-invoice])
change-event [::events/change-form [::edit-invoice]]
@@ -734,7 +852,7 @@
[:div.is-pulled-right
[:button.button.is-danger {:on-click (dispatch-event [::new-invoice])} "New Invoice"]
[:button.button.is-success {:on-click (dispatch-event [::new-invoice-clicked])} "New Invoice"]
(when current-client
@@ -850,17 +968,19 @@
:component-will-mount #(re-frame/dispatch-sync [::params-change {:status status}]) }))
(defn unpaid-invoices-page [{:keys [status]}]
[side-bar-layout {:side-bar [invoices-side-bar {}
^{:key "extra-filter"}
[:div
[:p.menu-label "Vendor"]
[:div [vendor-filter {:on-change-event [::change-selected-vendor]
:value (:vendor-filter @(re-frame/subscribe [::invoice-page]))
:vendors @(re-frame/subscribe [::subs/vendors])}]]
[:p.menu-label "Invoice #"]
[:div
[invoice-number-filter]]]]
:main [unpaid-invoices-content {:status status}]
:bottom [vendor-dialog {:vendor @(re-frame/subscribe [::subs/user-editing-vendor])
:save-event [::events/save-vendor]
:change-event [::events/change-nested-form-state [:user-editing-vendor]] :id :auto-ap.views.main/user-editing-vendor}]}])
(let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::new-invoice])]
[side-bar-layout {:side-bar [invoices-side-bar {}
^{:key "extra-filter"}
[:div
[:p.menu-label "Vendor"]
[:div [vendor-filter {:on-change-event [::change-selected-vendor]
:value (:vendor-filter @(re-frame/subscribe [::invoice-page]))
:vendors @(re-frame/subscribe [::subs/vendors])}]]
[:p.menu-label "Invoice #"]
[:div
[invoice-number-filter]]]]
:main [unpaid-invoices-content {:status status}]
:bottom [vendor-dialog {:vendor @(re-frame/subscribe [::subs/user-editing-vendor])
:save-event [::events/save-vendor]
:change-event [::events/change-nested-form-state [:user-editing-vendor]] :id :auto-ap.views.main/user-editing-vendor}]
:right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [edit-invoice-form]]}]))