Revert "Prep to select more than 1 client"

This reverts commit 085486043f.
This commit is contained in:
Bryce Covert
2021-10-18 06:32:24 -07:00
parent 8ecd34f18c
commit 574023fea0
4 changed files with 323 additions and 312 deletions

View File

@@ -554,7 +554,6 @@
:profit_and_loss {:type :profit_and_loss_report :profit_and_loss {:type :profit_and_loss_report
:args {:client_id {:type :id} :args {:client_id {:type :id}
:client_ids {:type '(list :id)}
:periods {:type '(list :date_range)}} :periods {:type '(list :date_range)}}
:resolve :get-profit-and-loss} :resolve :get-profit-and-loss}

View File

@@ -183,14 +183,9 @@
(defn get-profit-and-loss [context args value] (defn get-profit-and-loss [context args value]
(let [client-id (:client_id args) (let [client-id (:client_id args)
client-ids (or (some-> client-id vector) _ (assert-can-see-client (:id context) client-id)
(:client_ids args)) all-ledger-entries (full-ledger-for-client client-id)
_ (when (not (seq client-ids)) lookup-account (build-account-lookup client-id)]
(throw (ex-info "Please select a client." {:validation-error "Please select a client."})))
_ (doseq [client-id client-ids]
(assert-can-see-client (:id context) client-id))
all-ledger-entries (full-ledger-for-client (first client-ids))
lookup-account (build-account-lookup (first client-ids))]
(->graphql (->graphql
{:periods (reduce (fn [acc {:keys [start end]}] {:periods (reduce (fn [acc {:keys [start end]}]
(conj acc (conj acc

View File

@@ -7,9 +7,8 @@
[auto-ap.utils :refer [dollars-0? by ]] [auto-ap.utils :refer [dollars-0? by ]]
[auto-ap.forms :as forms] [auto-ap.forms :as forms]
[auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]] [auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar]]
[auto-ap.views.components.typeahead :refer [typeahead-v3]]
[auto-ap.views.components.modal :as modal] [auto-ap.views.components.modal :as modal]
[auto-ap.views.utils :refer [date->str date-picker date-picker-friendly bind-field standard pretty dispatch-event local-today ->% ->$ str->date with-user dispatch-value-change query-params]] [auto-ap.views.utils :refer [date->str date-picker bind-field standard pretty dispatch-event local-today ->% ->$ str->date with-user dispatch-value-change query-params]]
[cljs-time.core :as t] [cljs-time.core :as t]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[auto-ap.status :as status] [auto-ap.status :as status]
@@ -45,12 +44,9 @@
;; SUBS ;; SUBS
(re-frame/reg-sub (re-frame/reg-sub
::locations ::locations
:<- [::forms/form ::form]
(fn [db] (fn [db]
(->> db (->> db
:report ::report
:periods :periods
(mapcat :accounts) (mapcat :accounts)
(filter (comp in-range? :numeric-code)) (filter (comp in-range? :numeric-code))
@@ -67,6 +63,11 @@
"ZZZZZZ" "ZZZZZZ"
x)))))) x))))))
(re-frame/reg-sub
::report
(fn [db]
(-> db ::report)))
(re-frame/reg-sub (re-frame/reg-sub
::error ::error
(fn [db] (fn [db]
@@ -80,10 +81,9 @@
(re-frame/reg-sub (re-frame/reg-sub
::period-accounts ::period-accounts
:<- [::forms/form ::form]
(fn [db [_ which type only-location]] (fn [db [_ which type only-location]]
(->> (get-in db [:report :periods which :accounts]) (->> (get-in db [::report :periods which :accounts])
(map #(update % :amount js/parseFloat)) (map #(update % :amount js/parseFloat))
(filter (fn [{:keys [account-type location numeric-code]}] (filter (fn [{:keys [account-type location numeric-code]}]
(and (or (nil? only-location) (and (or (nil? only-location)
@@ -93,10 +93,9 @@
(re-frame/reg-sub (re-frame/reg-sub
::uncategorized-accounts ::uncategorized-accounts
:<- [::forms/form ::form]
(fn [db [_ ]] (fn [db [_ ]]
(->> (get-in db [:report :periods ]) (->> (get-in db [::report :periods ])
(mapcat :accounts) (mapcat :accounts)
(map #(update % :amount js/parseFloat)) (map #(update % :amount js/parseFloat))
(filter (fn [{:keys [account-type location numeric-code]}] (filter (fn [{:keys [account-type location numeric-code]}]
@@ -122,7 +121,6 @@
(re-frame/reg-sub (re-frame/reg-sub
::all-accounts ::all-accounts
:<- [::forms/form ::form]
(fn [db [_ which type only-location]] (fn [db [_ which type only-location]]
(transduce (transduce
(comp (comp
@@ -131,13 +129,12 @@
conj conj
[] []
(get-in db [:report :periods])))) (get-in db [::report :periods]))))
(re-frame/reg-event-db (re-frame/reg-event-db
::received ::received
[(forms/in-form ::form)]
(fn [db [_ data]] (fn [db [_ data]]
(-> db (assoc :report (:profit-and-loss data))))) (-> db (assoc ::report (:profit-and-loss data)))))
(re-frame/reg-sub (re-frame/reg-sub
::params ::params
@@ -162,53 +159,68 @@
(assoc db ::ledger-list-active? false))) (assoc db ::ledger-list-active? false)))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::params-change ::mounted
[with-user (forms/in-form ::form)] (fn [{:keys [db]}]
(fn [{:keys [db user] :as cofx}] (let [qp (query-params)]
(let [c @(re-frame/subscribe [::subs/client])] (if (:periods qp)
(cond-> {:graphql {:token user (let [periods (mapv (fn [[start end title]]
:owns-state {:single ::page} [
:query-obj {:venia/queries [[:profit-and-loss (str->date start standard)
{:client-ids (or (some-> (:id c) vector) (str->date end standard)
(some-> (:id (:client (:data db))) title])
vector)) (:periods qp))]
:periods (mapv (fn [[start end] ] {:start (date->str start standard) :end (date->str end standard)} ) {:dispatch [::range-selected periods (:include-deltas qp) nil]})
(:periods (:data db)))} {:dispatch [::range-selected (and-last-year [(t/minus (local-today) (t/period :years 1)) (local-today)]) true nil]}))))
[[:periods [[:accounts [:name :amount :account-type :id :count :numeric-code :location]]]]]]]}
:on-success [::received]}
:set-uri-params {:periods (mapv (fn [[start end title]]
[(date->str start standard)
(date->str end standard)])
(:periods (:data db)))
:client (select-keys (:client (:data db))
[:name :id])}
:db (dissoc db :report)}))))
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::period-inputs-change
(forms/change-handler ::form (fn [db [_ field value]]
(fn [data field value] (assoc-in db (into [::period-inputs ] field) value)))
(cond
(= [:periods] field)
[field (mapv (fn [period]
(mapv
(fn [dt]
(str->date dt standard))
period))
value)]
(and (= :periods (first field)) (re-frame/reg-event-fx
(= 2 (count field))) ::params-change
[field value] ;;already serialized (fn [cofx [_ params]]
(let [c @(re-frame/subscribe [::subs/client])]
(cond-> {:db (-> (:db cofx)
(assoc-in [::params] params)
(dissoc ::report))
:set-uri-params (update params
:periods
(fn [p]
(mapv (fn [[start end title]]
[(date->str start standard)
(date->str end standard)
title]
) p)))}
c (assoc :graphql (when @(re-frame/subscribe [::subs/client])
{:token (-> cofx :db :user)
:owns-state {:single ::page}
:query-obj {:venia/queries [[:profit-and-loss
{:client-id (:id c)
:periods (mapv (fn [[start end] ] {:start (date->str start standard) :end (date->str end standard)} )
(:periods params))}
[[:periods [[:accounts [:name :amount :account-type :id :count :numeric-code :location]]]]]]]}
:on-success [::received]}))))))
(= :periods (first field)) [field (str->date value standard) ]
:else nil)
)))
(re-frame/reg-event-fx
::date-picked
(fn [cofx [_ [_ period which] date]]
{:dispatch [::range-selected (assoc-in @(re-frame/subscribe [::periods]) [period which] (str->date date standard)) nil nil]}))
(re-frame/reg-event-fx
::range-selected
(fn [{:keys [db]} [_ periods include-deltas selected]]
{:dispatch [::params-change (cond-> @(re-frame/subscribe [::params])
true (assoc
:periods periods
:include-deltas include-deltas
:selected selected)
(not (nil? include-deltas)) (assoc :include-deltas include-deltas))]
:db (assoc db ::periods periods)}))
(defn data-params->query-params [params] (defn data-params->query-params [params]
@@ -276,7 +288,6 @@
:can-submit [::can-submit-period-form] :can-submit [::can-submit-period-form]
:id ::period-form})) :id ::period-form}))
;; 877 w fremont ave suite i4
(defn change-period-body [idx which] (defn change-period-body [idx which]
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::period-form]) (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::period-form])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} change-period-form] {:keys [form-inline horizontal-field field raw-field error-notification submit-button]} change-period-form]
@@ -308,17 +319,17 @@
}] )]))) }] )])))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::period-change-submitted ::period-change-submitted
(fn [{:keys [db]} [_ idx [start end] :as z]] (fn [{:keys [db]} [_ idx which]]
{ :db (-> db (forms/stop-form ::period-form)) { :db (-> db (forms/stop-form ::period-form)
(assoc-in [::periods idx] which))
:dispatch-n [[::modal/modal-closed ] :dispatch-n [[::modal/modal-closed ]
[::change [:periods idx ] [start end]]]})) [::range-selected (assoc (::periods db) idx which) nil nil]]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::period-removed ::period-removed
(fn [{:keys [db]} [_ idx which]] (fn [{:keys [db]} [_ idx which]]
{ :db (-> db (forms/stop-form ::period-form) { :db (-> db (forms/stop-form ::period-form)
(update-in [::forms/forms ::form :data :periods] (update ::periods (fn [ps]
(fn [ps]
(->> ps (->> ps
(map vector (range) ) (map vector (range) )
(filter (fn [[i _]] (filter (fn [[i _]]
@@ -598,57 +609,52 @@
])) ]))
(re-frame/reg-sub
::can-submit
(fn [db]
true))
(def pnl-form (forms/vertical-form {:can-submit [::can-submit]
:change-event [::change]
:submit-event [::params-change]
:id ::form}))
(defn profit-and-loss-content [] (def profit-and-loss-content
(with-meta
(fn []
(let [current-client @(re-frame/subscribe [::subs/client]) (let [current-client @(re-frame/subscribe [::subs/client])
clients @(re-frame/subscribe [::subs/clients])
user @(re-frame/subscribe [::subs/user]) user @(re-frame/subscribe [::subs/user])
status @(re-frame/subscribe [::status/single ::page]) status @(re-frame/subscribe [::status/single ::page])
params @(re-frame/subscribe [::params]) params @(re-frame/subscribe [::params])
unresolved-accounts @(re-frame/subscribe [::uncategorized-accounts]) period-inputs @(re-frame/subscribe [::period-inputs])
{:keys [data report active? error id]} @(re-frame/subscribe [::forms/form ::form]) periods @(re-frame/subscribe [::periods])
{:keys [form-inline field raw-field error-notification submit-button ]} pnl-form unresolved-accounts @(re-frame/subscribe [::uncategorized-accounts]) ]
periods (:periods data)]
(when data (if-not current-client
(form-inline {} [:div
[:h1.title "Profit and Loss " ]
[:h2.title.is-4 "Please choose a client first"]]
[:div [:div
[:h1.title "Profit and Loss - " (:name current-client)] [:h1.title "Profit and Loss - " (:name current-client)]
[status/status-notification {:statuses [[::status/single ::page]]}] [status/status-notification {:statuses [[::status/single ::page]]}]
[:div.report-controls [:div.report-controls
(when-not current-client
[:<>
[:h2.title.is-4 "Entities"]
(raw-field
[typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients])
:entity->text :name
:type "typeahead-v3"
:field [:client]}])])
[:h2.title.is-4 "Range"] [:h2.title.is-4 "Range"]
[:div [:div
[:div.field.is-grouped [:div.field.is-grouped
[:div.control [:div.control
[:div.field.has-addons [:div.field.has-addons
[:div.control [:div.control
(raw-field [bind-field
[date-picker-friendly {:placeholder "End date" [date-picker {:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "End date"
:next-month-button-label ""
:next-month-label ""
:type "date" :type "date"
:field [:thirteen-periods-end]}])] :field [:thirteen-periods-end]
:subscription period-inputs
:event [::period-inputs-change]}]]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "13 periods") "is-active") {:class (when (= (:selected params) "13 periods") "is-active")
:on-click (dispatch-event :on-click (dispatch-event
[::forms/change ::form [::range-selected
[:periods] (let [today (or (some-> (:thirteen-periods-end period-inputs) (str->date standard))
(let [today (or (some-> (:thirteen-periods-end data) (str->date standard))
(local-today))] (local-today))]
(into (into
[[(t/plus (t/minus today (t/weeks (* 13 4))) [[(t/plus (t/minus today (t/weeks (* 13 4)))
@@ -658,24 +664,33 @@
(for [i (range 13)] (for [i (range 13)]
[(t/plus (t/minus today (t/weeks (* (inc i) 4))) [(t/plus (t/minus today (t/weeks (* (inc i) 4)))
(t/days 1)) (t/days 1))
(t/minus today (t/weeks (* i 4)))])))])} (t/minus today (t/weeks (* i 4)))])))
false
"13 periods"])}
"13 periods"]]]] "13 periods"]]]]
[:div.control [:div.control
[:div.field.has-addons [:div.field.has-addons
[:div.control [:div.control
(raw-field [bind-field
[date-picker-friendly {:placeholder "End date" [date-picker {:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "End date"
:next-month-button-label ""
:next-month-label ""
:type "date" :type "date"
:field [:twelve-periods-end]}]) :field [:twelve-periods-end]
] :subscription period-inputs
:event [::period-inputs-change]}]]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "13 periods") "is-active") {:class (when (= (:selected params) "13 periods") "is-active")
:on-click (dispatch-event :on-click (dispatch-event
[::forms/change ::form [::range-selected
[:periods] (let [end-date (or (some-> (:twelve-periods-end period-inputs) (str->date standard))
(let [end-date (or (some-> (:twelve-periods-end data) (str->date standard))
(local-today)) (local-today))
this-month (t/local-date (t/year end-date) this-month (t/local-date (t/year end-date)
(t/month end-date) (t/month end-date)
@@ -689,41 +704,42 @@
(for [i (range 12)] (for [i (range 12)]
[(t/minus this-month (t/months (- 11 i))) [(t/minus this-month (t/months (- 11 i)))
(t/minus (t/minus this-month (t/months (- 10 i))) (t/minus (t/minus this-month (t/months (- 10 i)))
(t/days 1))])))])} (t/days 1))])))
false
"12 months"]
)}
"12 months"]]]] "12 months"]]]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Last week") "is-active") {:class (when (= (:selected params) "Last week") "is-active")
:on-click (dispatch-event :on-click (dispatch-event
[::forms/change ::form
[:periods]
(let [last-sunday (loop [current (local-today)] (let [last-sunday (loop [current (local-today)]
(if (= 7 (t/day-of-week current)) (if (= 7 (t/day-of-week current))
current current
(recur (t/minus current (t/period :days 1)))))] (recur (t/minus current (t/period :days 1)))))]
[::range-selected
(and-last-year [(t/minus last-sunday (t/period :days 6)) last-sunday]))])} (and-last-year [(t/minus last-sunday (t/period :days 6)) last-sunday])
true
"Last week"]))}
"Last week"]] "Last week"]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Week to date") "is-active") {:class (when (= (:selected params) "Week to date") "is-active")
:on-click (dispatch-event :on-click (dispatch-event [::range-selected
[::forms/change ::form
[:periods]
(and-last-year [(loop [current (local-today)] (and-last-year [(loop [current (local-today)]
(if (= 1 (t/day-of-week current)) (if (= 1 (t/day-of-week current))
current current
(recur (t/minus current (t/period :days 1))))) (recur (t/minus current (t/period :days 1)))))
(local-today)])])} (local-today)])
true
"Week to date"])}
"Week to date"]] "Week to date"]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Last Month") "is-active") {:class (when (= (:selected params) "Last Month") "is-active")
:on-click (dispatch-event :on-click (dispatch-event [::range-selected
[::forms/change ::form
[:periods]
(and-last-year [(t/minus (t/local-date (t/year (local-today)) (and-last-year [(t/minus (t/local-date (t/year (local-today))
(t/month (local-today)) (t/month (local-today))
1) 1)
@@ -731,67 +747,86 @@
(t/minus (t/local-date (t/year (local-today)) (t/minus (t/local-date (t/year (local-today))
(t/month (local-today)) (t/month (local-today))
1) 1)
(t/period :days 1))])])} (t/period :days 1))])
true
"Last Month"])}
"Last Month"]] "Last Month"]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Month to date") "is-active") {:class (when (= (:selected params) "Month to date") "is-active")
:on-click (dispatch-event :on-click (dispatch-event [::range-selected
[::forms/change ::form
[:periods]
(and-last-year [(t/local-date (t/year (local-today)) (and-last-year [(t/local-date (t/year (local-today))
(t/month (local-today)) (t/month (local-today))
1) 1)
(local-today)])] (local-today)])
)} true
"Month to date"])}
"Month to date"]] "Month to date"]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Year to date") "is-active") {:class (when (= (:selected params) "Year to date") "is-active")
:on-click (dispatch-event :on-click (dispatch-event [::range-selected
[::forms/change ::form
[:periods]
(and-last-year [(t/local-date (t/year (local-today)) 1 1) (and-last-year [(t/local-date (t/year (local-today)) 1 1)
(local-today)])])} (local-today)])
true
"Year to date"])}
"Year to date"]] "Year to date"]]
[:div.control [:div.control
[:a.button [:a.button
{:class (when (= (:selected params) "Full year") "is-active") {:class (when (= (:selected params) "Full year") "is-active")
:on-click (dispatch-event :on-click (dispatch-event [::range-selected
[::forms/change ::form
[:periods]
(and-last-year [(t/plus (t/minus (local-today) (t/period :years 1)) (and-last-year [(t/plus (t/minus (local-today) (t/period :years 1))
(t/period :days 1)) (t/period :days 1))
(local-today)])])} (local-today)])
true
"Full year"])}
"Full year"]]]] "Full year"]]]]
[:div [:div
[:div.field [:div.field
[:label.checkbox [:label.checkbox
(raw-field [bind-field
[:input {:type "checkbox" [:input {:type "checkbox"
:field [:show-advanced?]}]) :field [:show-advanced?]
:event [::period-inputs-change]
:subscription period-inputs}]]
" Show Advanced"]]] " Show Advanced"]]]
(when (:show-advanced? data) (when (:show-advanced? period-inputs)
(doall
(for [[_ i] (map vector periods (range))] (for [[_ i] (map vector periods (range))]
^{:key i} ^{:key i}
[:div.field.is-grouped [:div.field.is-grouped
[:div.control [:div.control
[:p.help "From"] [:p.help "From"]
(raw-field [bind-field
[date-picker-friendly {:type "date" [date-picker {:class-name "input"
:field [:periods i 0]}])] :class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "mm/dd/yyyy"
:next-month-button-label ""
:next-month-label ""
:type "date"
:field [:periods i 0]
:event [::date-picked]
:popper-props (clj->js {:placement "right"})
:subscription params}]]]
[:div.control [:div.control
[:p.help "To"] [:p.help "To"]
(raw-field [bind-field
[date-picker-friendly {:type "date" [date-picker {:class-name "input"
:field [:periods i 1]}])]]))) :class "input"
(submit-button "Run")] :format-week-number (fn [] "")
:previous-month-button-label ""
:placeholder "mm/dd/yyyy"
:next-month-button-label ""
:next-month-label ""
:type "date"
:field [:periods i 1]
:event [::date-picked]
:popper-props (clj->js {:placement "right"})
:subscription params}]]]]))]
[status/big-loader status] [status/big-loader status]
(when (and (not= :loading (:state status)) (when (not= :loading (:state status))
report)
[:div [:div
(when (seq unresolved-accounts) (when (seq unresolved-accounts)
[:div.notification.is-warning.is-light [:div.notification.is-warning.is-light
@@ -814,7 +849,9 @@
[:<> [:<>
(for [location @(re-frame/subscribe [::locations])] (for [location @(re-frame/subscribe [::locations])]
^{:key location} ^{:key location}
[location-rows location])]]]])])))) [location-rows location]
)]]]])])))
{:component-will-mount #(re-frame/dispatch [::mounted]) }))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted-pnl ::unmounted-pnl
@@ -825,18 +862,9 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::mounted-pnl ::mounted-pnl
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
(let [qp (query-params)] {::track/register {:id ::ledger-params
{:db (forms/start-form db ::form {:periods (->> qp
:periods
(mapv (fn [period]
(mapv
(fn [dt]
(str->date dt standard))
period))))
:client (:client qp)})
::track/register {:id ::ledger-params
:subscription [::data-page/params ::ledger] :subscription [::data-page/params ::ledger]
:event-fn (fn [params] [::ledger-params-change params])}}))) :event-fn (fn [params] [::ledger-params-change params])}}))
(defn ledger-list [_ ] (defn ledger-list [_ ]
[:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::ledger-list-closing])}] [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::ledger-list-closing])}]

View File

@@ -435,17 +435,6 @@
(do (do
(reagent/adapt-react-class (.-default react-datepicker)))) (reagent/adapt-react-class (.-default react-datepicker))))
(defn date-picker-friendly [params]
[date-picker (assoc params
:class-name "input"
:class "input"
:format-week-number (fn [] "")
:previous-month-button-label ""
:next-month-button-label ""
:next-month-label ""
:type "date")])
(defn local-now [] (defn local-now []
(t/to-default-time-zone (t/now))) (t/to-default-time-zone (t/now)))