From 2d52c2b6a7a2bd399d050833d7ed055865a6fd83 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 18 Oct 2021 06:34:23 -0700 Subject: [PATCH] Revert "Revert "pnl"" This reverts commit 53fba1483154e11a6e590ad8431749cfbc772780. --- src/clj/auto_ap/graphql.clj | 1 + src/clj/auto_ap/graphql/ledger.clj | 32 ++++++++++++------- .../views/pages/ledger/profit_and_loss.cljs | 8 +++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index f8025f6c..6fe35cdb 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -153,6 +153,7 @@ {:fields {:id {:type 'String} :amount {:type 'String} :location {:type 'String} + :client_id {:type :id} :count {:type 'Int} :numeric_code {:type 'Int} :account_type {:type :account_type} diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 12a4e2ad..7786f3a2 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -96,13 +96,14 @@ (let [account (lookup-account account-id) account-type (:account_type account)] -(conj acc (merge {:id (str account-id "-" location) :location (or location "") :count count + (conj acc (merge {:id (str account-id "-" location) + :location (or location "") + :count count :amount (if account-type (if (#{:account-type/asset :account-type/dividend :account-type/expense} account-type) (- debit credit) - (- credit debit) - ) + (- credit debit)) 0.0)} account))) ) @@ -142,7 +143,8 @@ :bank-account-type/credit :account-type/liability} (:db/ident (:bank-account/type (bank-accounts a))))) :numeric_code (or (:account/numeric-code (accounts a)) - (:bank-account/numeric-code (bank-accounts a)))}))) + (:bank-account/numeric-code (bank-accounts a))) + :client_id client-id}))) (defn full-ledger-for-client [client-id] (->> (d/query @@ -189,14 +191,22 @@ (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))] + all-ledger-entries (->> client-ids + (map (fn [client-id] + [client-id (full-ledger-for-client client-id)])) + (into {})) + lookup-account (->> client-ids + (map (fn [client-id] + [client-id (build-account-lookup client-id)])) + (into {})) + client-id (first client-ids)] (->graphql - {:periods (reduce (fn [acc {:keys [start end]}] - (conj acc - {:accounts (roll-up-until lookup-account all-ledger-entries (coerce/to-date end) (coerce/to-date start) )})) - [] - (:periods args))}))) + {:periods + (->> (:periods args) + (mapv (fn [{:keys [start end]}] + {:accounts (mapcat + #(roll-up-until (lookup-account %) (all-ledger-entries %) (coerce/to-date end) (coerce/to-date start) ) + client-ids)})))}))) (defn assoc-error [f] 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 79e8610f..23b53ae3 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 @@ -174,7 +174,7 @@ vector)) :periods (mapv (fn [[start end] ] {:start (date->str start standard) :end (date->str end standard)} ) (:periods (:data db)))} - [[:periods [[:accounts [:name :amount :account-type :id :count :numeric-code :location]]]]]]]} + [[:periods [[:accounts [:name :amount :client_id :account-type :id :count :numeric-code :location]]]]]]]} :on-success [::received]} :set-uri-params {:periods (mapv (fn [[start end title]] [(date->str start standard) @@ -424,7 +424,10 @@ {:key (str "between-" i)}))])) +;; reimplement include-deltas. Are numbers even correct? support actually using more than 1 client. + (defn grouping [{:keys [header type groupings location periods all-accounts]}] + (let [params @(re-frame/subscribe [::params])] [:<> (doall @@ -833,7 +836,8 @@ (fn [dt] (str->date dt standard)) period)))) - :client (:client qp)}) + :client (:client qp) + :include-deltas true}) ::track/register {:id ::ledger-params :subscription [::data-page/params ::ledger] :event-fn (fn [params] [::ledger-params-change params])}})))