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

@@ -761,5 +761,28 @@
(defn add-sorter-field [q sort-map args]
(merge-query q
{:query {:find ['?sorter]
:where (sort-map
(:sort-by args)
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
(defn apply-sort [args sort-fn results ]
(cond->> results
sort-fn (sort-by sort-fn)
(= (:asc args) false) (reverse)))
(defn apply-sort-2 [args results ]
(let [comparator (if (= (:asc args) false)
(fn [x y] (compare y x))
(fn [x y] (compare x y)))]
(sort comparator results )))
(defn apply-pagination [args results]
{:ids (->> results
(drop (:start args 0))
(take (:count args 100))
(map last))
:count (count results)})

View File

@@ -1,7 +1,7 @@
(ns auto-ap.datomic.ledger
(:require [datomic.api :as d]
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
[auto-ap.datomic :refer [merge-query ]]
[auto-ap.datomic :refer [merge-query apply-sort-2 apply-sort apply-pagination add-sorter-field]]
[auto-ap.datomic :refer [uri]]
[clj-time.coerce :as c]))
@@ -16,33 +16,8 @@
:else
(keyword "transaction" sort-by)))
(defn add-sorter-field [q sort-map args]
(merge-query q
{:query {:find ['?sorter]
:where (sort-map
(:sort-by args)
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
(defn apply-sort [args sort-fn results ]
(cond->> results
sort-fn (sort-by sort-fn)
(= (:asc args) false) (reverse)))
(defn apply-sort-2 [args results ]
(let [comparator (if (= (:asc args) false)
(fn [x y] (compare y x))
(fn [x y] (compare x y)))]
(sort comparator results )))
(defn apply-pagination [args results]
{:ids (->> results
(drop (:start args 0))
(take (:count args 100))
(map last))
:count (count results)})
(defn raw-graphql-ids [db args]
(println args)
(let [query (cond-> {:query {:find []
:in ['$ ]
:where []}
@@ -59,16 +34,17 @@
:where ['[?e :journal-entry/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
#_(:bank-account-id args)
#_(merge-query {:query {:in ['?bank-account-id]
:where ['[?e :transaction/bank-account ?bank-account-id]]}
:args [(:bank-account-id args)]})
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :journal-entry/client ?client-id]]}
:args [(:client-id args)]})
(:date-before args)
(merge-query {:query {:in ['?date-before]
:where ['[?e :journal-entry/date ?d]
'[(<= ?d ?date-before)]]}
:args [(:date-before args)]})
true
(merge-query {:query {:find ['?e] :where ['[?e :journal-entry/original-entity]]}}))]
(cond->> query

View File

@@ -1,6 +1,6 @@
(ns auto-ap.datomic.transactions
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri merge-query]]
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-sort apply-pagination add-sorter-field]]
[auto-ap.graphql.utils :refer [limited-clients]]
[clj-time.coerce :as c]))
@@ -15,31 +15,6 @@
:else
(keyword "transaction" sort-by)))
(defn add-sorter-field [q sort-map args]
(merge-query q
{:query {:find ['?sorter]
:where (sort-map
(:sort-by args)
(println "Warning, trying to sort by unsupported field" (:sort-by args)))}}))
(defn apply-sort [args sort-fn results ]
(cond->> results
sort-fn (sort-by sort-fn)
(= (:asc args) false) (reverse)))
(defn apply-sort-2 [args results ]
(let [comparator (if (= (:asc args) false)
(fn [x y] (compare y x))
(fn [x y] (compare x y)))]
(sort comparator results )))
(defn apply-pagination [args results]
{:ids (->> results
(drop (:start args 0))
(take (:count args 100))
(map last))
:count (count results)})
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find []

View File

@@ -67,6 +67,19 @@
:bank_code {:type 'String}
:bank_name {:type 'String}
:yodlee_account_id {:type 'Int}}}
:balance_sheet_account
{:fields {
:amount {:type 'String}
:name {:type 'String}}}
:balance_sheet_grouping
{:fields {:accounts {:type '(list :balance_sheet_account)}
:name {:type 'String}
:grouping_type {:type 'String}}}
:balance_sheet
{:fields {:balance_sheet_groupings {:type '(list :balance_sheet_grouping)}}}
:address
{:fields {:street1 {:type 'String}
:street2 {:type 'String}
@@ -256,6 +269,11 @@
:args {:client_id {:type :id}}
:resolve :get-invoice-stats}
:balance_sheet {:type :balance_sheet
:args {:client_id {:type :id}
:date {:type 'String}}
:resolve :get-balance-sheet}
:invoice_page {:type '(list :invoice_page)
:args {:import_status {:type 'String}
:status {:type 'String}
@@ -612,6 +630,7 @@
:get-accounts gq-accounts/get-accounts
:get-transaction-page gq-transactions/get-transaction-page
:get-ledger-page gq-ledger/get-ledger-page
:get-balance-sheet gq-ledger/get-balance-sheet
:get-expense-account-stats get-expense-account-stats
:get-invoice-stats get-invoice-stats

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)

View File

@@ -63,7 +63,7 @@
(remove-nils
{:journal-entry/source "transaction"
:journal-entry/client (:db/id (:transaction/client entity))
:journal-entry/date (:transaction/date entity)
:journal-entry/date (doto (:transaction/date entity) println)
:journal-entry/original-entity (:db/id entity)
:journal-entry/vendor (:db/id (:transaction/vendor entity))
:journal-entry/amount (Math/abs (:transaction/amount entity))
@@ -130,7 +130,6 @@
(process-one (d/tx-report-queue (d/connect uri) ))))
#_(process-one (d/tx-report-queue (d/connect uri) ))
#_(process-all)