calculates profit and loss.

This commit is contained in:
Bryce Covert
2019-04-23 09:53:14 -07:00
parent 45165d9e10
commit 5317310cc7
3 changed files with 70 additions and 20 deletions

View File

@@ -17,7 +17,7 @@
(keyword "transaction" sort-by)))
(defn raw-graphql-ids [db args]
(println args)
(let [query (cond-> {:query {:find []
:in ['$ ]
:where []}
@@ -67,7 +67,6 @@
(defn get-graphql [args]
(let [db (d/db (d/connect uri))
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))

View File

@@ -1,6 +1,7 @@
(ns auto-ap.graphql.ledger
(:require [auto-ap.datomic :refer [uri]]
[auto-ap.datomic.ledger :as l]
[auto-ap.utils :refer [by]]
[auto-ap.time :refer [parse iso-date]]
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients]]
[clj-time.coerce :as coerce]
@@ -98,20 +99,27 @@
(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)
ending-accounts (roll-up ending-results)
pnl starting-accounts
comparable-pnl ending-accounts]
pnl (fn [from-date to-date]
(let [[starting-results] (l/get-graphql {:client-id (:client_id args)
:date-before from-date
:count Integer/MAX_VALUE})
[ending-results] (l/get-graphql {:client-id (:client_id args)
:date-before to-date
:count Integer/MAX_VALUE})
starting-accounts (by :id (roll-up starting-results))
ending-accounts (by :id (roll-up ending-results))]
(reduce-kv
(fn [results k v]
(conj results (update v :amount (fn [amt]
(- amt
(get-in starting-accounts [k :amount] 0))))))
[]
ending-accounts)))]
(->graphql
{:balance-sheet-accounts pnl
:comparable-balance-sheet-accounts comparable-pnl})))
{:balance-sheet-accounts (pnl (coerce/to-date (:from_date args))
(coerce/to-date (:to_date args)))
:comparable-balance-sheet-accounts (pnl (coerce/to-date (coerce/to-date (time/minus (coerce/to-date-time (:from_date args)) (time/years 1))))
(coerce/to-date (coerce/to-date (time/minus (coerce/to-date-time (:to_date args)) (time/years 1)))))})))
#_(get-profit-and-loss nil {:client_id [:client/code "CBC"]
:from_date "2018-01-01"