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 { table-layout: fixed }
.table td {text-overflow: ellipsis; overflow: hidden; white-space: nowrap; padding: 9px; } .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.expandable {text-overflow: ellipsis; overflow: visible; white-space: nowrap; padding: 9px; }
.table td.no-border { border: none; }
.typeahead { .typeahead {
position:relative; position:relative;

View File

@@ -22,12 +22,34 @@
(fn [db] (fn [db]
(-> db ::change-expense-accounts))) (-> 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 (re-frame/reg-event-fx
::change-expense-accounts ::change-expense-accounts
(fn [{:keys [db]} [_ i]] (fn [{:keys [db]} [_ i]]
{:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}] {:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}]
:db (assoc-in db [::change-expense-accounts :invoice] i)})) :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 (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 ]]
@@ -40,7 +62,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 (:id ea) :expense-accounts (map (fn [ea] {:id (when-not (string? (:id ea)) (:id ea))
:amount (:amount ea) :amount (:amount ea)
:location (:location ea) :location (:location ea)
:expense-account-id (:expense-account-id ea)}) :expense-account-id (:expense-account-id ea)})
@@ -62,21 +84,20 @@
(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 :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 (re-frame/reg-event-db
::remove-expense-account-split ::remove-expense-account-split
(fn [db [_ index]] (fn [db [_ x]]
(update-in db [::change-expense-accounts :invoice :expense-accounts] (update-in db [::change-expense-accounts :invoice :expense-accounts]
(fn [expense-accounts] (fn [expense-accounts]
(vec (concat (take index expense-accounts) (vec (filter #(not= x (:id %)) expense-accounts) )))))
(drop (inc index) expense-accounts)))))))
(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 :as data} @(re-frame/subscribe [::change-expense-accounts])
multi-location? (> (count locations) 1) 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]) chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
expense-accounts-total (->> expense-accounts expense-accounts-total (->> expense-accounts
(map :amount) (map :amount)
@@ -105,17 +126,18 @@
[:th {:style {:width "300px"}} "Amount"] [:th {:style {:width "300px"}} "Amount"]
[:th {:style {:width "5em"}}]]] [:th {:style {:width "5em"}}]]]
[:tbody [:tbody
(for [[expense-account index] (map vector expense-accounts (range))] (doall (for [{:keys [id] :as expense-account} expense-accounts
^{:key index} :let [sub @(re-frame/subscribe [::expense-account (:id expense-account)])]]
^{:key id}
[:tr [:tr
[:td.expandable [:div.control [:td.expandable [:div.control
[bind-field [bind-field
[typeahead {:matches (map (fn [x] [(:id x) (str (:id x) " - " (:name x))]) chooseable-expense-accounts) [typeahead {:matches (map (fn [x] [(:id x) (str (:id x) " - " (:name x))]) chooseable-expense-accounts)
:type "typeahead" :type "typeahead"
:field [:invoice :expense-accounts index :expense-account-id] :field [:expense-account-id]
:event change-event :event [::change id]
:spec ::invoices-expense-accounts/expense-account-id :spec ::invoices-expense-accounts/expense-account-id
:subscription data}]]]] :subscription sub}]]]]
(when multi-location? (when multi-location?
[:td [:td
@@ -125,10 +147,10 @@
[:div.select [:div.select
[bind-field [bind-field
[:select {:type "select" [:select {:type "select"
:field [:invoice :expense-accounts index :location] :field [:location]
:spec (set locations) :spec (set locations)
:event change-event :event [::change id]
:subscription data} :subscription sub}
(map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])])
[:td [:td
@@ -138,23 +160,21 @@
[:p.control [:p.control
[bind-field [bind-field
[:input.input {:type "number" [:input.input {:type "number"
:field [:invoice :expense-accounts index :amount] :field [:amount]
:style {:text-align "right"} :style {:text-align "right"}
:event change-event :event [::change id]
:subscription data :subscription sub
:value (get-in data [:invoice :expense-accounts index :amount]) :value (get-in data [:amount])
:max (:total data) :max (:total data)
:step "0.01"}]]]]]] :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 [: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 ) )]] [:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]]
[:tr [:tr
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Remaining: "] [:td.no-border { :col-span (when multi-location? "2") :style { :text-align "right"} } "Difference: "]
[:td { :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) ) )]]]]]))
[:tr
[:td { :col-span (when multi-location? "2") :style { :text-align "right"} } "Invoice total: "]
[:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]]
]]]))