basic support for pnl
This commit is contained in:
@@ -166,10 +166,11 @@
|
||||
z (->> transaction-ids
|
||||
(map #(ledger/entity-change->ledger (d/db conn) [:transaction %]))
|
||||
(filter identity)
|
||||
(mapv identity))]
|
||||
(mapv #(vector %)))]
|
||||
z))
|
||||
|
||||
#_(test-run (bulk-load-transaction-ledger (d/connect auto-ap.datomic/uri)))
|
||||
#_(test-run (bulk-load-invoice-ledger (d/connect auto-ap.datomic/uri)))
|
||||
|
||||
#_(do (doseq [tran (convert-transactions (d/connect auto-ap.datomic/uri))]
|
||||
@(d/transact (d/connect auto-ap.datomic/uri) tran))
|
||||
|
||||
@@ -265,6 +265,12 @@
|
||||
:date {:type 'String}}
|
||||
:resolve :get-balance-sheet}
|
||||
|
||||
:profit_and_loss {:type :balance_sheet
|
||||
:args {:client_id {:type :id}
|
||||
:from_date {:type 'String}
|
||||
:to_date {:type 'String}}
|
||||
:resolve :get-profit-and-loss}
|
||||
|
||||
:invoice_page {:type '(list :invoice_page)
|
||||
:args {:import_status {:type 'String}
|
||||
:status {:type 'String}
|
||||
@@ -638,6 +644,7 @@
|
||||
: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-profit-and-loss gq-ledger/get-profit-and-loss
|
||||
:get-expense-account-stats get-expense-account-stats
|
||||
:get-invoice-stats get-invoice-stats
|
||||
|
||||
|
||||
@@ -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