minor refactoriing.
This commit is contained in:
@@ -14,12 +14,10 @@
|
||||
[re-frame.core :as re-frame]))
|
||||
|
||||
;; SUBS
|
||||
|
||||
(re-frame/reg-sub
|
||||
::can-submit-edit-invoice
|
||||
:<- [::forms/form ::new-invoice]
|
||||
:<- [::forms/form ::form]
|
||||
(fn [{:keys [data status]} _]
|
||||
|
||||
(let [min-total (if (= (:total (:original data)) (:outstanding-balance (:original data)))
|
||||
nil
|
||||
(- (:total (:original data)) (:outstanding-balance (:original data))))]
|
||||
@@ -29,11 +27,52 @@
|
||||
(or (not (:id data))
|
||||
(< (.abs js/Math (- (js/parseFloat (:total data)) (reduce + 0 (map (fn [ea] (js/parseFloat (:amount ea))) (:expense-accounts data))))) 0.001))))))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::submit-query
|
||||
(fn [db [_ command bank-account-id type]]
|
||||
(let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])]
|
||||
(condp = command
|
||||
:create {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddInvoice"}
|
||||
:venia/queries [{:query/data [:add-invoice
|
||||
{:invoice {:date date
|
||||
:vendor-id vendor-id
|
||||
:client-id client-id
|
||||
:invoice-number invoice-number
|
||||
:location location
|
||||
:total total
|
||||
}}
|
||||
invoice-read]}]}
|
||||
:edit {:venia/operation {:operation/type :mutation
|
||||
:operation/name "EditInvoice"}
|
||||
:venia/queries [{:query/data [:edit-invoice
|
||||
{:invoice {:id id
|
||||
:invoice-number invoice-number
|
||||
:date date
|
||||
:total total
|
||||
:expense-accounts (map (fn [ea]
|
||||
{:id (:id ea)
|
||||
:amount (:amount ea)})
|
||||
expense-accounts)}}
|
||||
invoice-read]}]}
|
||||
:add-and-print {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddAndPrintInvoice"}
|
||||
:venia/queries [{:query/data [:add-and-print-invoice
|
||||
{:invoice {:date date
|
||||
:vendor-id vendor-id
|
||||
:client-id client-id
|
||||
:invoice-number invoice-number
|
||||
:location location
|
||||
:total total}
|
||||
:bank-account-id bank-account-id
|
||||
:type type}
|
||||
[:pdf-url [:invoices invoice-read]]]}]}))))
|
||||
|
||||
;; EVENTS
|
||||
(re-frame/reg-event-db
|
||||
::adding
|
||||
(fn [db [_ new]]
|
||||
(-> db (forms/start-form ::new-invoice new))))
|
||||
(-> db (forms/start-form ::form new))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::editing
|
||||
@@ -41,7 +80,7 @@
|
||||
(let [edit-invoice (update which :date #(date->str % standard))
|
||||
edit-invoice (assoc edit-invoice :original edit-invoice)]
|
||||
(-> db
|
||||
(forms/start-form ::new-invoice {:id (:id edit-invoice)
|
||||
(forms/start-form ::form {:id (:id edit-invoice)
|
||||
:status (:status edit-invoice)
|
||||
:date (:date edit-invoice)
|
||||
:invoice-number (:invoice-number edit-invoice)
|
||||
@@ -60,75 +99,21 @@
|
||||
(let [first-location (-> @(re-frame/subscribe [::subs/clients-by-id])
|
||||
(get-in [value :locations])
|
||||
first)]
|
||||
{:dispatch [::forms/change ::new-invoice
|
||||
{:dispatch [::forms/change ::form
|
||||
[:client-id] value
|
||||
[:location] first-location]})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::create-submitted
|
||||
(fn [{:keys [db]} [_ invoice-created]]
|
||||
::submitted
|
||||
(fn [{:keys [db]} [_ invoice-created invoice-printed command bank-account-id type]]
|
||||
(when @(re-frame/subscribe [::can-submit-edit-invoice])
|
||||
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::new-invoice])]
|
||||
{:db (forms/loading db ::new-invoice)
|
||||
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])]
|
||||
{:db (forms/loading db ::form)
|
||||
:graphql
|
||||
{:token (-> db :user)
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddInvoice"}
|
||||
:venia/queries [{:query/data [:add-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 [::succeeded :create invoice-created nil]
|
||||
:on-error [::forms/save-error ::new-invoice]}}))))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::update-submitted
|
||||
(fn [{:keys [db]} [_ invoice-created]]
|
||||
(when @(re-frame/subscribe [::can-submit-edit-invoice])
|
||||
(let [{{:keys [date total invoice-number id expense-accounts]} :data} @(re-frame/subscribe [::forms/form ::new-invoice])]
|
||||
{:db (-> db
|
||||
(assoc-in [::forms/forms ::new-invoice :status] :loading)
|
||||
(assoc-in [::forms/forms ::new-invoice :error] nil))
|
||||
:graphql
|
||||
{:token (-> db :user)
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "EditInvoice"}
|
||||
:venia/queries [{:query/data [:edit-invoice
|
||||
{:invoice {:id id :invoice-number invoice-number :date date :total total :expense-accounts (map (fn [ea]
|
||||
{:id (:id ea)
|
||||
:amount (:amount ea)})
|
||||
expense-accounts)}}
|
||||
invoice-read]}]}
|
||||
:on-success [::succeeded :edit invoice-created nil]
|
||||
:on-error [::forms/save-error ::new-invoice]}}))))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::add-and-print-submitted
|
||||
(fn [{:keys [db]} [_ invoice-created invoice-printed bank-account-id type ]]
|
||||
(when @(re-frame/subscribe [::can-submit-edit-invoice])
|
||||
(let [{:keys [data]} @(re-frame/subscribe [::forms/form ::new-invoice])]
|
||||
{:db (forms/loading db ::new-invoice)
|
||||
:graphql
|
||||
{:token (-> db :user)
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "AddAndPrintInvoice"}
|
||||
:venia/queries [{:query/data [:add-and-print-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)}
|
||||
:bank-account-id bank-account-id
|
||||
:type type}
|
||||
[:pdf-url [:invoices invoice-read]]]}]}
|
||||
:on-success [::succeeded :add-and-print invoice-created invoice-printed]
|
||||
:on-error [::forms/save-error ::new-invoice]}}))))
|
||||
:query-obj @(re-frame/subscribe [::submit-query command bank-account-id type])
|
||||
:on-success [::succeeded command invoice-created invoice-printed]
|
||||
:on-error [::forms/save-error ::form]}}))))
|
||||
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
@@ -140,53 +125,30 @@
|
||||
:create (:add-invoice result)
|
||||
|
||||
:add-and-print (first (:invoices (:add-and-print-invoice result))))
|
||||
db (condp = command
|
||||
:edit (-> db (forms/stop-form ::new-invoice))
|
||||
:create (-> db
|
||||
(forms/stop-form ::new-invoice)
|
||||
(forms/start-form ::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])))}))
|
||||
:add-and-print (-> db
|
||||
(forms/stop-form ::new-invoice)
|
||||
(forms/start-form ::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])))})))
|
||||
db (cond-> db
|
||||
true (forms/stop-form ::form)
|
||||
|
||||
(#{:create :add-and-print} command) (forms/start-form ::form {:client-id (:id @(re-frame/subscribe [::subs/client]))
|
||||
:status :unpaid
|
||||
:date (date->str (c/now) standard)
|
||||
:location (first (:locations @(re-frame/subscribe [::subs/client])))}))
|
||||
events (cond-> [(conj invoice-created invoice)]
|
||||
(= :add-and-print command) (conj (conj invoice-printed (:pdf-url (:add-and-print-invoice result)))))]
|
||||
|
||||
{:db db
|
||||
:dispatch-n events})))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::invoice-created-and-printed
|
||||
(fn [{:keys [db]} [_ {:keys [add-and-print-invoice]}]]
|
||||
{:db (-> db
|
||||
(forms/stop-form ::new-invoice)
|
||||
(forms/start-form ::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])))})
|
||||
(update-in [::invoice-page :invoices]
|
||||
(fn [is]
|
||||
(into (vec (map #(assoc % :class "live-added")
|
||||
(:invoices add-and-print-invoice)))
|
||||
is)))
|
||||
(assoc-in [::check-results :shown?] true)
|
||||
(assoc-in [::check-results :pdf-url] (:pdf-url add-and-print-invoice)))}))
|
||||
|
||||
|
||||
;; VIEWS
|
||||
|
||||
(defn form [{:keys [can-change-amount? invoice-created invoice-printed]}]
|
||||
[forms/side-bar-form {:form ::new-invoice }
|
||||
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::new-invoice])
|
||||
[forms/side-bar-form {:form ::form }
|
||||
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
|
||||
exists? (:id data)
|
||||
current-client @(re-frame/subscribe [::subs/client])
|
||||
can-change-amount? (#{:unpaid ":unpaid"} (:status data))
|
||||
change-event [::forms/change ::new-invoice]
|
||||
change-event [::forms/change ::form]
|
||||
locations (get-in @(re-frame/subscribe [::subs/clients-by-id]) [(:client-id data) :locations])
|
||||
multi-location? (> (count locations) 1)
|
||||
|
||||
@@ -202,8 +164,8 @@
|
||||
(.stopPropagation e)
|
||||
(.preventDefault e))
|
||||
(if exists?
|
||||
(re-frame/dispatch-sync [::update-submitted invoice-created])
|
||||
(re-frame/dispatch-sync [::create-submitted invoice-created])))}
|
||||
(re-frame/dispatch-sync [::submitted invoice-created nil :edit])
|
||||
(re-frame/dispatch-sync [::submitted invoice-created nil :create])))}
|
||||
[:h1.title.is-2 "New Invoice"]
|
||||
(when-not @(re-frame/subscribe [::subs/client])
|
||||
[:div.field
|
||||
@@ -215,7 +177,7 @@
|
||||
:auto-focus (if @(re-frame/subscribe [::subs/client]) false true)
|
||||
:field [:client-id]
|
||||
:disabled exists?
|
||||
:event [::change-new-invoice-client [::new-invoice]]
|
||||
:event [::change-new-invoice-client [::form]]
|
||||
:spec ::invoice/client-id
|
||||
:subscription data}]]]])
|
||||
|
||||
@@ -260,18 +222,7 @@
|
||||
:field [:date]
|
||||
:event change-event
|
||||
:spec ::invoice/date
|
||||
:subscription data}]
|
||||
#_[:input.input {:type "date"
|
||||
:field [:date]
|
||||
:event change-event
|
||||
:spec ::invoice/date
|
||||
:subscription data}]]]]
|
||||
|
||||
|
||||
#_[horizontal-field
|
||||
[:label.label "Date 2 "]
|
||||
|
||||
]
|
||||
:subscription data}]]]]
|
||||
|
||||
[:div.field
|
||||
[:p.help "Invoice #"]
|
||||
@@ -298,9 +249,9 @@
|
||||
:subscription data
|
||||
:spec ::invoice/total
|
||||
:step "0.01"}]]]]]]
|
||||
|
||||
|
||||
(when exists?
|
||||
[:div
|
||||
[:div
|
||||
[:h2.subtitle "Expense Accounts"]
|
||||
|
||||
(for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))]
|
||||
@@ -310,7 +261,7 @@
|
||||
|
||||
(when multi-location?
|
||||
[:div.column location])
|
||||
|
||||
|
||||
[:div.column
|
||||
[:div.control
|
||||
[:div.field.has-addons.is-extended
|
||||
@@ -320,7 +271,7 @@
|
||||
[:input.input {:type "number"
|
||||
:field [:expense-accounts index :amount]
|
||||
:style {:text-align "right"}
|
||||
:event [::forms/change ::new-invoice]
|
||||
:event [::forms/change ::form]
|
||||
:subscription data
|
||||
:value (get-in expense-account [:amount])
|
||||
:max (:total data)
|
||||
@@ -329,13 +280,13 @@
|
||||
(when error
|
||||
^{:key error} [:div.notification.is-warning.animated.fadeInUp
|
||||
error])
|
||||
|
||||
|
||||
[:div.columns
|
||||
(when-not exists?
|
||||
[:div.column
|
||||
[drop-down {:header [:button.button.is-info.is-outlined.is-medium.is-fullwidth {:aria-haspopup true
|
||||
:type "button"
|
||||
:on-click (dispatch-event [::events/toggle-menu ::add-and-print-invoice ])
|
||||
:on-click (dispatch-event [::events/toggle-menu ::add-and-print-invoice ])
|
||||
:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice])
|
||||
""
|
||||
"disabled")
|
||||
@@ -354,11 +305,11 @@
|
||||
(if (= :cash type)
|
||||
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-invoice invoice-created invoice-printed id :cash])} "With cash"]
|
||||
(list
|
||||
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-submitted invoice-created invoice-printed id :check])} "Print checks from " name]
|
||||
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print-submitted invoice-created invoice-printed id :debit])} "Debit from " name]))))]]])
|
||||
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :check])} "Print checks from " name]
|
||||
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::submitted invoice-created invoice-printed :add-and-print id :debit])} "Debit from " name]))))]]])
|
||||
[:div.column
|
||||
[:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-invoice])
|
||||
""
|
||||
"disabled")
|
||||
:class (str @(re-frame/subscribe [::forms/loading-class ::new-invoice])
|
||||
:class (str @(re-frame/subscribe [::forms/loading-class ::form])
|
||||
(when error " animated shake"))} "Save"]] ]])])
|
||||
|
||||
Reference in New Issue
Block a user