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,72 +32,7 @@
(= :account-type/expense (:db/ident (:account/type account)))) (= :account-type/expense (:db/ident (:account/type account))))
(defn get-balance-sheet [context args value] (defn roll-up [results]
(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))
(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))))
line-items))}))
) {}))]
(->graphql
{:balance-sheet-groupings
(-> []
(into (->> (get accounts "Assets")
(map (fn [[n accounts]]
{:name n
:grouping-type "Assets"
:accounts accounts}
))))
(into (->> (get accounts "Liabilities")
(map (fn [[n accounts]]
{:name n
:grouping-type "Liabilities"
:accounts accounts}
)))))})))
(defn get-profit-and-loss [context args value]
(let [args (assoc args :id (:id context))
[starting-results] (l/get-graphql {:client-id (:client_id args)
:date-before (coerce/to-date (:from_date args))
:count Integer/MAX_VALUE})
[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 (->> results
(mapcat :journal-entry/line-items) (mapcat :journal-entry/line-items)
(group-by :journal-entry-line/account) (group-by :journal-entry-line/account)
@@ -123,14 +58,65 @@
:else :else
0)) 0))
line-items))})) 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 (roll-up results)
accounts (reduce-kv
(fn [result account line-item]
(update-in result [(cond
(expense-account? account)
"Expenses"
(credit-account? account)
"Liabilities"
(debit-account? account)
"Assets")
(if (credit-account? account)
"Accounts Payable"
"1100 Cash and Bank Accounts" )
]
conj
line-item
)) ))
{}
accounts)
]
(->graphql
{:balance-sheet-groupings
(-> []
(into (->> (get accounts "Assets")
(map (fn [[n accounts]]
{:name n
:grouping-type "Assets"
:accounts accounts}
))))
(into (->> (get accounts "Liabilities")
(map (fn [[n accounts]]
{:name n
:grouping-type "Liabilities"
:accounts accounts}
)))))})))
(defn get-profit-and-loss [context args value]
(let [args (assoc args :id (:id context))
[starting-results] (l/get-graphql {:client-id (:client_id args)
:date-before (coerce/to-date (:from_date args))
:count Integer/MAX_VALUE})
[ending-results] (l/get-graphql {:client-id (:client_id args)
:date-before (coerce/to-date (:to_date args))
:count Integer/MAX_VALUE})
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}))))