Balance sheet has correct data, needs formatting.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
(ns auto-ap.graphql.ledger
|
||||
(:require [auto-ap.datomic :refer [uri]]
|
||||
[auto-ap.datomic.ledger :as l]
|
||||
[auto-ap.time :refer [parse iso-date]]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients]]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as d]))
|
||||
[clj-time.core :as time]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.parse.templates :as t]))
|
||||
|
||||
(defn get-ledger-page [context args value]
|
||||
(let [args (assoc args :id (:id context))
|
||||
@@ -15,6 +18,8 @@
|
||||
:count (count journal-entries)
|
||||
:start (:start args 0)
|
||||
:end (+ (:start args 0) (count journal-entries))}))
|
||||
|
||||
;; TODO a better way to do this might be to accumulate ALL credits and ALL debits, and then just do for credits: balance = credits - debits. and for debits balance = debits - credits
|
||||
(defn credit-account? [account]
|
||||
(or
|
||||
(#{:account-type/liability
|
||||
@@ -41,72 +46,51 @@
|
||||
(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))
|
||||
;; TODO fix
|
||||
(when-not (or (:bank-account/name account) (:account/name account))
|
||||
(println "WARNING " account line-items))
|
||||
(conj result
|
||||
{:name (or (:bank-account/name account) (:account/name account))
|
||||
:id (:db/id account)
|
||||
:numeric-code (or (:account/numeric-code account)
|
||||
(and (:bank-account/type account)
|
||||
1100))
|
||||
:account-type (or (:db/ident (:account/type account))
|
||||
({:bank-account-type/check :asset
|
||||
:bank-account-type/credit :liability}
|
||||
(:db/ident (:bank-account/type 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 (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/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))
|
||||
(and (debit-account? account) (:journal-entry-line/credit line-item))
|
||||
(- (:journal-entry-line/credit line-item))
|
||||
|
||||
:else
|
||||
0))
|
||||
line-items))}))
|
||||
{})))
|
||||
: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 (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)
|
||||
]
|
||||
[comparable-results] (l/get-graphql {:client-id (:client_id args)
|
||||
:date-before (coerce/to-date (time/minus (parse (:date args) iso-date) (time/months 1)))
|
||||
:count Integer/MAX_VALUE})]
|
||||
(->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}
|
||||
)))))})))
|
||||
{:balance-sheet-accounts (roll-up results)
|
||||
:comparable-balance-sheet-accounts (roll-up comparable-results)})))
|
||||
|
||||
|
||||
(defn get-profit-and-loss [context args value]
|
||||
|
||||
Reference in New Issue
Block a user