Almost finished with invoice page

This commit is contained in:
Bryce Covert
2020-08-16 21:59:41 -07:00
parent 3eae5fa52f
commit 85a110d878
4 changed files with 210 additions and 279 deletions

View File

@@ -10,8 +10,8 @@
[re-frame.core :as re-frame]))
(re-frame/reg-sub
::can-submit-advanced-print-checks
:<- [::forms/form ::advanced-print-checks]
::can-submit
:<- [::forms/form ::form]
(fn [{ {:keys [invoices invoice-amounts]} :data}]
(cond (seq (filter
(fn [{:keys [id outstanding-balance]}]
@@ -22,15 +22,15 @@
:else
true)))
(def advanced-print-checks-form (forms/vertical-form {:submit-event [::advanced-print-checks-submitted]
:change-event [::forms/change ::advanced-print-checks]
:can-submit [::can-submit-advanced-print-checks]
:id ::advanced-print-checks}))
(def advanced-print-checks-form (forms/vertical-form {:submit-event [::save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn print-checks-modal []
(defn form []
(let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::advanced-print-checks])
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} advanced-print-checks-form]
(form-inline {}
@@ -71,16 +71,15 @@
::show
(fn [{:keys [db]} [_ invoices]]
{:dispatch [::modal/modal-requested {:title "Print Checks"
:body [print-checks-modal]
:body [form]
:confirm {:value "Print checks"
:status-from [::status/single ::advanced-print-checks]
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::advanced-print-checks-submitted])
:can-submit [::can-submit-advanced-print-checks]
:close-event [::status/completed ::advanced-print-checks]}}]
:on-click (dispatch-event [::save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/stop-form ::form/form)
(forms/start-form ::advanced-print-checks
(forms/start-form ::form
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts])))
:invoices invoices
:invoice-amounts (into {}
@@ -90,48 +89,33 @@
(defn print-checks-query [invoice-payments bank-account-id type client-id]
{:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"}
:venia/queries [[:print-checks
{:invoice_payments invoice-payments
:type type
:bank_account_id bank-account-id
:client_id client-id}
[[:invoices invoice-read]
:pdf_url]]]})
(re-frame/reg-event-fx
::advanced-print-checks-submitted
[with-user (forms/in-form ::advanced-print-checks) ]
::save
[with-user (forms/in-form ::form) ]
(fn [{:keys [db user]} [_ bank-account-id]]
(let [type (->> @(re-frame/subscribe [::subs/client])
:bank-accounts
(filter #(= bank-account-id (:id %)))
first
:type)
(let [type (or (->> @(re-frame/subscribe [::subs/client])
:bank-accounts
(filter #(= bank-account-id (:id %)))
first
:type)
:check)
{:keys [date invoices invoice-amounts check-number bank-account-id client]} (:data db)]
{:graphql
{:token user
:owns-state {:single ::advanced-print-checks}
:query-obj (print-checks-query (map (fn [x]
{:invoice-id (:id x)
:amount (get-in invoice-amounts [(:id x) :amount])})
invoices)
bank-account-id
(cond (= type :check)
:check
(= type :cash)
:cash
:else
:check)
(->> invoices first :client :id))
:owns-state {:single ::form}
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"}
:venia/queries [[:print-checks
{:invoice_payments (map (fn [x]
{:invoice-id (:id x)
:amount (get-in invoice-amounts [(:id x) :amount])})
invoices)
:type type
:bank_account_id bank-account-id
:client_id (->> invoices first :client :id)}
[[:invoices invoice-read]
:pdf_url]]]}
:on-success (fn [result]
[::checks-printed
(:invoices (:print-checks result))

View File

@@ -176,18 +176,18 @@
(re-frame/reg-event-fx
::add-and-print
[with-user (forms/in-form ::form)]
(fn [{:keys [user] {:keys [data]} :db} [_ params bank-account-id type]]
(fn [{:keys [user] {:keys [data]} :db} [_ bank-account-id type]]
{:graphql
{:token user
:owns-state {:single ::form}
:query-obj @(re-frame/subscribe [::add-and-print-query bank-account-id type])
:on-success [::added-and-printed params]
:on-success [::added-and-printed]
:on-error [::forms/save-error ::form]}}))
(re-frame/reg-event-fx
::saving
[with-user (forms/in-form ::form)]
(fn [{:keys [user] {:keys [data]} :db} [_ params]]
(fn [{:keys [user] {:keys [data]} :db} _]
(let [command (if (:id data)
:edit
:create)]
@@ -206,10 +206,15 @@
(re-frame/reg-event-fx
::added-and-printed
(fn [{:keys [db]} [_ {:keys [invoice-printed]} result]]
(fn [{:keys [db]} [_ result]]
(let [invoice (first (:invoices (:add-and-print-invoice result)))]
{:dispatch-n [[::updated (assoc invoice :class "live-added")]
(conj invoice-printed (:pdf-url (:add-and-print-invoice result)))]})))
[::checks-printed [invoice] (:pdf-url (:add-and-print-invoice result))]]})))
(re-frame/reg-event-db
::checks-printed
(fn [db [_ invoices pdf-url]]
db))
@@ -331,10 +336,10 @@
(list
(for [{:keys [id number name type]} (->> (:bank-accounts (:client data)) (filter :visible) (sort-by :sort-order))]
(if (= :cash type)
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :cash])} "With cash"]
^{:key id} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :cash])} "With cash"]
(list
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :check])} "Print checks from " name]
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print params id :debit])} "Debit from " name]))))]]])
^{:key (str id "-check")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :check])} "Print checks from " name]
^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::add-and-print id :debit])} "Debit from " name]))))]]])
[:div.column
(submit-button "Save")]]])

View File

@@ -0,0 +1,137 @@
(ns auto-ap.views.pages.invoices.handwritten-checks
(:require [auto-ap.entities.invoice :as invoice]
[auto-ap.forms :as forms]
[auto-ap.status :as status]
[auto-ap.subs :as subs]
[auto-ap.utils :refer [by]]
[auto-ap.views.components.modal :as modal]
[auto-ap.views.pages.data-page :as data-page]
[auto-ap.views.pages.invoices.common
:refer
[does-amount-exceed-outstanding? invoice-read]]
[auto-ap.views.pages.invoices.form :as form]
[auto-ap.views.utils
:refer
[date-picker dispatch-event horizontal-field with-user]]
[clojure.string :as str]
[re-frame.core :as re-frame]))
(def handwrite-checks-form (forms/vertical-form {:submit-event [::save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn form []
(let [real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} handwrite-checks-form]
(form-inline {}
[:<>
[:div.field
[:label.label "Pay using"]
[:div.control
[:span.select
[raw-field
[:select {:type "select"
:field :bank-account-id}
(for [{:keys [id number name]} real-bank-accounts]
^{:key id} [:option {:value id} name])]]]]]
(field "Date"
[date-picker {:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "mm/dd/yyyy"
:next-month-button-label ""
:next-month-label ""
:type "date"
:field [:date]
:spec ::invoice/date}])
(field "Check number"
[:input.input {:type "number"
:field [:check-number]}])
[:table.table.is-fullwidth
[:thead
[:tr
[:th "Invoice ID"]
[:th {:style {"width" "14em"}} "Payment"]]]
[:tbody
(doall
(for [{:keys [payment outstanding-balance invoice-number id] :as i} (:invoices data)]
^{:key id}
[:tr
[:td invoice-number]
[:td [:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
(raw-field
[:input.input.has-text-right {:type "number"
:field [:invoice-amounts id :amount]
#_#_:max outstanding-balance
:step "0.01"}])]]]]))]]])))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::form]
(fn [{ {:keys [check-number date invoices invoice-amounts]} :data}]
(boolean (cond (seq (filter
(fn [{:keys [id outstanding-balance]}]
(does-amount-exceed-outstanding? (get-in invoice-amounts [id :amount]) outstanding-balance ))
invoices))
false
:else
(and (not (str/blank? check-number))
(not (str/blank? date)))))))
(re-frame/reg-event-fx
::show
(fn [{:keys [db]} [_ invoices]]
{:dispatch [::modal/modal-requested {:title "Handwrite checks"
:body [form]
:confirm {:value "Save handwritten check"
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/start-form ::form
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts])))
:invoices invoices
:invoice-amounts (into {}
(map (fn [i] [(:id i)
{:amount (:outstanding-balance i)}])
invoices))}))}))
(re-frame/reg-event-fx
::save
[with-user (forms/in-form ::form)]
(fn [{:keys [db user]} _]
(let [{:keys [date invoices invoice-amounts check-number bank-account-id]} (:data db)]
{:graphql
{:token user
:owns-state {:single ::form}
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddHandwrittenCheck"}
:venia/queries [{:query/data [:add-handwritten-check
{:date date
:invoice_payments (map (fn [x]
{:invoice-id (:id x)
:amount (get-in invoice-amounts [(:id x) :amount])})
invoices)
:check-number check-number
:bank-account-id bank-account-id}
[[:invoices invoice-read]]]}]}
:on-success (fn [result]
[::succeeded (:invoices (:add-handwritten-check result))])}})))
(re-frame/reg-event-fx
::succeeded
[(forms/triggers-stop ::form)]
(fn [{:keys [db]} [_ invoices]]
{:dispatch [::modal/modal-closed]}))

View File

@@ -22,9 +22,8 @@
[appearing-side-bar side-bar-layout]]
[auto-ap.views.components.modal :as modal :refer [modal]]
[auto-ap.views.pages.data-page :as data-page]
[auto-ap.views.pages.invoices.advanced-print-checks
:as
advanced-print-checks]
[auto-ap.views.pages.invoices.advanced-print-checks :as advanced-print-checks]
[auto-ap.views.pages.invoices.handwritten-checks :as handwritten-checks]
[auto-ap.views.pages.invoices.common :refer [invoice-read does-amount-exceed-outstanding?]]
[auto-ap.views.pages.invoices.form :as form]
[auto-ap.views.utils
@@ -37,28 +36,11 @@
[reagent.core :as r]
[vimsical.re-frame.fx.track :as track]))
(re-frame/reg-sub
::invoice-page
(fn [db]
(-> (::invoice-page db)
(update :invoices
(fn [is]
(mapv
(fn [i]
(assoc i :checkable? (not (:automatically-paid-when-due i))))
is))))))
(re-frame/reg-sub
::change-expense-accounts
(fn [db]
(-> db ::change-expense-accounts)))
(re-frame/reg-sub
::check-results
(fn [db]
(-> db ::check-results)))
(re-frame/reg-sub
::params
:<- [::subs/client]
@@ -93,10 +75,10 @@
(re-frame/reg-event-fx
::unmounted
(fn [{:keys [db]} _]
{:db (dissoc db ::invoice-page ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::last-params)
{:db (dissoc db ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::last-params)
:dispatch [::data-page/dispose :invoices]
:forward-events [{:unregister ::updated}
{:unregister ::advanced-print-checks-printed}]
{:unregister ::checks-printed}]
::track/dispose [{:id ::params}]}))
(re-frame/reg-event-fx
@@ -109,9 +91,8 @@
:forward-events [{:register ::updated
:events #{::table/invoice-updated ::form/updated}
:dispatch-to [::data-page/entity-updated :invoices]}
{:register ::checks-printed
:events #{::form/checks-printed ::advanced-print-checks/checks-printed}
:events #{::form/checks-printed ::advanced-print-checks/checks-printed ::handwritten-checks/succeeded}
:dispatch-to [::checks-printed]}]}))
@@ -127,7 +108,6 @@
(defn print-checks-query [invoice-payments bank-account-id type client-id]
{:venia/operation {:operation/type :mutation
:operation/name "PrintChecks"}
:venia/queries [[:print-checks
{:invoice_payments invoice-payments
:type type
@@ -139,9 +119,7 @@
(re-frame/reg-event-fx
::print-checks
(fn [{:keys [db]} [_ bank-account-id type]]
{:db (-> db
(assoc-in [::invoice-page :print-checks-shown?] false ))
:graphql
{:graphql
{:token (-> db :user)
:owns-state {:single ::print-checks}
@@ -152,27 +130,26 @@
bank-account-id
type
(:client db))
:on-success [::checks-created]}}))
:on-success (fn [result]
[::checks-printed
[nil
(:invoices (:print-checks result))
(:pdf-url (:print-checks result))]])}}))
(re-frame/reg-event-fx
::checks-printed
(fn [{:keys [db]} [_ [_ invoices pdf-url]]]
(let [invoices-by-id (by :id invoices) ]
{:db (-> db
(assoc-in [::check-results :shown?] true)
(assoc-in [::check-results :pdf-url] pdf-url))
:dispatch-n (mapv
(fn [i]
[::data-page/entity-updated :invoices [nil (assoc i :class "live-added")]])
invoices)})))
{:dispatch-n (cond->> (mapv
(fn [i]
[::data-page/entity-updated :invoices [nil (assoc i :class "live-added")]])
invoices)
pdf-url (into [[::modal/modal-requested {:title "Your checks are ready!"
:body [:div
[:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."]
[:div [:em "Remember to turn off all scaling and margins."]]]}]]))}))
(re-frame/reg-event-db
::close-check-results
(fn [db _]
(assoc-in db [::check-results :shown?] false )))
(re-frame/reg-event-fx
::new-invoice-clicked
(fn [{:keys [db]} _]
@@ -181,171 +158,16 @@
#_#_:date (date->str (c/now) standard)
:location (first (:locations @(re-frame/subscribe [::subs/client])))}]}))
(re-frame/reg-event-fx
::invoice-edited
[(re-frame/path [::invoice-page :invoices])]
(fn [{:keys [db]} [_ invoice]]
(let [invoice (assoc invoice :class "live-added")]
(let [by-id (by :id db )]
{:db (if (by-id (:id invoice))
(merge-by db :id invoice)
(into [invoice] db))}))))
(re-frame/reg-event-fx
::expense-accounts-updated
(fn [{:keys [db]} [_ data]]
(let [updated (:edit-expense-accounts data)]
{:dispatch [::events/modal-completed ::expense-accounts-dialog/change-expense-accounts]
{:dispatch-n [[::events/modal-completed ::expense-accounts-dialog/change-expense-accounts]
[::data-page/entity-updated :invoices [nil updated]]]
:db (-> db
(update-in [::invoice-page :invoices]
(fn [is]
(replace-if #(= (:id %1) (:id %2)) updated is)))
(dissoc ::change-expense-accounts))})))
;; HANDWRITE CHECKS
(def handwrite-checks-form (forms/vertical-form {:submit-event [::handwrite-checks-save]
:change-event [::forms/change ::handwrite-checks]
:can-submit [::can-submit-handwrite-checks]
:id ::handwrite-checks}))
(defn handwrite-checks-modal []
(let [{:keys [checked]} @(re-frame/subscribe [::invoice-page])
real-bank-accounts @(re-frame/subscribe [::subs/real-bank-accounts])
{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::handwrite-checks])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} handwrite-checks-form]
(form-inline {}
[:<>
[:div.field
[:label.label "Pay using"]
[:div.control
[:span.select
[raw-field
[:select {:type "select"
:field :bank-account-id}
(for [{:keys [id number name]} real-bank-accounts]
^{:key id} [:option {:value id} name])]]]]]
(field "Date"
[date-picker {:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "mm/dd/yyyy"
:next-month-button-label ""
:next-month-label ""
:type "date"
:field [:date]
:spec ::invoice/date}])
(field "Check number"
[:input.input {:type "number"
:field [:check-number]}])
[:table.table.is-fullwidth
[:thead
[:tr
[:th "Invoice ID"]
[:th {:style {"width" "14em"}} "Payment"]]]
[:tbody
(doall
(for [{:keys [payment outstanding-balance invoice-number id] :as i} (:invoices data)]
^{:key id}
[:tr
[:td invoice-number]
[:td [:div.field.has-addons.is-extended
[:p.control [:a.button.is-static "$"]]
[:p.control
(raw-field
[:input.input.has-text-right {:type "number"
:field [:invoice-amounts id :amount]
#_#_:max outstanding-balance
:step "0.01"}])]]]]))]]])))
(re-frame/reg-sub
::can-submit-handwrite-checks
:<- [::forms/form ::handwrite-checks]
(fn [{ {:keys [check-number date invoices invoice-amounts]} :data}]
(boolean (cond (seq (filter
(fn [{:keys [id outstanding-balance]}]
(does-amount-exceed-outstanding? (get-in invoice-amounts [id :amount]) outstanding-balance ))
invoices))
false
:else
(and (not (str/blank? check-number))
(not (str/blank? date)))))))
(re-frame/reg-event-fx
::handwrite-checks
(fn [{:keys [db]} _]
(let [invoices @(re-frame/subscribe [::checked-invoices])]
{:dispatch [::modal/modal-requested {::handwrite-checks {:visible? true}
:title "Handwrite checks"
:body [handwrite-checks-modal]
:confirm {:value "Save handwritten check"
:status-from [::status/single ::handwrite-checks]
:class "is-primary"
:on-click (dispatch-event [::handwrite-checks-save])
:can-submit [::can-submit-handwrite-checks]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/stop-form ::form/form)
(forms/start-form ::handwrite-checks
{:bank-account-id (:id (first @(re-frame/subscribe [::subs/real-bank-accounts])))
:invoices invoices
:invoice-amounts (into {}
(map (fn [i] [(:id i)
{:amount (:outstanding-balance i)}])
invoices))}))})))
(re-frame/reg-event-fx
::handwrite-checks-save
[with-user (forms/in-form ::handwrite-checks)]
(fn [{:keys [db user]} _]
(let [{:keys [date invoices invoice-amounts check-number bank-account-id]} (:data db)]
{:graphql
{:token user
:owns-state {:single ::handwrite-checks}
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddHandwrittenCheck"}
:venia/queries [{:query/data [:add-handwritten-check
{:date date
:invoice_payments (map (fn [x]
{:invoice-id (:id x)
:amount (get-in invoice-amounts [(:id x) :amount])})
invoices)
:check-number check-number
:bank-account-id bank-account-id}
[[:invoices invoice-read]]]}]}
:on-success [::handwrite-checks-succeeded]}})))
(re-frame/reg-event-fx
::handwrite-checks-succeeded
[(forms/triggers-stop ::handwrite-checks)]
(fn [{:keys [db]} [_ {:keys [add-handwritten-check] :as result}]]
(let [invoices-by-id (by :id (:invoices add-handwritten-check))]
{:dispatch-n (into [[::modal/modal-closed]]
(map
(fn [i]
[::data-page/entity-updated :invoices [_ (assoc i :class "live-added")]])
(:invoices add-handwritten-check)))})))
(defn pay-button []
(let [current-client @(re-frame/subscribe [::subs/client])
checked-invoices @(re-frame/subscribe [::checked-invoices])]
@@ -386,7 +208,7 @@
^{:key "advanced-divider"} [:hr.dropdown-divider]
(when (= 1 (count (set (map (comp :id :vendor) checked-invoices))))
^{:key "handwritten"} [:a.dropdown-item {:on-click (dispatch-event [::handwrite-checks])} "Handwritten Check..."])
^{:key "handwritten"} [:a.dropdown-item {:on-click (dispatch-event [::handwritten-checks/show checked-invoices])} "Handwritten Check..."])
^{:key "advanced"} [:a.dropdown-item {:on-click (dispatch-event [::advanced-print-checks/show checked-invoices])} "Advanced..."])]])]]
[:div.is-pulled-right {:style {:margin-right "0.5rem"}}
(into [:div.tags ] (map (fn [{:keys [id invoice-number]}]
@@ -394,29 +216,13 @@
[:button.delete.is-small {:on-click
(dispatch-event [::data-page/remove-check :invoices id])}]]) checked-invoices))]]))
(defn check-results-dialog []
(let [{check-results-shown? :shown? pdf-url :pdf-url} @(re-frame/subscribe [::check-results])]
(when check-results-shown?
(if pdf-url
[modal
{:title "Your checks are ready!"
:hide-event [::close-check-results]}
[:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."]
[:div [:em "Remember to turn off all scaling and margins."]]
]
[modal
{:title "Payment created!"
:hide-event [::close-check-results]}
[:div [:em "Your payment was created."]]
]))))
(defn unpaid-invoices-content [{:keys [status] :as params}]
(let [page @(re-frame/subscribe [::data-page/page :invoices])
current-client @(re-frame/subscribe [::subs/client])]
[:div
#_[:h1.title (str (str/capitalize (or status "all")) " invoices")]
[:h1.title (str (str/capitalize (name (or status :all))) " invoices")]
[status/status-notification {:statuses [[::status/single ::print-checks]]}]
(when (= status :unpaid)
[pay-button])
[table/invoice-table {:id (:id page)
@@ -440,6 +246,5 @@
[side-bar-layout {:side-bar [invoices-side-bar {}]
:main [unpaid-invoices-content params]
:bottom [:div
[check-results-dialog]
[change-expense-accounts-modal {:updated-event [::expense-accounts-updated]}]]
:right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {:invoice-printed [::checks-printed]}]]}]))}))
:right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))}))