Fixing the math.

This commit is contained in:
Bryce Covert
2019-04-13 22:31:13 -07:00
parent 7f6df3865c
commit 4018d9aa8d
2 changed files with 58 additions and 66 deletions

View File

@@ -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]

View File

@@ -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}))))