From 2d3b7967935a9476ef891ab6b688fe5ed0070b35 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 10 Dec 2022 13:49:01 -0800 Subject: [PATCH] Revert "Trying one fix" This reverts commit 5e260f2f9d173091409b5af3d0e8612dad27c7f7. --- src/clj/auto_ap/datomic/ledger.clj | 25 ++-- src/clj/auto_ap/graphql/ledger.clj | 8 +- src/cljc/auto_ap/ledger/reports.cljc | 113 ++++++++++++------ .../views/pages/ledger/balance_sheet.cljs | 8 +- .../views/pages/ledger/cash_flows.cljs | 8 +- .../views/pages/ledger/profit_and_loss.cljs | 8 +- 6 files changed, 110 insertions(+), 60 deletions(-) diff --git a/src/clj/auto_ap/datomic/ledger.clj b/src/clj/auto_ap/datomic/ledger.clj index 9c962d30..894df445 100644 --- a/src/clj/auto_ap/datomic/ledger.clj +++ b/src/clj/auto_ap/datomic/ledger.clj @@ -53,22 +53,25 @@ '[(<= ?date ?end-date)]]} :args [(c/to-date (:end (:date-range args)))]}) - (or (seq (:numeric-code args)) + (or (:from-numeric-code args) + (:to-numeric-code args) (:bank-account-id args) (not-empty (:location args))) (merge-query {:query {:where ['[?e :journal-entry/line-items ?li]]}}) - (seq (:numeric-code args)) - (merge-query {:query {:in ['[[?from-numeric-code ?to-numeric-code]]] + (or (:from-numeric-code args) + (:to-numeric-code args)) + (merge-query {:query {:in [] :where ['[?li :journal-entry-line/account ?a] '(or-join [?a ?c] [?a :account/numeric-code ?c] - [?a :bank-account/numeric-code ?c]) - '[(>= ?c ?from-numeric-code)] - '[(<= ?c ?to-numeric-code)]]} - :args [(vec (for [{:keys [from to]} (:numeric-code args)] - [(or from 0) (or to 99999)]))]}) + [?a :bank-account/numeric-code ?c])]} + :args []}) + (:from-numeric-code args) + (merge-query {:query {:in ['?from-numeric-code] + :where ['[(>= ?c ?from-numeric-code)]]} + :args [(:from-numeric-code args)]}) (:amount-gte args) (merge-query {:query {:in ['?amount-gte] @@ -93,6 +96,11 @@ '[?li2 :journal-entry-line/account ?a2]]} :args [(:account-id args)]}) + (:to-numeric-code args) + (merge-query {:query {:in ['?to-numeric-code] + :where ['[(<= ?c ?to-numeric-code)]]} + :args [(:to-numeric-code args)]}) + (not-empty (:location args)) (merge-query {:query {:in ['?location] :where ['[?li :journal-entry-line/location ?location]]} @@ -114,7 +122,6 @@ true (merge-query {:query {:find ['?sort-default '?e] :where ['[?e :journal-entry/date ?sort-default]]}}))] - (clojure.pprint/pprint query) (->> query (d/query) (apply-sort-3 (update args :sort conj {:sort-key "default-2" :asc true})) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 11475a19..6f7a041f 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -754,10 +754,7 @@ :resolve :mutation/delete-external-ledger}}) (def input-objects - {:numeric_code_range - {:fields {:from {:type 'Int} - :to {:type 'Int}}} - :ledger_filters + {:ledger_filters {:fields {:client_id {:type :id} :vendor_id {:type :id} :account_id {:type :id} @@ -766,7 +763,8 @@ :bank_account_id {:type :id} :date_range {:type :date_range} :location {:type 'String} - :numeric_code {:type '(list :numeric_code_range)} + :from_numeric_code {:type 'Int} + :to_numeric_code {:type 'Int} :start {:type 'Int} :per_page {:type 'Int} :only_external {:type 'Boolean} diff --git a/src/cljc/auto_ap/ledger/reports.cljc b/src/cljc/auto_ap/ledger/reports.cljc index 50c0e290..6c6736dd 100644 --- a/src/cljc/auto_ap/ledger/reports.cljc +++ b/src/cljc/auto_ap/ledger/reports.cljc @@ -21,6 +21,20 @@ :cljs (au/date->str d au/pretty))) +(def ranges + {:assets [11000 19999] + :liabilities [20100 28999] + :equities [30000 39999] + :sales [40000 49999] + :cogs [50000 59999] + :payroll [60000 69999] + :controllable [70000 79999] + :fixed-overhead [80000 89999] + :ownership-controllable [90000 99999] + :operating-activities [10000 99999] + :investment-activities [10000 99999] + :financing-activities [10000 99999] + :bottom-line [10000 99999]}) (def groupings {:assets [["1100 Cash and Bank Accounts" 11000 11999] @@ -79,24 +93,18 @@ :investment-activities [["14000-18000 Investments" 14000 17999]] :financing-activities [["28000-35000 Financing Activities" 28000 34999] - ["35000-40000 Other" 35000 39999]] + ["35001-40000 Other" 35001 39999]] :bottom-line [["11000-12000 Bottom Line" 11000 11999]]}) - -(def flat-categories - (for [[category groups] groupings - [_ start end] groups] - [category start end])) - (defn in-range? [code] (if code (reduce - (fn [acc [_ start end]] + (fn [acc [start end]] (if (<= start code end) (reduced true) acc)) false - flat-categories) + (vals ranges)) false)) (defn client-locations [pnl-data] @@ -127,8 +135,8 @@ (reduce (fnil + 0.0) 0.0 (map :amount (:data pnl-data)))) (defn best-category [a] - (->> flat-categories - (filter (fn [[category start end]] + (->> ranges + (filter (fn [[_ [start end]]] (and (:numeric-code a) (<= start (:numeric-code a) end)))) first @@ -156,29 +164,19 @@ data))) (update :filters (fn [f] (assoc f - :numeric-code [{:from from - :to to}]))))) + :from-numeric-code from + :to-numeric-code to))))) -(defn account-belongs-in-category? [numeric-code category] - (->> (groupings category) - (some (fn [[_ from to]] - (<= from (or numeric-code 0) to))))) - -;; TODO make click-through work for multiple ranges :fliters (defn filter-categories [pnl-data categories] - (-> pnl-data - (update :data - (fn [data] - (for [account data - :when (some #(account-belongs-in-category? (:numeric-code account) %) categories)] - account))) - (update :filters - (fn [f] - (assoc f :numeric-code - (for [category categories - [_ from to] (groupings category)] - {:from from - :to to})))))) + (if (= 1 (count categories)) + (let [[from to] (ranges (first categories))] + (-> pnl-data + (filter-numeric-code from to))) + (-> pnl-data + (update :data (fn [data] + (mapcat identity + ((apply juxt categories) + (group-by best-category data)))))))) (defn filter-period [pnl-data period] (-> pnl-data @@ -218,11 +216,10 @@ :bold true}] (map (fn [p] - (println "FILTERS" (:filters p)) (merge {:format :dollar :value (aggregate-accounts p) - :filters (when (:numeric-code (:filters p)) ;; don't allow filtering when you don't at least filter numeric codes + :filters (when (:from-numeric-code (:filters p)) ;; don't allow filtering when you don't at least filter numeric codes (:filters p))} (:cell-args p) cell-args)) @@ -476,12 +473,52 @@ (into (detail-rows pnl-datas :bottom-line - (str prefix " Bottom Line")))) - + (str prefix " Bottom Line"))) + #_(into (detail-rows pnl-datas + :cogs + (str prefix " COGS"))) + #_(into (detail-rows + pnl-datas + :payroll + (str prefix " Payroll"))) + #_(conj (subtotal-by-column-row (map #(filter-categories % [:payroll :cogs]) + pnl-datas) + (str prefix " Prime Costs"))) + #_(conj (subtotal-by-column-row (map #(-> % + (filter-categories [:sales :payroll :cogs]) + (negate #{:payroll :cogs})) + pnl-datas) + (str prefix " Gross Profits"))) + #_(into (detail-rows + pnl-datas + :controllable + (str prefix " Controllable Expenses"))) + #_(into (detail-rows + pnl-datas + :fixed-overhead + (str prefix " Fixed Overhead"))) + #_(into (detail-rows + pnl-datas + :ownership-controllable + (str prefix " Ownership Controllable"))) + #_(conj (subtotal-by-column-row (map #(filter-categories % [:controllable :fixed-overhead :ownership-controllable]) pnl-datas) + (str prefix " Overhead"))) + #_(conj (subtotal-by-column-row (map #(-> % + (filter-categories [:sales :cogs :payroll :controllable :fixed-overhead :ownership-controllable]) + (negate #{:cogs :payroll :controllable :fixed-overhead :ownership-controllable})) + pnl-datas) + (str prefix " Net Income")))) + #_table #_(if (seq client-datas) + (conj table (subtotal-by-column-row (map #(-> % + (filter-categories [:sales :cogs :payroll :controllable :fixed-overhead :ownership-controllable]) + (negate #{:cogs :payroll :controllable :fixed-overhead :ownership-controllable})) + client-datas) + "All Location Net Income")) + table) percent-of-sales (calc-percent-of-sales table pnl-datas) deltas (into [] (calc-deltas table))] {:header (headers pnl-datas title) - :rows table})) + :rows (combine-tables pnl-datas table percent-of-sales deltas)})) (defn warning-message [pnl-data] (let [errors (->> pnl-data @@ -562,6 +599,7 @@ (for [[client-id location] (client-locations pnl-data)] (-> pnl-data (filter-client client-id) + (filter-location location) (filter-period period) (zebra i))) [(-> pnl-data @@ -573,6 +611,7 @@ (cash-flows-location-detail-table (for [[period i] (map vector (-> pnl-data :args :periods ) (range))] (-> pnl-data (filter-client client-id) + (filter-location "A") (filter-period period) (zebra i))) (str (-> pnl-data :clients-by-id (get client-id)) " Detail") diff --git a/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs b/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs index 4670e7d0..2a4da002 100644 --- a/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs @@ -30,7 +30,8 @@ :per-page (:per-page params) :vendor-id (:id (:vendor params)) :client-id (:client-id params ) - :numeric-code (:numeric-code params) + :from-numeric-code (:from-numeric-code params) + :to-numeric-code (:to-numeric-code params) :date-range (:date-range params)})) @@ -119,10 +120,11 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-fx ::investigate-clicked - (fn [{:keys [db]} [_ {:keys [numeric-code date-range]}]] + (fn [{:keys [db]} [_ {:keys [from-numeric-code to-numeric-code date-range]}]] {:db (-> db (assoc ::ledger-list-active? true)) :dispatch [::data-page/additional-params-changed ::ledger {:client-id (:id @(re-frame/subscribe [::subs/client])) - :numeric-code numeric-code + :from-numeric-code from-numeric-code + :to-numeric-code to-numeric-code :date-range {:start "2000-01-01" :end (date->str date-range standard)}}]})) diff --git a/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs b/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs index 3c7431d1..569f4ccc 100644 --- a/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs @@ -170,7 +170,8 @@ NOTE: Please review the transactions we may have question for you here: https:// :per-page (:per-page params) :vendor-id (:id (:vendor params)) :client-id (:client-id params) - :numeric-code (:numeric-code params) + :from-numeric-code (:from-numeric-code params) + :to-numeric-code (:to-numeric-code params) :location (:location params) :date-range (:date-range params)})) @@ -206,11 +207,12 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-fx ::investigate-clicked - (fn [{:keys [db]} [_ {:keys [location numeric-code client-id] + (fn [{:keys [db]} [_ {:keys [location from-numeric-code to-numeric-code client-id] {:keys [start end]} :date-range}]] {:db (-> db (assoc ::ledger-list-active? true)) :dispatch [::data-page/additional-params-changed ::ledger {:client-id client-id - :numeric-code numeric-code + :from-numeric-code from-numeric-code + :to-numeric-code to-numeric-code :location location :date-range {:start (date->str start standard) :end (date->str end standard)}}]})) diff --git a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs index 4f79666c..c8578b20 100644 --- a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs @@ -170,7 +170,8 @@ NOTE: Please review the transactions we may have question for you here: https:// :per-page (:per-page params) :vendor-id (:id (:vendor params)) :client-id (:client-id params) - :numeric-code (:numeric-code params) + :from-numeric-code (:from-numeric-code params) + :to-numeric-code (:to-numeric-code params) :location (:location params) :date-range (:date-range params)})) @@ -206,11 +207,12 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-fx ::investigate-clicked - (fn [{:keys [db]} [_ {:keys [location numeric-code client-id] + (fn [{:keys [db]} [_ {:keys [location from-numeric-code to-numeric-code client-id] {:keys [start end]} :date-range}]] {:db (-> db (assoc ::ledger-list-active? true)) :dispatch [::data-page/additional-params-changed ::ledger {:client-id client-id - :numeric-code numeric-code + :from-numeric-code from-numeric-code + :to-numeric-code to-numeric-code :location location :date-range {:start (date->str start standard) :end (date->str end standard)}}]}))