adds trial balance export.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
(ns auto-ap.routes.exports
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn pull-many]]
|
||||
[auto-ap.datomic :refer [conn pull-many pull-attr]]
|
||||
[auto-ap.datomic.accounts :as accounts]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
@@ -21,14 +21,16 @@
|
||||
[config.core :refer [env]]
|
||||
[datomic.api :as dc]
|
||||
[ring.middleware.json :refer [wrap-json-response]]
|
||||
[venia.core :as venia]))
|
||||
[venia.core :as venia]
|
||||
[auto-ap.logging :as alog]))
|
||||
|
||||
(defn wrap-csv-response [handler]
|
||||
(fn [request]
|
||||
(let [response (handler request)]
|
||||
(println "RESPONSE IS" response)
|
||||
(update response :body #(with-open [w (java.io.StringWriter.)]
|
||||
(csv/write-csv w %)
|
||||
(.toString w))))))
|
||||
(csv/write-csv w %)
|
||||
(.toString w))))))
|
||||
|
||||
(defn aggregated-sales-export [{:keys [query-params]}]
|
||||
(let [client-id (Long/parseLong (get query-params "client-id"))
|
||||
@@ -412,6 +414,49 @@
|
||||
(:transaction/expected-deposit i) (update-in [:transaction/expected-deposit :expected-deposit/date] to-date))))
|
||||
transactions)})))
|
||||
|
||||
(defn export-trial-balance [{{:strs [client-code as-of]} :query-params}]
|
||||
(let [db (dc/db conn)
|
||||
_ (alog/info ::trial-report-for
|
||||
:client-code client-code
|
||||
:as-of as-of)
|
||||
client (pull-attr db :db/id [:client/code client-code])
|
||||
as-of (coerce/to-date (atime/parse as-of atime/iso-date))]
|
||||
|
||||
{:body
|
||||
(->>
|
||||
(dc/index-pull db {:index :avet
|
||||
:selector [:db/id :journal-entry-line/debit :journal-entry-line/location :journal-entry-line/credit {:journal-entry-line/account [:db/id {:account/type [:db/ident]}]} :journal-entry-line/client+account+location+date]
|
||||
:start [:journal-entry-line/client+account+location+date
|
||||
[client]]})
|
||||
(take 100)
|
||||
(take-while (fn [jel]
|
||||
(let [[c _ _ _] (:journal-entry-line/client+account+location+date jel)]
|
||||
(= c client))))
|
||||
(filter (fn [jel]
|
||||
(let [[_ _ _ d] (:journal-entry-line/client+account+location+date jel)]
|
||||
(<= (compare (or d #inst "2000-01-01") as-of) 0))))
|
||||
(into [])
|
||||
(reduce
|
||||
(fn [acc jel]
|
||||
(update acc [(:db/id (:journal-entry-line/account jel)) (:journal-entry-line/location jel)]
|
||||
(fn [v]
|
||||
(if (#{:account-type/asset
|
||||
:account-type/dividend
|
||||
:account-type/expense} (:db/ident (:account/type (:journal-entry-line/account jel))))
|
||||
(update (or v {}) :debit (fnil + 0.0) (or (:journal-entry-line/debit jel) 0.0))
|
||||
(update (or v {}) :debit (fnil + 0.0) (or (:journal-entry-line/credit jel) 0.0))))))
|
||||
{})
|
||||
(map (fn [[[a l] {:keys [debit credit]}]]
|
||||
[(or (pull-attr db :account/name a)
|
||||
(pull-attr db :bank-account/name a))
|
||||
(or (pull-attr db :account/numeric-code a)
|
||||
(pull-attr db :bank-account/numeric-code a))
|
||||
l
|
||||
(or debit 0.0)
|
||||
(or credit 0.0)]))
|
||||
(sort-by second))
|
||||
:status 200}))
|
||||
|
||||
(defn export-raw [{:keys [query-params identity]}]
|
||||
(assert-admin identity)
|
||||
(log/info "Executing raw query " (get query-params "query" ))
|
||||
@@ -428,14 +473,15 @@
|
||||
"clients/" {#"export/?" {:get :export-clients}}
|
||||
"vendors/" {#"export/?" {:get :export-vendors}
|
||||
"company/" {#"export" {:get :export-company-vendors}}}
|
||||
"ledger/" {#"export/?" {:get :export-ledger}}
|
||||
"ledger/" {#"export/?" {:get :export-ledger}
|
||||
#"trial-balance/export/?" {:get :export-trial-balance}}
|
||||
"accounts/" {#"export/?" {:get :export-accounts}}
|
||||
"transactions/" {#"export/?" {:get :export-transactions}
|
||||
#"export2/?" {:get :export-transactions2}}
|
||||
#"export2/?" {:get :export-transactions2}}
|
||||
#"raw/?" {:get :export-raw}}})
|
||||
|
||||
(def match->handler {:aggregated-sales-export (wrap-csv-response aggregated-sales-export)
|
||||
:export-invoices (-> export-invoices wrap-json-response wrap-secure )
|
||||
:export-invoices (-> export-invoices wrap-json-response wrap-secure)
|
||||
:export-payments (-> export-payments wrap-json-response wrap-secure)
|
||||
:export-sales (-> export-sales wrap-json-response wrap-secure)
|
||||
:export-expected-deposits (-> export-expected-deposits wrap-json-response wrap-secure)
|
||||
@@ -443,6 +489,7 @@
|
||||
:export-vendors (-> export-vendors wrap-json-response wrap-secure)
|
||||
:export-company-vendors (-> export-company-vendors wrap-csv-response wrap-secure)
|
||||
:export-ledger (-> export-ledger wrap-json-response wrap-secure)
|
||||
:export-trial-balance (-> export-trial-balance wrap-csv-response wrap-secure)
|
||||
:export-accounts (-> export-accounts wrap-json-response wrap-secure)
|
||||
:export-transactions (-> export-transactions wrap-json-response wrap-secure)
|
||||
:export-transactions2 (-> export-transactions2 wrap-json-response wrap-secure)
|
||||
|
||||
Reference in New Issue
Block a user