making expense accounts dialog not calculate on the fly
This commit is contained in:
@@ -50,7 +50,31 @@
|
|||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::change-expense-accounts-saving
|
::change-expense-accounts-saving
|
||||||
(fn [{:keys [db]} [_ on-success id ]]
|
(fn [{:keys [db]} [_ on-success id ]]
|
||||||
|
(let [{{:keys [expense-accounts total]} :invoice} @(re-frame/subscribe [::change-expense-accounts])
|
||||||
|
expense-accounts-total (->> expense-accounts
|
||||||
|
(map :new-amount)
|
||||||
|
(map js/parseFloat)
|
||||||
|
(map #(or % 0.0))
|
||||||
|
(map #(if (js/Number.isNaN %)
|
||||||
|
0.0
|
||||||
|
%))
|
||||||
|
(reduce + 0))
|
||||||
|
does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)]
|
||||||
|
(if (and does-add-up?
|
||||||
|
(every? :new-amount expense-accounts)
|
||||||
|
(s/valid? (s/* ::invoices-expense-accounts/invoices-expense-account) expense-accounts))
|
||||||
|
|
||||||
|
{:dispatch [::change-expense-accounts-submit on-success id]}
|
||||||
|
{:dispatch [::events/modal-status ::change-expense-accounts {:saving? false :error-message "Expense accounts do not add up."}]}))))
|
||||||
|
|
||||||
|
(re-frame/reg-event-db
|
||||||
|
::saving-error
|
||||||
|
(fn [db [_ message]]
|
||||||
|
(assoc-in db [::change-expense-accounts :error] message)))
|
||||||
|
|
||||||
|
(re-frame/reg-event-fx
|
||||||
|
::change-expense-accounts-submit
|
||||||
|
(fn [{:keys [db] } [_ on-success id]]
|
||||||
(let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])]
|
(let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])]
|
||||||
{:graphql
|
{:graphql
|
||||||
{:token (-> db :user)
|
{:token (-> db :user)
|
||||||
@@ -60,7 +84,7 @@
|
|||||||
:venia/queries [{:query/data [:edit-expense-accounts
|
:venia/queries [{:query/data [:edit-expense-accounts
|
||||||
{:invoice-id id
|
{:invoice-id id
|
||||||
:expense-accounts (map (fn [ea] {:id (when-not (string? (:id ea)) (:id ea))
|
:expense-accounts (map (fn [ea] {:id (when-not (string? (:id ea)) (:id ea))
|
||||||
:amount (:amount ea)
|
:amount (:new-amount ea)
|
||||||
:location (:location ea)
|
:location (:location ea)
|
||||||
:expense-account-id (:expense-account-id ea)})
|
:expense-account-id (:expense-account-id ea)})
|
||||||
expense-accounts)}
|
expense-accounts)}
|
||||||
@@ -77,7 +101,7 @@
|
|||||||
(fn [db _]
|
(fn [db _]
|
||||||
(let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-expense-accounts])]
|
(let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-expense-accounts])]
|
||||||
(update-in db [::change-expense-accounts :invoice :expense-accounts]
|
(update-in db [::change-expense-accounts :invoice :expense-accounts]
|
||||||
conj {:amount 0 :id (str (random-uuid)) :expense-account-id {} :location (first locations)}))))
|
conj {:amount "0.0" :id (str (random-uuid)) :expense-account-id {} :location (first locations)}))))
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::remove-expense-account-split
|
::remove-expense-account-split
|
||||||
@@ -88,25 +112,25 @@
|
|||||||
|
|
||||||
|
|
||||||
(defn change-expense-accounts-modal [{:keys [updated-event]}]
|
(defn change-expense-accounts-modal [{:keys [updated-event]}]
|
||||||
(let [{{:keys [expense-accounts total ] :or {expense-accounts [] total 0} {:keys [locations]} :client} :invoice :as data} @(re-frame/subscribe [::change-expense-accounts])
|
(let [{{:keys [expense-accounts total] :or {expense-accounts [] total 0} {:keys [locations]} :client} :invoice error :error :as data} @(re-frame/subscribe [::change-expense-accounts])
|
||||||
multi-location? (> (count locations) 1)
|
multi-location? (> (count locations) 1)
|
||||||
change-event [::change]
|
change-event [::change]
|
||||||
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
|
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
|
||||||
expense-accounts-total (->> expense-accounts
|
expense-accounts-total (->> expense-accounts
|
||||||
(map :amount)
|
(map :new-amount)
|
||||||
(map js/parseFloat)
|
(map js/parseFloat)
|
||||||
(map #(or % 0.0))
|
(map #(or % 0.0))
|
||||||
(map #(if (js/Number.isNaN %)
|
(map #(if (js/Number.isNaN %)
|
||||||
0.0
|
0.0
|
||||||
%))
|
%))
|
||||||
(reduce + 0))
|
(reduce + 0))]
|
||||||
does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.01)]
|
|
||||||
[action-modal {:id ::change-expense-accounts
|
[action-modal {:id ::change-expense-accounts
|
||||||
:title "Change expense accounts"
|
:title "Change expense accounts"
|
||||||
:action-text "Save"
|
:action-text "Save"
|
||||||
:save-event [::change-expense-accounts-saving updated-event]
|
:save-event [::change-expense-accounts-saving updated-event]
|
||||||
:can-submit? (and does-add-up?
|
:can-submit? true}
|
||||||
(s/valid? (s/* ::invoices-expense-accounts/invoices-expense-account) expense-accounts))}
|
(when error
|
||||||
|
[:div error] )
|
||||||
|
|
||||||
[:div
|
[:div
|
||||||
[:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]]
|
[:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]]
|
||||||
@@ -116,6 +140,7 @@
|
|||||||
[:th {:style {:width "500px"}} "Expense Account"]
|
[:th {:style {:width "500px"}} "Expense Account"]
|
||||||
(when multi-location?
|
(when multi-location?
|
||||||
[:th {:style {:width "200px"}} "Location"])
|
[:th {:style {:width "200px"}} "Location"])
|
||||||
|
[:th {:style {:width "200px"}} "Original Amount"]
|
||||||
[:th {:style {:width "300px"}} "Amount"]
|
[:th {:style {:width "300px"}} "Amount"]
|
||||||
[:th {:style {:width "5em"}}]]]
|
[:th {:style {:width "5em"}}]]]
|
||||||
[:tbody
|
[:tbody
|
||||||
@@ -146,6 +171,8 @@
|
|||||||
:subscription sub}
|
:subscription sub}
|
||||||
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
|
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
|
||||||
|
|
||||||
|
[:td
|
||||||
|
(str "$" (get-in sub [:amount]))]
|
||||||
[:td
|
[:td
|
||||||
[:div.control
|
[:div.control
|
||||||
[:div.field.has-addons.is-extended
|
[:div.field.has-addons.is-extended
|
||||||
@@ -153,21 +180,29 @@
|
|||||||
[:p.control
|
[:p.control
|
||||||
[bind-field
|
[bind-field
|
||||||
[:input.input {:type "number"
|
[:input.input {:type "number"
|
||||||
:field [:amount]
|
:field [:new-amount-temp]
|
||||||
:style {:text-align "right"}
|
:style {:text-align "right"}
|
||||||
|
:on-blur (dispatch-event [::change id [:new-amount] (:new-amount-temp sub)])
|
||||||
|
:on-key-down (fn [e ]
|
||||||
|
(if (= 13 (.-keyCode e))
|
||||||
|
(do
|
||||||
|
|
||||||
|
(re-frame/dispatch [::change id [:new-amount] (:new-amount-temp sub)])
|
||||||
|
true)
|
||||||
|
false))
|
||||||
:event [::change id]
|
:event [::change id]
|
||||||
:subscription sub
|
:subscription sub
|
||||||
:value (get-in data [:amount])
|
:value (get-in data [:new-amount-temp])
|
||||||
|
|
||||||
:max (:total data)
|
:max (:total data)
|
||||||
:step "0.01"}]]]]]]
|
:step "0.01"}]]]]]]
|
||||||
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]]))
|
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]]))
|
||||||
[:tr
|
[:tr
|
||||||
[:td.no-border { :col-span (when multi-location? "2") :style { :text-align "right"} } "Invoice total: "]
|
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "]
|
||||||
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
|
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
|
||||||
[:tr
|
[:tr
|
||||||
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Account total: "]
|
[:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "]
|
||||||
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
|
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
|
||||||
[:tr
|
[:tr
|
||||||
[:td.no-border { :col-span (when multi-location? "2") :style { :text-align "right"} } "Difference: "]
|
[:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "]
|
||||||
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]]))
|
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]]))
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
(-> [modal {:title [:span title
|
(-> [modal {:title [:span title
|
||||||
]
|
]
|
||||||
:foot [:input.button.is-primary {:type "submit"
|
:foot [:input.button.is-primary {:type "submit"
|
||||||
:tab-index "0"
|
:tabindex "1"
|
||||||
:form id
|
:form id
|
||||||
:disabled (cond saving?
|
:disabled (cond saving?
|
||||||
"disabled"
|
"disabled"
|
||||||
|
|||||||
Reference in New Issue
Block a user