From 5293b7f3e692c8f42fb592d50a35b118fa45a910 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 3 Apr 2023 12:34:50 -0700 Subject: [PATCH] (cloud) leaving an artificat for speending up p&l later --- src/clj/auto_ap/graphql/ledger.clj | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 4611cd31..9cfacb58 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -189,6 +189,86 @@ #(roll-up-until (lookup-account %) (all-ledger-entries %) (coerce/to-date end) (coerce/to-date start) ) client-ids)})))}))) + +;; profit and loss based off of index +#_(defn get-profit-and-loss [context args _] + (let [client-id (:client_id args) + client-ids (or (some-> client-id vector) + (filter identity (:client_ids args))) + _ (when (not (seq client-ids)) + (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)) + _ (when (and (:include_deltas args) + (:column_per_location args)) + (throw (ex-info "Please select one of 'Include deltas' or 'Column per location'" {:validation-error "Please select one of 'Include deltas' or 'Column per location'"}))) + db (dc/db conn) + all-used-account-locations (dc/q '[:find ?c ?a ?l + :in $ [?c ...] + :where + (or-join [?c ?a ?l] + (and + [?a :account/numeric-code] + (not [?a :account/location]) + [?c :client/locations ?l]) + (and + [?a :account/numeric-code] + [?a :account/location ?l] + [?c :client/locations ?l]) + (and + [?c :client/bank-accounts ?a] + [(ground "A") ?l]))] + (dc/db conn) + client-ids) + lookup-account (->> client-ids + (map (fn [client-id] + [client-id (build-account-lookup client-id)])) + (into {}))] + (->graphql + {:periods + (->> (:periods args) + (mapv (fn [{:keys [start end]}] + (let [start (coerce/to-date start) + end (coerce/to-date end)] + {:accounts (mapcat + (fn [[c a l]] + (let [start-point (->> (dc/index-pull db + {:index :avet + :selector [:db/id :journal-entry-line/running-balance :journal-entry-line/client+account+location+date] + :start [:journal-entry-line/client+account+location+date [c a l start]] + :reverse true + :limit 1}) + (take-while (fn [result] + (= [c a l] + (take 3 (:journal-entry-line/client+account+location+date result))))) + (drop-while (fn [{[_ _ _ date] :journal-entry-line/client+account+location+date}] + (>= (compare date start) 0))) + first) + end-point (->> (dc/index-pull db + {:index :avet + :selector [:db/id :journal-entry-line/running-balance :journal-entry-line/client+account+location+date] + :start [:journal-entry-line/client+account+location+date [c a l end]] + :reverse true + :limit 1}) + (take-while (fn [result] + (= [c a l] + (take 3 (:journal-entry-line/client+account+location+date result))))) + (take 1) + (drop-while (fn [{[_ _ _ date] :journal-entry-line/client+account+location+date}] + (>= (compare date end) 0))) + first)] + (when end-point + [(merge {:id (str a "-" l) + :location (or l "") + :count 0 + :debits 0 + :credits 0 + :amount (- (or (:journal-entry-line/running-balance end-point) 0.0) + (or (:journal-entry-line/running-balance start-point) 0.0)) + } + ((lookup-account c) a))]))) + all-used-account-locations)}))))}))) + (defn profit-and-loss-pdf [context args value] (let [data (get-profit-and-loss context args value) result (print-pnl (:id context) args data)]