diff --git a/src/clj/auto_ap/datomic/ledger.clj b/src/clj/auto_ap/datomic/ledger.clj index 894df445..9c962d30 100644 --- a/src/clj/auto_ap/datomic/ledger.clj +++ b/src/clj/auto_ap/datomic/ledger.clj @@ -53,25 +53,22 @@ '[(<= ?date ?end-date)]]} :args [(c/to-date (:end (:date-range args)))]}) - (or (:from-numeric-code args) - (:to-numeric-code args) + (or (seq (:numeric-code args)) (:bank-account-id args) (not-empty (:location args))) (merge-query {:query {:where ['[?e :journal-entry/line-items ?li]]}}) - (or (:from-numeric-code args) - (:to-numeric-code args)) - (merge-query {:query {:in [] + (seq (:numeric-code args)) + (merge-query {:query {:in ['[[?from-numeric-code ?to-numeric-code]]] :where ['[?li :journal-entry-line/account ?a] '(or-join [?a ?c] [?a :account/numeric-code ?c] - [?a :bank-account/numeric-code ?c])]} - :args []}) + [?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)]))]}) - (: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] @@ -96,11 +93,6 @@ '[?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]]} @@ -122,6 +114,7 @@ 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 6f7a041f..11475a19 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -754,7 +754,10 @@ :resolve :mutation/delete-external-ledger}}) (def input-objects - {:ledger_filters + {:numeric_code_range + {:fields {:from {:type 'Int} + :to {:type 'Int}}} + :ledger_filters {:fields {:client_id {:type :id} :vendor_id {:type :id} :account_id {:type :id} @@ -763,8 +766,7 @@ :bank_account_id {:type :id} :date_range {:type :date_range} :location {:type 'String} - :from_numeric_code {:type 'Int} - :to_numeric_code {:type 'Int} + :numeric_code {:type '(list :numeric_code_range)} :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 6c6736dd..50c0e290 100644 --- a/src/cljc/auto_ap/ledger/reports.cljc +++ b/src/cljc/auto_ap/ledger/reports.cljc @@ -21,20 +21,6 @@ :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] @@ -93,18 +79,24 @@ :investment-activities [["14000-18000 Investments" 14000 17999]] :financing-activities [["28000-35000 Financing Activities" 28000 34999] - ["35001-40000 Other" 35001 39999]] + ["35000-40000 Other" 35000 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 - (vals ranges)) + flat-categories) false)) (defn client-locations [pnl-data] @@ -135,8 +127,8 @@ (reduce (fnil + 0.0) 0.0 (map :amount (:data pnl-data)))) (defn best-category [a] - (->> ranges - (filter (fn [[_ [start end]]] + (->> flat-categories + (filter (fn [[category start end]] (and (:numeric-code a) (<= start (:numeric-code a) end)))) first @@ -164,19 +156,29 @@ data))) (update :filters (fn [f] (assoc f - :from-numeric-code from - :to-numeric-code to))))) + :numeric-code [{:from from + :to 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] - (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)))))))) + (-> 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})))))) (defn filter-period [pnl-data period] (-> pnl-data @@ -216,10 +218,11 @@ :bold true}] (map (fn [p] + (println "FILTERS" (:filters p)) (merge {:format :dollar :value (aggregate-accounts p) - :filters (when (:from-numeric-code (:filters p)) ;; don't allow filtering when you don't at least filter numeric codes + :filters (when (:numeric-code (:filters p)) ;; don't allow filtering when you don't at least filter numeric codes (:filters p))} (:cell-args p) cell-args)) @@ -473,52 +476,12 @@ (into (detail-rows pnl-datas :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) + (str prefix " Bottom Line")))) + percent-of-sales (calc-percent-of-sales table pnl-datas) deltas (into [] (calc-deltas table))] {:header (headers pnl-datas title) - :rows (combine-tables pnl-datas table percent-of-sales deltas)})) + :rows table})) (defn warning-message [pnl-data] (let [errors (->> pnl-data @@ -599,7 +562,6 @@ (for [[client-id location] (client-locations pnl-data)] (-> pnl-data (filter-client client-id) - (filter-location location) (filter-period period) (zebra i))) [(-> pnl-data @@ -611,7 +573,6 @@ (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 2a4da002..4670e7d0 100644 --- a/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs @@ -30,8 +30,7 @@ :per-page (:per-page params) :vendor-id (:id (:vendor params)) :client-id (:client-id params ) - :from-numeric-code (:from-numeric-code params) - :to-numeric-code (:to-numeric-code params) + :numeric-code (:numeric-code params) :date-range (:date-range params)})) @@ -120,11 +119,10 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-fx ::investigate-clicked - (fn [{:keys [db]} [_ {:keys [from-numeric-code to-numeric-code date-range]}]] + (fn [{:keys [db]} [_ {:keys [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])) - :from-numeric-code from-numeric-code - :to-numeric-code to-numeric-code + :numeric-code 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 569f4ccc..3c7431d1 100644 --- a/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/cash_flows.cljs @@ -170,8 +170,7 @@ 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) - :from-numeric-code (:from-numeric-code params) - :to-numeric-code (:to-numeric-code params) + :numeric-code (:numeric-code params) :location (:location params) :date-range (:date-range params)})) @@ -207,12 +206,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 [location from-numeric-code to-numeric-code client-id] + (fn [{:keys [db]} [_ {:keys [location 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 - :from-numeric-code from-numeric-code - :to-numeric-code to-numeric-code + :numeric-code 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 c8578b20..4f79666c 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,8 +170,7 @@ 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) - :from-numeric-code (:from-numeric-code params) - :to-numeric-code (:to-numeric-code params) + :numeric-code (:numeric-code params) :location (:location params) :date-range (:date-range params)})) @@ -207,12 +206,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 [location from-numeric-code to-numeric-code client-id] + (fn [{:keys [db]} [_ {:keys [location 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 - :from-numeric-code from-numeric-code - :to-numeric-code to-numeric-code + :numeric-code numeric-code :location location :date-range {:start (date->str start standard) :end (date->str end standard)}}]}))