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)))) (= :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] (defn get-balance-sheet [context args value]
(let [args (assoc args :id (:id context)) (let [args (assoc args :id (:id context))
[results] (l/get-graphql {:client-id (:client_id args) [results] (l/get-graphql {:client-id (:client_id args)
:date-before (coerce/to-date (:date args)) :date-before (coerce/to-date (:date args))
:count Integer/MAX_VALUE}) :count Integer/MAX_VALUE})
accounts (->> results accounts (roll-up results)
(mapcat :journal-entry/line-items) accounts (reduce-kv
(group-by :journal-entry-line/account)
(reduce-kv (fn [result account line-item]
(fn [result account line-items] (update-in result [(cond
(if (expense-account? account) (expense-account? account)
result "Expenses"
(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))
(and (credit-account? account) (:journal-entry-line/credit line-item)) (credit-account? account)
(:journal-entry-line/credit line-item) "Liabilities"
(debit-account? account)
"Assets")
(and (debit-account? account) (:journal-entry-line/debit line-item)) (if (credit-account? account)
(:journal-entry-line/debit line-item) "Accounts Payable"
"1100 Cash and Bank Accounts" )
(and (debit-account? account) (:journal-entry-line/credit line-item)) ]
(- (:journal-entry-line/credit line-item)))) conj
line-items))})) line-item
))
) {}))] {}
accounts)
]
(->graphql (->graphql
{:balance-sheet-groupings {:balance-sheet-groupings
(-> [] (-> []
@@ -95,42 +113,10 @@
[ending-results] (l/get-graphql {:client-id (:client_id args) [ending-results] (l/get-graphql {:client-id (:client_id args)
:date-before (coerce/to-date (:to_date args)) :date-before (coerce/to-date (:to_date args))
:count Integer/MAX_VALUE}) :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) starting-accounts (roll-up starting-results)
ending-accounts (roll-up ending-results) ending-accounts (roll-up ending-results)
accounts (into
starting-accounts
ending-accounts)
accounts (reduce-kv accounts (reduce-kv
(fn [result account line-item] (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-items [(remove-nils{:journal-entry-line/account (:db/id (:transaction/account entity))
:journal-entry-line/location "HQ" :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)) (remove-nils {:journal-entry-line/account (:db/id (:transaction/bank-account entity))
:journal-entry-line/location "HQ" :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})))) :journal-entry/cleared true}))))