basic support for pnl
This commit is contained in:
@@ -86,5 +86,96 @@
|
||||
:accounts accounts}
|
||||
)))))})))
|
||||
|
||||
#_(get-balance-sheet nil {:client_id 17592186046468
|
||||
:date "2017-03-01"} nil)
|
||||
|
||||
(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
|
||||
(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))
|
||||
|
||||
(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))
|
||||
|
||||
:else
|
||||
0))
|
||||
line-items))}))
|
||||
{})
|
||||
))
|
||||
|
||||
starting-accounts (roll-up starting-results)
|
||||
ending-accounts (roll-up ending-results)
|
||||
accounts (into
|
||||
starting-accounts
|
||||
ending-accounts)
|
||||
|
||||
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
|
||||
(update line-item :amount #(- % (:amount (starting-accounts account 0) 0)) )
|
||||
))
|
||||
{}
|
||||
ending-accounts)
|
||||
]
|
||||
(->graphql
|
||||
{:balance-sheet-groupings
|
||||
(-> []
|
||||
(into (->> (get accounts "Assets")
|
||||
(map (fn [[n accounts]]
|
||||
{:name n
|
||||
:grouping-type "Assets"
|
||||
:accounts accounts}
|
||||
))))
|
||||
|
||||
(into (->> (get accounts "Expenses")
|
||||
(map (fn [[n accounts]]
|
||||
{:name n
|
||||
:grouping-type "Expenses"
|
||||
:accounts accounts}
|
||||
))))
|
||||
(into (->> (get accounts "Liabilities")
|
||||
(map (fn [[n accounts]]
|
||||
{:name n
|
||||
:grouping-type "Liabilities"
|
||||
:accounts accounts}
|
||||
)))))})))
|
||||
|
||||
#_(get-profit-and-loss nil {:client_id [:client/code "CBC"]
|
||||
:from_date "2018-01-01"
|
||||
:to_date "2019-04-01"} nil)
|
||||
|
||||
Reference in New Issue
Block a user