adding ability to preserve checks across pages

This commit is contained in:
Bryce Covert
2019-02-08 07:24:59 -08:00
parent 41bf4dbabb
commit dccaecf86b
3 changed files with 99 additions and 63 deletions

View File

@@ -140,7 +140,8 @@
)] )]
[invoice payment]))) [invoice payment])))
(filter identity) (filter identity)
(map remove-nils))) (map remove-nils)
))
(defroutes routes (defroutes routes
(wrap-routes (wrap-routes

View File

@@ -133,10 +133,12 @@
} }
""]]] ""]]]
[:tbody [:tbody
(println checked)
(if (:loading @status) (if (:loading @status)
[:tr [:tr
[:td {:col-span 5} [:td {:col-span 5}
[:i.fa.fa-spin.fa-spinner]]] [:i.fa.fa-spin.fa-spinner]]]
(for [{:keys [client payments expense-accounts invoice-number date total outstanding-balance id vendor] :as i} (:invoices @invoice-page)] (for [{:keys [client payments expense-accounts invoice-number date total outstanding-balance id vendor] :as i} (:invoices @invoice-page)]
^{:key id} ^{:key id}
[:tr {:class (:class i)} [:tr {:class (:class i)}
@@ -146,7 +148,8 @@
"checked" "checked"
"") "")
:on-change (fn [x e] (when on-check-changed :on-change (fn [x e] (when on-check-changed
(on-check-changed id)))} ]]) (println id i)
(on-check-changed id i)))} ]])
(when-not selected-client (when-not selected-client
[:td (:name client)]) [:td (:name client)])
[:td (:name vendor)] [:td (:name vendor)]

View File

@@ -17,7 +17,7 @@
[auto-ap.views.components.invoices.side-bar :refer [invoices-side-bar]] [auto-ap.views.components.invoices.side-bar :refer [invoices-side-bar]]
[auto-ap.expense-accounts :as expense-accounts] [auto-ap.expense-accounts :as expense-accounts]
[auto-ap.entities.invoices-expense-accounts :as invoices-expense-accounts] [auto-ap.entities.invoices-expense-accounts :as invoices-expense-accounts]
[auto-ap.views.utils :refer [active-when dispatch-event bind-field horizontal-field date->str str->date pretty standard]] [auto-ap.views.utils :refer [active-when dispatch-event bind-field horizontal-field date->str date-time->str str->date pretty standard]]
[auto-ap.utils :refer [by replace-if]] [auto-ap.utils :refer [by replace-if]]
[auto-ap.views.pages.check :as check] [auto-ap.views.pages.check :as check]
[auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
@@ -81,22 +81,28 @@
:graphql {:token (-> cofx :db :user) :graphql {:token (-> cofx :db :user)
:query-obj (invoice-table/query (doto (assoc params :imported true) println)) :query-obj (invoice-table/query (doto (assoc params :imported true) println))
:on-success [::received]}})) :on-success [::received]}}))
(re-frame/reg-event-db
::unmount-invoices
(fn [db [_ data]]
(-> db
(dissoc ::invoice-page ))))
(re-frame/reg-event-db (re-frame/reg-event-db
::received ::received
(fn [db [_ data]] (fn [db [_ data]]
(-> db (-> db
(assoc ::invoice-page (first (:invoice-page data))) (update ::invoice-page merge (first (:invoice-page data)))
(assoc-in [:status :loading] false)))) (assoc-in [:status :loading] false))))
(re-frame/reg-event-db (re-frame/reg-event-db
::toggle-check ::toggle-check
(fn [db [_ data]] (fn [db [_ id invoice]]
(update-in db [::invoice-page :checked] (fn [x] (-> db
(let [x (or x #{})] (update-in [::invoice-page :checked] (fn [x]
(if (x data) (let [x (or x {})]
(disj x data) (if (x id)
(conj x data))))))) (dissoc x id)
(assoc x id invoice))))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::print-checks-clicked ::print-checks-clicked
@@ -111,8 +117,8 @@
(update-in [::invoice-page :print-checks-shown?] #(not %) ) (update-in [::invoice-page :print-checks-shown?] #(not %) )
(assoc-in [::advanced-print-checks] {:shown? true (assoc-in [::advanced-print-checks] {:shown? true
:bank-account-id (:id (first (:bank-accounts @(re-frame/subscribe [::subs/clients])))) :bank-account-id (:id (first (:bank-accounts @(re-frame/subscribe [::subs/clients]))))
:invoices (->> invoices :invoices (->> checked
(filter (comp checked :id)) vals
(map #(assoc % :amount (:outstanding-balance %))))} ))))) (map #(assoc % :amount (:outstanding-balance %))))} )))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@@ -171,22 +177,20 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::print-checks ::print-checks
(fn [{:keys [db]} [_ bank-account-id type]] (fn [{:keys [db]} [_ bank-account-id type]]
(let [invoice-amounts (by :id :outstanding-balance (get-in db [::invoice-page :invoices]))] {:db (-> db
(assoc-in [::invoice-page :print-checks-shown?] false )
(assoc-in [::invoice-page :print-checks-loading?] true ))
:graphql
{:token (-> db :user)
{:db (-> db :query-obj (print-checks-query (map (fn [[id invoice]]
(assoc-in [::invoice-page :print-checks-shown?] false ) {:invoice-id id
(assoc-in [::invoice-page :print-checks-loading?] true )) :amount (:outstanding-balance invoice)})
:graphql (get-in db [::invoice-page :checked]))
{:token (-> db :user) bank-account-id
type
:query-obj (print-checks-query (map (fn [id] (:client db))
{:invoice-id id :on-success [::checks-created]}}))
:amount (invoice-amounts id)})
(get-in db [::invoice-page :checked]))
bank-account-id
type
(:client db))
:on-success [::checks-created]}})))
@@ -675,7 +679,7 @@
(if (and (= key :vendor-id) (if (and (= key :vendor-id)
(not= value (get-in db [::params :vendor-id]))) (not= value (get-in db [::params :vendor-id])))
(do (do
(re-frame/dispatch [::params-change (assoc (::params updated) :vendor-id value)]) (re-frame/dispatch [::params-change (assoc (::params updated) :vendor-id value :start 0)])
(assoc-in updated [::params :vendor-id] value)) (assoc-in updated [::params :vendor-id] value))
updated)))) updated))))
@@ -685,47 +689,74 @@
[:div.control [:input.input {:placeholder "AP-123" [:div.control [:input.input {:placeholder "AP-123"
:value invoice-number-like :value invoice-number-like
:on-change (fn [x] :on-change (fn [x]
(re-frame/dispatch [::params-change (assoc params :invoice-number-like (.. x -target -value)) ]) (re-frame/dispatch [::params-change (assoc params :invoice-number-like (.. x -target -value) :start 0) ])
)} ]]])) )} ]]]))
(defn pay-button [{:keys [print-checks-shown? checked-invoices print-checks-loading?]}] (defn pay-button [{:keys [print-checks-shown? checked-invoices print-checks-loading?]}]
(let [current-client @(re-frame/subscribe [::subs/client])] (let [current-client @(re-frame/subscribe [::subs/client])]
[:div.is-pulled-right [:div
[:button.button.is-danger {:on-click (dispatch-event [::new-invoice])} "New Invoice"]
(when current-client
[:div.dropdown.is-right {:class (if print-checks-shown?
"is-active"
"")}
[:div.dropdown-trigger
[:button.button.is-success {:aria-haspopup true
:on-click (dispatch-event [::print-checks-clicked ])
:disabled (if (seq checked-invoices)
""
"disabled")
:class (if print-checks-loading?
"is-loading"
"")}
"Pay "
[:span " "]
[:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]]
[:div.dropdown-menu {:role "menu"}
[:div.dropdown-content
(list
(for [{:keys [id number name type]} (:bank-accounts current-client)]
(if (= :cash type)
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :cash])} "With cash"]
(list
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :check])} "Print checks from " name]
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :debit])} "Debit from " name])))
^{:key "advanced-divider"} [:hr.dropdown-divider]
(when (= 1 (count checked-invoices))
^{:key "handwritten"} [:a.dropdown-item {:on-click (dispatch-event [::handwrite-checks])} "Handwritten Check..."]) [:div.is-pulled-right
^{:key "advanced"} [:a.dropdown-item {:on-click (dispatch-event [::advanced-print-checks])} "Advanced..."])]]])]))
[:button.button.is-danger {:on-click (dispatch-event [::new-invoice])} "New Invoice"]
(when current-client
[:div.dropdown.is-right {:class (if print-checks-shown?
"is-active"
"")}
[:div.dropdown-trigger
[:button.button.is-success {:aria-haspopup true
:on-click (dispatch-event [::print-checks-clicked ])
:disabled (if (seq checked-invoices)
""
"disabled")
:class (if print-checks-loading?
"is-loading"
"")}
"Pay "
(when (> (count checked-invoices ))
(str
(count checked-invoices)
" invoices "
"(" (->> checked-invoices
vals
(map (comp js/parseFloat :outstanding-balance))
(reduce + 0)
(gstring/format "$%.2f" ))
")"))
[:span " "]
[:span.icon.is-small [:i.fa.fa-angle-down {:aria-hidden "true"}]]]]
[:div.dropdown-menu {:role "menu"}
[:div.dropdown-content
(list
(for [{:keys [id number name type]} (:bank-accounts current-client)]
(if (= :cash type)
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :cash])} "With cash"]
(list
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :check])} "Print checks from " name]
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :debit])} "Debit from " name])))
^{:key "advanced-divider"} [:hr.dropdown-divider]
(when (= 1 (count checked-invoices))
^{:key "handwritten"} [:a.dropdown-item {:on-click (dispatch-event [::handwrite-checks])} "Handwritten Check..."])
^{:key "advanced"} [:a.dropdown-item {:on-click (dispatch-event [::advanced-print-checks])} "Advanced..."])]]])]
[:div.is-pulled-right
(into [:div.tags {:style {:margin-right ".5 rem;"}}] (map (fn [[id invoice]] [:span.tag.is-medium (:invoice-number invoice) [:button.delete.is-small {:on-click (dispatch-event [::toggle-check id invoice])}]]) checked-invoices))]]
))
(defn unpaid-invoices-content [{:keys [status]}] (defn unpaid-invoices-content [{:keys [status]}]
(r/create-class {:display-name "unpaid-invoices-content" (r/create-class {:display-name "unpaid-invoices-content"
:component-will-unmount (fn [this]
(re-frame/dispatch [::unmount-invoices])
)
:reagent-render (fn [{:keys [status]}] :reagent-render (fn [{:keys [status]}]
(let [{:keys [checked print-checks-shown? print-checks-loading? advanced-print-shown? vendor-filter]} @(re-frame/subscribe [::invoice-page]) (let [{:keys [checked print-checks-shown? print-checks-loading? advanced-print-shown? vendor-filter]} @(re-frame/subscribe [::invoice-page])
current-client @(re-frame/subscribe [::subs/client]) current-client @(re-frame/subscribe [::subs/client])
@@ -736,6 +767,7 @@
[pay-button {:print-checks-shown? print-checks-shown? :checked-invoices checked :print-checks-loading? print-checks-loading?}]) [pay-button {:print-checks-shown? print-checks-shown? :checked-invoices checked :print-checks-loading? print-checks-loading?}])
[invoice-table {:id :unpaid [invoice-table {:id :unpaid
:params (re-frame/subscribe [::params]) :params (re-frame/subscribe [::params])
:invoice-page (re-frame/subscribe [::invoice-page]) :invoice-page (re-frame/subscribe [::invoice-page])
@@ -749,8 +781,8 @@
(re-frame/dispatch [::params-change params])) (re-frame/dispatch [::params-change params]))
:check-boxes (= status "unpaid") :check-boxes (= status "unpaid")
:checked checked :checked checked
:on-check-changed (fn [which] :on-check-changed (fn [which invoice]
(re-frame/dispatch [::toggle-check which])) (re-frame/dispatch [::toggle-check which invoice]))
:expense-event [::expense-accounts-dialog/change-expense-accounts]}] :expense-event [::expense-accounts-dialog/change-expense-accounts]}]
[print-checks-modal] [print-checks-modal]