adding most simple balance sheet report.

This commit is contained in:
Bryce Covert
2019-04-11 22:46:30 -07:00
parent effbff04c6
commit 6ab9d89dfd
13 changed files with 298 additions and 74 deletions

View File

@@ -1,8 +1,10 @@
(ns auto-ap.graphql.ledger
(:require [datomic.api :as d]
(:require [auto-ap.datomic :refer [uri]]
[auto-ap.datomic.ledger :as l]
[auto-ap.graphql.utils :refer [->graphql <-graphql]]
[auto-ap.datomic :refer [uri merge-query]]))
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients]]
[clj-time.coerce :as coerce]
[clojure.string :as str]
[datomic.api :as d]))
(defn get-ledger-page [context args value]
(let [args (assoc args :id (:id context))
@@ -13,5 +15,72 @@
:count (count journal-entries)
:start (:start args 0)
:end (+ (:start args 0) (count journal-entries))}))
(defn credit-account? [account]
(= (:account/numeric-code account ) 2110))
#_(get-ledger-page nil nil nil)
(defn debit-account? [account]
(not (credit-account? account)))
(defn expense-account? [account]
(and (:account/name account)
(not (str/starts-with? (:account/name account "") "A"))))
(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})
_ (println results)
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}
)))))})))
#_(get-balance-sheet nil {:client_id 17592186046468
:date "2017-03-01"} nil)