simplifying the expense account model, somewhat at least.

This commit is contained in:
Bryce Covert
2019-01-25 15:50:06 -08:00
parent 36f6eabcba
commit d7766c4688
2 changed files with 48 additions and 27 deletions

View File

@@ -290,6 +290,7 @@ nav.navbar .navbar-item.is-active {
.table { table-layout: fixed }
.table td {text-overflow: ellipsis; overflow: hidden; white-space: nowrap; padding: 9px; }
.table td.expandable {text-overflow: ellipsis; overflow: visible; white-space: nowrap; padding: 9px; }
.table td.no-border { border: none; }
.typeahead {
position:relative;

View File

@@ -22,12 +22,34 @@
(fn [db]
(-> db ::change-expense-accounts)))
(re-frame/reg-sub
::expense-account
:<- [::change-expense-accounts]
(fn [{{:keys [expense-accounts] :or {expense-accounts [] }} :invoice} [_ expense-account-id]]
(println "subbing")
(doto (first (filter #(= expense-account-id (:id %)) expense-accounts))
println)))
(re-frame/reg-event-fx
::change-expense-accounts
(fn [{:keys [db]} [_ i]]
{:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}]
:db (assoc-in db [::change-expense-accounts :invoice] i)}))
(re-frame/reg-event-db
::change
(fn [db [_ id f v]]
(update-in db [::change-expense-accounts :invoice :expense-accounts]
(fn [expense-accounts]
(mapv
(fn [expense-account]
(println expense-account id)
(if (= id (:id expense-account))
(assoc-in expense-account f v)
expense-account))
expense-accounts)))))
(re-frame/reg-event-fx
::change-expense-accounts-saving
(fn [{:keys [db]} [_ on-success id ]]
@@ -40,7 +62,7 @@
:venia/queries [{:query/data [:edit-expense-accounts
{:invoice-id id
:expense-accounts (map (fn [ea] {:id (:id ea)
:expense-accounts (map (fn [ea] {:id (when-not (string? (:id ea)) (:id ea))
:amount (:amount ea)
:location (:location ea)
:expense-account-id (:expense-account-id ea)})
@@ -62,21 +84,20 @@
(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 :expense-account-id nil :location (first locations)}))))
conj {:amount 0 :id (str (random-uuid)) :expense-account-id {} :location (first locations)}))))
(re-frame/reg-event-db
::remove-expense-account-split
(fn [db [_ index]]
(fn [db [_ x]]
(update-in db [::change-expense-accounts :invoice :expense-accounts]
(fn [expense-accounts]
(vec (concat (take index expense-accounts)
(drop (inc index) expense-accounts)))))))
(vec (filter #(not= x (:id %)) expense-accounts) )))))
(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])
multi-location? (> (count locations) 1)
change-event [::events/change-form [::change-expense-accounts]]
change-event [::change]
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
expense-accounts-total (->> expense-accounts
(map :amount)
@@ -105,17 +126,18 @@
[:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]]
[:tbody
(for [[expense-account index] (map vector expense-accounts (range))]
^{:key index}
(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 [:invoice :expense-accounts index :expense-account-id]
:event change-event
:field [:expense-account-id]
:event [::change id]
:spec ::invoices-expense-accounts/expense-account-id
:subscription data}]]]]
:subscription sub}]]]]
(when multi-location?
[:td
@@ -125,10 +147,10 @@
[:div.select
[bind-field
[:select {:type "select"
:field [:invoice :expense-accounts index :location]
:field [:location]
:spec (set locations)
:event change-event
:subscription data}
:event [::change id]
:subscription sub}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
[:td
@@ -138,23 +160,21 @@
[:p.control
[bind-field
[:input.input {:type "number"
:field [:invoice :expense-accounts index :amount]
:field [:amount]
:style {:text-align "right"}
:event change-event
:subscription data
:value (get-in data [:invoice :expense-accounts index :amount])
: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 index])} [:i.fa.fa-times]]]])
[:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]]))
[:tr
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Subtotal: "]
[:td.no-border { :col-span (when multi-location? "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 { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
[:tr
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Remaining: "]
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]
[:tr
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Invoice total: "]
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
]]]))
[:td.no-border { :col-span (when multi-location? "2") :style { :text-align "right"} } "Difference: "]
[:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]]))