diff --git a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs index 2fb9a9ba..d0d03db4 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs @@ -50,7 +50,31 @@ (re-frame/reg-event-fx ::change-expense-accounts-saving (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])] {:graphql {:token (-> db :user) @@ -60,7 +84,7 @@ :venia/queries [{:query/data [:edit-expense-accounts {:invoice-id id :expense-accounts (map (fn [ea] {:id (when-not (string? (:id ea)) (:id ea)) - :amount (:amount ea) + :amount (:new-amount ea) :location (:location ea) :expense-account-id (:expense-account-id ea)}) expense-accounts)} @@ -77,7 +101,7 @@ (fn [db _] (let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-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 ::remove-expense-account-split @@ -88,25 +112,25 @@ (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) change-event [::change] chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts]) expense-accounts-total (->> expense-accounts - (map :amount) + (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.01)] + (reduce + 0))] [action-modal {:id ::change-expense-accounts :title "Change expense accounts" :action-text "Save" :save-event [::change-expense-accounts-saving updated-event] - :can-submit? (and does-add-up? - (s/valid? (s/* ::invoices-expense-accounts/invoices-expense-account) expense-accounts))} + :can-submit? true} + (when error + [:div error] ) [:div [:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]] @@ -116,58 +140,69 @@ [:th {:style {:width "500px"}} "Expense Account"] (when multi-location? [:th {:style {:width "200px"}} "Location"]) + [:th {:style {:width "200px"}} "Original Amount"] [:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "5em"}}]]] [:tbody (doall (for [{:keys [id] :as expense-account} expense-accounts - :let [sub @(re-frame/subscribe [::expense-account (:id expense-account)])]] - ^{:key id} - [:tr - [:td.expandable [:div.control - [bind-field - [typeahead {:matches (map (fn [x] [(:id x) (str (:id x) " - " (:name x))]) chooseable-expense-accounts) - :type "typeahead" - :field [:expense-account-id] - :event [::change id] - :spec ::invoices-expense-accounts/expense-account-id - :subscription sub}]]]] + :let [sub @(re-frame/subscribe [::expense-account (:id expense-account)])]] + ^{:key id} + [:tr + [:td.expandable [:div.control + [bind-field + [typeahead {:matches (map (fn [x] [(:id x) (str (:id x) " - " (:name x))]) chooseable-expense-accounts) + :type "typeahead" + :field [:expense-account-id] + :event [::change id] + :spec ::invoices-expense-accounts/expense-account-id + :subscription sub}]]]] - (when multi-location? - [:td - (if-let [forced-location (-> expense-account :expense-account-id expense-accounts/expense-accounts :location)] - [:div.select - [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] - [:div.select - [bind-field - [:select {:type "select" - :field [:location] - :spec (set locations) - :event [::change id] - :subscription sub} - (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) - - [:td - [:div.control - [:div.field.has-addons.is-extended - [:p.control [:a.button.is-static "$"]] - [:p.control - [bind-field - [:input.input {:type "number" - :field [:amount] - :style {:text-align "right"} - :event [::change id] - :subscription sub - :value (get-in data [:amount]) - - :max (:total data) - :step "0.01"}]]]]]] - [:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]])) + (when multi-location? + [:td + (if-let [forced-location (-> expense-account :expense-account-id expense-accounts/expense-accounts :location)] + [:div.select + [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] + [:div.select + [bind-field + [:select {:type "select" + :field [:location] + :spec (set locations) + :event [::change id] + :subscription sub} + (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) + + [:td + (str "$" (get-in sub [:amount]))] + [:td + [:div.control + [:div.field.has-addons.is-extended + [:p.control [:a.button.is-static "$"]] + [:p.control + [bind-field + [:input.input {:type "number" + :field [:new-amount-temp] + :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] + :subscription sub + :value (get-in data [:new-amount-temp]) + + :max (:total data) + :step "0.01"}]]]]]] + [:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]])) [: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 ) )]] [: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 ) )]] [: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) ) )]]]]])) diff --git a/src/cljs/auto_ap/views/components/modal.cljs b/src/cljs/auto_ap/views/components/modal.cljs index e8ab876a..3d0d7fcd 100644 --- a/src/cljs/auto_ap/views/components/modal.cljs +++ b/src/cljs/auto_ap/views/components/modal.cljs @@ -27,7 +27,7 @@ (-> [modal {:title [:span title ] :foot [:input.button.is-primary {:type "submit" - :tab-index "0" + :tabindex "1" :form id :disabled (cond saving? "disabled"