From 4018d9aa8d482b961920aa1a4e3c6e61db843a37 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 13 Apr 2019 22:31:13 -0700 Subject: [PATCH] Fixing the math. --- src/clj/auto_ap/graphql/ledger.clj | 114 +++++++++++++---------------- src/clj/auto_ap/ledger.clj | 10 ++- 2 files changed, 58 insertions(+), 66 deletions(-) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 8d50d26c..6d182a4c 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -32,44 +32,62 @@ (= :account-type/expense (:db/ident (:account/type account)))) +(defn roll-up [results] + (->> results + (mapcat :journal-entry/line-items) + (group-by :journal-entry-line/account) + (reduce-kv (fn [result account line-items] + (assoc result + account + {:name (or (:bank-account/name account) (:account/name account)) + :amount (reduce + 0 (map + (fn [line-item] + (cond + (and (credit-account? account) (:journal-entry-line/debit line-item)) + (- (:journal-entry-line/debit line-item)) + + (and (credit-account? account) (:journal-entry-line/credit line-item)) + (:journal-entry-line/credit line-item) + + (and (debit-account? account) (:journal-entry-line/debit line-item)) + (:journal-entry-line/debit line-item) + + (and (debit-account? account) (:journal-entry-line/credit line-item)) + (- (:journal-entry-line/credit line-item)) + + :else + 0)) + line-items))})) + {}))) + (defn get-balance-sheet [context args value] (let [args (assoc args :id (:id context)) [results] (l/get-graphql {:client-id (:client_id args) :date-before (coerce/to-date (:date args)) :count Integer/MAX_VALUE}) - accounts (->> results - (mapcat :journal-entry/line-items) - (group-by :journal-entry-line/account) - (reduce-kv - (fn [result account line-items] - (if (expense-account? account) - result - (update-in result [(if (credit-account? account) - "Liabilities" - "Assets") - (if (credit-account? account) - "Accounts Payable" - "1100 Cash and Bank Accounts" ) - ] - conj - {:name (or (:bank-account/name account) (:account/name account)) - :amount (reduce + 0 (map - (fn [line-item] - (cond - (and (credit-account? account) (:journal-entry-line/debit line-item)) - (- (:journal-entry-line/debit line-item)) + accounts (roll-up results) + accounts (reduce-kv + + (fn [result account line-item] + (update-in result [(cond + (expense-account? account) + "Expenses" - (and (credit-account? account) (:journal-entry-line/credit line-item)) - (:journal-entry-line/credit line-item) + (credit-account? account) + "Liabilities" + (debit-account? account) + "Assets") - (and (debit-account? account) (:journal-entry-line/debit line-item)) - (:journal-entry-line/debit line-item) - - (and (debit-account? account) (:journal-entry-line/credit line-item)) - (- (:journal-entry-line/credit line-item)))) - line-items))})) - - ) {}))] + (if (credit-account? account) + "Accounts Payable" + "1100 Cash and Bank Accounts" ) + ] + conj + line-item + )) + {} + accounts) + ] (->graphql {:balance-sheet-groupings (-> [] @@ -95,42 +113,10 @@ [ending-results] (l/get-graphql {:client-id (:client_id args) :date-before (coerce/to-date (:to_date args)) :count Integer/MAX_VALUE}) - _ (println (:from_date args) (:to_date args)) - - roll-up (fn [results] - (->> results - (mapcat :journal-entry/line-items) - (group-by :journal-entry-line/account) - (reduce-kv (fn [result account line-items] - (assoc result - account - {:name (or (:bank-account/name account) (:account/name account)) - :amount (reduce + 0 (map - (fn [line-item] - (cond - (and (credit-account? account) (:journal-entry-line/debit line-item)) - (- (:journal-entry-line/debit line-item)) - - (and (credit-account? account) (:journal-entry-line/credit line-item)) - (:journal-entry-line/credit line-item) - - (and (debit-account? account) (:journal-entry-line/debit line-item)) - (:journal-entry-line/debit line-item) - - (and (debit-account? account) (:journal-entry-line/credit line-item)) - (- (:journal-entry-line/credit line-item)) - - :else - 0)) - line-items))})) - {}) - )) starting-accounts (roll-up starting-results) ending-accounts (roll-up ending-results) - accounts (into - starting-accounts - ending-accounts) + accounts (reduce-kv (fn [result account line-item] diff --git a/src/clj/auto_ap/ledger.clj b/src/clj/auto_ap/ledger.clj index 8d7b3e7b..ad4791c2 100644 --- a/src/clj/auto_ap/ledger.clj +++ b/src/clj/auto_ap/ledger.clj @@ -68,11 +68,17 @@ :journal-entry/line-items [(remove-nils{:journal-entry-line/account (:db/id (:transaction/account entity)) :journal-entry-line/location "HQ" - :journal-entry-line/debit (Math/abs (:transaction/amount entity))}) + :journal-entry-line/debit (when (< (:transaction/amount entity) 0.0) + (Math/abs (:transaction/amount entity))) + :journal-entry-line/credit (when (>= (:transaction/amount entity) 0.0) + (Math/abs (:transaction/amount entity)))}) (remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity)) :journal-entry-line/location "HQ" - :journal-entry-line/credit (Math/abs (:transaction/amount entity))}) + :journal-entry-line/credit (when (< (:transaction/amount entity) 0.0) + (Math/abs (:transaction/amount entity))) + :journal-entry-line/debit (when (>= (:transaction/amount entity) 0.0) + (Math/abs (:transaction/amount entity)))}) ] :journal-entry/cleared true}))))